var browser = 0;
var cur = 'GBP';
var cur_symbol = '%A3';

$(function()
{
	getCurrency();
	setSunday(1);
	setGuests(2);
	window.name='HDBookPage';
	//showKidsAge2();
	resetAdvancedSearch(1);

	if (currency == 'euro')
	{
		cur = 'EUR';
		cur_symbol = '%E2%82%AC';
	}
	if (currency == 'usdollar')
	{
		cur = 'USD';
		cur_symbol = '%24';
	}
	
	if($.browser.msie && $.browser.version=="7.0")
	{
		browser = 7;
		$('.search_button').css({'bottom':'4px'});
	}
	if($.browser.msie && $.browser.version=="8.0")
	{
		browser = 8;
		$('.search_button').css({'bottom':'4px'});
	}

	$('div.city-hotels p').mouseover(function(){
		$(this).addClass('green');
		$('a', this).addClass('underline');
	}).mouseout(function(){
		$(this).removeClass('green');
		$('a', this).removeClass('underline');
	}).click(function(){
		//doGoal2($('a', this).attr('href'));
		document.location.href = $('a', this).attr('href');
	});
	
	for (var i = 0; i < f.city_select.length; i++)
	{
		if (f.city_select[i].checked == true)
		{
			$(f.city_select[i]).parent().addClass("selected");
			showAdvSearchLink($(f.city_select[i]).val());
		}
	}
	
	$(".city_select").click(function(){
		$('div.city').removeClass("selected");
		$(this).parent().addClass("selected");
		$('input#city_name').val("Enter your destination").css({'color':'#666'});
		$('form#home_form input[name="city"]').val($(this).val());
	});
	
	$(".button").mouseover(function(){
		$(this).addClass("over");
	});
	$(".button").mouseout(function(){
		$(this).removeClass("over");
	});
	
	$('#room_exp').click(function(e){
		openWindow($(this).attr('href'),'700','600','y');
		e.preventDefault();
	});
	
	$(".link").mouseover(function(){
		$("a", this).attr("style", "color:#000000");
	});
	$(".link").mouseout(function(){
		$("a", this).attr("style", "");
	});
	
	$(".link").click(function(){
		location.href = $("a", this).attr("href");
		return false;
	});
	
	$("#rooms").change(function(){
		for (i = 2; i <= 5; i++)
		{
			$("#room_" + i).hide();
		}
		for (i = 2; i <= $(this).val(); i++)
		{
			$("#room_" + i).show();
		}
		$('select.room').each(function(){
			if (!$(this).parent().is(':visible'))
			{
				$(this).get(0).selectedIndex = 0;
			}
		});
		if ($(this).val() > 1)
		{
			$("#roomtype1").parent().find('p.sleeps').html('Room 1 sleeps');
			$('#search-form').addClass('kids');
			$('#accommodates').show();
			setGuests(3);
		}
		else
		{
			$("#roomtype1").parent().find('p.sleeps').html('Room sleeps');
			$('#search-form').removeClass('kids');
			$('#accommodates').hide();
		}
		showHideKids();
	});
	
	$(".room").change(function(){
		showHideKids();
		setGuests(3);
	});
	
	$(".kidsage").change(function(){
		setGuests(3);
	});
	
	$("#nights").change(function(){
		setEndDate($(this).val());
	});
	
	$("#arrivedate, #arrivedate2").change(function(){
		setEndDate($("#nights").val());
	});
	
	$("#departdate, #departdate2").change(function(){
		setNights();
	});
	
	$(".advanced_2").change(function(){
		sel = $(this).val();
		resetHomeAdvanced();
		$(".advanced_2").css({'color':'#666'});
		$("#postcode").val("Postcode").css({'color':'#666'});
		$('input#city_name').val('Enter your destination').css({'color':'#666'});
		$(this).val(sel);
		$(this).css({'color':'#000'});
		if (sel == '' || sel == '%%')
		{
			$(this).css({'color':'#666'});
		}
	}).focus(function(){
		$(this).css({'color':'#000'});
	}).blur(function(){
		sel = $(this).attr("selectedIndex");
		if (sel == '' || sel == '%%')
		{
			$(this).css({'color':'#666'});
		}
	});
	
	$(".advanced_2").css({'color':'#666'});
	$(".advanced_2 option:selected").each(function(){
		sel = $(this).val();
		$(this).parent().css({'color':'#000'});
		if (sel == '' || sel == '%%')
		{
			$(this).parent().css({'color':'#666'});
		}
	});
	
	$("#show_search").click(function(){
		$('#advanced_search').slideToggle('fast', function(){
			if ($(this).is(":visible"))
			{
				document.cookie = "advanced=1";
				if (browser == 7)
				{
					$('.search_button').css({'top':'427px'});
				}
			}
			else
			{
				document.cookie = "advanced=0";
				if (browser == 7)
				{
					$('.search_button').css({'top':'205px'});
				}
			}
			if (browser == 8)
			{
				$('.search_button').css({'bottom':'4px'});
			}
		});
		return false;
	});
	
	$(".city_select").click(function(){
		showAdvSearchLink($(this).val());
	});

	$("#city2").change(function(){
		if ($(this).val() == "London" || $(this).val() == "Barcelona" || $(this).val() == "Dublin" || $(this).val() == "Edinburgh" || $(this).val() == "Paris" || $(this).val() == "Rome" || $(this).val() == "Amsterdam" || $(this).val() == "Prague" || $(this).val() == "New York" || $(this).val() == "Dubai")
		{
			$('#show_search').show();
			populateAdvanced($(this).val());
		}
		else
		{
			$('#show_search').hide();
			$('#advanced_search').hide();
		}
		c = document.form.city.length;
		checkcity = 0;
		for (i=0; i<c; i++)
			document.form.city[i].checked=false;
	});

	$("#postcode").click(function(){
		if ($(this).val() == "Postcode")
		{
			$(this).val("").css({'color':'#000'});
		}
	}).blur(function(){
		if ($(this).val() == "")
		{
			$(this).val("Postcode").css({'color':'#666'});
		}
		else
		{
			var pc = $(this).val();
			resetHomeAdvanced();
			$(".advanced_2").css({'color':'#666'});
			$(this).val(pc);
		}
	}).val("Postcode").css({'color':'#666'});
	
	$("#last").submit(function(){
		if ($("#last_city").val() == "")
		{
			alert('Please select a city');
			return false;
		}
	});
	
	$("#review_submit").click(function(){
		if ($("#review_city").val() == "")
		{
			alert('Please select a city');
			return false;
		}
		else
		{
			document.location = $("#review_city").val();
		}
	});

	$('.search_button').css({'bottom':'4px'});

	$("#city_name").autocomplete(availableCities, {
		width: 320,
		highlight: false,
		multiple: false,
		scroll: false,
		scrollHeight: 300
	}).focus(function(){
		f = document.form;
		for (var i = 0; i < f.city_select.length; i++)
		{
			f.city_select[i].checked = false;
		}
		$('div.city').removeClass("selected");
		if ($(this).val() == "Enter your destination")
			$(this).val("").css({'color':'#333'});
	}).blur(function(){
		if ($(this).val() == "")
			$(this).val("Enter your destination").css({'color':'#666'});
		showAdvSearchLink($(this).val());
	}).css({'color':'#666'}).val("Enter your destination");

	$('form#home_form').submit(function(){
		if ($.trim($('input#city_name').val()) != '' && $.trim($('input#city_name').val()) != 'Enter your destination')
		{
			var dest = $.trim($('input#city_name').val());
			dest = checkSpelling(dest);
			var new_city = '';
			var new_location = '';
			var new_attraction = '';
			for (var x = 0; x < cityLocations.length; x++)
			{
				if (dest.toLowerCase().indexOf(cityLocations[x].location.toLowerCase()) > -1)
				{
					new_location = cityLocations[x].location;
					if (cityLocations[x].location_alt != '')
					{
						new_location = cityLocations[x].location_alt;
					}
					if (cityLocations[x].attraction)
					{
						new_attraction = cityLocations[x].attraction;
					}
					new_city = cityLocations[x].city;
					break;
				}
			}
			if (new_location != '')
			{
				for (var i = 0; i < f.city_select.length; i++)
				{
					if (f.city_select[i].value == capitalizeString(new_city))
					{
						f.city_select[i].checked = true;
						$(f.city_select[i]).parent().addClass("selected");
						showAdvSearchLink($(f.city_select[i]).val());
					}
				}
				$('input[name="city"]').val(capitalizeString(new_city));
				$('input[name="city_name"]').val(capitalizeString(new_location));
				resetHomeAdvanced();
				document.cookie = "advanced=0";
				
				if (new_attraction != '')
				{
					$('input[name="attraction"]').val(new_attraction + '|' + new_location);
				}
				else
				{
					$('input[name="location"]').val(new_location);
				}
				return checkCitySearch();
			}
			else
			{
				var new_dest = '';
				if (dest.toLowerCase().indexOf('new york')==-1)
				{
				 for (var i = 0; i < checkCities.length; i++)
				 {
					if (dest.toLowerCase().indexOf(checkCities[i]) > -1)
					{
						new_dest = checkCities[i];
						break;
					}
                                 }
				}
				if (new_dest != '')
				{
					$('form#home_form input[name="city"]').val(capitalizeString(new_dest));
					return checkHomeSearch();
				}
				else
				{
					var test = checkCitySearch();
					if (test == undefined)
					{
						whiteLabelSearch(wordToUpper(dest));
					}
					return false;
				}
			}
		}
		else
		{
			return checkHomeSearch();
		}
		return false;
	});
	
	$('div.viewed').mouseover(function(){
		$('p', this).addClass('underline').css({'color':'#333'});
	}).mouseout(function(){
		$('p', this).removeClass('underline').css({'color':'#3a9938'});
	}).click(function(){
		//doGoal2($('a', this).attr('href'));
		document.location.href = $('a', this).attr('href');
	});
	
	$('div.viewed-more').mouseover(function(){
		$('p.name', this).addClass('underline').css({'color':'#333'});
	}).mouseout(function(){
		$('p.name', this).removeClass('underline').css({'color':'#3a9938'});
	}).click(function(){
		//doGoal2($('a', this).attr('href'));
		document.location.href = $('a', this).attr('href');
	});
	
	getLastViewed();
});

function showAdvSearchLink(city)
{
	if ((city == "London") || (city == "Barcelona") || (city == "Dublin") || (city == "Edinburgh") || (city == "Paris") || (city == "Rome") || (city == "Amsterdam") || (city == "Prague") || (city == "New York") || (city == "Dubai"))
	{
		$('#show_search').show();
		$('#adv_city').html(city);
		populateAdvanced(city);
		readAdvancedCookie();
	}
	else
	{
		$('#show_search').hide();
		$('#advanced_search').hide();
	}
}

function resetHomeAdvanced()
{
	$('input[name="attraction"]').val('');
	$('input[name="location"]').val('%%');
	$('select#attraction').val('')
	$('select#rail').val('%%')
	$('select#tube').val('%%')
	$('input#postcode').val('Postcode')
	$('select#location').val('%%')
	$('select#category').val('%%')
	$('select#price').val('%%')
	$('select#hotelname').val('%%')
	$('select#facilities').val('')
}
