(function($) {   
	$(document).ready(function(){
	
	  $('.tableNews li').live('mouseover',function(){
		$(this).addClass('hov');
	  }).live('mouseout',function(){$(this).removeClass('hov')})
	  // main researchBlock
	  $('#researchMainList li:first').addClass('lastRight');
	  $('#researchMainList li:last').addClass('lastLeft');
	  
	  // partners slide block
	  psl = parseInt($('#parntersSlideList li:first').width()) + parseInt($('#parntersSlideList li:first').css('marginRight'));
	  c = $('#parntersSlideList li').length;
	  $('#parntersSlideList').css('width',(psl*c)+'px');
	  $('#parntersSlideRight').live('click',function(){
		var marg = parseInt($('#parntersSlideList li:first').width()) + parseInt($('#parntersSlideList li:first').css('marginRight'));
		var c = $('#parntersSlideList li').length;
		var shift = $('#parntersSlideList').data('shift');
		if(empty(shift)) shift = 0;
		if(shift<c-2){
		  shift++;
		  $('#parntersSlideList').data('shift',shift);
		  $('#parntersSlideList').animate({left:((-1)*shift*marg)+'px'},'slow')
		}
	    return false;
	  })
	  $('#parntersSlideLeft').live('click',function(){
		var marg = parseInt($('#parntersSlideList li:first').width()) + parseInt($('#parntersSlideList li:first').css('marginRight'));
		var shift = $('#parntersSlideList').data('shift');
		if(empty(shift)) shift = 0;
		if(shift>0){
		  shift--;
		  $('#parntersSlideList').data('shift',shift);
		  $('#parntersSlideList').animate({left:((-1)*shift*marg)+'px'},'slow')
		}
	    return false;
	  })
	  //
	  $('.mainBannerBlockCenter').each(function(){
		subitems = $(this).find('.banner_content');
	    if(subitems.length<1) $(this).css('display','none');
	  })
	  
	  
	  // corners
	  /*
	  $('.cornedBlock').each(function(){
	    l = $(this).find('.corner');
	    if(l.length==0){
	      $(this).append('<div class="corner cornerLeftTop"></div>'+
			'<div class="corner cornerLeftBottom"></div>'+
			'<div class="corner cornerRightTop"></div>'+
			'<div class="corner cornerRightBottom"></div>'+
			'<div class="shadow shadowRight"></div>'+
			'<div class="shadow shadowLeft"></div>'+
			'<div class="shadow shadowBottom"></div>'+
			'<div class="shadow shadowTop"></div>');
	    }
	  })
	  */
	  // teasers/news  indus was there
	  $('.teasersList td').mouseenter(function(event){
			table = $(this).parent().parent();
			last = $(table).data('lastActive');
			index = $(table).find('td').index(this);
			if(empty(last) && last!==undefined) {last = 0;$(table).data('lastActive',0);} // по умолчанию
			else if(last===undefined) {if(index<=2) last = index+3; else last = index-3;$(table).data('lastActive',last);}
			if(last == index){
			  if(index<=2) last = index+3;
			  else last = index-3;
			} 
			last_tr = $(table).find('tr').index($(table).find('td:eq('+last+')').parent());
			index_tr= $(table).find('tr').index($(this).parent());
			if(last_tr == index_tr){
			  if(index<last) $(this).find('.articleArr').addClass('articleArrRight');
			  else $(this).find('.articleArr').addClass('articleArrLeft');
			} else{
			  if(last_tr<index_tr) $(this).find('.articleArr').addClass('articleArrTop');
			  else $(this).find('.articleArr').addClass('articleArrBottom');
			}
			$(table).find('td:eq('+last+')').find('.wrap').append('<div class="teaserImgShow">'+$(this).find('.teaserImg').html()+'</div>');
			$(this).addClass('hover');
		return false
		//alert(index+' - '+last);
	  })
	  $('.teasersList td').mouseleave(function(event){
			table = $(this).parent().parent();
			last = $(table).data('lastActive');
			index = $(table).find('td').index(this);
			$(table).data('lastActive',index);
			$(this).removeClass('hover');
			$(table).find('.articleArr').removeClass('articleArrBottom').removeClass('articleArrTop').removeClass('articleArrRight').removeClass('articleArrLeft');
			$(table).find('.teaserImgShow').remove();
	    return false
	  })
	  $('.teasersList').mouseout(function(){
	    $(this).data('lastActive',0)
	  })
	  $('.teasersList td img,.teasersList td .articleArr').mouseover(function(event){
			$(this).parent().mouseleave(); 	
	    return false
	  })
	  /*dynamic_resolution();
	  $(window).resize(dynamic_resolution);*/
	});
	
	// -- обрабочик изменения размера окна браузера
	dynamic_resolution = function() {
		c = false;
		w = $(window).width();
		r = $(window).data('resolution');
		if(w <= 1220 && (r == 0 || typeof r == 'undefined')) {
			$('#site').addClass('site17');
			$(window).data('resolution', 1);
			c = true;
			r = 1;			
			
		} else if(w > 1220 && (r == 1 || typeof r == 'undefined')) {
			$('#site').removeClass('site17');
			$(window).data('resolution', 0);
			c = true;
			r = 0;
		}
		if(c) {
			$.ajax({
				url: '/application/set_resolution/'+r+'/',
				success: function(msg) { }
			});
		}
	}
	/*
	// подписка
	$('.headRightLinks .subscribeBtn,.mainLastNews .archiveContainer .subscribeBtn,.mainNewsBlock_1 .subscribeBtn').live('click',function(){
		$.ajax({
				url: '/distribution/form/',
				success: function(res) { 
				  $('body').prepend(res);
				  var doc    = document.documentElement || document.body;
				  var elTop = Math.round($(document).scrollTop()+($(window).height()-$('#subscribeForm').height())/2)+'px';
				  //var elTop  = Math.round(doc.scrollTop+(doc.clientHeight-$('#subscribeForm').height())/2)+'px';
				  $('#subscribeForm').css('top',elTop).show();
				  $('#shadow').show();
				  return false;
				}
		});
		return false;
	})
	*/
	
	// $('#mainSubscriptionForm').live('submit',function(){
		// $.ajax({
				// url: '/distribution/form/',
				// data: $(this).serialize(),
				// type:	  	'POST',
				// cache:    	false,
				// success: function(res) { 
				  // $('body').prepend(res);
				  // var doc    = document.documentElement || document.body;
				  // var elTop = Math.round($(document).scrollTop()+($(window).height()-$('#subscribeForm').height())/2)+'px';
				  // // var elTop  = Math.round(doc.scrollTop+(doc.clientHeight-$('#subscribeForm').height())/2)+'px';
				  // $('#subscribeForm').css('top',elTop).show();
				  // $('#shadow').show();
				  // return false;
				// }
		// });
		// return false;
	// });
	
	/*
	$('#mainSubscriptionForm .customSubscribeBtn').live('click',function(){
	  $.ajax({
				url: '/distribution/form/',
				data: $('#mainSubscriptionForm').serialize(),
				type:	  	'POST',
				cache:    	false,
				success: function(res) { 
				  $('body').prepend(res);
				  var doc    = document.documentElement || document.body;
				  var elTop = Math.round($(document).scrollTop()+($(window).height()-$('#subscribeForm').height())/2)+'px';
				  //var elTop  = Math.round(doc.scrollTop+(doc.clientHeight-$('#subscribeForm').height())/2)+'px';
				  $('#subscribeForm').css('top',elTop).show();
				  $('#shadow').show();
				  return false;
				}
		});
		return false;
	})
	*/
	$('#mainSubscriptionForm .customSubscribeBtn').live('click',function(){
		$('#mainSubscriptionForm').submit();
	})
	
	subscribeFormSubmit = function(){
		$.ajax({
				url: 		$('#subscribeForm').attr('action'),
				type:	  	'POST',
				cache:    	false,
				data:  		$('#subscribeForm').serialize(),
				
				success: function(res) { 
				  switch(res){
				    case '-1':
					  alert('Поле email заполнено не по формату');
					break;
					case '-2':
					  alert('Не выделено ни одной рубрики для подписки');
					break;
					case '-3':
					  if(confirm('Такой email уже присутствует в базе подписки. Выслать на почту инструкции по изменению параметров подписки?')){
					    $.ajax({
					      url:			'/distribution/send_change_email/',
						  type:	  		'POST',
						  cache:    	false,
						  data:  		$('#subscribeForm').serialize(),
						  success: function(res) { 
						    alert('Письмо с инструкциями по изменению параметров рассылки отправлено');
						  }
					    });
					  }
					break;
				    case '1':
					  alert('Вы подписаны на бесплатную рассылку с сайта "Автостат"');
					  $('#subscribeForm').remove();
					  $('#shadow').hide();
					break;
				  }
				  return false;
				}
		});
		return false;
	}


	//$('#subscribeForm').live('submit',subscribeFormSubmit);
	$('#subscribeForm button').live('click',function(){
	  $('#subscribeForm').remove();
	  $('#shadow').hide();
	  return false;
	})
	
	$('#mainResearchLeft').live('click',function(){
	  if($('#researchMainList').data('animate')) {
		return false;
	  }
	  $('#researchMainList').data('animate', true);
	  index = $('#researchMainList li').index($('#researchMainList li.lastLeft'));
	  c = 
	  $('<li '+($('#researchMainList li:first').hasClass('dark')?'':'class="dark"')+'>'+$('#researchMainList li:eq('+index+')').html()+'</li>').prependTo($('#researchMainList')).css('marginLeft','-196px').animate({
		marginLeft: '2px'
	  }, 'slow', function() {
		$('#researchMainList').data('animate', false);
	  });
	  $('#researchMainList li.lastLeft').removeClass('lastLeft');
	  $('#researchMainList li:eq('+(index)+')').addClass('lastLeft');
	  return false;
	})
	
	$('#mainResearchRight').live('click', function() {
		if($('#researchMainList').data('animate')) {
			return false;
		}
		$('#researchMainList').data('animate', true);
		index = $('#researchMainList li').index($('#researchMainList li.lastRight'));
		shift = $('#researchMainList li.lastRight').outerWidth(true);
		left = parseInt($('#researchMainList').css('marginLeft')) - shift;
		$('#researchMainList').append('<li '+($('#researchMainList li:last').hasClass('dark')?'':'class="dark"')+'>'+$('#researchMainList li:eq('+index+')').html()+'</li>');
		$('#researchMainList li.lastRight').removeClass('lastRight');
		$('#researchMainList li:eq('+(index+1)+')').addClass('lastRight');
		$('#researchMainList').animate({
			marginLeft: left+'px'
		}, 'slow', function() {
			$('#researchMainList').data('animate', false);
		});
		return false;
	})
	
	// yandex_search
	initSearchBtn = function() {    		
		// $('#site')
		 // .find('.yaform__submit')
		  // .hide() 
		   // .after('<a href="#" id="searchBtn2"></a>');
		// $('#searchBtn2').css({'margin-right':'8px'}).live('click',function(){
			// $('.yaform__form').submit();
		// });
		
		
		
		// $('div#yandexform0').css('background-image','none');
		// $('#yandexform0 .yaform__search').css('background-color','#f00 !important');
		// $('#yandexform0 .yaform__search td').css('background-color','#f00 !important');
		// $('#yandexform0 .yaform__precise-i').css('background-color','#f00 !important');
		
		
		// $('#site').find('.yaform__input-text').css({'width':'130px','float':'right'});
		// $('#site').find('.yaform__input-text').live('change', function() { 
			// $('.b-suggest').hide();  
		// });  		
	}
	// end yandex search
})(jQuery);


