// Site Script

var g_openingShortList = false;
var g_shortListOpen = false;

$(document).ready(function() { InitPage(); } );

function InitPage() 
{
	InitSearchBox();
    InitSearchPanel();
	CheckLanguage();
	PageFixes();
	
	$("#shortlist a").click(ToggleShortlist);
	$("#shortlist a").attr("href", "#");
	$("p.ex_AddButton a[href*='epaction=add']").unbind();
	$("p.ex_AddButton a[href*='epaction=add']").toggle(AddProduct,RemoveProduct);
	$("p.ex_AddButton a[href*='epaction=remove']").unbind();
	$("p.ex_AddButton a[href*='epaction=remove']").toggle(RemoveProduct,AddProduct);

	$("*").click(function()
	{
		if( $(this).hasClass("dynMapContainer") )
		{
			$(this).find("p.ex_AddButton a").each(function()
			{
				$(this).unbind();
				
				if( $(this).hasClass("ep_Adding") )
					$(this).click(AddProduct);
				else if( $(this).hasClass("ep_Added") )
					$(this).click(RemoveProduct);
					
				$(this).click();
				return false;
			});
		}
		return true;
	});
	
}

function ClickFunction(objItem) {
	if( $(objItem).hasClass("dynMapContainer") )
	{
		$(objItem).find("p.ex_AddButton a").each(function()
		{
			$(objItem).unbind();
			
			if( $(objItem).hasClass("ep_Adding") )
				$(objItem).click(AddProduct);
			else if( $(objItem).hasClass("ep_Added") )
				$(objItem).click(RemoveProduct);
				
			$(objItem).click();
			return false;
		});
	}
	return true;	
}

function DisplayMessageBox(id, message)
{
	if( id.indexOf("#") == -1 )
		id = "#" + id;
		
	$(id + " .message").html(message);
	$(id).show();
}

/* checks to see if the user has selected a language */
function CheckLanguage()
{
	var SiteURL = location.href;
	
	$("#languageSelect").show();

	$("#languageSelect").hoverIntent(function()
	{
		if (SiteURL.indexOf("au.vne.nmdemo.net") != -1 || SiteURL.indexOf("northeastengland.com.au") != -1)
		// AU Language Selector
		{
			$("#languageSelect .uk img").attr("src", "/images/global/au-flag-on.jpg");				
		}
		else if (SiteURL.indexOf("nz.vne.nmdemo.net") != -1 || SiteURL.indexOf("northeastengland.co.nz") != -1)
		// NZ Language Selector
		{
			$("#languageSelect .uk img").attr("src", "/images/global/nz-flag-on.jpg");				
		}
		else
		{
			// $("#header").css("position","relative");
			$("#languageSelect .uk img").attr("src", "/images/global/uk-flag-on.jpg");	
		}

		$("#languageSelect").animate({height: "229px"}, 200);
	
		BrowserFix(1);
		
	},
	function()
	{
		if (SiteURL.indexOf("au.vne.nmdemo.net") != -1 || SiteURL.indexOf("northeastengland.com.au") != -1)
		// AU Language Selector
		{
			$("#languageSelect .uk img").attr("src", "/images/global/au_lang_selector.jpg");
		}
		else if (SiteURL.indexOf("nz.vne.nmdemo.net") != -1 || SiteURL.indexOf("northeastengland.co.nz") != -1)
		// NZ Language Selector
		{
			$("#languageSelect .uk img").attr("src", "/images/global/nz_lang_selector.jpg");
		}
		else
		{
			$("#languageSelect .uk img").attr("src", "/images/global/lang_selector.jpg");
		}

		$("#languageSelect").animate({height: "28px"}, 200);
		// $("#header").css("position","static");			

		BrowserFix(0);
	});
	
	// only do this for the home page
	/*if( window.location.pathname == "/" && $.cookie("language") == null )
	{
		window.location.href = "/site/language";
	}*/
}

function BrowserFix(intToggle)
{
	if ( $.browser.msie )
		{
			BrowserVersion = $.browser.version;
			
			if (BrowserVersion.indexOf('6.') != -1)
			{
				if (intToggle)
				{
					$("select#src_category").css("visibility","hidden");
					$("select#src_polygon").css("visibility","hidden");				
				}
				else
				{
					$("select#src_category").css("visibility","visible");
					$("select#src_polygon").css("visibility","visible");				
				}
			}
		}
}

var g_searchBgImage;

/* hides the search panel if needed */
function InitSearchPanel()
{
	if( window.location.href.indexOf("/search-results/") != -1 || 
			window.location.href.indexOf("/tic-search-results/") != -1 )
	{
		var ele = $("#colright .accommodation-search form.searchform");
		
		if (ele.length == 0 ) ele = $("#colright .event-search form.searchform");
		if (ele.length == 0 ) ele = $("#colright .seeanddo-search form.searchform");
		
		if( ele.length == 1 && window.location.href.indexOf("refine-availability=on") == -1 )
		{
			g_searchBgImage = ele.css("background-image");
			ele.css({height:"0px",paddingTop:"30px",backgroundImage:"url(/images/srch_new_hdr.png)",overflow:"hidden", cursor:"pointer"});
			ele.find("ul.advanced").hide();
			ele.attr("title", "Click to do a new search.");
			ele.one("click", function() 
			{
				$(this).css({height:"auto",paddingTop:"37px",backgroundImage:g_searchBgImage,cursor:"default"});
				ele.removeAttr("title");
				ele.find("ul.advanced").show();
			});
		}
		
		// Change Accommodation Search Results Header if booking online
		if (window.location.href.indexOf("accommodation/search-results/") != -1 && window.location.href.indexOf("refine-availability=on") != -1)
		{$("div#header h1").html("Book Accommodation Online");}
	}
}

/* animates the add product click */
function AddProduct()
{
	var id = $(this).attr("class").replace("ep_","").replace(" ep_Add","");
	var offset;

	$.ajax(
	{
		type: "GET",
		url: "/site/shortlist-current?epaction=add&epkey=" + id,
		cache: false,
		success: function() 
		{
			$.ajax(
			{
				type: "GET",
				url: "/site/shortlist-current?epaction=savedms",
				cache: false
			});
		}
	});
	

	offset = $(this).offset();
	newA = $(this).clone();
	newA.css("position","absolute");
	newA.css("z-index","3500");
	newA.css("opacity","0.8");
	newA.css("background-color", "#f1ebdb");
	$("body").append(newA);
	newA.css({top:offset.top,left:offset.left});
	toLeft = $("#shortlist").offset().left;
	
	//newA.animate( {top:$("html").scrollTop(), left:toLeft + "px"}, 753, function() 
	newA.animate( {top:$("html").scrollTop(), left:toLeft + "px"}, 1000, function() 																				   
	{
		$(this).fadeOut(function() 
		{
			$(this).remove();		
		});
	});
	
	$(this).removeClass("ep_Add");
	$(this).addClass("ep_Added");
	$(this).find("img").attr("alt", $(this).find("img").attr("alt").replace("Add ", "Remove "));
	$(this).find("img").attr("src", "/images/button_removefromitinerary.gif");
	
	return false;
}

function RemoveProduct()
{
	id = $(this).attr("class").replace("ep_","").replace(" ep_Added","");
	id = id.replace(" ep_Add", "");
	jQuery.get("/site/shortlist-current?epaction=remove&epkey=" + id);
	
	$(this).removeClass("ep_Added");
	$(this).addClass("ep_Add");
	$(this).find("img").attr("alt", $(this).find("img").attr("alt").replace("Remove ", "Add "));
	$(this).find("img").attr("src", "/images/button_addtoitinerary.gif");
	
	return false;
}

/** shows or hides the shortlist */
function ToggleShortlist() 
{
	if( !g_openingShortList && g_shortListOpen == false )
	{
		g_openingShortList = true;
		DisplayShortlist(true);
	}
	else if( !g_openingShortList && g_shortListOpen == true )
	{
		g_openingShortList = true;
		DisplayShortlist(false);
	}
	
	return false;
}

/* initialises the site search box */
function InitSearchBox()
{
	var defaultSearchText = "Search site";
	$("div#header div.sitesearch span.field input").val(defaultSearchText);
	
	$("div#header div.sitesearch span.field input").focus(function()
	{
		if( $(this).val() == defaultSearchText)
			$(this).val("");
	});
	
	$("div#header div.sitesearch span.field input").blur(function()
	{
		if( $(this).val() == "")
			$(this).val(defaultSearchText);
	});
}

/* removes the unwanted areas of the page */
function RemoveGlobalAreas()
{
	$("#header").remove();
	$("#footer").remove();
	$("#bodyfooter").remove();
	$("#bodywrapper").css("background","#fff");
	$("html").css("background-color","#fff");
	$("#wrapper").css("margin","0");
	$("#wrapper").css("width","340px");
}

/* Last minute page bug fixes */
function PageFixes()
{
	// Ensure right col Accomm  goes to correct page in Leisure Cycling Accomm
	if (window.location.href.indexOf("/what-to-see-and-do/activities/leisure-cycling/cyclists-welcome-accommodation") != -1 )
	{
		$("div.accommodation-search form").attr("action","/site/accommodation/search-results/searchresults");
		$("div.accommodation-search ul.advanced li a").attr("href","/site/accommodation/search-results/advanced");
	}

	// Ensure right col Accomm  goes to correct page in Walking Accomm
	if (window.location.href.indexOf("/what-to-see-and-do/activities/walking/walkers-welcome-accommodation") != -1 )
	{
		$("div.accommodation-search form").attr("action","/site/accommodation/search-results/searchresults");
		$("div.accommodation-search ul.advanced li a").attr("href","/site/accommodation/search-results/advanced");
	}

	// Ensure right col See-Do goes to correct page in Family Fun
	if (window.location.href.indexOf("/what-to-see-and-do/family-fun/") != -1 )
	{
		$("div.seeanddo-search form").attr("action","/site/what-to-see-and-do/search-results/searchresults");
		$("div.seeanddo-search ul.advanced li a").attr("href","/site/what-to-see-and-do/search-results/advanced");
	}

	// Ensure right col Events goes to correct page in Family Fun
	if (window.location.href.indexOf("/what-to-see-and-do/family-fun/events") != -1 )
	{
		$("div.event-search form").attr("action","/site/what-to-see-and-do/events/search-results/searchresults");
		$("div.event-search ul.advanced li a").attr("href","/site/what-to-see-and-do/events/search-results/advanced");
	}
	
	// Add Previous/Next arrows on paging
	$("ul.paging li a").each ( function(i)
	{
		if ( $(this).html() == "prev" )
		{ $(this).html("<img src='/images/pages_leftarrow.gif' alt='previous page'>"); }

		if ( $(this).html() == "next" )
		{ $(this).html("<img src='/images/pages_rightarrow.gif' alt='next page'>"); }
	});

	// Ensure Location is retained in Right Col Searches
	strURL = window.location.href;
	
	if (strURL.indexOf('src_polygon=') != -1 && strURL.indexOf('/search-results/') != -1)
	{
		strFind = strURL.split("src_polygon=");
		strFind = strFind[1].split("&");
		strComma = /%2C/g;
		strCurrentPolygon = strFind[0].replace(strComma, ",");
		
		// Select relevant location on right-column
		$("#colright select#src_polygon option").each(function()
		{
			value = $(this).attr("value");
			
			if (value == strCurrentPolygon)
			{$(this).attr("selected","selected");}
		});
	}
	
	// Add Call to Book on product detail
	if (strURL.indexOf('/accommodation/search-results/') != -1 && $("#colmain .ctl_ProductDetail") )
	{
		if ($("#colmain .ctl_ProductDetail p.bookonline").length == 0)
		{
			$("#colmain .ctl_ProductDetail").prepend("<p class='bookbytel'><img src='/images/main/proddetail_callnowtobook.gif' alt='Book by phone'/></p>");
		}
	}
	
	// Add Hover to Book Online
	$("input.btnBookStay").hover(
		function()
		{
			$("input.btnBookStay").attr("src","/images/main/booking/book_hover.gif");
		},
		function()
		{
			$("input.btnBookStay").attr("src","/images/main/booking/book.gif");
		});
	
	// Hide attractions special offer on Accomm Advanced Search
	if (strURL.indexOf('/site/accommodation/search-results/advanced') != -1)
	{
		SpecList = document.getElementById("src_specoff");
		SpecList.remove(4);
		SpecList.remove(4);
	}

	// Hide accomm special offer on See & Do Advanced Search
	if (strURL.indexOf('/site/what-to-see-and-do/search-results/advanced') != -1)
	{
		SpecList = document.getElementById("src_specoff");
		SpecList.remove(1);
		SpecList.remove(1);		
	}
	
	// Accessiblity Logo ALT text
	$("div.tfa a").attr("title","View accessibility information");
	$("div.tfa a img").attr("alt","View accessibility information");
	
	// Defaulting Fuzzy Search to +/- 7 days for Self-Catering
	$("div.ctl_ProductSearch select#src_category").change(function(){
	
		var ProdTypeSelID = $(this).attr("selectedIndex");	
		var ProdTypeList = $(this).attr("options");
		var ProdType = ProdTypeList[ProdTypeSelID].text;
		
		var FuzzySelID = $("div.ctl_ProductSearch select#fuzzy").attr("selectedIndex");
		var FuzzyList = $("div.ctl_ProductSearch select#fuzzy").attr("options");
		var FuzzyVal = FuzzyList[FuzzySelID].value;
			
		if (ProdType == 'Self Catering' && FuzzyVal == 0)
		{
			$("div.ctl_ProductSearch select#fuzzy").attr("selectedIndex","3");
		}
	});	
	
}
