$(function() {
	$("#numbers_count").change(function () {
		var tpl = $("#numbers_type li:first").html();
		var tpl_i = 0;
		var numbers_count = this.value;
		
		$("#numbers_type li").each(function(i) {
			if(i >= numbers_count) {
				$(this).remove();
			}
			
			tpl_i++;
		});
		
		if(numbers_count == 1) {
			$('#numbers_type span').html('<span>&nbsp;</span>');
		}
		
		if(tpl_i > numbers_count) {
			return false;
		}
		
		for (i = tpl_i; i < numbers_count; i++) {
			$("#numbers_type").append('<li class="search-index__select-numbers-type-item">' + tpl + '</li>');
		}
		
		if(numbers_count > 1) {
			$('#numbers_type span').empty();
			
			$("#numbers_type li").each(function(i) {
				$(this).children('span').text('Номер ' + (i+1));
			});
		}
	});
	
	$("#more_options_link").click(function () {
		$(this).toggleClass('slide_show');
		$("#more_options_content").toggle();
	});
	
	$(".index-form-btn .link").click(function () {
		document.forms["index_form"].submit();
	});
	
	$("#index_tab .g-link").click(function () {
		var index_tab_index = $("#index_tab .g-link").index(this);
		var index_tab = $("#index_tab li");
		
		if (index_tab.index($("#index_tab li.nav-horizontal_selected")) == index_tab_index) {
			return false;
		}
		
		index_tab.removeClass('nav-horizontal_selected');
		index_tab.eq(index_tab_index).addClass('nav-horizontal_selected');
		
		$("#index_content .corners-gray").hide();
		$("#index_content .corners-gray").eq(index_tab_index).show();
	});
	
	 $("#index_content .index-list__item").bind("mouseenter",function(){
		 $(this).css('background-color', '#fff');
	 }).bind("mouseleave",function(){
		 $(this).css('background-color', '#F1F1F1');
	 });
	 
	 var index_list_top = $("#index_list_top");
	 var index_list_top_offset = index_list_top.offset().top;
	 var index_content = $("#index_content").width();
	
	 $(window).scroll(function () { 
		 var index_list_top_position = index_list_top.css('position');
		 
		 if (index_list_top_offset < $(window).scrollTop()) {
			 if (index_list_top_position != 'fixed') {
				 index_list_top.css({width:index_content-10,position: 'fixed',top:0,left:40});
			 }
		 } else {
			 if (index_list_top_position == 'fixed') {
				 index_list_top.css({width:'100%',position: 'static'});
			 }
		 }
	 });
	 
	//map.preview(P_index_list_row);
	 
	$('#location_index_list .g-link').click(function () {
		var obj = $(this);
			
		$.get("/hotels/location/", {location_id: obj.attr('_id')}, function(data){
			$('#overview_content').html(data);
			$().overview_show();
			var location = new Array($('#location_content_map').attr('_lat'),$('#location_content_map').attr('_lng'));

			YMap = new $.YMaps({
				div: '#location_content_map',
				lat: location[0],
				lng: location[1],
				zoom: 14,
				controls_scaleline : true
			});
				
			YMap.placemark_add($('#location_content .location-content__title').text(), $('#location_content .location-content__address').text(), location[0], location[1]);
		});		
	});
});
