(function($) {
  $(document).ready(function(){
	resolution_x = document.body.clientWidth;
	  if(resolution_x>1220){
	    resolution = 1;
	  }
	  else {resolution=0;$('#site').addClass('site17');}
	  $(window).resize(dynamic_resolution);
	$('#cities_choise_redirect select').live('change',function(){
	  $('#cities_choise_redirect').submit();
	})
    $('#site .mainMenu td div.menu_container_1').append('<div class="mm_active_left"></div><div class="mm_active_right"></div>');
	$('#site .mainMenu td.active a:first').addClass('item_link');
	
	$('#site #rightContentBlock .table').each(function(){
	  $(this).find('tr').each(function(){
	    index = $(this).parent().find('tr').index($(this));
		if(index%2 == 0) $(this).addClass('hov');
	  })
	});
	$('#site #rightContentBlock .table').each(function(){
	  $(this).find('tr:last').addClass('noback');
	})
	// main menu
		$('#site .mainMenu td.childs').live('mouseover',function(){$(this).addClass('hov')}).live('mouseout',function(){$(this).removeClass('hov')});
		$('#site .mainMenu td .submenu').append('<div class="corner leftTop"></div><div class="corner leftBottom"></div><div class="corner rightTop"></div><div class="corner rightBottom"></div>');
		$('#site .mainMenu td .submenu').each(function(){
			$(this).css('width',$(this).parent().width()); 
			$(this).css('margin-left',12);
		})

	// calendars
	$('#holydays_container .calendars .calendar_container .calendar a').live('click',function(){
		var target = 'holyday_load_container' + ($(this).attr('rel') > 1 ? $(this).attr('rel') : '');
		$('#'+target).load($(this).attr('href')+'/1');
		return false;
	})
	$('.calendar_container h3 a').live('click',function(){
	  $(this).parent().parent().parent().load($(this).attr('href'));
	  $('.calendar_container h3 a.'+($(this).hasClass('right')?'right':'left')).not(this).each(function(){
	    $(this).parent().parent().parent().load($(this).attr('href'));
	  });
	  return false;
	})
	$('#main_calendar_block .calendar a').live('click',function(){
	  return false;
	})
	$('#moon_calendar_block .calendar a').live('click',function(){
	  return false;
	})
	// calendars end
	// main page
	$('#main_day_holydays ul').each(function(){
	  $(this).find('li:last').css('border','0');
	  $(this).find('li:first').css('paddingLeft','0');
	})
	// main page end
	// tables
	$('table.partnership tr').each(function(){
	  $(this).find('td:last').addClass('noborder');
	})
	// trademarks left block
	$('.trademarks_block a.left').live('click',function(){
	  to_move = $(this).parent().find('ul'); 
	  v = $(to_move).data('move');
	  if(empty(v) || v == 'undefined' || v == undefined) v=0;
	  if(v<0){
	    v++;
		$(to_move).data('move',v);
		def = parseInt($(to_move).find('li:first').width())+22;
		$(to_move).animate({'marginLeft':((v*def)+22)+'px'},'fast');
	  } if(v>=0) $(this).hide();
	  $('.trademarks_block a.right').show();
	  return false;
	})
	$('.trademarks_block a.right').live('click',function(){
	  to_move = $(this).parent().find('ul'); 
	  count = $(to_move).find('li').length;
	  v = $(to_move).data('move');
	  if(empty(v) || v == 'undefined' || v == undefined) v=0;
	  if(Math.abs(v)<count-1){
	    v--;
		$(to_move).data('move',v);
		def = parseInt($(to_move).find('li:first').width())+22;
		$(to_move).animate({'marginLeft':((v*def)+22)+'px'},'fast');
	  }
	  if(Math.abs(v)>=count-1) $(this).hide();
	  $('.trademarks_block a.left').show();
	  return false;
	})
	// trademarks left block end
	$('.cornerBlock .img').live('mouseover',function(){$(this).addClass('hover');}).live('mouseout',function(){$(this).removeClass('hover')})
	/* where to buy */
	$('#expand_shops').live('click',function(){
	  $('#shops_list_show').animate({maxHeight:'1000px',height:'auto'},'slow');
	  $(this).hide();
	  $('#turn_shops').show();
	  return false;
	})
	$('#turn_shops').live('click',function(){
	  $('#shops_list_show').animate({maxHeight:'190px'},'slow');
	  $(this).hide();
	  $('#expand_shops').show();
	  return false;
	})
	$('#wheretobuy_select').live('change',function(){
	  $.ajax({
			url:   '/shops/load_city_shops/'+$(this).val(),
			type:  'POST',
			cache: false,
			success: function(response) { 
				if(response!=''){
					$('#wheretobuy_shops').html(response);
				}
			}
		}); 
		return false;
	})
	
	/* where to buy end */
	/* recalls begin */
	$('.recalls_list li a').live('click',function(){
	  var answer_element = $(this).parent().parent().find('.loadAnswerForm');
	  $.ajax({
			url:   $(this).attr('href'),
			type:  'POST',
			cache: false,
			success: function(response) { 
				if(response!=''){
					$(answer_element).html(response);
				}
			}
		}); 
		return false;
	})
	/* recalls end */
	/* main_page recalls scrollbar */
	

	/* main page recalls scrollbar end */
	/* contests */
	$('#oneParticipant .likeItBtn a,#participants_image .likeItBtn a, #view_participant .likeItBtn a').live('click',function(){
	   response_item = ($(this).hasClass('imgLink')?$(this).parent().find('likeItBtn a'):$(this));
	   $.ajax({
			url:   $(this).attr('href'),
			type:  'POST',
			cache: false,
			success: function(response) { 
				switch(response){
				  case '0': alert('Ошибка при голосовании');break;
				  case '-1': alert('Вы уже голосовали за эту работу. Попробуйте проголосовать завтра или проголосуйте за другого номинанта');break;
				  case '-2':alert('Голосование в рамках этого конкурса уже закончилось');break;
				  case '1': alert('Спасибо. Ваш голос учтен');count = parseInt($(response_item).text());$(response_item).text(count+1);break;
				}
			}
		}); 
	  return false;
	})
	$('#contestParticipantsBlock p.img img,#participants_image a.imgLink').live('click',function(){
	  $.ajax({
			url:   '/contests/load_participants/'+$(this).attr('rel'),
			type:  'POST',
			cache: false,
			success: function(response) { 
			  $('#oneParticipant').remove();
			  if(response=='0') return false; // если что-то нахимичили
			  $('#shadow').show();
			  $('body').prepend(response);
			  var elTop = Math.round($(document).scrollTop()+($(window).height()-$('#oneParticipant').height())/2)+'px';
			  $('#oneParticipant').css('top',elTop);
			}
		}); 
		return false;
	})
	$('#ParticipantClose').live('click',function(){
	  $('#oneParticipant').remove();
	  $('#shadow').hide();
	  return false;
	})
	$('#ParticipantRight,#ParticipantLeft').live('click',function(){
	  $.ajax({
			url:   $(this).attr('href'),
			type:  'POST',
			cache: false,
			success: function(response) { 
			  if(response!='0')$('#oneParticipant').html(response);
			}
		}); 
		return false;
	})
	$('#contestsAllWorks').live('click',function(){
	  $.ajax({
			url:   $(this).attr('href'),
			type:  'POST',
			data: {'is_ajax':'1'},
			cache: false,
			success: function(response) { 
			  if(response!='0') $('#participants_text,#participants_image').html(response); 
			}
		}); 
		return false;
	})
	$('#participantRecallsSlide').live('click',function(){
	  if($(this).hasClass('participantRecallsSlideDown')){
	    $(this).text('Свернуть список').removeClass('participantRecallsSlideDown');
	  }else{
	    $(this).text('Развернуть список').addClass('participantRecallsSlideDown');
	  }
	  $('.recalls_list').slideToggle('slow');
	  return false;
	})
	/* contests end */
	$('#wheretobuy_redirect_select').live('change',function(){
	  $('#catalogWhereToBuy').submit();
	})
  })
})
(jQuery);

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) { }
			});
		}
		$('#site .mainMenu td .submenu').each(function(){
			$(this).css('width',$(this).parent().width()+12)
		})
	}
