$(document).ready(function(){
 var bodywidth = $("body").attr("clientWidth");
 var htmlheight = $("html").attr("scrollHeight");
 $("#contact_more").click(function () {$(".contact_more").toggle("slow");});
 $("#posts_more").click(function () {$(".posts_more").toggle("slow");});   
 $("a.anch").hover(function () {
  var idi = $(this).attr('href').substring(1);
  var pos = $(this).position();
  $("#anchor"+idi+"").css({'left' : pos.left+11+'px', 'top' : pos.top+11+'px'});
  $("#anchor"+idi+"").show();
  }, 
  function () {
   var idi = $(this).attr('href').substring(1);
   $("#anchor"+idi+"").hide();
  }
 );
 // Region filter
 $("#region_filtr a").click(function () {
  $("#region_filtr").load("/netcat/modules/default/region_filter.php");
 });
 var b5width = $("#b5").attr("clientWidth");
 var countb5div = $("#b5 div.item").length;
 var b5div = (b5width/countb5div)-20;
 $("#b5 div.item").each(function(){$(this).attr("width",b5div+"px","height","100px");});
 var balans = $("#currBalans").html();

 function checkCost(){
  var summa = parseInt("0");
  $(".podForm .small > :checked").each(function () {
   var cid = $(this).attr("id");
   var addSumma = $("#cost_"+cid).val();
   summa = summa  + parseInt(addSumma);
  });
  $("#costPlace").val(summa);
  $("#costPlaceval").html(summa);
  if(parseInt(balans)<summa) {blinkText("Не достаточно средств для размещения");$(".submit").attr("disabled","disabled");}
  else {$("#noMoney").html("");$(".submit").removeAttr("disabled");}
 }
 // Select sub for present   
 $(".l1 > :input").change(function(){
  var subid = $(this).attr("id");
  // if sub =  news -> disable all shild checkbox
  if(subid=="sub365"){
   if($(this).is(":checked")) {$("#subNews .l2 > :input").attr("disabled","disabled");}
   else {$("#subNews .l2 > :input").removeAttr("disabled");}
  }
  // if sub =  public -> disable all shild checkbox
  if(subid=="sub374"){
   if($(this).is(":checked")) {$("#subPubl .l2 > :input").attr("disabled","disabled");}
   else {$("#subPubl .l2 > :input").removeAttr("disabled");}
  }  
  if ($(this).is(":checked")) {$("#pod_"+subid).show();}
  else {
   $("#pod_"+subid+" .small > :input").removeAttr("checked"); 
   $("#pod_"+subid).hide();
  }
  checkCost();
 });
 $(".podForm .small > :input").change(function(){checkCost();});
 
 
 // SUBMIT PRESENT FORM
  var options = { 
    // элемент, который будет обновлен по ответу сервера 
  	target: "#Response",
	url: "/netcat/modules/news/present.php",
    beforeSubmit: makePresent, // функция, вызываемая перед передачей 
    success: showResponse, // функция, вызываемая при получении ответа
    notsuccess: errorAjax, // функция, вызываемая при получении ошибки
    timeout: 30000 // тайм-аут
  };
 $("#presentForm").submit(function(){
  $(this).ajaxSubmit(options);
  return false;
 });
 // END SUBMIT PRESENT FORM 
});
// END JQuery  
function makePresent(){
 var countText  = $("input.subText:checked").length;
 var countVideo = $("input.subVideo:checked").length; 
 if(countText==0 && countVideo==0) {alert("Выберите хотя бы один тип размещения");return false;}
 if(countVideo>0){
  if($("#f_VideoKod").val().length > 5) {}
  else if($("#f_Photo").val().length > 5) {}
  else {alert("Укажите код видео или фотографию."); return false;}
 }
 return true;
}
// вызов после получения ответа 
function showResponse(responseText, statusText)  { 
	$("#Response").show();
	$("#presentForm").hide();
}
function errorAjax(statusText) {
	alert("error = "+statusText);
}

function makeComment() {$("#InnerForm").show();}

function blinkText(blink_text){
 var speed=700
 var n=navigator.appName
 var ns=(n=="Netscape")
 var ie=(n=="Microsoft Internet Explorer")
 if (ns){$("#noMoney").html('<blink>'+blink_text+'</blink>')}
 else if (ie){var verify = 1;blink();}

 function blink(){
  if (verify == 1){$("#noMoney").html(blink_text);verify=0;}  
  else {$("#noMoney").html("");verify=1;}
  setTimeout("blink()",speed);
 }
}
