$(document).ready(function(){
	$('#scroll_next').click(function(){
		$("#scroll_return").html('<img style="margin:51px 0 0 461px" src="' + loading + '">');
		$.post(
			LOCAL_URL + 'ajax/specials.html', 
			{'mode':$(this).attr('mode')},
			function(data){$("#scroll_return").html(data);}
		);
	})	

	hs.graphicsDir = TEMP_JS + 'highslide/graphics/';
	hs.outlineType = 'rounded-white';
	hs.wrapperClassName = 'draggable-header';


	$("#weather_code").change(function(){
		$("#weather_a").attr("href","http://www.gismeteo.ua/towns/"+this.value+".htm");
		$("#weather_inf_desc_smar").html('<img src="' + loading + '">');
		$.post(
			LOCAL_URL + 'ajax/weather.html', 
			{code: this.value},
			function(data){$("#weather_inf_desc_smar").html(data);}
		);
	});

	$('#search_block_first select').change(function(){
		if($(this).attr('id') == 'id_street')return;
		var id_region = $('#id_region').val() || 0
		var id_city = $('#id_city').val() || 0
		$('#id_region').html('<option>Идет загрузка</option>');
		$('#id_region').html('<option>Идет загрузка</option>');
		$('#id_street').html('<option>Идет загрузка</option>');
		if(!$(this).attr('controller_name')) return;
		$.post(
			LOCAL_URL+'ajax/ajaxregions', 
			{ controller_name: $(this).attr('controller_name'),'city_id':id_city,'region_id':id_region},
			function(data)
			{	
				$('#id_region').html(data.region_select);
				$('#id_street').html(data.street_select);
				$('#id_city').html(data.city_select);
				return;
			}
			,'json'
		);
	})
	
	$("#search_toggle").click(function(){
		$('#panel').css({'height':'350px'});
		$(this).toggleClass("active");
		$('#panel').slideToggle(800);
		return;
/*		
		$(".reactivepanel").animate({height: "+350px"}, 400);
		$(".activepanel").animate({height: "0px"}, 400);
		$("#panel").toggleClass("activepanel");
		$(this).toggleClass("active"); return false;
//		$(this).find('.show_but').toggle();
		return false;*/
	});

	$('ul.Butt_slide li').mouseover(function(){
		$(this).addClass('Butt_sl02').removeClass('Butt_sl01');
	}).mouseout(function(){
		$(this).addClass('Butt_sl01').removeClass('Butt_sl02');
	})
});

	function switch_search_form(id){
		$('div.radio_input div').removeClass('ss02');
		$('#div_'+id).addClass('ss02');
		$('span.switch_').hide();	
		$('span.switch_ input').attr('checked',false);	
		$('span.switch_'+id).show();	
	}
	

	
	
	/* favourites */
// Функуии для работы блокнота

function addToFavorites(id_offer, id_category)
{
	$.post(
		BASE_URL_WITH_MODULE+'ajax/addfavorite',  //TODO: remove '/hotel'
		{ id_offer: id_offer, id_category: id_category },
		function(data)
		{
//	   		document.getElementById('blocknote_link').style.display = 'inline';
	   		$('#add_blocknote_'+id_offer).html('');
	   		$('#detail_id_move').css({'left':'10px'});
	   		$('#del_blocknote_'+id_offer).show();
	   		$('#fixme_count').text(data);
	   		
	   		var display_div = document.getElementById('fixme');
	   		display_div.style.display = 'block';
	   		var count_in_display_div = document.getElementById('fixme_count');
	   		count_in_display_div.innerHTML = data;
			$('#add_blocknote_'+id_offer+' input').attr('checked',false);
			$('#del_blocknote_'+id_offer+' input').attr('checked',false);
		}
	);
}


function removeFromFavorites(id_offer, id_category)
{
	if (confirm(message))
	{
		$.post(
			BASE_URL_WITH_MODULE+'ajax/removefavorite', //TODO: remove '/hotel'
			{ id_offer: id_offer, id_category: id_category },
			function(data)
			{
		   		$('#add_blocknote_'+id_offer).show();
		   		$('#del_blocknote_'+id_offer).hide();
		   		$('#fixme_count').text(data);
		   		
		   		var display_div = document.getElementById('fixme');
		   		if (data == 0) display_div.style.display = 'none';
		   		else
		   		{
			   		var count_in_display_div = document.getElementById('fixme_count');
			   		count_in_display_div.innerHTML = data;
		   		}
		   		$('#one_block_' + id_category + '_' + id_offer).removeClass('hotbox').hide();
		   		if($('.favblock_' + id_category + ' .hotbox').length == 0){
		   			$('.favblock_' + id_category).hide();	
		   			if($('.favblock_1 .hotbox').length == 0 && $('.favblock_4 .hotbox').length == 0 && $('.favblock_5 .hotbox').length == 0) {
		   				$('#items_cont').html('<h3>' + empty_favs + '</h3>');
		   			}
		   			
		   		}
			}
		);
	}
	else
	{	
		$('#del_blocknote_'+id_offer+' input').attr('checked',false);
		return false;
	}
}	

function printWindow(location)
{
	window.open(location ,"mywindow","menubar=0,resizable=1,scrollbars=1,width=950,height=650");
}

function findDimensions()
{
  	if(window.innerWidth)
	{ 
	    screen_width = window.innerWidth; 
	    screen_height = window.innerHeight; 
	} 
	else if(document.documentElement && document.documentElement.clientWidth)
	{ 
	    screen_width = document.documentElement.clientWidth; 
	    screen_height = document.documentElement.clientHeight; 
	} 
	else if(document.body && document.body.clientWidth)
	{ 
	    screen_width = document.body.clientWidth; 
	    screen_height = document.body.clientHeight; //screen.width
	} 
	else if(IE)
	{ 
	    screen_width = screen.width; 
	    screen_height = screen.height; //document.body.clientHeight; //screen.width
	}
} 

findDimensions();

function openMailForm()
{
	findDimensions()
	var mail_div = document.getElementById('mail_link_div');
	mail_div.style.top = screen_height / 2 + document.getElementById("cHTML").scrollTop - 150 + 'px';
	mail_div.style.left = screen_width / 2 + document.getElementById("cHTML").scrollLeft / 2 - 100 + 'px';
	$('#mail_link_div').toggle();
}

if (navigator.appName.indexOf("Microsoft Internet Explorer") != -1) IE = true;
else IE = false;
var screen_width = 0, screen_height = 0; 

function getMouseXY(e) 
{
	if (IE) 
	{
		tempX = event.clientX + document.getElementById("cHTML").scrollLeft;
		tempY = event.clientY + document.getElementById("cHTML").scrollTop;
	} 
	else
	{
		tempX = e.pageX;
		tempY = e.pageY;
	}
	if (tempX < 0){tempX = 0}
	if (tempY < 0){tempY = 0}
	l = tempX;
	t = tempY;
	return true;
}

function validateSend(form) {	
	var form_data = $(form).serializeArray();
	var action_file = form.action;
	var  data =	$.ajax({
			   type: "POST",
			   url: action_file,
			   data: form_data,		   
			   dataType: "json",			  			   
			   beforeSend: beforeAjaxSucces,
			   async: false,
			   success: callbackSend
			 });				
	
}

function beforeAjaxSucces(){
		$("#loading").show();
	}
	
	function callbackSend(data){
	var errors = ["mail_to","mail_from"];
	$("#loading").hide();
	if (data.validation != 1){		
		showErrors(data,errors);
		validation = false;		
	}else {
		$('#mail_link_div').toggle();
		$("#loading").hide();
	}	
}


function showErrors_(errors){
	$('span.err_label,div.err_label').attr('title','').removeClass('err_label').tooltip({});
	$('.err_input').removeClass('err_input').tooltip({});
	for (i in errors){
//		$('#' + i + '_error').html(errors[i]['comment']).show();		
		$('#' + i + '_error').attr('title',errors[i]['comment']).addClass('err_label');		
		$('#' + i + '_error').next().find('input,select').attr('title',errors[i]['comment']).addClass('err_input');		
	}
	$('span.err_label,div.err_label,input.err_input,select.err_input').tooltip({track: true,delay: 0,showURL: false,showBody: " .. "});	
}

function showErrors(data,errors){
    var located = false;
	for (x in errors){
				if (data.error[errors[x]])	{								
						$('#'+errors[x]+'_error').text(data.error[errors[x]]['comment']);
						$('#'+errors[x]+'_error').show();						
						$('#'+errors[x]+'_label').addClass('ErrorMessage');	
						if (!located) {
							window.location = '#'+errors[x]+'_a';
							located = true;
						}
					}else{
						$('#'+errors[x]+'_error').text("");
						$('#'+errors[x]+'_error').hide();								
						$('#'+errors[x]+'_label').removeClass('ErrorMessage');	
					}		
				}		
}

	function serialize_form(form){
		var rez = '';
		var i = 0;
		var t_c = $(form+" input").length + $(form+" select").length;
		$(form+" input").each(function(){
			if($(this).attr("type") == "checkbox"){
				if($(this).attr("checked")){
					rez += ($(this).attr("name"))+"="+($(this).val())+"&";
				}else{
					rez += ($(this).attr("name"))+"="+($(this).val())+"&";
				}
			}else if ($(this).attr("type") == "radio" && $(this).attr("checked")){
				rez += ($(this).attr("name"))+"="+($(form+' input[name='+$(this).attr("name")+']:checked').val())+"&"
			}else if($(this).attr("type") != "radio"){
				rez += ($(this).attr("name"))+"="+($(this).val())+"&";
			}
		})
		$(form+" textarea").each(function(){
			rez += ($(this).attr("name"))+"="+($(this).val())+"&";
		})
			$(form+" select").each(function(){
			rez += ($(this).attr("name"))+"="+($(this).val())+"&";
		})
	
	return (rez);
	}
