$("document").ready(function(){
$(function(){
    var fb = 0;
    var animated=0;
    var cont_height = $(".container").height();
    $(".favor_podl").height(cont_height);
    
//	$('.item_name img').click(function (e) {
//	  var n =  $(this).attr("rel");
//	  $('.item_det'+n).modal({overlayClose:true});
//	  return false;
//	});
	
/////////////////////////////////// HIDING CATH
$('.a_hide').click(function () {
	  $(this).hide();
	  $(this).next(".a_show").show();
      $(this).parent("td").parent("tr").parent("tbody").parent("table").parent("div").children(".item_full").animate({height: 'hide'  }, 800, function() { });
	  return false;
});

/////////////////////////////////////////////
//////////////// AJAX REQUEST////////////////
///////////////////////////////////////////////
$('.a_show').click(function () {	 
        var page = parseInt($(this).attr("id").slice(3));
	if(!$("#full"+page).hasClass("opened")){
	$("#loading-overlay").css("display", "block").show();
	$.ajax({
		   type: "POST",
		   url: "/ajax/catalog_items.php",
		   data: "page="+page,
		   success: function(data){$("#full"+page).html(data); $("#loading-overlay").hide();
},
		   complete: function(){$("#full"+page).animate({height: 'show'}, 800); $("#full"+page).addClass("opened");
		   $("#loading-overlay").fadeOut(2000);}
		 });
	}else{
		$("#full"+page).animate({height: 'show'}, 800);
	}
    $(this).hide();
    $(this).prev(".a_hide").show();
});

$('.dmd1').click(function () {	 
        var page = parseInt($(this).attr("id").slice(2));
        if(!$("#full"+page).hasClass('opened')){
		$("#loading-overlay").fadeIn(100);
	$.ajax({
		   type: "POST",
		   url: "/ajax/catalog_items.php",
		   data: "page="+page,
		   success: function(data){$("#full"+page).html(data);},
	           complete: function(){$("#full"+page).animate({height: 'toggle'}, 800); $("#full"+page).addClass('opened');
		   $("#loading-overlay").fadeOut(2000);}
		 });
	}else{
		$("#full"+page).animate({height: 'toggle'}, 800);
	}
		if($("#full"+page).css("height")=="auto"){
			$(this).parent("td").parent("tr").children("td").children(".a_show").hide();
			$(this).parent("td").parent("tr").children("td").children(".a_hide").show();
		}else{
			$(this).parent("td").parent("tr").children("td").children(".a_show").show();
		 	$(this).parent("td").parent("tr").children("td").children(".a_hide").hide();	
		}
		
    return false;
});
///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
//$(".top_menu a").hover(
//  function () {
//    if (animated == 0) {
//    animated = 1;
//    var n =  $(this).attr("rel");
//    $(".ah"+n).animate({height: 'hide' }, 1000, function() { animated = 0 });
// }  },
//  function () {
//
//    var n =  $(this).attr("rel");
//    $(".ah"+n).animate({height: 'show'  }, 1000, function() {  });
//
//  }
//);
/////////////////////////////////////////////////////
//////////////////AJAXED FAVOR MENU///////////////
///////////////////////////////////////////////////

$(".ft_ul a, .card_paginate a").live("click", function(){
	var page = parseInt($(this).attr("rel"));
	$(".pcard_table").fadeOut(300);
        if(!$("#full"+page).hasClass("opened")){
	$("#loading-overlay").fadeIn(10);
	var n =  $(this).attr("rel");
	$.ajax({
		   type: "POST",
		   url: "/ajax/catalog_items.php",
		   data: "page="+page,
		   success: function(data){
			   $("#full"+page).html(data);
			   $(".favor_podl").hide();
			   $(".favorites").animate({left: '-310px'}, 100, function() { fb=0;  });
			   $(".menu_item"+n+" .a_show").hide();
			   $(".menu_item"+n+" .a_hide").show();
		   },
	           complete: function(){$("#full"+page).animate({height: 'show'}, 800); $("#full"+page).addClass('opened');
		   $("#loading-overlay").fadeOut(2000);}
		 });	
	}else{
		$("#full"+page).animate({height: 'toggle'}, 800);

	}
});

/////////////////////////////////////////////////////
//////////////////// AJAXED FAQ FORM/////////////////
/////////////////////////////////////////////////////
//FAQ INPUT SECURITY 
$(".send1").live("click", function(){
   var fcustomer = $("#fcustomer").val();
    var ftext = $("#ftext").val();
    var fplace = $("#fplace").val();
    var fmail = $("#fmail").val();
    var msg = "error";
    if(fcustomer==""||fmail == ""||fcustomer=="ошибка"||fmail == "ошибка"){
    	$("#q_answer").fadeIn();
    	$("#q_answer").html('<p style="color:red;">ошибка! - поля заполнены не верно</p>');
	    }else{
	    $.post("/ajax/faq.php",{'fcustomer':fcustomer,'fmail':fmail,'ftext':ftext,'fplace':fplace},function(msg){
	       if((fcustomer!="" && fmail!="")){
	    	   msg = '<p style="color:green;">Сообщение отправлено на модерацию...</p>'; 
	    	   $("#fcustomer, #fplace, #ftext").val("");
	       }else{
	    	   msg = '<p style="color:red;">ошибка! - заполните обязательные поля</p>';
		   }
        	$("#q_answer").fadeIn();
        	$("#q_answer").html(msg);
        	$("#q_answer").fadeOut(3000); 	
	 });
    }
return false;
});

/////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////


$(".otz_link a").click(function(){
  var n =  $(this).attr("rel");
  if (n==1) {$(".ftable").animate({height:'hide'}, 400); $(this).hide(); $(".oa2").show(); return false; }
  else {$(".ftable").animate({height:'show'}, 400);$(this).hide(); $(".oa1").show(); return false; }
});
$(document).keypress(function(e){
	if(e.which==13){
		$(".ftable").animate({height:'show'}, 400);
		$("#q_answer").css("display", "none");
	}
	if(e.which==0){
		$(".ftable").animate({height:'hide'}, 400);
	}
});

$(".fav_butbbb").hover(
  function () { $(".favorites").animate({left: '0px'}, 800, function() { fb=1;  });  },
  function () { $(".favorites").animate({left: '-310px'}, 800, function() { fb=0;  });   }
);

$(".favorites").hover(
  function () { if (animated == 0) {
    animated = 1;
    $(".favorites").animate({left: '0px'}, 300, function() { fb=1; animated=0;  });
      }
  },
  function () { if (animated == 0) {
    animated = 1; $(".favorites").animate({left: '-310px'}, 300, function() { fb=0; animated=0;  });
  }   }
);

//$(".item_ul li").hover(
//  function () { var n =  $(this).attr("rel"); $(".nb_prev").show(); $(".nb"+n).show();   },
//  function () {  $(".nb_prev").hide(); $(".nb").hide();   }
//);


$(window).scroll(function () {
    var offset1 = $(document).scrollTop();
    var offset2 = $(document).scrollTop();
    var content_top = $(".content").scrollTop();
    var height = $(document).height();
    var heights = $(window).height();
    
    var heightz = $('.fixed_block').height();
//    document.title=height-heights-offset1;
    if (offset1>170) {
        if (height-offset1-heightz>160) {
            //$('.fixed_block').css("marginTop",offset1+"px")
            $('.fixed_block').stop().animate({marginTop: offset1 - 130});
            $('.favorites').stop().animate({marginTop: -105});

        }
    }else{
    $('.fixed_block').stop().animate({marginTop: 10});
    $('.favorites').stop().animate({marginTop: 0});
    }
});
});

function equalHeight(group) {
        tallest = 0;
        group.each(function() {
            thisHeight = $(this).height();
            if(thisHeight > tallest) {
                tallest = thisHeight;
            }
        });
        group.height(tallest);
}
});
