var config = {
	sensitivity: 1000, // number = sensitivity threshold (must be 1 or higher)
	interval: 100, // number = milliseconds for onMouseOver polling interval
	over: makeTall, // function = onMouseOver callback (REQUIRED)
	timeout: 100, // number = milliseconds delay before onMouseOut
	out: makeShort // function = onMouseOut callback (REQUIRED)
};

function makeShort()
{
	$(this).stop();
	var div = $(this).find('p').next();
	div.attr('show', 0);
	div.stop().animate({height: 0}, 200, function(){div.hide();});
	$(this).removeClass('hovered').removeClass('hovered-p');
}
function makeTall()
{
	$(this).stop();
    var div = $(this).find('p').next();
    var height = div.attr('_hei') ? div.attr('_hei') : div.height();
    div.stop().css({height:0}).animate({height: height}, 300, function(){div.css("overflow","visible");})
    $(this).addClass('hovered').addClass('hovered-p');
    div.attr('_hei', height);
}
function unhtmlspecialchars(text)
{
   var chars = Array("&amp;", "&lt;", "&gt;", "&quot;", "'");
   var replacements = Array("&", "<", ">", '"', "'");
   for (var i=0; i<chars.length; i++)
   {
       var re = new RegExp(chars[i], "gi");
       if(re.test(text))
       {
           text = text.replace(re, replacements[i]);
       }
   }
   return text;
}
$(document).ready(function(){

  $(".show-options, .hide-options").mouseout(function() {
    $(this).removeClass('underline');
  }).mouseover(function() {
    $(this).addClass('underline');
  }).click(function () {
    $(this).parent().parent().parent().find('.cat-options').slideToggle(200);
    $(this).parent().hide();
    $(this).parent().parent().parent().find('.opts'+ ($(this).hasClass('show-options') ? '2' : '')).show();
  });

        $(".popup").click(function() {
              myw= window.open ("", "mywindow1","status=1,width="+screen.width+",height="+screen.height);
              myw.document.write('<img alt="" src="'+$(this).parent('a').attr('href')+'"/>');
              myw.focus();
              return false;
          });

        $('.filter input[type=radio]').change(function() {
           if ($(this).attr('checked')) {
            $(this).parent().parent().parent().find('label').css({'font-weight': 'normal'});
            $('label[for='+$(this).attr('id')+']').css({'font-weight': 'bold'});
           }
        });
        $('.filter input[type=radio]:checked').each(function() {
            $('label[for='+$(this).attr('id')+']').css({'font-weight': 'bold'});
        });

        $('.quest form').submit(function() {
            window.location.href = $(this).attr('action') + ($('.quest input:checked').val() ? '~/a1/' + $('.quest input:checked').val() + '/' : '');
            return false;
        });


//        $('.pagesizes').change(function() {
//            window.location.href = $(this).val();
//        });

/*slider*/

	$(".figures").hover(function() {

        }, function() {			  
					$(this).removeClass('hovered');									
        });	
		
		
    $(".sliding-group td").each(function() {
		

		$(this).find('.figure-fire').hover(function() {
					$('.hov', this).fadeIn(80);
					$('.stat', this).fadeOut(80);					
					$(this).next().fadeIn(80);
	
	
			}, function() {			
					$('.stat', this).fadeIn(80);								
					$('.hov', this).fadeOut(80);
					$(this).next().fadeOut(80);
								
			});			
				

	 });		
	 

		 
     
	
	
	$('.sliding-group').each(function() {
		$('.default-inactive .shade ', this).show();
		$('.figures ', this).show();
		$('.title ', this).show();							
		
		$('.sliding-group td').hover(function() {
					
					if ($(this).hasClass('default-inactive')) {		
						  $(this).find('.shade').fadeOut(100);	
						  $('.default-active').find('.shade').fadeIn(100);	
						  
					} else {											

						  $(this).find('.shade').fadeOut(100);	
						  $('.default-inactive').find('.shade').fadeIn(100);							 							
						  
					}	
	
	
			}, function() {			
			
					
					if ($(this).hasClass('default-inactive')) {		
						  $(this).find('.shade').fadeIn(100);	
						  $('.default-active').find('.shade').fadeOut(100);						
						  
					} else {											

						  $(this).find('.shade').fadeOut(100);	
						  $('.default-inactive').find('.shade').fadeIn(100);	
						  
					}				
		
								
			});			
		
		
	})		

    $(".slide-right").click(function() {
        $('.slide-area').animate({marginLeft: -450}, 300, function() {
            $('td.sliding-group:last', this).after($('td.sliding-group:first', this));
            $(this).css({marginLeft: 0});					
        });
    });
	 
	 
    
    $(".slide-left").click(function() {
        $('.slide-area').each(function() {
            $('td.sliding-group:first', this).before($('td.sliding-group:last', this));
            $(this)
                .css({marginLeft: -450})
                .animate({marginLeft: 0}, 300);
        })
    });
	
	


	





/*horizontal menu level-1*/			
	
	 $(".navigation div").click(
	  function(){

				  window.location = $('a', this).attr('href');	
				  return false;

	  }
	  );		

	$(".navigation div").hover(function() {
					$(this).addClass('hovered');
        }, function() {			  
					$(this).removeClass('hovered');									
        });	
		
		
		
/*horizontal menu with drop-down*/		

	$('.nav-hor-2 td').hoverIntent(config);


	
  $(".nav-hor-2 td").each(function() {

        //var td = $(this)
        //var p = $(this).find('p');
        //var div = $(this).find('p').next();		  	  
        //var height = div.height();

        //div.css({height: 0})		  
		/*
        td.hover(function(){
        	//if ($(this).hasClass('navi-select')) {return false;} else
        	{
        		div.attr('show', 1);
        		setTimeout(function(){
        			if (div.attr('show') == 1)div.stop().show().animate({height: height}, 200);
        		}, 500);
        		$(this).addClass('hovered');
        		$(this).addClass('hovered-p');
        	}
        }, function() {
        	//if ($(this).hasClass('navi-select')) {return false;} else
        	{
        		div.attr('show', 0);
        		div.stop().animate({height: 0}, 300, function(){ div.hide();});
        		$(this).removeClass('hovered');
        		$(this).removeClass('hovered-p');
        	}
        });
        */
    });
    
    
		

			
	$(".nav-hor-2 p").click(
		function(){

					window.location = $('a', this).attr('href');	
					return false;

		}
		);			
		

	$(".nav-hor-2 span").hover(function() {
					$(this).parent().addClass('link-hover');
        }, function() {			  
					$(this).parent().removeClass('link-hover');									
        });		


	$(".nav-hor-2 span").click(
		function(){

					window.location = $('a', this).attr('href');	
					return false;

		}
		);	
		
		
	
		
		
/*page listing*/		

	$(".pages .arrow div").hover(function() {

				if ($(this).hasClass('forward')) {		

					  $(this).addClass('hover-right');
						
				} else {							
					  $(this).addClass('hover-left');
				}	


        }, function() {			  
				if ($(this).hasClass('forward')) {		

					  $(this).removeClass('hover-right');
						
				} else {							
					  $(this).removeClass('hover-left');
				}									
        });	
		
		
	$(".pages .marker div").hover(function() {
				if ($(this).hasClass('active')) {		

						return false
						
				} else {							
					$(this).addClass('hover');
				}			
		
        }, function() {			  
					$(this).removeClass('hover');									
        });							
		
		
	 $(".pages .arrow div, .pages .marker div").click(
	  function(){
		  
				if ($(this).hasClass('active')) {		

						return false
						
				} else {							
				  window.location = $('a', this).attr('href');	
				  return false;
				}					  

	  }
	  );
	  



/*image gallary*/


  $(".prevs td:first").addClass('selected');

  $(".prevs img").each(function() {
        var image = $(this);
        var layer = $(this).parent();
		image.click(function(){
			if (layer.hasClass('selected')) {return false;}
			else {
              var i = $('#item_image1').css('display') == 'none' ?  $('#item_image') : $('#item_image1');
              var j = $('#item_image').css('display') == 'none' ?  $('#item_image') : $('#item_image1');
      		  i.fadeOut(200);
			  $('.prevs td').removeClass('selected');
			  layer.addClass('selected');
			  setTimeout(function() {
                    j.attr('src', image.attr('_src'))
                    .attr('_src2', image.attr('_src2'))
                    .attr('_w', image.attr('_w'))
                    .attr('_h', image.attr('_h'))
                    .fadeIn(100);                                
				}, 100);
				setTimeout("$('.largeim').height("+image.attr('_hcard')+");", 200);  
			}
		});			

	 });
  $('#item_image, #item_image1').click(function(){
    var j = $('#item_image').css('display') == 'none' ?  $('#item_image1') : $('#item_image');
    $('<img class="bimg" src="'+j.attr('_src2')+'" width="'+j.attr('_w')+'" height="'+j.attr('_h')+'"/>').insertAfter('.main').ready(function () {
    	$('.bimg').modal({closeClass: "bimg", opacity: 40, maxHeight: $('.bimg').height(), maxWidth: $('.bimg').width(), onClose: function () {$.modal.impl.close();$(".bimg").remove();}});
        $('#simplemodal-overlay').width($(window).width()).bgiframe();
    });
  });

  


  $('.prw2').click(function(){
    $('<img class="bimg" src="' + $(this).attr('_src2') + '"/>').insertAfter('.main').ready(function () {
    	$('.bimg').modal({closeClass: "bimg", opacity: 40, maxHeight: $('.bimg').height(), maxWidth: $('.bimg').width(), onClose: function () {$.modal.impl.close();$(".bimg").remove();}});
        $('#simplemodal-overlay').width($(window).width()).bgiframe();
    });
  });

	 
/*.inner-link in catalog-inner.html*/	 
	$(".inner-link span").click(
		function(){

					window.location = $('a', this).attr('href');	
					return false;

		}
		);		 
		
		
/*dropdown on catalog-inner.html*/		

		$(".dropdown-link span").toggle(
		  function () {
							$('#'+$(this).attr('_id')).slideToggle(100);				  
							$(this).parent().addClass("marg")
							$(this).addClass("arrow-top");
							$(this).addClass("under");															
							$(this).removeClass("unselected");
							$(this).find('b').html('Скрыть');
							scrollTo($('#'+$(this).attr('_id')));
		  },
		  function () {
							$('#'+$(this).attr('_id')).slideToggle(100 ,function() {
							$(".dropdown-link").removeClass("marg");
							});				  																					
							$(this).removeClass("arrow-top");					
							$(this).removeClass("under");														
							$(this).addClass("unselected");
							$(this).find('b').html('Посмотреть');
		  }
		);	 		


		
/*partners-showing layers*/

	$(".pic-area").hover(function() {
				var path = $(this).find('.intro');
				$(this).addClass('back');
				path.animate({opacity: 0}, 0);
			    path.stop().show().animate({opacity: 1}, 100);

        }, function() {			  
				var path = $(this).find('.intro');		
				$(this).removeClass('back');				
			    path.stop().hide().animate({opacity: 0}, 100 ,function() {
				path.hide();
				});				  						
        });			



	/*$(".pic-area table").click(
		function(){

					window.location = $('a', this).attr('href');	
					return false;

		}
		);	
		*/
		
/*Decision.html cards*/


	$(".holder").each(function() {
			var pic_holder = $(".pic-holder", this);
			var text = $('.text', this);  
			var number = $('.number', this);    
		    pic_holder.css({height: $(this).height()-text.height()-number.height()-20});    
		  
		});
		

	$(".holder").hover(function() {
				$(this).addClass('active-bg');
				$(this).find('p').addClass('active-link');				
				

        }, function() {			  
				$(this).removeClass('active-bg');
				$(this).find('p').removeClass('active-link');								
        });			
		
	$(".holder").click(
		function(){

					window.location = $('a', this).attr('href');	
					return false;

		}
		);			
		
		

/*decison listing*/		

	$(".decision-large div").hover(function() {

					  $(this).addClass('hover-right');


        }, function() {			  

					  $(this).removeClass('hover-right');
		
        });			


	$(".decision-large div").click(
		function(){

					window.location = $('a', this).attr('href');	
					return false;

		}
		);		
		

/*index directions*/
		

	$(".inner-directions").hover(function() {
					  $('h2', this).addClass('hover-h');

					  $('.number-hover', this).show();
					  $('.number-active', this).hide();					  
			
        }, function() {			  
					  $('h2', this).removeClass('hover-h');		
					  
					  $('.number-hover', this).hide();
					  $('.number-active', this).show();					  
        });			
		
	$(".inner-directions").click(
		function(){

					window.location = $('a', this).attr('href');	
					return false;

		}
		);		
		
		
/*index bookmark*/		

  $(".bookmark:first").addClass('pushed');

  $(".bookmark").click(function(){
    if ($(this).hasClass('pushed')) {
      return false;
    } else {
      $(".bookmark").removeClass('pushed');
      $(this).addClass('pushed');
      $(".bookmark .replacing-left").removeClass('lighter-left');
      $(".bookmark .replacing-right").removeClass('lighter-right');
      $(".bookmark .replacing-left").addClass('darker-left');
      $(".bookmark .replacing-right").addClass('darker-right');
      $(".replacing-left", this).removeClass('darker-left');
      $(".replacing-right", this).removeClass('darker-right');
      $(".replacing-left", this).addClass('lighter-left');
      $(".replacing-right", this).addClass('lighter-right');
      $(".card-holder").hide();
      $($(".card-holder")[$(".bookmark").index($(this))]).show();
    }
  });



  

  $('select[name=catalogue_id], select[name=brand_id]').change(function() {
    if ($('select[name=catalogue_id]').val() == 0 && $(this).attr('name') == 'catalogue_id') {
        $('select[name=brand_id] option[value!=0]').remove();
        $('select[name=item_id] option[value!=0]').remove();
    }
    else
       if ($('select[name=brand_id]').val() == 0 && $(this).attr('name') == 'brand_id') {
        $('select[name=item_id] option[value!=0]').remove();
      }else
        $.post($(this).val(), $(this).serializeArray(), function(data) {
          if (data.brands) {
            $('select[name=brand_id] option[value!=0]').remove();
            $.map(data.brands, function(item){
             if (item.name)
                $('select[name=brand_id]').append(
                  $('<option />').text(unhtmlspecialchars(item.name)).attr('value', item.url)
              );
            });
          }
          $('select[name=item_id] option[value!=0]').remove();
          if (data.items) {
            $.map(data.items, function(item){
              $('select[name=item_id]').append(
                $('<option />').text(unhtmlspecialchars(item.name)).attr('value', item.url)
              );
            });
          }
        }, 'json');
    return false;
  });
  //Filter from cookie
  function update_filter_brands() {
      if ($('#filter select[name=brand_id] option').length)
        $('#filter select[name=brand_id]').val($.cookie('filter_brand')).change();
      else
        setTimeout(function() {update_filter_brands();}, 500);
  }
  if ($.cookie('filter_catalogue')) {
    $('#filter select[name=catalogue_id]').val($.cookie('filter_catalogue')).change();
    setTimeout(function() {update_filter_brands();}, 800);
  }



  $('#filter input[type=submit]').click(function() {
    var l;
    $('#filter select').each(function() {
      if ($(this).val() != 0) {
        l = $(this).val();
        if ($(this).attr('name') == 'item_id') {
          $.cookie('filter_catalogue', $('#filter select[name=catalogue_id]').val(), {expires: 1, path: '/'});
          $.cookie('filter_brand', $('#filter select[name=brand_id]').val(), {expires: 1, path: '/'});
        }
      }
    });
    if (l)
      window.location = l;
    return false;
  });

  

  

  $('.filter_submit').click(function () {
   var temp = '';
   $('.filter input:checked').each(function () {temp = temp + $(this).val()});
   $('.filter input[type=text]').each(function () {temp = temp + '/'+$(this).attr('name')+'/'+$(this).val()});
   window.location = $('#filter_catalogue').attr('action') + (temp ? '~' + temp : '');
   return false;
  });


});
function scrollTo(selector) {
var targetOffset = $(selector).offset().top;
$('html,body').animate({scrollTop: targetOffset}, 500);
}
