$(document).ready(function(){
    showHeaderDrop("#lang-menu-link1", "#lang-menu-div1");
    showHeaderDrop("#head-menu-link1", "#head-menu-div1");
    showHeaderDrop("#head-menu-link2", "#head-menu-div2");
    showHeaderDrop("#head-menu-link3", "#head-menu-div3");
    showHeaderDrop("#auth-menu-link1", "#auth-menu-div1");
    //showHotelDrop("#price_hotel_link1", "#price_hotel_popup1");
    //showHotelDrop("#price_hotel_link2", "#price_hotel_popup2");  
	
	//$(".gallery a[rel^='prettyPhoto']").prettyPhoto({theme:'dark_rounded'});
	$('.tip').tooltip({delay:0,fade:250,showURL:false});
	
	//$('.date-pick').datePicker().val(new Date().asString()).trigger('change');
	$('.date-pick').datePicker();

/*	
    $("#actual_slider").easySlider({prevId:'actual_slider_prev',nextId:'actual_slider_next',nextClass:'sliderNext',prevClass:'sliderPrev',count:4,controlsFade:false});
    $("#video_slider").easySlider({prevId:'video_slider_prev',nextId:'video_slider_next',count:4});
    $("#photo_slider").easySlider({prevId:'photo_slider_prev',nextId:'photo_slider_next',count:4});
    
	$("#our_direction_slider").jCarouselLite({
	    auto: 4000,
	    speed: 2000,
	    visible: 1,
	    //btnNext: "#our_direction .next",
    	//btnPrev: "#our_direction .prev",
    	scroll: 1
	});
*/	
	/*$("input").focus(function () {
		$(this).effect("highlight", {}, 1000);
        //$(this).css('border','1px solid #0066CC');
        $(this).addClass("input_selected");
    });
    $("input").blur(function () {
    	$(this).effect("highlight", {}, 1000);
        //$(this).css('border','1px solid #0066CC');
        $(this).removeClass("input_selected");
    });*/

	// Hide Calendar		
	document.onmousedown=function(e){
		var n=!e?self.event.srcElement.name:e.target.name;
		if (document.layers) {
			with (gfPop) var l=pageX, t=pageY, r=l+clip.width, b=t+clip.height;
			if (n!="popcal"&&(e.pageX>r||e.pageX<l||e.pageY>b||e.pageY<t)) $('iframe.iframeCalendar').hide();
			return routeEvent(e);	// must use return here.
		} else if (n!="popcal") $('iframe.iframeCalendar').hide();
	}
	if (document.layers) document.captureEvents(Event.MOUSEDOWN);
	
	// Автозаполнение главной поисковой строки
    function liFormat (row, i, num) {
        var result = row[0] + '<p class=qnt>' + row[1] + '</p>';
        return result;
    }
    function selectItem(li) {
        if( li == null ) var sValue = 'ничего не выбрано!';
        if( !!li.extra ) var sValue = li.extra[2];
        else 
            var sValue = li.selectValue;            
        window.location = 'http://' + window.location.hostname + '/ru/hotel/' + sValue; 
    } 
    $("#search_hotel").autocomplete("/ru/hotel/null/suggest", {
        delay:200,
        minChars:2,
        matchSubset:1,
        autoFill:false,
        matchContains:1,
        cacheLength:1,
        selectFirst:true,
        formatItem:liFormat,
        maxItemsToShow:15,
        onItemSelect:selectItem
    });    
    //------- Get Printed Materials By Mail
    var options_GetByMailPM = {
		success: function(html) {
			if (html=='true'){$("#captca_image-error").remove();}
			else{
	 			$("#captca_image-error").remove();
	 			$('#captca_image-input').parent().append("<div id='captca_image-error' class='error'>Введите код указаный на картинке</div>");
				$('#captca_image-input').addClass('error');
	 		}
		}		
	};    
    
    $(".show_hide").click(function () {
        $(this).next().css({"background-color":"transparent"});
        $(this).next().toggle(100);
    });

    $("#chAgree").click(function () {
        if($(this).is(':checked'))
        $("#btnBook").removeAttr("disabled");
        else
        $("#btnBook").attr("disabled","disabled");
    });

});
function GetRecommendedHotels(){}

function open_close_loginform(){
	var parent = "#auth-link-come";
	var child = "#auth-div-come";	
	jQuery.each(jQuery.browser, function(i, val) {
		if(i == 'msie' && val){
			$(child).css({'left' : $(parent).position().left + 'px'});
		}
	});
	if ($(parent).hasClass("drop-menu-el-link-hover")){
		$(parent).removeClass("drop-menu-el-link-hover");
		$('#login_form').hide();
		$(child).hide();
	}else{
		$(parent).addClass("drop-menu-el-link-hover");		
		$('#login_form').show();
		$(child).show();
	}
	$('#recover_password').hide();
	$('#auth_error').hide();
}
//---- FORM ----
// - Reset form
// - delete all selected="selected"
function reset_from(form){
	$(form).resetForm();
	$("form select option").each(function(n,element){
    	$(element).removeAttr("selected");
	});
}
function send_form(form){
	for (i=0; i<form.length; i++) {
		if (form.elements[i].value==""){
			$(form.elements[i]).attr('disabled')='disabled';
		}
	}
	$(form).submit();
}
function selectOption(select_id, option_val){
    $('#'+select_id+' option:selected').removeAttr('selected');
    $('#'+select_id+' option[value='+option_val+']').attr('selected','selected');
}
function send_search_form(form){
	if(($(form.search).val()=='')||($(form.search).val().toString().length<=2)){
		$('#head_search .error_search_mess').show('slow');
		setTimeout(function(){$('#head_search .error_search_mess').hide('slow');}, 3000);
	}else{
		$(form).submit();
	}
}
function send_search_form_KeyPress(myfield,e,form){
	var keycode;
	 if (window.event) keycode = window.event.keyCode;
	 else if (e) keycode = e.which;
	 else return true;	
	 if (keycode == 13) {
		send_search_form(form);
	 	return false;
	 }
}
function submitForm(form){
	$(form).submit();
}
// ORDER
function post_to_url(path, params, method) {
    method = method || "post";
    var form = document.createElement("form");
    form.setAttribute("method", method);
    form.setAttribute("action", path);
    for(var key in params) {
        var hiddenField = document.createElement("input");
        hiddenField.setAttribute("type", "hidden");
        hiddenField.setAttribute("name", key);
        hiddenField.setAttribute("value", params[key]);
        form.appendChild(hiddenField);
    }
    document.body.appendChild(form);
    form.submit();
}
// SET Multiselect
function loopSelected(id_multiselect,id_text){
	var txtSelectedValuesObj = document.getElementById(id_text);
	var selectedArray = new Array();
	var selObj = document.getElementById(id_multiselect);
	var i;
	var count = 0;
	for (i=0; i<selObj.options.length; i++) {
		if (selObj.options[i].selected) {
		selectedArray[count] = selObj.options[i].value;
		count++;
		}
	}
	txtSelectedValuesObj.value = selectedArray;
}
//------Change search----
var type_search = '';
function set_type_search(type,element){
	type_search = type;
	$('#change_search_type li a').removeClass("active");
	$(element).addClass("active");
	$('#head_search #hotel').hide();
	$('#head_search #allsite').hide();
	$('#head_search #numbertoure').hide();
	$('#change_search_type').hide();
	$('#'+type).show();	
}
function changeBg() {
    var bg = $("#header").css("background-image");
    if(bg == 'url("http://devel.turtess.com.ua/public/i.zf/www/design/header-bg8.jpg")')
    $("#header").css('background-image', 'url(/public/i.zf/www/design/header-bg.jpg)');
    else
    $("#header").css('background-image', 'url(/public/i.zf/www/design/header-bg8.jpg)');
}
function clearBg() {
    var bg = $("#header").css("background-color");
    if(bg == 'transparent' || bg == '#ffffff')
    $("#header").css('background-color', '#bce1fb');
    else
    $("#header").css('background-color', '#ffffff');
    $("#header").css('background-image', 'none');
}
function showTopMenuDrop(parent,child) {
	if (($(parent))&&(child!=false)){
		$(parent).mouseover( function(){
			jQuery.each(jQuery.browser, function(i, val) {
			  if(i == 'msie' && val){
				$(child).css({'left' : $(parent).position().left + 'px'});
			  }
			});
			$(child).show();
			$(parent).addClass("drop-menu-el-link-hover");
		});
		var drop_el = $(parent).parent();
		$(parent).mouseout(function () {
			$(child).hide();
			$(this).removeClass("drop-menu-el-link-hover");
		});
		$(child).mouseover(function () {
			$(this).show();
			$(parent).addClass("drop-menu-el-link-hover");
		});
		$(child).mouseout(function () {
			$(this).hide()
			$(parent).removeClass("drop-menu-el-link-hover");
		});
	}else{	
		$(parent).mouseover(function () {
			$(parent).addClass("without_cat");
		});
		$(parent).mouseout(function () {
			$(parent).removeClass("without_cat");
		});		
	}
}

function showHotelDrop(parent,child) {
    if (($(parent))&&(child!=false)){
        $(parent).mouseover( function(){
            jQuery.each(jQuery.browser, function(i, val) {
              if(i == 'msie' && val){
                 $(child).css({'left' : $(parent).position().left + 200 +'px'});
                 $(child).css({'top' : $(parent).position().top - 18 +'px'});
              } });
            $(child).css({'left' : $(parent).position().left + 200 +'px'});
            $(child).css({'top' : $(parent).position().top - 18 +'px'}); 
            
            
            $(this).oneTime(250, 'timer2', function(i) {    $(child).show(); });
            
             
        });
        $(parent).mouseout(function () {  $(this).stopTime('timer2');   $(child).hide();  });
        
        
        $(child).mouseover(function () {  $(this).show(); });
        $(child).mouseout(function () {  $(this).hide() });
    }else{    
        $(parent).mouseover(function () {
            $(parent).addClass("without_cat");
        });
        $(parent).mouseout(function () {
            $(parent).removeClass("without_cat");
        });        
    }           
}

function showHeaderDrop(parent,child) { /* Отображение по клику - переименовать на showTopMenuDrop, а showTopMenuDrop - на _old*/
    if (($(parent))&&(child!=false)){
        $(parent).click( function(){
            jQuery.each(jQuery.browser, function(i, val) {
              if(i == 'msie' && val){
                $(child).css({'left' : $(parent).position().left + 'px'});
              }
            });
            $(".drop-menu-el-div:not(#" + child + ")").hide();
            $(".drop-menu-el table:not(#" + parent + ")").removeClass("drop-menu-el-link-hover");
            $(child).toggle();
            $(parent).toggleClass("drop-menu-el-link-hover");
        });
    }
}





// === AUTORIZATION ===
function login_KeyPress(event){
	if (event.keyCode==65){
		login();
	}
}
function login(){
	var user = $('#user_login_ajax').val();
	var pass = $('#user_password_ajax').val();
	var remember_me = $('#remember_me').is(':checked');
	var data = 'user_login='+user + '&user_password='+pass + '&oper=login&remember_me='+remember_me+'';
	$.ajax({
		type:"POST", url:"/login/", data:data,cache:false,
		success: function(html){			
	         if (html=='isError'){$('#auth_error').show();}
	         else{
	 			$('#auth').html(html);
	 			window.location.reload();
	 		 }
		},
		error: function(html){
			$('#auth_error').show();
		}
	});
}
function logout(){	
	$.ajax({
		type:"POST", url:"/login/", data:'oper=logout', cache: false,
		success: function(html){ 
			if (html=='isError'){
				 $('#auth_error').show();
	 		}else{
	 			$('#auth').html(html);
	 			window.location.reload();
	 		} 
		}
	});
}
function recoveryPass(){
	var login = $('#user_login_recover_ajax').val()
	var user = $('#input_restore_email').val()
	var data = 'user_login='+login +'&user_email='+user + '&oper=recovery';
	$.ajax({
		type: "POST",
		url: "/login/",
		data: data,
		cache: false,
		success: function(html){
			 if (html=='isError'){
				 $('#recover_password_error').show();
	 		}else {
	 			$('#recover_password').hide();
	 			if(html=='isSucces_one'){
	 				$('#recover_password_inf_one').show();
	 			}
	 			if(html=='isSucces_all'){
	 				$('#recover_password_inf_all').show();
	 			}
	 		}
		}
	});
}
function closeLogin(element){
	var parent = $(element).parent();
	$(parent).hide();
}
function submitenter(myfield,e){
	var keycode;
	 if (window.event) keycode = window.event.keyCode;
	 else if (e) keycode = e.which;
	 else return true;
	 if (keycode == 13) {
		login();
	 	return false;
	 }
}
if (document.images) {
}
// stop hiding -->
shown=''
function popup(url, width, height) {
if( shown=='' || shown.closed) {
    shown=window.open("/static/img.php?imgpath="+url,"",'scrollbars=no,status=no, high, width='+width+', height='+height);
}
else{ 
    shown.close();
    shown=window.open("/static/img.php?imgpath="+url,"",'scrollbars=no,status=no, high, width='+width+', height='+height);
}
}
function popup2(url, width, height) {
if( shown=='' || shown.closed) {
    shown=window.open(url,"",'scrollbars=yes,status=no, high, width='+width+', height='+height);
}
else{ 
    shown.close();
    shown=window.open(url,"",'scrollbars=yes,status=no, high, width='+width+', height='+height);
}
}