var autocompleteString = "";

$(window).load(function () {
	window.focus();		
	if (window.preloader_timer)
	{
		clearInterval(preloader_timer);
	}
	$("#preloader").css("display", "none");
	$("#base").css("visibility", "visible");
});


/* PRELOADER START */
$(".preloader").ready(function(){
	var preloader_i 	= 1;
	var preloader_max	= 3;
	preloader_timer = setInterval(function (){
		$("#preloader_item_" + preloader_i).addClass("active");
		if (preloader_i == 1)
		{
			$("#preloader_item_" + preloader_max).removeClass("active");						
		}
		else		
		{
			$("#preloader_item_" + (preloader_i - 1)).removeClass("active");
		}		
		if (preloader_i == preloader_max)
		{			
			preloader_i = 0;
		}
		preloader_i++;
	}, 300);
});
/* PRELOADER END */	

$(document).ready(function(){		

/* MENU MOUSEOVER & MOUSEOUT EFFECTS START */	
   $(".menu .item").mouseover(function(event){
     $(this).addClass("hover");
	 $(this).find('a').addClass("hover");
	 $(this).find('div').addClass("hover");
   });
   $(".menu .item").mouseout(function(event){
     $(this).removeClass("hover");
	 $(this).find('a').removeClass("hover");
	 $(this).find('div').removeClass("hover");
   });
/* MENU MOUSEOVER & MOUSEOUT EFFECTS END */

/* DEFAULT VALUES FOR INPUT and TEXTAREA START */
	$("input:not('.autocomplete_city'), textarea").focus(function(){
		$(this).attr("value", "");				
	});

	$("input, textarea").blur(function(){
		if ($(this).attr("value") == "")
		{			
			$(this).attr("value", $(this).attr("title"))
		}
	});
	
	$('input, textarea').each(function(){
		if ($(this).attr("value") == "")
		{
			$(this).attr("value", $(this).attr("title"))
		}
	});	
/* DEFAULT VALUES FOR INPUT and TEXTAREA END */

/* CALENDAR START */
	$("#datepicker_start, #datepicker_start_2, #datepicker_end, #datepicker_end_2").datepicker({
		minDate: "today", 
		maxDate: "+18m",		
		showOn: 'both',
		buttonImage: '/images/calendar.png',
		buttonImageOnly: true,
		dateFormat: 'd M yy',
		altFormat: 'yy-mm-dd',
		onClose: function(dateText, inst){			
			//alert($.datepicker.parseDate('mm/dd/yy', dateText));
		}
	});
	$("#datepicker_start").datepicker("option", "altField", '#date_start');
	$("#datepicker_end").datepicker("option", "altField", '#date_end');
	$("#datepicker_start_2").datepicker("option", "altField", '#date_start_2');
	$("#datepicker_end_2").datepicker("option", "altField", '#date_end_2');
/* CALENDAR END */

/* TAB START */
	$(".tab .cell.active").find('.mark').css('margin-left', ($(".tab .cell.active").width() / 2 - 4));
	
	$(".tab .cell:not(.active):not(.extra)").mouseover(function(event){
		$(this).find('.corner').css('background-position', "0px bottom");
		$(this).find('.label-cell').addClass("hover");	 
		$(this).find('.label a').addClass("hover");		
		$(this).find('.mark').css('margin-left', ($(this).width() / 2 - 4));
		$(this).find('.mark').css('display', "block");
		
	});
	$(".tab .cell:not(.active):not(.extra)").mouseout(function(event){
		$(this).find('.corner').css('background-position', "0px top");
		$(this).find('.label-cell').removeClass("hover");	 
		$(this).find('.label a').removeClass("hover");
		$(this).find('.mark').css('display', "none");
	});
/* TAB END */

/* AREA MAP START */
	$(".map_world a").mouseover(function(event){
		var s = $(this).attr("class");		
		$(".map_world .image").css('background-position', "0px -" + (117 * (parseInt(s.substring(s.indexOf("_") + 1, s.length)) + 1)) + "px");
	});
	$(".map_world a").mouseout(function(event){
		$(".map_world .image").css('background-position', "0px 0px");     
	});
/* ARAE MAP END */

/* FILTERS START */
	$(".filters .title").toggle(
		function () {			
			$(this).find('.mark').addClass("active");
			$(this).parent().find('.content').show("fast");			
		},
		function () {			
			$(this).find('.mark').removeClass("active");
			$(this).parent().find('.content').hide("fast");
		}
	);
	
	if (trackbar.getObject('one').leftLimit != trackbar.getObject('one').leftValue || trackbar.getObject('one').rightLimit != trackbar.getObject('one').rightValue || $(".filters .row.price input.availability").val() == "on")
	{
		$(".filters .row.price .title").click();
	}
	
	$(".rating .check.star input").each(function(index){
		if ($(this).is(":checked"))
		{
			$(".filters .row.star .title").click();
			return false;
		}
	});	
	
	
/* FILTERS END */

/* RATING CHECKBOX START */
	$(".rating .check.star").click(function (){
		if (!$(this).is(":checked"))
			$(".rating_all").attr("checked","");
    });
	$(".rating .check.all").click(function (){
		$(".rating .check.star input").each(function(index){
			$(this).attr("checked","");
		});			
    });
/* RATING CHECKBOX END */
	
/* READ MORE START */
	$(".expand .link").toggle(
		function () {
			$(this).html("Minimize &laquo;");
			$(this).parent().find('.short').hide();			
			$(this).parent().find('.long').show();			
		},
		function () {			
			$(this).html("Read More &raquo;");
			$(this).parent().find('.short').show();			
			$(this).parent().find('.long').hide();			
		}
	);
/* READ MORE END */

/* MAILLIST START */
	$("#maillist").submit(function(){		
		var reg = /^[a-zA-Z0-9._-]+@[a-z0-9._-]+.[a-z]{2,4}$/;
		var email = jQuery.trim($(".sign_up").val());		
		if (reg.test(email))
		{
			$.ajax({ 
				url: "/ajax/mail_list.php", 
				data: "email=" + escape(email),
				success: function (data)
				{					
					if ($.trim(data) == "ok")
					{
						$(".maillist-cell").html("<span style='font-size: 11px;'>Thanks, you have successfully subscribed to our mailing list with the email address <b>" + $(".sign_up").val() + "</b>.<br><br>We\'ll send you an occassional newsletter full of news, reviews & hot deals.</span>");				
					}
					else
					{
						$(".maillist-cell").css("display", "none");
						$(".maillist-error").css("display", "block");
					}
				}
			});
			
		}
		else
		{
			alert("Please check your email address.");
		}
		
		return false;
	});
/* MAILLIST END */

/* RESULTS PER PAGE START */
	$("#per_page").change(function(){		
		$("#form_per_page").submit();
	});
/* RESULTS PER PAGE END */

/* HOTEL IMAGE START */
	if ($("a#hotel_image").length != 0)
		$("a#hotel_image").fancybox();
	
	if ($(".gallery a.image").length != 0)
		$(".gallery a.image").fancybox();
/* HOTEL IMAGE END */

/* YBOSS IMAGES START */
	if ($(".yboss a.image").length != 0)
		$(".yboss a.image").fancybox();
/* YBOSS IMAGES END */

/* CHANGE DATES START */
	if ($(".change_dates_link").length != 0)
		$(".change_dates_link").fancybox({
			'scrolling'		: 'no'		
		});
/* CHANGE DATES END */

/* AUTOCOMPLETE START */
	if ($(".autocomplete_city").length != 0)
	{
		$(".autocomplete_city").autocomplete("/ajax/city.php");
		$(".autocomplete_city").result(function(event, data, formatted){						
			$(this).val(data);	
			window.autocompleteString = data;			
		});
		$(".autocomplete_city").focus(function(){
			if ($(this).val() != window.autocompleteString)
			{
				$(this).attr("value", "");				
			}			
		});
		$(".autocomplete_city").blur(function(){			
			window.autocompleteString = "";
		});		
	}
/* AUTOCOMPLETE END */

/* GOOGLE MAP ENLARGE START */
	if ($(".google_map").length != 0)
		$(".google_map").fancybox({
			'width': 500,
			'height': 500,
			'scrolling'	: 'no'
		});
/* GOOGLE MAP ENLARGE END */

/* NAVIGATOR START */
	$(".navigator .number").mouseover(function(event){
		$(this).addClass("hover");	 
	});
	$(".navigator .number").mouseout(function(event){
		$(this).removeClass("hover");	 
	});
/* NAVIGATOR END */

/* CONTACT FORM VALIDATION START */
	if ($(".contact form").length != 0)
	{
		$(".contact form").validate({
			errorPlacement: function(error, element) {
				error.appendTo( element.parent().parent().find(".status") );			
			},
			submitHandler: function(form) {
				$("#c_check").val(1);			
				form.submit();
			}
		});
	}
/* CONTACT FORM VALIDATION END */
});
