var f;
$(document).ready(function(){
    
    var query = $("#q").autocomplete('/search/', {delay:10,minChars:1,matchSubset:1,cacheLength:10,selectFirst:false,formatItem:liFormat,maxItemsToShow:11,onSelectItem:onSelectItem, preShowResults: preShowResults, width: 535, extraParams: {w: $('#w').val()}});
	$('#ppage').change(function(){window.location = $(this).val();});								
	$.ocd();
	
	$('#q').keypress(function(e) {
    	if(e.keyCode == 13) {
    	  $('#w').remove();
    	}
    });
    $(".search-btt").click(function(){
        if ($('#ts input[name=q]').val()!='') $('#ts').submit();
	});
	$('#ts input[name=q]').keypress(function(e) {
    	if(e.keyCode == 13) {
    	  if($(this).val()!='') $(this).submit();
    	  return false;
    	}
    });
    $(".captcha").each(function() {
        $(this).attr('src','/si.php?n='+$(this).attr('id')+'&rid=' + Math.random());
    }).click(function(){
        $(this).attr('src','/si.php?n='+$(this).attr('id')+'&rid=' + Math.random());
	});
    $(".else-links input[type=submit]").click(function() {
      f = $(this).closest('form');
      f.find("input[type=text], textarea").each(function() {
          if ($(this).attr('_value') == $(this).val())
              $(this).val('');
      });
      var g = f.serializeArray();
      f.find("input[type=text], textarea").each(function() {
          if ('' == $(this).val())
              $(this).val($(this).attr('_value'));
      });
      $.post(f.attr('action'), g, function (data) {eval(data);});
      return false;
    });

    $(".else-links input[type=text], .else-links textarea").each(function() {
      $(this).attr('_value', $(this).val());
    }).focus(function() {
      if ($(this).val() == $(this).attr('_value')) $(this).val('');
    }).blur(function() {
      if ($(this).val() == '') $(this).val($(this).attr('_value'));
    });/*.keypress(function(e) {
    	if(e.keyCode == 13) {
    	  if($(this).val()!='') 
    	  {
    	    f = $(this).closest('form');
    	    f.find("input[type=text], textarea").each(function() {
                if ($(this).attr('_value') == $(this).val()) 
                    $(this).val('');
            });
            var g = f.serializeArray();
    	    f.find("input[type=text], textarea").each(function() {
                if ('' == $(this).val()) 
                    $(this).val($(this).attr('_value'));
            });
    	    $.post(f.attr('action'), g, function (data) {eval(data);});
    	  }  
    	  return false;
    	}
    });*/
     
    $(".layer-link").click(
		 function(){			 
			if ($(this).hasClass('select')) {
			  $(this).removeClass('selected-link');
			  $(this).prev().hide();
			  $(this).removeClass('select');					  
			  return false;
			} else {
			  $('.layer-link').removeClass('selected-link');
			  $('.layer-link').prev().hide();
			  $('.layer-link').removeClass('select');		

			  $(this).addClass('selected-link');
			  $(this).prev().show();	
			  $(this).addClass('select');
			  return false;
			}					 
		}            
	 );

	$(".listing-next, .listing-prev, .bunner-container").click(
		function(){
			
			window.location = $('a', this).attr('href');
			
			return false;			
						 
					}
		);


	$(".listing-next").hover(
		function(){
					if ($(this).hasClass('unselect')) {
								return false; 
					} else {		

					  $(this).addClass('hovered-forward');
					}			 
					},   
		function(){				
					 $(this).removeClass('hovered-forward');
			}
		);	
	
	$(".listing-prev").hover(
		function(){
					if ($(this).hasClass('unselect')) {
								return false; 
					} else {		

					  $(this).addClass('hovered-backward');
					}			 
					},   
		function(){				
					 $(this).removeClass('hovered-backward');
			}
		);		



/*
	 console.log(this);														 
	 */


	$(".main-navigation div").each(function() {
	$(this).css({width: $(this).next('span').width()});
	});	
	
	

		
	$(".page-listing").find('.nomber').hover(
		function(){
					if ($(this).hasClass('selected')) {
								return false; 
					} else {		
					  $(this).addClass('hovered');
					}			 
					},   
		function(){				
					  $(this).removeClass('hovered');
			}
		);	
	

	$(".page-listing").find('.nomber').click(
		function(){
			window.location = $('a', this).attr('href');
			return false;				 
					}
		);	



	
	$(".hang-item .text div").click(
		 function(){
			 			 
				if ($(this).hasClass('selected')) {
					
					  $(this).find('.check2').hide();
					  $(this).find('.check1').show();					  
					  $(this).removeClass('selected');
					

				} else {		
				
					  $(this).find('.check1').hide();
					  $(this).find('.check2').show();					  
					  $(this).addClass('selected');
				}					 
			 
			}            
	 );	
	
	
	
	
	$('.hang-item').each(function() {
		var Hauauau = this;
		$('.check1', Hauauau).click(function() {

		setTimeout(function() {
								  
			$(Hauauau).fadeOut(300);
			$(Hauauau).find('.green_act').fadeOut(300);						
			$(Hauauau).find('.gray_act').fadeIn(300);			
			$(Hauauau).next('.green-small').fadeOut(300);			
			$(Hauauau).next().next('.gray-small').fadeIn(300);	
			$(Hauauau).next('.green-large').fadeOut(300);			
			$(Hauauau).next().next('.gray-large').fadeIn(300);				
			/*
			setTimeout(function() {					
				$(Hauauau).next().next().next('.notify-small').fadeIn(600, function() {$(Hauauau).next().next().next('.notify-small').fadeOut(600);});;	
				}, 300);*/
			
			setTimeout(function() {					
				$(Hauauau).next().next().next('.notify-small').fadeIn(600);
				}, 300);			
			setTimeout(function() {					
				$(Hauauau).next().next().next('.notify-small').fadeOut(600);
				}, 2000);						
						
							 
			}, 300);	});
		
		
		/*
		$('.check2', Hauauau).hover(
			function(){
			$(this).css({cursor: 'default'});															 
						},   
			function(){				
			$(this).css({cursor: 'default'});															 
				}
			);	*/		
		

		$('.check2', Hauauau).click(function() {
			$(this).css({cursor: 'default'});															 
			return false;
			});
		
		
	});	
	
	
	
			
	

	$(document.body).click(function(event){	
	 $(".hang-item").fadeOut(300); 
	})


	$(".hang-item").click(
	 function(event){	
		   event.stopPropagation();
			}            
	 );	
	

	$(".text-items").find('div .green-small, .gray-small, .green-large, .gray-large').click(
	 function(event){
	 event.stopPropagation();				 
			$('.hang-item').fadeOut(300);		 
			$(this).prevAll('.hang-item:first').fadeIn(300);	
			}            
	 );


	$(".text-items").find('div').hover(
		function(){
			
			if ($(this).find('.text div').hasClass('selected')) {
				
				$(this).find('.green-small').hide();
				$(this).find('.gray-small').show();			
				$(this).find('a.active').css({backgroundColor: "#f2f5c3"})

			} else {		
			
			$(this).find('.green-small').show();
			$(this).find('a.active').css({backgroundColor: "#f2f5c3"})
			}				
					},   
		function(){	


			if ($(this).find('.text div').hasClass('selected')) {
				
			$(this).find('.gray-small').hide();	
			$(this).find('.green-small').hide();				
			$(this).find('a.active').css({backgroundColor: "#ffffff"});	

			} else {		
			
			$(this).find('.green-small').hide();
			$(this).find('a.active').css({backgroundColor: "#ffffff"});	
			}

			}
		);

	$(".point").hover(
		function(){
			$(this).addClass('marker-selection');
					},   
		function(){				
			$(this).removeClass('marker-selection');
			}
		);	
		

	$(".text").hover(
		function(){
			$(this).find('span').addClass('marker-selection');
					},   
		function(){				
			$(this).find('span').removeClass('marker-selection');
			}
		);	



		$(".unselect").click(
			 function(){
				 
				$('.selected').find('.fon2').removeClass('fon2');				 
				$('.selected').find('img').hide();	
				 
				}            
		 );
		
		
		
		
		$(".push").click(
			 function(){
				 	if ($(this).hasClass('selected')) {
								return false; 
					} else {		
						  $(".push").removeClass('selected');
						  $(".push").prev('td').find('img').hide();
						  $(".push").next('td').find('img').hide();	
						  $(this).prev('td').find('img').show();
						  $(this).next('td').find('img').show();			
						  $(this).addClass('selected');
					      $('#w').val($('.push').index($(this))); 
					      if (query.length) {
					        query[0].autocompleter.setExtraParams({w: $('#w').val()}).flushCache().refresh();
					        //return false;
					      }						  
					}					 
				 
				}            
		 );			
		
		
	$(".hover-color").hover(
		function(){
			$(this).find('span').addClass('colored');
					},   
		function(){				
			$(this).find('span').removeClass('colored');
			}
		);			



function imageholderclass(){
  this.over=new Array();
  this.down=new Array();
  this.src=new Array();
  this.store=store;
  
  function store(src, down, over){
    var AL=this.src.length;
    this.src[AL]=new Image(); this.src[AL].src=src;
    this.over[AL]=new Image(); this.over[AL].src=over;
    this.down[AL]=new Image(); this.down[AL].src=down;
  }
}

var ih = new imageholderclass();
var mouseisdown=0;

function preloader(t){
  for(i=0;i<t.length;i++){
    if(t[i].getAttribute('srcover')||t[i].getAttribute('srcdown')){
      
      storeimages(t[i]);
      var checker='';
      checker=(t[i].getAttribute('srcover'))?checker+'A':checker+'';
      checker=(t[i].getAttribute('srcdown'))?checker+'B':checker+'';
      
      switch(checker){
      case 'A' : mouseover(t[i]);mouseout(t[i]); break;
      case 'B' : mousedown(t[i]); mouseup2(t[i]); break;
      case 'AB' : mouseover(t[i]);mouseout(t[i]); 
                  mousedown(t[i]); mouseup(t[i]); break;
      default : return;      
      }
      
      if(t[i].src){t[i].setAttribute("oldsrc",t[i].src);}
    }
  }
}

function mouseup(t){
  var newmouseup;
  if(t.onmouseup){
    t.oldmouseup=t.onmouseup;
    newmouseup= 
      function(){mouseisdown=0;
                 this.src=this.getAttribute("srcover");
                 this.oldmouseup();}

  }
  else{
    newmouseup = 
      function(){mouseisdown=0;
                 this.src=this.getAttribute("srcover");}
  }
  t.onmouseup=newmouseup;
}

function mouseup2(t){
  var newmouseup;
  if(t.onmouseup){
    t.oldmouseup=t.onmouseup;
    newmouseup=function(){mouseisdown=0;
               this.src=this.getAttribute("oldsrc");
               this.oldmouseup();}
    }
  else{newmouseup=function(){mouseisdown=0;
                  this.src=this.getAttribute("oldsrc");}}
  t.onmouseup = newmouseup;
}

function mousedown(t){
  var newmousedown;
  if(t.onmousedown){
    t.oldmousedown=t.onmousedown;
    newmousedown=
      function(){if(mouseisdown==0)
                 {this.src=this.getAttribute("srcdown");
                  this.oldmousedown();}}
  }
  else{newmousedown=
       function(){if(mouseisdown==0){
                      this.src=this.getAttribute("srcdown");}}}
  t.onmousedown=newmousedown;
}

function mouseover(t){
  var newmouseover;
  if(t.onmouseover){
    t.oldmouseover=t.onmouseover;
    newmouseover= function(){this.src=this.getAttribute("srcover");
                             this.oldmouseover();}
  }
  else{newmouseover=function(){this.src=this.getAttribute("srcover");}}
  t.onmouseover=newmouseover;
}

function mouseout(t){
  var newmouseout;
  if(t.onmouseout){
    t.oldmouseout=t.onmouseout;
    newmouseout = 
   function(){this.src=this.getAttribute("oldsrc");this.oldmouseout();}
  }
  else{newmouseout=function(){this.src=this.getAttribute("oldsrc");}}
  t.onmouseout=newmouseout;
}

function storeimages(t){
  var s=(t.getAttribute('src'))?t.getAttribute('src'):'';
  var d=(t.getAttribute('srcdown'))?t.getAttribute('srcdown'):'';
  var o=(t.getAttribute('srcover'))?t.getAttribute('srcover'):'';
  ih.store(s,d,o);
}

function preloadimgsrc(){
  if(!document.getElementById) return;
  var it=document.getElementsByTagName('IMG');
  var it2=document.getElementsByTagName('INPUT');
  preloader(it);
  preloader(it2);
}

if(window.addEventListener)
  {window.addEventListener("load", preloadimgsrc, false);} 
else{
  if(window.attachEvent){window.attachEvent("onload", preloadimgsrc);}
  else{if(document.getElementById){window.onload=preloadimgsrc;}}
}



		
		
		/*****end*****/




$('.invisible').find('img').animate({opacity: 0}, 300);


jQuery('#parallax').jparallax({yorigin: '0.97'});	

jQuery('#parallax2').jparallax({yorigin: '0.33'});	

jQuery('#parallax3').jparallax({yorigin: '0.80'});	

jQuery('#parallax4').jparallax({yorigin: '0.49'});	

jQuery('#parallax5').jparallax({yorigin: '0.01'});	

jQuery('#parallax6').jparallax({yorigin: '0.17'});	





	$(function() {
		$("#accordion").accordion({ event: 'mouseover' });
	});
	

	$("#parallax, #parallax2, #parallax3, #parallax4, #parallax5, #parallax6").hover(
		function(){
			$('.invisible').find('img').css({display: 'block'}, 0);			
			$('.invisible').find('img').animate({opacity: 1}, 300);
					},   
		function(){		
	
			$('.invisible').find('img').animate({opacity: 0.2}, 300);
								
			}
		);


		var images = $('.motion img');
		var i = 0;
		function move() {
			images.filter(':visible').fadeOut(300, function() {
				images.eq(i % images.length).fadeIn(300);
			});
			i++;
			setTimeout(move, 2000)
		}
		  
		 move();





		$(".image-navigation td").each(function() {
		var a = $(this);
		
		 $("img", a).hover(											 
		  function(){	  	  
			 $('.border', a).show();
			 $('.border', a.siblings()).hide();   
			  },   
		  function(){    
			 $('.border', a).hide();
			}
		  ); 
		
		
		});



 

	$(".text-block, .text-block2").hover(
		function(){
			$('.text-area', this).addClass('color');
			$('.head', this).css({ color: '#666666', textDecoration: 'underline' });	
			$('.text3 p', this).css({ color: '#666666' });				
			$('.bottom-table1 img, .bottom-table2 img', this).show();			
					},   
		function(){				
			$('.text-area', this).removeClass('color');			
			$('.head', this).css({ color: '#ffffff', textDecoration: 'none'  });	
			$('.text3 p', this).css({ color: '#ffffff' });							
			$('.bottom-table1 img, .bottom-table2 img', this).hide();						
			}
		);	
	
	
	$(".card").hover(
		function(){
			$(this).addClass('fon');
			$('.bottom-image img', this).show(1);
					},   
		function(){				
			$(this).removeClass('fon');
			$('.bottom-image img', this).hide(1);
			}
		);		
	
	
	
	
		$(".mrk").click(
			 function(){
				 
				 
					if ($(this).hasClass('selected')) {

								return false; 
					} else {		
					
						  $('.mrk').prev('.select').fadeOut(200);				  			 					  
						  $(this).prev('.select').fadeIn(200);
						  
							var N = $('.selected').attr('id').replace('a', '');
							$('#b' + N).fadeOut(200);						  
						  
						  $('.mrk').removeClass('selected');				  			 					  
						  $(this).addClass('selected');							  
						  
							var N = $(this).attr('id').replace('a', '');
							$('#b' + N).fadeIn(200);						
					
					
					}					 
				 
				}            
		 );	
	
	


	$(".button").hover(
		function(){

			$('.button td').css({'text-decoration': 'underline'});
					},   
		function(){				
			$('.button td').css({'text-decoration': 'none'});
			}
		);	




		
		
	});

function preShowResults(results) {
$(".cover").hover(
function(){
	$(this).addClass('marker-selection');
			},   
function(){				
	$(this).removeClass('marker-selection');
	}
);
}
		
function onSelectItem(li, i) {
window.location = $(li).find('a').attr('href');
}	
function liFormat (row, i, num) {
  var result;
  if (i == (num - 1))  {
    var r=row.toString().split(',');
    var t = '';
    if (r.length == 3) {
        r[0] = r[0] - r[1] - r[2];
        if (r[0] > 0) t += r[0] + ' '+ declension(r[0], Array('тема', 'темы', 'тем')) + ', ';
        r.shift();
    }
    if (r.length == 2) {
        if (r[0] > 0) t += r[0] + ' '+ declension(r[0], ($('#w').val() == 2 ? Array('тема', 'темы', 'тем') : Array('автор', 'автора', 'авторов'))) + ', ';               r.shift();    
    }
    if (r.length == 1) 
       t += r[0] + ' '+ declension(r[0], Array('результат', 'результата', 'результатов')); 
    result = '<div class="cover2"><p class="point2"><a href="/search/?q='+ $('#q').val() +'" id="countresults">' + t + '</a></p></div>';
  }
  else {
    result = '<div class="cover"><p class="point">' + row + '</p></div>';
  }
  return result;
}

function declension(i, e)
{
    var t = i % 100;
    if (t>=5 && t<=20) {
     return e[2];
    }
    else {
        var t1 = t % 10;
        if (t1==1)
            return e[0]; 
         else 
            if (t1 >=2 && t1<=4)
                return e[1];
            else       
              return e[2];
    }
}
function b_ok(a){
    $(a).insertAfter(f);
    f.hide().find('input[type=text], textarea').each(function() {
      $(this).val($(this).attr('_value'));
    });
    f.find('.captcha').click();
    setTimeout("$('.selected-link').click();$('#p_ok').remove();f.show();", 1500);
}

 $(document.body).click(function(e) {
      var offsetT = $('.select').prev().offset();
      if (offsetT)
      if (!((offsetT.left <= e.pageX) && (e.pageX <= offsetT.left + $('.select').prev().width())
            && (offsetT.top <= e.pageY) && (e.pageY <= offsetT.top + $('.select').prev().height()))
         )
         $('.select').click();
      return true;
  });
