// TUI-UK PowerSearch Javascript Library v0.01 - 23 November 06

// Copyright (c) 2006 TUI-UK (http://www.thomson.co.uk)
// Contributors:
//     David Eglin (http://www.influxx.co.uk)
//     Prem Ghinde (http://smwweb.co.uk)


// Show/Hide div script, for use with search_results.html
// initially, and later with any other page which requires
// this functionality.  This script also includes the ability
// to change the link text appropriately.
//
// NOTE: Removed legacy support for document.layers etc

function toggleLayer(whichLayer,linkID)
{
   var parentStyle = document.getElementById(whichLayer).parentNode.style;
	if (document.getElementById)
	{
      var style2 = document.getElementById(whichLayer).style;
		style2.display = style2.display? "":"block";
	}

   var content = document.getElementById(linkID);
	if ( linkID != null ){
		if ( linkID == "viewresultmap" ) {
			if ( content.innerHTML == "View results on a map" ) {
			content.innerHTML = "Hide map";
			}
			else {
			content.innerHTML = "View results on a map";
			}
		}

		else {
			if (content.innerHTML == "Close Hotel details" ) {
			content.innerHTML = "Expand Hotel details";
			parentStyle.background = parentStyle.background? "":"#FFFFFF";

			}
			else {
			content.innerHTML = "Close Hotel details";
			parentStyle.background = parentStyle.background? "":"#FFFFE7";

			}
		}
	}
}




// TogglePopup layer modified to accomodate closing of overlapping popups.
// This is especially used for 2 overlapping popup pairs

function toggle2Popups(layerName, action) {

   if(layerName == 'premiumpopup' || layerName == 'extralegroom' || layerName == 'excessbaggage' || layerName == 'selectyourseat') {

      if(document.getElementById('premiumpopup')) document.getElementById('premiumpopup').style.display="none";
      if(document.getElementById('extralegroom')) document.getElementById('extralegroom').style.display="none";
      if(document.getElementById('excessbaggage')) document.getElementById('excessbaggage').style.display="none";
      if(document.getElementById('selectyourseat')) document.getElementById('selectyourseat').style.display="none";
   }

   if(layerName == 'excessWaiverBreakUp' || layerName == 'newInsuranceBreakUp') {
      if(document.getElementById('excessWaiverBreakUp')) document.getElementById('excessWaiverBreakUp').style.display="none";
      if(document.getElementById('newInsuranceBreakUp')) document.getElementById('newInsuranceBreakUp').style.display="none";
	}

   if(action =='open')
   {
   document.getElementById(layerName).style.display="block";

    if(layerName == 'premiumpopup' || layerName == 'extralegroom'|| layerName == 'excessbaggage'|| layerName == 'selectyourseat')
    {
     if (navigator.appVersion.substr(22,3)=="6.0")
             hideDropdowns($(layerName),true);
    }
   }
   if(action=='close')
   {
    document.getElementById(layerName).style.display="none";

     if(layerName == 'premiumpopup' || layerName == 'extralegroom'|| layerName == 'excessbaggage'|| layerName == 'selectyourseat')
       {
       if (navigator.appVersion.substr(22,3)=="6.0")
            hideDropdowns($(layerName),false);
       }
    }


}

// Toggle Popup function
//
// Same as ToggleLayer, but without the additional fuctions
// to change text and colours etc

function togglePopup(whichLayer,whatToDo)
	{
   //1.Code added for the BoardBasis section under MSC
   for(i=0;i<25;i++) {
      for(j=0;j<10;j++) {
         for(k=0;k<10;k++) {
            if($("room"+i+"_Option"+j+"_popup"+k)) {
               if(whichLayer != $("room"+i+"_Option"+j+"_popup"+k) && whatToDo == "open")
                  $("room"+i+"_Option"+j+"_popup"+k).style.display = "";
            } // added below for the popups under the "upgrade" section of MSC
            if($("room"+i+"_allInc"+j+"_popup"+k)) {
               if(whichLayer != $("room"+i+"_allInc"+j+"_popup"+k) && whatToDo == "open")
                  $("room"+i+"_allInc"+j+"_popup"+k).style.display = "";
            }
            if($("accomExtra"+i+"_Option"+j+"_popup"+k)) {
               if(whichLayer != $("accomExtra"+i+"_Option"+j+"_popup"+k) && whatToDo == "open")
                  $("accomExtra"+i+"_Option"+j+"_popup"+k).style.display = "";
            }
         }
      }
   }

   //2.Usual flow as before MSC
   if (document.getElementById) {
	// this is the way the standards work
      var style2 = document.getElementById(whichLayer).style;
		style2.display = style2.display? "":"block";
	}
	else if (document.all) /* Really shouldnt be needed any more */
	{
		// this is the way old msie versions work
		var style2 = document.all[whichLayer].style;
		style2.display = style2.display? "":"block";
	}
	else if (document.layers) /* Really shouldnt be needed any more */
	{
		// this is the way nn4 works
		var style2 = document.layers[whichLayer].style;
		style2.display = style2.display? "":"block";
	}
}

// Scroll div contents script - Allows us to scroll the photo
// thumbnails in the thumbnail column for the new Flickr photo
// gallery motif.

// Usage: scrollStart(direction, target, button)

function scrollStart(direction, divID){
// REPEATED CALL EITHER scrollUp OR scrollDown
ourInterval = setInterval("scroll"+direction+"('"+divID+"')", scrollSpeed);
}
function scrollEnd(which){
// STOP CALLING THE SCROLL FUNCTION
clearInterval(ourInterval);
}
function scrollUp(which){
// SET THE SCROLL TOP
$(which).scrollTop = $(which).scrollTop - scrollHeight;
}
function scrollDown(which){
// SET THE SCROLL TOP
$(which).scrollTop = $(which).scrollTop + scrollHeight;
}


// Load gallery images script - Allows us to dynamically load
// the full size images into the holder DIV without preloading
// them into the document first, thus reducing load time.

// Usage: getImage{image id, new image URL}

function getImage(pExistingImageID, pImageURL){
    var img = document.createElement('img');
    img.onload = function (evt) {
        $(pExistingImageID).src=this.src;
        $(pExistingImageID).width=this.width;
        $(pExistingImageID).height=this.height;
    }
    img.src = pImageURL;
}


// Find and change active "tabs" in expanded content section
//
// This will detect which tab should be selected and remove
// the class "active" from all others in the selected range
// (ie. for each result).  This script assumes each tab has
// a unique ID, generated as follows:
//
// "expandedmenu_"[element name]"_"[unique result Id]
//
// This should produce an Id like "expandedmenu_facilities_254"
// or something similar, which can then be used by the script.

// Usage: setSelected(menuItem, resultId)
// eg: setSelected('features','243')
// NOTE: The number MUST be in quotes as above for the script to
// work


/* Generate the Id for the menu item from trunctated data */
function createId(listItem,resultId) {
	return "expandedmenu_"+listItem+"_"+resultId;
}

function getSelectedElement(elementId){
	if(!elementId){
		return;
	}
	return $(elementId);
}

function setSelected(reference, accomodationId){
	var elements = new Array;
	elements[0] = "features";
	elements[1] = "location";
	elements[2] = "facilities";
	elements[3] = "information";
	elements[4] = "photos";
	elements[5] = "videos";
	elements[6] = "map";
	elements[7] = "reviews";

// Get the Element Id for the selected list.
	var selectedElementId = createId(reference, accomodationId); /* 'Hotel_254'; */
	var element,
		tempElementId;

	for(var x=0;x<elements.length;x++){
		tempElementId = createId(elements[x], accomodationId); /* 'Hotel_254', 'Location_254', 'Room_254' */
		element = getSelectedElement(tempElementId); /* DOM element related to the tempElementId */
		if(tempElementId == selectedElementId){
			setElementActive(element);
		}
		else /* Set the classes back to normal */
		{
			if(element.className == "active icon"){
				element.className = "icon";
			}
			if(element.className == "active gap"){
				element.className = "gap";
			}
			if(element.className == "active"){
				element.className = "";
			}
		}
	}

}

function setElementActive(element){
	if(!element){
		return null;
	}
else  /* add the 'active' class to the item */
	{
	if(element.className == "active icon"){
		element.className = "active icon";
	}
	if(element.className == "icon"){
		element.className = "active icon";
	}
	if(element.className == "gap"){
		element.className = "active gap";
	}
	if(element.className == ""){
		element.className = "active";
	}
}

}


// Favorites demo script - *** NOT ACTUAL FUNCTIONALITY ***
//
// Simulates display of favorites in the "My Shortlist" section
// of the results pages.  Based on a basic show/hide script.
// Includes demo of add to / remove from shortlist functions,
// counting of shortlisted hotels, default shortlist text etc.

function getFavId(idNumber) {
	return "fav"+idNumber;
}

function getLinkId(idNumber) {
	return "favlink_"+idNumber;
}

function getResultId(idNumber) {
	return "result_"+idNumber;
}

function getFavIdNumber(favId) {
	var favtext = 'fav';
	var favIdNumber = favId.substring(favtext.length,favId.length);
	return favIdNumber;
}

function getResultIdNumber(resultId) {
	var resultText = 'result';
	var resultIdNumber = resultId.substring(resultText.length,resultId.length)
	return resultIdNumber;
}

function toggleFavs(accomId, linkId) { // accomId =
	var favId = getFavId(accomId)
	var favorite = $(favId).style
	favorite.display = favorite.display? "":"block";
	var linkId = getLinkId(accomId);
	var content = $(linkId);

	if (content.innerHTML == "Add to shortlist")
	{
		content.parentNode.innerHTML = '<img src="images/search_results/heart_minus.gif" alt=" " />&nbsp;<a href="'+content+'" id="'+linkId+'">Remove from shortlist</a>';
	}
	else
	{
		var tempSTR;
		tempSTR = '<img src="images/search_results/ps_icon_fav.gif" alt="Add to shortlist" width="17" height="15" />&nbsp;<a href="';
		tempSTR = tempSTR + content;
		tempSTR = tempSTR + '" id="';
		tempSTR = tempSTR + linkId;
		tempSTR = tempSTR + '">Add to shortlist</a>';
		content.parentNode.innerHTML = tempSTR;
		favCounter--;
	}

	var favs = getElementsByClass('shortlist_row');
	var results = getElementsByClass('resultbox');
	var favIds = {};
	var favCounter = 0;
	for (var i = 0; i < favs.length; i++){
	/* on each loop through the results array, for each element in the shortlist array, loop through the following */
		var favId = favs[i].id;
		var favIdNumber = getFavIdNumber(favId);
			favIdNumber = parseInt(favIdNumber);
		var favNode = $(favId);
		if((favNode.style.display == 'block')){
			favCounter++;
		}
	}
   var slLink = document.getElementById('shortlist_counter');
		slLink.innerHTML = "Your shortlist ("+favCounter+")";

   var favsText = document.getElementById('favstop');
	if (favCounter != 0){
		favsText.innerHTML = 'You have chosen to save the following hotels: <a href="javascript:filterResultsByFavs();" class="floatright">Display only these hotels</a>';
	}
	else{
		favsText.innerHTML = 'You have not shortlisted any holidays. Click add to shortlist to select the holidays that take your interest from above.';
	}
	if (favCounter == 1){
		favsText.innerHTML = 'You have chosen to save the following hotel: <a href="javascript:filterResultsByFavs();" class="floatright">Display only this hotel</a>';
	}
}

function filterResultsByFavs() {
	var favs = getElementsByClass('shortlist_row');
	var results = getElementsByClass('resultbox');
	var favIds = {};
	for (var i = 0; i < favs.length; i++){
	/* on each loop through the results array, for each element in the shortlist array, loop through the following */
		var favId = favs[i].id;
		var favIdNumber = getFavIdNumber(favId);
			favIdNumber = parseInt(favIdNumber);
		var favNode = $(favId);
			favIds[favIdNumber] = false;
		if((favNode.style.display == 'block')){
		/* if the favorite item is displayed, then do the following */
			favIds[favIdNumber] = true;
		}
	}

	for (var j = 0; j < results.length; j++) {
	/* for each element in the results array, loop through the following */
		var resultId = results[j].id;
		var resultIdNumber = getResultIdNumber(resultId);
			resultIdNumber = parseInt(resultIdNumber);
		var resultNode = $(resultId);
		if(favIds[resultIdNumber] == true){
			resultNode.style.display = 'block';
		}
		else {
			resultNode.style.display = 'none';
		}
	}

   var favsText = document.getElementById('favstop');
	if (favsText.innerHTML.search(/Show all Hotels/) == -1){
		favsText.innerHTML = favsText.innerHTML + '<br clear="all" /><br clear="all" /><a href="javascript:showAllHotels();" class="floatright">Show all Hotels</a>';
	}
}

function showAllHotels(){
	var favs = getElementsByClass('shortlist_row');
	var results = getElementsByClass('resultbox');
	var favIds = {};
	for (var i = 0; i < favs.length; i++){
	/* on each loop through the results array, for each element in the shortlist array, loop through the following */
		var favId = favs[i].id;
		var favIdNumber = getFavIdNumber(favId);
			favIdNumber = parseInt(favIdNumber);
		var favNode = $(favId);
			favIds[favIdNumber] = false;
		if((favNode.style.display != 'block')){
		/* if the favorite item is displayed, then do the following */
			favIds[favIdNumber] = true;
		}
	}

	for (var j = 0; j < results.length; j++) {
	/* for each element in the results array, loop through the following */
		var resultId = results[j].id;
		var resultIdNumber = getResultIdNumber(resultId);
			resultIdNumber = parseInt(resultIdNumber);
		var resultNode = $(resultId);
		if(favIds[resultIdNumber] == true){
			resultNode.style.display = 'block';
		}
		else {
			resultNode.style.display = 'block';
		}
	}
}


// Prototype $ function
//
// An easy and quick way that we can grab an element, or multiple elements,
// by their Id.  Taken from the 'Prototype' script library.

function $() {
	var elements = new Array();
	for (var i = 0; i < arguments.length; i++) {
		var element = arguments[i];
		if (typeof element == 'string')
         element = document.getElementById(element);
		if (arguments.length == 1)
			return element;
		elements.push(element);
	}
	return elements;
}


// Prototype 'getElementsByClass' function
//
// Allows us to grab elements by their assigned classes rather than relying
// on unique Ids.  Taken from the 'Prototype' script library.

function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (var i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}


// Toggle function - Can be used to show/hide an element by Id

function toggle(obj) {
	var el = $(obj);
	if ( el.style.display != 'none' ) {
		el.style.display = 'none';
	}
	else {
		el.style.display = '';
	}
}


// Map numbers show/hide script - Used to display or hide the
// result numbers on the map when the user clicks the links or
// checkboxes to the right.

function getMapNumber(number){
	return "marker"+number
}

function toggleMapObj(objectNumber){
	var mapNumber = getMapNumber(objectNumber);
	var el = $(mapNumber);
	if ( el.style.display != 'none' ) {
		el.style.display = 'none';
	}
	else {
		el.style.display = '';
	}
}


// Map numbers show/hide all script - Does exactly what it
// says on the tin - Shows or hides all of the map markers
// when a link is clicked!
//
// Usage: modify_boxes([true/false], number of boxes)

function modify_boxes(to_be_checked,total_boxes){
 	for ( var i=0 ; i < total_boxes ; i++ ){
		var mapBoxes = getElementsByClass('mapmarker')[i]
   		if (to_be_checked){
 			document.forms[0].chkboxarray[i].checked=true;
			mapBoxes.style.display = 'block';
		}
   		else{
 			document.forms[0].chkboxarray[i].checked=false;
			mapBoxes.style.display = 'none';
   		}
 	}
}


// Map hover extra info script
//
// Allows us to display an advanced tooltip when the user hovers
// over each item on the list of hotels in the "view results on
// a map" section of the page. These popups contain extra
// functionality that there would otherwise not be room for.
// Based on the original "suckerfish" menu system, which can be
// found at www.alistapart.com

startList = function() {
	if (document.all&&document.getElementById) {
      listRoot = document.getElementById("maplist");
		for (i=0; i<listRoot.childNodes.length; i++) {
			node = listRoot.childNodes[i];
			if (node.nodeName=="LI") {
				node.onmouseover=function() {
				this.className+=" over";
				}
					node.onmouseout=function() {
					this.className=this.className.replace(" over", "");
				}
			}
		}
	}
}


// Map location beacon script
//
// Creates a div within a map marker to locate it on the map
// using DOM, then removes it after giving it time to display

function locateMarker(marker) {
   var original = $(marker);
   /* Now create the outer-most div */
   var beaconContainer = document.createElement('div');
   beaconContainer.className = 'beaconcontainer';
   var beacon = document.createElement('div');
   beacon.className = 'beacon';
   /* Swap out the original (we'll put it back later) */
   original.appendChild(beaconContainer);
   beaconContainer.appendChild(beacon);
   setTimeout(function() {removeBeacon(marker);}, 2300);
}

function removeBeacon(marker) {
	var original = $(marker);
  	var child1 = original.childNodes[1];
	original.removeChild(child1);
}

// show hide function for top global nav
function showHideNavPop(elemID, showHide) {
   if(document.getElementById(elemID)) {
      obj = document.getElementById(elemID);
      if (showHide == 'show') {
         obj.style.display = 'block';
         if (navigator.appVersion.substr(22,3)=="6.0")
            hideDropdowns(obj,true);
      } else {
         obj.style.display = 'none';
         if (navigator.appVersion.substr(22,3)=="6.0")
            hideDropdowns(obj,false);
	}
}
}

/*~~~~~~~~~~~ LD: Iframe Shim addition ~~~~~~~~~~~~*/

function hideDropdowns(obj, bool){
   var mnuShim=document.getElementById("iframe_shim");
   if(bool){
      mnuShim.style.left=getPageOffsetLeft(obj)+"px";
      mnuShim.style.top=getPageOffsetTop(obj)+"px";
      mnuShim.style.width=obj.offsetWidth+"px";
      mnuShim.style.height=obj.offsetHeight+"px";
      obj.style.zIndex="999";
      mnuShim.style.zIndex= obj.style.zIndex - 1;
      mnuShim.style.display="block";
      mnuShim.style.filter="progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)";
   } else
      mnuShim.style.display="none";
}

function getPageOffsetLeft(el){
   var x;
   x=el.offsetLeft;
   if (el.offsetParent!=null)
      x+=getPageOffsetLeft(el.offsetParent);
   return x;
}

function getPageOffsetTop(el){
   var y;
   y=el.offsetTop;
   if (el.offsetParent!=null)
      y+=getPageOffsetTop(el.offsetParent);
   return y;
}

/*~~~~~~~~~~~ LD: Iframe Shim addition ~~~~~~~~~~~~*/


// JumpTo function
//
// Allows javascript to cause the page to jump to
// a named anchor, where the href property of a link
// is already being used

function jumpTo(anchor) {
	window.location.hash=anchor;
}
function openPlusPopup(whichLayer)
{
   var style2 = document.getElementById(whichLayer).style;
	style2.display = "block";
}
function closePlusPopup(whichLayer)
{
	if (document.powerSearchResult2 != null)
	{
		if (document.powerSearchResult2.sortOption != null)
		{
			if (document.powerSearchResult2.sortOption.style.display =='none')
			{
				document.powerSearchResult2.sortOption.style.display ='inline';
			}
		}
	}
   var style2 = document.getElementById(whichLayer).style;
	style2.display = "none";
}

function keepPopupOpen(id)
{
   document.getElementById(id).style.display = 'block';
}


function closeAttributePopup(whichLayer)
{
   var style2 = document.getElementById(whichLayer).style;
	style2.display = "none";
	if (typeof document.body.style.maxHeight != "undefined") {
   // IE 7, mozilla, safari, opera 9
 }
 else {

	     ShowDropDownList(1,'panel');
      }

}

function openLonghaulPopup(whichLayer,accomId)
{
   if(document.getElementById("thomsonFlyLonghaulLi_"+accomId))
	{
       var style1 = document.getElementById(whichLayer).style;
		style1.display="block";
		style1.marginBottom="555px";
	}
	else
	{
       var style2 = document.getElementById(whichLayer).style;
	    style2.marginBottom="0px";
		style2.display = "block";
	}
}

function closeLonghaulPopup(whichLayer)
{
   var style1 = document.getElementById(whichLayer).style;
   style1.display = "none";
}

//Need to refactor this function later.
function toggleExtraFacility_prebookable(layerName, action)
{
	if($('infantcare_GBM'))
	  $('infantcare_GBM').style.display="none";
	if($('infantcare_GBN'))
	  $('infantcare_GBN').style.display="none";
    if($('infantcare_GBO'))
	  $('infantcare_GBO').style.display="none";
	if($('infantcare_GCW'))
	  $('infantcare_GCW').style.display="none";
	if($('infantcare_GCU'))
	  $('infantcare_GCU').style.display="none";
	if($('infantcare_GCV'))
	  $('infantcare_GCV').style.display="none";
	if($('infantcare_LCL'))
	  $('infantcare_LCL').style.display="none";
	if($('infantcare_LCO'))
	  $('infantcare_LCO').style.display="none";
	if($('infantcare_LCK'))
	  $('infantcare_LCK').style.display="none";
	if($('infantcare_LCR'))
	  $('infantcare_LCR').style.display="none";
	if($('infantcare_CCF'))
	  $('infantcare_CCF').style.display="none";
	if($('infantcare_CCG'))
	  $('infantcare_CCG').style.display="none";
	if($('infantcot'))
	  $('infantcot').style.display="none";
	if($('senscreche_HEB'))
	  $('senscreche_HEB').style.display="none";
	if($('senscreche_HEC'))
	  $('senscreche_HEC').style.display="none";
	if($('senscreche_HAA'))
	  $('senscreche_HAA').style.display="none";
	if(action =='open')
	{
	 $(layerName).style.display="block";
		 if (navigator.appVersion.substr(22,3)=="6.0")
             hideDropdowns($(layerName),true);
	}
	else if(action=='close')
	{
	 $(layerName).style.display="none";
	    if (navigator.appVersion.substr(22,3)=="6.0")
            hideDropdowns($(layerName),false);
	}
}

function rowcolor(){
	   var table = document.getElementById("thetable");
	   var rows = table.getElementsByTagName("tr");
	   for(i = 0; i < rows.length; i++)
	   {
	      if(i % 2 == 0)
	      {
	       rows[i].className = "even";
	      }else{
	       rows[i].className = "odd";
	     }
	   }

	}


