// JavaScript Document

/// -----------------------------------------------------------------------------
/// <procedure>externallinks</procedure>
/// <summary>
/// 	Opens external links in a new window whilst conforming xhtml strict standards
/// </summary>
/// <remarks>
/// </remarks>
/// <history>
/// 	[Gavin Bruce] 10/03/2007 Created
/// </history>
/// -----------------------------------------------------------------------------
function externallinks()
{
    var c=document.getElementById('container');
    if(c)
    {
        var ls=c.getElementsByTagName('a');
        for(var i=0;i<ls.length;i++){
            if(ls[i].getAttribute('rel')=='external')
            {
                ls[i].onclick=function(){window.open(this.href);return false}
            }
        }
    }
}

function cboDestinations_onchange(el) {
	//here we are using an <a> element called pathtohome, this determines how deep we are in the folder structure and it helps build the redirections.
	if (el.options[el.selectedIndex].value != '') {
		if (document.getElementById('pathtohome')) {
			window.location.href = 	document.getElementById('pathtohome').href + 'destinations/' + el.options[el.selectedIndex].value;
		}
	}
}

ie = document.all?1:0;
ns4 = document.layers?1:0;

function bn_rel() {
    var c=document.getElementById('container');
    if(c)
    {
        var ls=c.getElementsByTagName('a');
        for(var i=0;i<ls.length;i++){
            if(ls[i].getAttribute('rel')=='booknowlink')
            {
                ls[i].onclick=function(){
					el = this;
					var s = '';
					if (ie) {
						
						//get offer id from href attribute.
						var offerId = el.href;
						offerId = offerId.substr(offerId.length-1, 1);

						//need to go up the top up through the first table, then to the tr element of the parent table.
						while (el.tagName!="TABLE") {
							el = el.parentElement;
						}
						while (el.tagName!="TR") {
							el = el.parentElement;
						}
						var s = el.firstChild.firstChild.href;
					} else { // NN
						//need to go up the top up through the first table, then to the tr element of the parent table.
						while (el.tagName!="TABLE") {
							el = el.parentNode;
						}
						while (el.tagName!="TR") {
							el = el.parentNode;
						}
						
						//for some reason, ff thinks that firstchild is undefined and i have to loop thru the children to get the 2nd one.
						var children = el.childNodes;
						for (var i = 0; i < children.length; i++) 
						{
							if(children[i].tagName == 'TD' && s == '') {
								s = children[i].firstChild.href;
								alert(s);
							}
						};
					}
					var pathtohome = document.getElementById('pathtohome').href;
					s = s.substr(s.indexOf('/resorts/', 0)+9, s.length); // +9 to remove /resorts/ from string
					if (s != '' ) {
						window.location.href = pathtohome + 'book-now/?offerId=' + offerId + '&resort=' + s;return false;
					}
					
				}
            }
        }
    }


/*
		alert('xx');
		//need to get url from header link.
		ie = document.all?1:0;
		ns4 = document.layers?1:0;

		if (ie) {
			while (el.tagName!="TR")	
				{el = el.parentElement;}
			}
		else { // NN
			while (el.tagName!="TR")
				 {el = el.parentNode;}
		}
	
	*/
}
function booknow(offerId) {
	var pathtohome = document.getElementById('pathtohome').href;
	var s = window.location.href;	
	var ln = s.length;
	s = s.substr(s.indexOf('/resorts/', 0)+9, ln); // +9 to remove /resorts/ from string
	if (s != '' ) {
		window.location.href = pathtohome + 'book-now/?offerId=' + offerId + '&resort=' + s;
	}
	//
}


//only load gmap on first click of 'view map';
var resort_map = false;
function toggleMap() {
	if (document.getElementById('googlemapcontainer')) {
		if (document.getElementById('googlemapcontainer').style.display == 'block') {
			document.getElementById('googlemapcontainer').style.display = 'none';
		} else {
			document.getElementById('googlemapcontainer').style.display = 'block';
			if (!resort_map) {
				resort_map = true;
				loadmap();
			} 
		}
	}
}

window.onload=function(){
	
    if(!document.getElementById && !document.createTextNode){return;}
    externallinks();
	//bn_rel();
	
	
	//preview centre page google maps.
	if (document.getElementById('googlemappreviewcentre')) {
		loadmap();
	}
	//preview centre page google maps.
	if (document.getElementById('googlemappreviewcentre2')) {
		loadmap2();
	}
	
	//This function will highlight the top nav if on a brand page.
	highlightTopNav();
	
	//if we are on the /escape/frequently-asked-questions/ page, we want to change the txt next to the order online button. It would link to itself.
	escapeManualOrderOnlineText();
	
	//This function will highlight the club nav if on a club page.
	highlightClubNav();
	
	
}


function highlightTopNav() {
	
	//get page filename
	var pathname = window.location.pathname;
	

	if (pathname.indexOf('/brands/sofitel/') != -1) {
		//highlight sofitel/
		document.getElementById('topnav1').className = 'selectedtopnav';
	
	} else if (pathname.indexOf('/brands/grand-mercure/') != -1) {
		//highlight grand-mercure/
		document.getElementById('topnav2').className = 'selectedtopnav';
	
	} else if (pathname.indexOf('/brands/novotel/') != -1) {
		//highlight novotel/
		document.getElementById('topnav3').className = 'selectedtopnav';
	
	} else if (pathname.indexOf('/brands/mercure/') != -1) {
		//highlight mercure/
		document.getElementById('topnav4').className = 'selectedtopnav';
	
	} else if (pathname.indexOf('/brands/ibis-hotel/') != -1) {
		//highlight ibis-hotel/
		document.getElementById('topnav5').className = 'selectedtopnav';
	
	}
}

function escapeManualOrderOnlineText() {
	//get page filename
	var pathname = window.location.pathname;
	if (pathname.indexOf('/escape-manual/frequently-asked-questions/') != -1) {
		var el = document.getElementById('offerorderonlinetext');
		el.innerHTML = 'Click here to find out more<br /><a href="../">The Hot Deal</a>';		
	}
}

function highlightClubNav() {
	
	//get page filename
	var pathname = window.location.pathname;
	var el;
	var highlight_item_text = '';
	
	//GB: Club Overview must come last.
	//Every url in this section will match /club/
	if (pathname.indexOf('/club/properties/') != -1) {
		//Club Properties
		highlight_item_text = 'Club Properties';
		el = document.getElementById('clubpropertycol1'); //This page uses a different <DIV ID>
	
	} else if (pathname.indexOf('/club/testimonials/') != -1) {
		//Testimonials
		highlight_item_text = 'Testimonials';
		el = document.getElementById('clubleftcontent');
	
	} else if (pathname.indexOf('/club/become-a-member/') != -1) {
		//Become a member
		highlight_item_text = 'Become a member';
		el = document.getElementById('clubleftcontent');
	
	} else if (pathname.indexOf('/club/frequently-asked-questions/') != -1) {
		//Frequently Asked Questions
		highlight_item_text = 'Frequently Asked Questions';
		el = document.getElementById('clubleftcontent');
	
	} else if (pathname.indexOf('/club/contact/') != -1) {
		//Contact us
		highlight_item_text = 'Contact us';
		el = document.getElementById('clubleftcontent');
	
	} else if (pathname.indexOf('/club/') != -1) {
		//Club Overview
		highlight_item_text = 'Club Overview';
		el = document.getElementById('clubleftcontent');
		
	}
			   
			   
			  
			  
	if (highlight_item_text != '') {
	
		//This will highlight the club nav item we want.
		for (i=0; i<el.childNodes.length; i++){
			
			//search for ul
			if (el.childNodes[i].nodeName == 'UL') {
				ul = el.childNodes[i];
				
				for (j=0; j<ul.childNodes.length; j++){
					
					if (ul.childNodes[j].nodeName == 'LI') {
						li = ul.childNodes[j];
						//alert(ul.childNodes[1].firstChild.href);
						a = li.firstChild;
						aText = a.firstChild.nodeValue;
						
						//highlight the class.
						if (aText.toLowerCase() == highlight_item_text.toLowerCase()) {
							//a.className = 'selectedclubnav';	
							a.style.color = '#007BC4';
						}
					}
				}
			}
		}
	}	
}

// Header functions
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function headerNavigation() {
	var strLocation = document.frmNav.txtNavigation.value;
	if(strLocation != "")
		window.location.href = strLocation;
	else
		alert("Please select one.");
}