﻿// global variables
var popUpDivClassName = "popUpDivClass";
var pageLoaded = false;
var SelectedDropDown = '';

// This function creates an opaque overlay div 
// and adds the content of the contentPopup parameter to the center of the overlay
function CreateOverlayPopUpWindow(contentPopupId, closeButtonText, entityId) {
	if (CreateOuterOverlay(contentPopupId, closeButtonText, entityId))
		CreateOverlayContent(contentPopupId, closeButtonText, entityId);
}

// This function creates an opaque overlay div 
// and adds the content of the contentPopup parameter to the center of the overlay
function CreateFlightTicketsOverlayPopUpWindow(contentPopupId, closeButtonText, url, selectId) {
	if (CreateOuterOverlay(contentPopupId, closeButtonText, null)) {
		if (selectId != 'undefined' && selectId != null) {
			RememberSelection(selectId);
			SelectedDropDown = selectId;
		}
		CreateFlightOverlayContent(contentPopupId, closeButtonText, url);
	}
}

// This function creates an opaque overlay div for the google pop-up only
// and adds the content of the contentPopup parameter to the center of the overlay
function CreateGoogleOverlayPopUpWindow(contentPopupId, closeButtonText, entityId, score, url) {
	if (pageLoaded) {
		if (CreateOuterOverlay(contentPopupId, closeButtonText, entityId))
			CreateGoogleOverlayPopupContent(contentPopupId, closeButtonText, entityId, score, url);
	}
}

// This checks if the user accepts cookies. If it doesn't than the popup is not displayed
// NOTE: The test cookie is added in the control for the site selection overlay
function CheckCookie() {
    if (document.cookie == "") {
        return false;
    } else {
        return true;
    }
}

function CreateOuterOverlay(contentPopupId, closeButtonText, entityId) {
	var wucPopUpOverlayExtender = document.getElementById(contentPopupId);
	var lTempDivId = "tempDiv_" + contentPopupId;
	if (wucPopUpOverlayExtender == null) {
		//only show the pop-up when the overlay external control
		//in rendered into the page
		var tempDiv = document.getElementById(lTempDivId);
		if (tempDiv != null)
		//removes the div from page
			tempDiv.parentNode.removeChild(tempDiv);
		return false;
	}

	//set banners visibility
	//solution for the overlay banner pop-up problem
	ShowFlashObjects(false);
	ShowCountryFlashMap(false);
	ShowFrames(false);

	//create the overlay div
	var modalDiv = document.getElementById(lTempDivId);
	if (modalDiv == null) {
		modalDiv = document.createElement("div");
		modalDiv.name = lTempDivId;
		modalDiv.id = lTempDivId;

		if (typeof (tempDivClassName) != 'undefined') {
			//for syndication pages, it is set in the syndication pages
			modalDiv.className = tempDivClassName;
		}
		else {
			modalDiv.className = "modalBackground";
			modalDiv.style.width = screen.width + 'px';
			modalDiv.style.height = screen.height + 'px';

			if (typeof (BrowserDetector.browser) == 'undefined') {
				BrowserDetector.init();
			}
			if ((BrowserDetector.browser == 'IE' || BrowserDetector.browser == 'Explorer') && BrowserDetector.version < 7) {
				modalDiv.className += " modalBackgroundIE";
				modalDiv.style.height = document.body.offsetHeight;
			}
		}

		document.body.appendChild(modalDiv);
	}

	if (BrowserIsIE6())
	{
	    SetDropDownsVisibility(false);
    }
		
	return true;
}

function BrowserIsIE6() {
    return getInternetExplorerVersion() == 6;
}

// Returns the version of Internet Explorer or a -1 (indicating the use of another browser).
function getInternetExplorerVersion()
{
    var rv = -1; // Return value assumes failure.
    if (navigator.appName == 'Microsoft Internet Explorer') {
        var ua = navigator.userAgent;
        var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");

        if (re.exec(ua) != null) {
            rv = parseFloat(RegExp.$1);
        }
    }
    return rv;
}

// show - bool value to show or hide drop down lists (true = visible)
function SetDropDownsVisibility(show) {
    if (typeof document.body.style.maxHeight == "undefined") // This returns "undefined" only for IE6
    {
        var ddls = document.getElementsByTagName("select");
        for (i = 0; i < ddls.length; i++) {
            ddls[i].style.visibility = show ? "visible" : "hidden";
        }
    }
}

function SetCustomVisibleDelay (timeout)
{
	setTimeout("setVisibility()", timeout);	
}

function setVisibility() {
    var div = document.getElementById("popupTopCloseButton");
    if (div != null) {
        div.style.display = "block";
    }
}

function CloseOverlay(lTempDivId, hideFlashMapCountry)
{
    var modalDiv = document.getElementById(lTempDivId);

    if (modalDiv != null) {
        if (BrowserIsIE6()) {
            SetDropDownsVisibility(true);
        }
    	modalDiv.style.display = "none";
    }    

    var popUpDiv = document.getElementById("temp_popupOverlayExternal");
    if(popUpDiv != null){
	    popUpDiv.style.display = "none";
	}
	
    //set banners visibility
    //solution for the overlay pop-up problem
	ShowFlashObjects(true);
	if (hideFlashMapCountry != true) {
		ShowCountryFlashMap(true);
	}
	ShowFrames(true);
	
	if (typeof (BrowserDetector.browser) == 'undefined') {
		BrowserDetector.init();
	}
	
	if (BrowserDetector.browser == 'Firefox' || BrowserDetector.browser == 'Mozilla') {
		//This piece of code makes sure that the videos from the page are not over the vertical supplier links if they are visible
		var lVerticalSuppliers = document.getElementById("SuppliersIFrame");
		if (lVerticalSuppliers != "undefined" && lVerticalSuppliers != null && lVerticalSuppliers.style.display == "block") {
			lVerticalSuppliers.contentWindow.document.body.innerHTML = lVerticalSuppliers.contentWindow.document.body.innerHTML;
			lVerticalSuppliers.style.display = "block";
		}
	}
}

function CloseOverlayFlight(lTempDivId) {
	var lHideFlash = false;
	if (SelectedDropDown.indexOf("flight_supp") > 0) {
		lHideFlash = true;
	}
	CloseOverlay(lTempDivId, lHideFlash);
	
	SetRememberSelections();
}

function CreateOverlayContent(contentPopupId, closeButtonText, entityId) 
{
    if (contentPopupId == 'InvisiblepopupOverlayExternal') {
        CreateExternalOverlayContent(contentPopupId, closeButtonText);
    }
    if (contentPopupId == 'SiteSelectionOverlayPopup') {
        CreateUserSiteSelectionOverlayContent(contentPopupId);
    }
   }

function CreateFlightOverlayContent(contentPopupId, closeButtonText, url) {
	var popUpDiv = document.getElementById("temp_popupOverlayExternal");

	if (popUpDiv == null) {
		popUpDiv = document.createElement("div");
		popUpDiv.name = "temp_popupOverlayExternal";
		popUpDiv.id = "temp_popupOverlayExternal";
		popUpDiv.className = popUpDivClassName;
		popUpDiv.style.left = Math.abs((screen.width - 990) / 2) + 145 + 'px';
		popUpDiv.style.top = 315 + 'px';
		document.body.appendChild(popUpDiv);
	}
	else {
		popUpDiv.style.display = "";
		document.getElementById("tempDiv_" + contentPopupId).style.display = "";
	}

	popUpDiv.innerHTML = "<div id='popupOverlayExternal' class='overlayExternal google-pop-up'>"
            + "<div id='popupTopCloseButton' onclick='CloseOverlayFlight(\"tempDiv_" + contentPopupId + "\")' class='close' style='cursor:pointer'>"
            + "<span>" + closeButtonText + "</span></div>"
            + "<iframe src='" + url + "' width='660' height='480' frameborder='0' marginheight='0' marginwidth='0' scrolling='no'></iframe>"
			+ "</div>";

	SetPopupScrollPosition(popUpDiv);   
}

function CreateExternalOverlayContent(contentPopupId, closeButtonText) 
{
    var popUpDiv = document.getElementById("temp_popupOverlayExternal");

    if (popUpDiv == null) {
        popUpDiv = document.createElement("div");
        popUpDiv.name = "temp_popupOverlayExternal";
        popUpDiv.id = "temp_popupOverlayExternal";
        popUpDiv.className = popUpDivClassName;
        popUpDiv.style.left = Math.abs((screen.width - 990) / 2) + 255 + 'px';
        popUpDiv.style.top = 10 + 'px';
        popUpDiv.innerHTML = "<div id='popupOverlayExternal' class='overlayExternal' style='width:550px'>"
            + "<div id='popupTopCloseButton' onclick='CloseOverlay(\"tempDiv_" + contentPopupId + "\")' class='topOverlayExternalCloseButton' style='display:none'>"
            + "<span>" + closeButtonText + "</span></div>"
			+ document.getElementById(contentPopupId).innerHTML
			+ "</div>";

        document.body.appendChild(popUpDiv);
    }
    SetCustomVisibleDelay(5000);
}

function CreateUserSiteSelectionOverlayContent(contentPopupId) {
    var popUpDiv = document.getElementById("temp_popupOverlayExternal");

    if (popUpDiv == null) {
    		popUpDiv = document.createElement("div");
        popUpDiv.name = "temp_popupOverlayExternal";
        popUpDiv.id = "temp_popupOverlayExternal";
        popUpDiv.className = popUpDivClassName;
        popUpDiv.style.top = 315 + 'px';
        popUpDiv.style.width = 'auto';
        popUpDiv.innerHTML = "<div id='popupOverlayExternal' class='overlayExternal'>"
			+ document.getElementById(contentPopupId).innerHTML
			+ "</div>";
        
        document.body.appendChild(popUpDiv);

        var lWidth = 0;
        if (popUpDiv.clientWidth)
        	lWidth = popUpDiv.clientWidth;
        if (lWidth != 0)
        	popUpDiv.style.marginLeft = Math.abs((screen.width - lWidth) / 2) + 'px';
    }
}

function CreateGoogleOverlayPopupContent(contentPopupId, closeButtonText, entityId, score, url) {
	var popUpDiv = document.getElementById("temp_popupOverlayExternal");

	if (popUpDiv == null) {
		popUpDiv = document.createElement("div");
		popUpDiv.name = "temp_popupOverlayExternal";
		popUpDiv.id = "temp_popupOverlayExternal";
		popUpDiv.className = popUpDivClassName;
		popUpDiv.style.left = Math.abs((screen.width - 990) / 2) + 145 + 'px';
		popUpDiv.style.top = 315 + 'px';
		document.body.appendChild(popUpDiv);
	}
	else
	{
		popUpDiv.style.display = "";
		document.getElementById("tempDiv_" + contentPopupId).style.display = ""; 
	}

	popUpDiv.innerHTML = "<div id='popupOverlayExternal' class='overlayExternal google-pop-up'>"
            + "<div id='popupTopCloseButton' onclick='CloseOverlay(\"tempDiv_" + contentPopupId + "\")' class='close' style='cursor:pointer'>"
            + "<span>" + closeButtonText + "</span></div>"
            + "<iframe src='" + url + "?entid=" + entityId + "&score=" + score
            + "' width='660' height='464' frameborder='0' marginheight='0' marginwidth='0' scrolling='no'></iframe>"
			+ "</div>";
			
	SetPopupScrollPosition(popUpDiv);
}

function SetPopupScrollPosition(popUpDiv)
{
	var lScrollY, lDefaultY = 100;

	if (window.scrollY)
		lScrollY = window.scrollY;
	else
		if (document.documentElement.scrollTop)
		lScrollY = document.documentElement.scrollTop;
	else
		lScrollY = document.body.scrollTop;

	popUpDiv.style.top = lScrollY + lDefaultY + 'px';
}

var BrowserDetect = {
	init: function() {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function(data) {
		for (var i = 0; i < data.length; i++) {
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function(dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index + this.versionSearchString.length + 1));
	},
	dataBrowser: [
		{ string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS: [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};

function SetPageLoaded() {
	pageLoaded = true;
}

BrowserDetect.init();

if (BrowserDetect.browser == 'Explorer') {
	window.onload = SetPageLoaded;
}
else {
	SetPageLoaded();
}
