$(function() {
    $("#main-loading").height($('#layout').height());
    $("#main-loading").css({opacity:0.5});

    $('#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;
    });
    
    jQuery.fn.center = function(id) {
		var w = $(window);
		var id = $("#" + id);
		
		var h = (w.height() - id.height()) / 2 + w.scrollTop();
		
		if(h < 10)
		{
			h = 10;	
		}
		
		id.css("position","absolute");
		id.css("top", h + "px");
		id.css("left", (w.width() - id.width()) / 2 + w.scrollLeft() + "px");
		
		return id;
	}

    jQuery.fn.popup_close = function(){
   	 	$("#popup").hide();
        $("#main-loading").hide();
        $("#popupMessage").empty();
        $("#popupContent").empty();
    }
    
    $('.popup-close').click(function(){
    	$().popup_close();
    });

    jQuery.fn.searchSelect = function(){
    	$("#main-loading").height($('#layout').height());
    	$().center("main-loading-i");
        $("#main-loading").show();

        $.get("/ajax/locations/", {}, function(data){
        	$('#popupContent').html(data);
        	
        	$("#popup").css({width: 930});        	
        	$().center("popup");
            $("#popup").show();

            $("#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() + (60000 * 60));

        $.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").height($('#layout').height());
    	
    	if(numberId == '0')
    	{
    		posId = hotelId;
    	}
    	else
    	{
    		posId = numberId;
    	}
    	
    	$().center("main-loading-i");    	
    	$("#main-loading").show();

    	$.get("/ajax/order/" + hotelId + "/" + numberId + "/", {}, function(data) {
    		$("#popupContent").html(data);
    		
 	        $("#popup").css({width: 800}); 	        
 	        $().center("popup");
 	    	$("#popup").show();
 	    	
 	    	$(".datepicker").datepicker();
	 	   	$(".datepicker").datepicker('option', {		
	 	   		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();
 	    	 }else{
 	    		 $("#authorizationBlock").show();
 	    	 }

 	    	$("#orderType").attr("value", this.id);

 	       });

    		var orderForm = {
    			dataType : "json",
    			url : "/ajax/order/",
    			type : "post",
    			success : function(data) {
    				if(data.reducer == 0) {
    					$('#popupMessage').html(data.message);
    					$("#orderSubmit").attr("disabled","");
    				} else {
    					
    					$('#popupMessage').empty();
    					$('#popupContent').html('<p class="text-center"><b>Ваша заявка на бронирование принята. Наш менеджер свяжется с Вами в течение 30 минут.</b><br /><br /><a href="" onclick="$().popup_close();return false;"><b>OK</b></a></p>');
    					$().center("popup");
    					
    					setTimeout(function(){ $().popup_close() }, 3000);
    				}
    			}
    		};

    		$("#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 = "";
        }
    });

    /**
     * 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
     */
	$('#rightFast a').click(function() {
		$('#rightFast li').removeClass("selected");
		$(this).parent('li').addClass("selected");

		$('#formFeedbackFastContent').hide();
		$('#formLoginFastContent').hide();

		$('#' + this.id + 'Content').show();
	});

	$('#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 popup_win(href, w, h)
{
	popupWin = window.open(href, 'contacts', 'location,width=' + w + ',height=' + h + ',top=0');
	popupWin.focus();

	return false;
}

function roller(id){
    div = document.getElementById(id);
    
    if (div.style.display == 'block') {
        div.style.display = 'none';
    }
    else {
        div.style.display = 'block';
    }
}

var params = {id:"b_h", bgcolor:"#fff", allowFullScreen:"true", allowScriptAccess:"always", wmode:"transparent",wmode:"opaque"};
new swfobject.embedSWF("http://otz.ru/share/t.swf", "b_h", "210", "305", "9.0.115", false, false, params);
