$(document).ready(function(){
   $(".close").click(function () {
      $(this).parents(".spup").fadeOut('slow');
   });
   $(".doom a").click(function () {
      var value = $(this).attr('href').substr(1);
      $('.doom').parents("form").find('input[name=size]').val(value);
      $('.doom').parents("form").submit();
   });
   $("#slogan a").click(function () {
      el = $("#popup");
      if( $.browser.msie && $.browser.version == 6 ) {
         var ifrel = $('<iframe scrolling="no" frameborder="no"></iframe>').css({position: 'absolute', left: 0, top: 0, width: el.css('width'), height: el.css('height'), 'z-index': 2000}).appendTo(el);
      }
      $('.searchp').css('top', '');
      $(el).fadeIn('slow');
   });
   
   $(".actor").each(function() {
      $(this).click(function () {
         if( !$(this).hasClass('on') ) {

            var str = $(this).find("a").text();
            var str_old = $(".actor").find("span").text();
            if( str_old ) $(".actor").find("span").parent().removeClass('on').html( "<a href='javascript://'>" + str_old + "</a>" );
            $(this).addClass('on').html( "<span>" + str + "</span>" );
            $('.searchp:visible').css("display", "none");
            $('.searchp').eq($(".actor").index(this)).css("display", "block");
            if( $(".actor").index(this) == 1 && ( $("#popup").find(".det-pop-box:first").css("top") == "0px" || $("#popup").find(".det-pop-box:first").css("top") == "auto" || !$("#popup").find(".det-pop-box:first").css("top") ) ) { 
            set_pup();
            showInfos();
         }

         }
         return false;    
      });   
   });
   $('#popup1 input:radio').click(function() {
     showInfo1();
   });
});
function showInfo1(){
   var dataString = $('#popup1').find("input:not(:radio:not(:checked))");
   if( $('#popup1').find("input:not(:radio:not(:checked))").length > 0 ) {
      $.ajax({
        type: "POST",  
        url: "/cgi-bin/catalog/show_info.pl",
        data: dataString,
        success: function( response ) {
         $('.owen').html( 'Будет найдено товаров: ' + response );
         //show_text(response);
        // alert(response);
         if( response == 0 ) 
            $('.owen').next().next().attr("src", "/i/next-off.gif");
         else
            $('.owen').next().next().attr("src", "/i/next.gif");
        },
        error: function( ) {
         alert( ' \
            При обращени к серверу произошла ошибка, \n \
            попробуйте перезагрузить страницу. \n \
            Если ошибка повотрится сообщите нам об этом. \
         ' );
        }
      });
   }

}
function go(el){
   if( $(el).attr("src") != "/i/next-off.gif" ) {
      var dataString = $(el).parent().parent().find("input:not(:radio:not(:checked))");
      $.ajax({
        type: "POST",  
        url: "/cgi-bin/catalog/selection.pl",  
        data: dataString,
        success: function( response ) {
         $("#popup1").find(".popup-box").html( response );
         $("#popup:visible").fadeOut();
         $("#popup1:hidden").fadeIn('slow');
         showInfo1();
        },
        error: function( response ) {
         $("#popup1").find(".popup-box").html( response );
         $("#popup:visible").fadeOut();
         $("#popup1:hidden").fadeIn('slow');
        }
      });
   }
};

function go_to(num){
   var params = '';
   var dataString = '';
   var name = new Array();
   for( i=0; i<num; i++ ) {
      dataString += dataString ? "&" : "";
      dataString += $("#popup1").find("input:hidden").eq(i).attr('name')+"="+$("#popup1").find("input:hidden").eq(i).val();
   }
   $.ajax({
     type: "POST",  
     url: "/cgi-bin/catalog/selection.pl",  
     data: dataString,
     success: function( response ) {
      $("#popup1").find(".popup-box").html( response );
      $("#popup1:hidden").fadeIn('slow');
      showInfo1();
     },
     error: function( response ) {
      $("#popup1").find(".popup-box").html( response );
      $("#popup1:hidden").fadeIn('slow');
     }
   });
};

function set_pup() {
   $("#popup").find(".det-pop-box").each(function() {
      var offset = $(this).parent().position();
      var top = offset.top - $(this).height();
      var left = offset.left - 70;
      $(this).css("top", top + "px");
      $(this).css("left", left + "px");
   });
}

function showInfos(){
	var dataString = '';
	if ( $('#filter input[name=offer]:checked').length )
		dataString = $('#filter input'); 
	else 
		dataString = $('#filter input:not(:checkbox)'); 
//   if( $("#filter input[value='']:not(:checkbox)").length < 7 || $('#filter input[name=offer]:checked').length ) {
		$.ajax({
		  type: "POST",  
		  url: "/cgi-bin/catalog/show_info.pl",
		  data: dataString,
		  success: function( response ) {
			$('#filter .budet').html( 'Будет найдено товаров: ' + response );
		  },
		  error: function( ) {
			alert( ' \
				При обращени к серверу произошла ошибка, \n \
				попробуйте перезагрузить страницу. \n \
				Если ошибка повотрится сообщите нам об этом. \
			' );
		  }
		});
 //  }

}

$(function() {
   /*var hash;
   var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
   for(var i = 0; i < hashes.length; i++) {
      hash = hashes[i].split('=');
      if( hash[0] == 'a' && hash[1] == 'b' ) {
         initMenu2();
      }
   }*/
	initMenu2();
});

var sma_over_start = false;
var sma_inside = false;
var ppb_inside = false;
function initMenu2() {
   $('.pp-block').css('top', '40px');

   $('#same-menu ul li a').mouseover(function() {
      var id = $(this).attr('m_id');

      var left = $(this).position().left + $(this).width() / 2 - 258 / 2;
      $('#ppb_'+id).css('left', left+'px');

      $('.pp-block').hide();
      $('#ppb_'+id).show();

      sma_over_start = true;
      setTimeout(function(){ sma_over_start = false; }, 10);
   });
   $('#same-menu ul li a').mouseout(function() {
      if( sma_over_start || ppb_inside ) return;

      setTimeout(function(){
                              if( ppb_inside ) return;

                              $('.pp-block').hide();
                           }, 10);
   });
   $('.pp-block').mouseleave(function() {
      if( sma_inside ) return;

      $('.pp-block').hide();

   });
   $('#same-menu ul li a').hover(function(){
      sma_inside = true;
   }, function(){
      sma_inside = false;
   });
   $('.pp-block').hover(function(){
      ppb_inside = true;
   }, function(){
      ppb_inside = false;
   });
}
