var hotel_preview_i = 0;

$(function() {
    $("#main-loading").height($('body').height());
    $("#main-loading").css({opacity:0.5});

    hotel_preview_i = $("#hotel_preview_i").offset().top;

    $('#searchAdvancedLink').click(function(){
        div = document.getElementById('searchAdvancedContent');

        if (div.style.display == 'block') {
            div.style.display = 'none';

            $("#searchAdvanced").val(0);
        }
        else {
            div.style.display = 'block';

            $("#searchAdvanced").val(1);
        }

        return false;
    });

    $('#popupClose').click(function(){
        $("#popup").hide();
        $("#main-loading").hide();
        $("#popupMessage").empty();
        $("#popupContent").empty();
    });

    jQuery.fn.searchSelect = function(){
        $("#main-loading").show();

        $.get("/gethttp.popup.php", {
            reducer: "locations"
        }, function(data){

        	$("#popup").css({ top:$("#searshSelectLocationSelected").offset().top - 250, width: 930});

            $("#popup").show();
            $('#popupContent').html(data);

            $("#main-loading").height($('body').height());
        });
    }

    jQuery.fn.uncheckAll = function(){
    	$("#locationSelectedBlock input:checked").each(function(){
    		$(this).attr("checked","");
    	});
    }

    jQuery.fn.getLocationSelected = function(){
        var c = '-1';

        $("#searshSelectLocationSelected").empty();

        $("#locationSelectedBlock input:checked").each(function(){
            $("#searshSelectLocationSelected").append(this.value + '<input type="hidden" name="location[]" value="' + this.id.substr(10) + '"><br>');

            c += ',' + this.id
        });

        if(c == '-1')
        {
        	$("#searshSelectLocationSelected").append('Любое');
        }

        var date = new Date();

        date.setTime(date.getTime() + (3 * 24 * 60 * 60 * 1000));

        $.cookie('serchLocation', c, {
            path: '/',
            expires: date
        });

        $("#popup").hide();
        $("#main-loading").hide();
        $("#popupMessage").empty();
        $("#popupContent").empty();
    }

    var tpl_i = 0;

    jQuery.fn.getNumber = function(){
    	if(tpl_i > 3) {
    		alert("Сожалеем, но в одной заявке нельзя бронировать больше 5 номеров");

    		return false;
    	}

    	deleteTemplate = '<a href="javascript:void(0)" onclick="$().deleteNumber($(this).parents(\'.order-tpl\').attr(\'id\'))" class="order-tpl-btn"><img src="/i/btn_close_mini.png" alt="" /></a>';

    	$('#orderNumbersAdd').append('<div class="order-tpl" id="orderTemplate_' + tpl_i + '">' + $('#orderPeoplesTemplate').html() + $('#orderNumbersTemplate').html() + deleteTemplate + '</div>');

    	tpl_i++;
    }

    jQuery.fn.deleteNumber = function(id){
    	$('#' + id).remove();

    	$("#orderNumbersAdd .order-tpl").each(function(i){
    		$(this).attr("id","orderTemplate_" + i);
    	});

    	tpl_i--;
    }

    /**
     * Order main function
     */
    jQuery.fn.orderHotel = function(hotelId, numberId, afterId) {
    	$("#main-loading").show();

    	if(numberId == '0')
    	{
    		posId = hotelId;
    	}
    	else
    	{
    		posId = numberId;
    	}

    	$.get("/gethttp.popup.php", {
    		hotelId : hotelId,
    		numberId : numberId,
    		reducer : 'order',
    		param: 'getForm'
    	}, function(data) {
    		$("#popupContent").html(data);

 	        $("#popup").css({ top:$("#btnOrderId" + posId + afterId).offset().top - 170, width: 800});

 	    	$("#popup").show();

    		$("#orderDateInput").datepicker();
 	        $('#orderDateInput').datepicker('option', {
 	            dateFormat: 'dd-mm-yy',
 	            numberOfMonths: 2,
 	            duration: 10
 	        });

 	        $("#orderDateOutput").datepicker();
 	        $('#orderDateOutput').datepicker('option', {
 	            dateFormat: 'dd-mm-yy',
 	            numberOfMonths: 2,
 	            duration: 10
 	        });

 	       $("a.registration-link").click(function(){
 	    	  this.blur();

 	    	 $("a.registration-link").removeClass("selected");
 	    	 $("#" + this.id).addClass("selected");

 	    	 if(this.id == 'nobodyLink'){
 	    		 $("#authorizationBlock").hide();
 	    		 $("#orderEmailNoAuthBlock").show();
 	    	 }else{
 	    		 $("#authorizationBlock").show();
 	    		 $("#orderEmailNoAuthBlock").hide();
 	    	 }

 	    	 if(this.id == 'authorizationLink'){
 	    		 $("#registrationBlock").hide();
 	    	 }else{
 	    		 $("#registrationBlock").show();
 	    	 }

 	    	$("#orderType").attr("value", this.id);

 	       });

		    $("#orderHotels").change( function() {
		    	$("#main-loading").css({"z-index":"1010"});

		    	$.get("/gethttp.popup.php", {
		    		hotelId : this.value,
		    		reducer : 'order',
		    		param: 'getNumbes'
		    	}, function(data) {
		    		$('#orderNumbersContent').html(data);
		    		$("#main-loading").css({"z-index":"1004"});
		    	});
		    });

    		var orderForm = {
    			dataType : "json",
    			url : "/gethttp.popup.php",
    			type : "post",
    			success : function(data) {
    				if(data.reducer == 0) {
    					$('#popupMessage').html(data.message);
    					$("#orderSubmit").attr("disabled","");
    				} else {
    					document.location.href = '/' + headerRedirectLink + '/';
    				}
    			}
    		};

    		$("#orderForm").validate({
    			submitHandler : function(form) {
    				$("#main-loading").show();

					$("#orderSubmit").attr("disabled","disabled");

    				$(form).ajaxSubmit(orderForm);
    			},
    			focusInvalid : false,
    			focusCleanup : true,
    			rules : {
	                orderDateInput: {
	                    required: true
	                },
	                orderDateOutput: {
	                    required: true
	                }
    			},
    			errorPlacement : function(error, element) {
    				var er = element.attr("name");

    				if(er == 'orderCheckbox')
    				{
    					$("#" + er + "Block").addClass("error");
    				}

    				return false;
    			}
    		});

    	}, "text");
    }

    /**
     * Serach form
     */
	var searchKeyword = 'Поиск';

    if ($("#searshSelectInput").val() != searchKeyword) {
        $("#searshSelectInput").attr("style", "color:#000");
    }

    $("#searshSelectInput").focus(function() {
        $(this).attr("style", "color:#000");

        if ($("#searshSelectInput").val() == searchKeyword) {
            this.value = "";
        }
    });

    // set cookie currency by date
    $('a.get-currency').click(function(){
        var date = new Date();

        date.setTime(date.getTime() + (60000 * 60));

        $.cookie('currency_cookie', this.id, {
            path: '/',
            expires: date
        });
    });

    /**
     * Sort get cookies
     */
    $('li.get-sort a').click(function() {
        var date = new Date();

        date.setTime(date.getTime() + (60000 * 60));

        $.cookie('sort_cookie', this.id, {
            path: '/',
            expires: date
        });
    });

    /**
     * Column right tabs
     */
	$('.search-form-header a').click(function() {
		$('.search-form-header a').removeClass("selected");
		$(this).addClass("selected");

		$('#search_name_content').hide();
		$('#search_params_content').hide();

		$('#' + this.id + '_content').show();

		return false;
	});

	$('#photogallery img').click(function(){
		var path = $(this).parent().attr('href');
		var alt = $(this).attr('alt');

		$('#photogalleryContent').animate({opacity: 0}, 200,function(){
			$(this).html('<img src=' + path + ' />').find('img').bind('load', function() {
				$(this).parent().append('<em>' + alt + '</em>').animate({opacity: 1}, 200);
		    });
		});

		return false;
	});

	$('#photogallery img:first').click();
});

function roller(id){
    div = document.getElementById(id);

    if (div.style.display == 'block') {
        div.style.display = 'none';
    }
    else {
        div.style.display = 'block';
    }

    return false;
}

function popup_win(href, w, h)
{
	popupWin = window.open(href, 'contacts', 'location,width=' + w + ',height=' + h + ',top=0');
	popupWin.focus();

	return false;
}

var params = {id:"b_h", bgcolor:"#fff600", allowFullScreen:"true", allowScriptAccess:"always", wmode:"transparent",wmode:"opaque"};
new swfobject.embedSWF("/files/header.swf", "b_h", "990", "140", "9.0.115", false, false, params);

$(window).scroll(function () {
	div = document.getElementById('hotel_preview_i');
	
	if(div === null)
	{
		return;
	}

	if((document.documentElement.scrollTop || document.body.scrollTop) > hotel_preview_i) {
		div.style.position = 'fixed';
	} else {
		div.style.position = 'relative';
	}
});