﻿window.onload = function()
{
    var el;
    
    // Add event to close button.
    if (el = document.getElementById('closeWindowButton'))
    {
        el.onclick = function() { window.close(); }
    }
    
    // Add event to print button.
    if (el = document.getElementById('printButton'))
    {
        if (window.print)
            el.onclick = function() { window.print(); }
    }
    
    // Resize the window if there are an element with the id impMap (Image popup).
    if (el = document.getElementById('imgMap'))
    {
        resizeImageWindow();
    }
    
    // Resize the window if there are an element with the id imgSeatMap (FootballTicket popup).
    if (el = document.getElementById('imgSeatMap'))
    {
        resizeFootballWindow();
    }
    
    // Resize the window if there are an element with the id imgAirportImage (Airport popup).
    if (el = document.getElementById('imgAirportImage'))
    {
        resizeAirportWindow();
    }
    
    // Resize the window if there are an element with the id imgHotelImage (Hotel popup).
    if (el = document.getElementById('imgHotelImage'))
    {
        resizeHotelWindow();
    }
    
    // Add rounded corners.
    Nifty("div.rounded","normal");
}

// Check if the user has filled in the personal info before choosing travelers.
//function checkOrderContact()
//{
//    if(document.getElementById("txbFirstName").value == "" || document.getElementById("txbLastName").value == "" || document.getElementById("txbEMail").value == "")
//	{
//		alert("Du måste fylla i uppgifterna på beställaren först.");
//		document.getElementById("ddlOrderTraveller").value = 0;
//	}
//}

// Resize the image popup, rename function.
function resizeImageWindow()
{
    iWidth = document.images[0].width + 40;
    iHeight = document.images[0].height + 120;
    
    window.resizeTo(iWidth, iHeight);
}

// Resize the footballticket popup.
function resizeFootballWindow()
{ 
    if (document.images[0].height < document.getElementById("right-side").offsetHeight)
        iHeight = document.getElementById("right-side").offsetHeight;
    else
        iHeight = document.images[0].height;
    
    iWidth = document.images[0].width + 260;
    iHeight += 180;
    
    document.getElementById("left-side").style.width = document.images[0].width;
    
    window.resizeTo(iWidth, iHeight);
}

// Resize the airport popup.
function resizeAirportWindow()
{
    if (document.images[0].height < document.getElementById("right-side").offsetHeight)
        iHeight = document.getElementById("right-side").offsetHeight;
    else
        iHeight = document.images[0].height;
        
    iWidth = document.images[0].width + 310;
    iHeight += 230;
    
    window.resizeTo(iWidth, iHeight);
}

// Resize the hotel popup.
function resizeHotelWindow()
{
    if (document.getElementById("left-side").offsetHeight < document.getElementById("right-side").offsetHeight)
        iHeight = document.getElementById("right-side").offsetHeight;
    else
        iHeight = document.getElementById("left-side").offsetHeight;
        
    iWidth = 500;
    
    if (document.getElementById("bottom"))
        iHeight += 220;
    else
        iHeight += 170;
    
    window.resizeTo(iWidth, iHeight);
}

function closeDiv(strId)
{
	document.getElementById(strId).style.visibility = "hidden";
}

function centerDiv(strId)
{
	document.getElementById(strId).style.top =  milouGeneral.getWindowCenterX(strId);
	document.getElementById(strId).style.left = milouGeneral.getWindowCenterY(strId);
}

function setCssClass(strId, strClass)
{
	document.getElementById(strId).className = strClass;
}

var milouUser = {
	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"
		}
	]
};

var milouGeneral = {
    getWindowCenterX: function(strContentId) {
		objContent = document.getElementById(strContentId);
		if(!objContent)
			return null;
		
		var documentWidth;
		var calunit = milouUser.browser == "Netscape"? "" : "px"
		var intWidth = objContent.scrollWidth;
		if(milouUser.browser == "Explorer")
			documentWidth= document.documentElement.offsetWidth/2+document.documentElement.scrollLeft-20;
		else if(milouUser.browser == "Netscape")
			documentWidth=window.innerWidth/2+window.pageXOffset-20;
		else
			documentWidth=self.innerWidth/2+window.pageXOffset-20;
		return documentWidth-(intWidth/2)+calunit;
	},
	getWindowCenterY: function(strContentId) {
		objContent = document.getElementById(strContentId);
		var documentHeight;
		var calunit = milouUser.browser == "Netscape"? "" : "px";
		var intHeight = objContent.scrollHeight;
	   
		if(milouUser.browser == "Explorer")
			documentHeight = document.documentElement.offsetHeight/2+document.documentElement.scrollTop-20;
		else if(milouUser.browser == "Netscape")
			documentHeight=window.innerHeight/2+window.pageYOffset-20;
		else
			documentHeight=self.innerHeight/2+window.pageYOffset-20;
		
		return documentHeight-(intHeight/2)+calunit;
	},
	FixPng: function(imgObj) {
		if(milouUser.browser != "Explorer" && imgObj.src.indexOf("png")<0)
			return;
		
		if ((milouUser.version >= 5.5) && (milouUser.version < 7) && (document.body.filters)) 
		{
		   var imgID = (imgObj.id) ? "id='" + imgObj.id + "' " : ""
		   var imgClass = (imgObj.className) ? "class='" + imgObj.className + "' " : ""
		   var imgTitle = (imgObj.title) ? 
						 "title='" + imgObj.title  + "' " : "title='" + imgObj.alt + "' "
		   var imgStyle = "display:inline-block;" + imgObj.style.cssText
		   var strNewHTML = "<span " + imgID + imgClass + imgTitle
					  + " style=\"" + "width:" + imgObj.width 
					  + "px; height:" + imgObj.height
					  + "px;" + imgStyle + ";"
					  + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
					  + "(src=\'" + imgObj.src + "\', sizingMethod='scale');\"></span>"
		   imgObj.outerHTML = strNewHTML	  
		}
	},
	setCookie : function (name,value,days) {
		if (days) {
		    var date = new Date();
		    date.setTime(date.getTime()+(days*24*60*60*1000));
		    var expires = "; expires="+date.toGMTString();
	    }
	    else var expires = "";
	    document.cookie = name+"="+value+expires+"; path=/";
	},
	getCookie : function (name) {
		var nameEQ = name + "=";
	    var ca = document.cookie.split(';');
	    for(var i=0;i < ca.length;i++) {
		    var c = ca[i];
		    while (c.charAt(0)==' ') c = c.substring(1,c.length);
		    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	    }
	    return null;
	},
	deleteCookie : function (name) {
		this.setCookie(name,"",-1);
	},
	getCheckedValue: function (strRadioName) {
	    var radioObj = document.getElementsByName(strRadioName);
	    if(!radioObj)
		    return "";
	    var radioLength = radioObj.length;
	    if(radioLength == undefined)
		    if(radioObj.checked)
			    return radioObj.value;
		    else
			    return "";
	    for(var i = 0; i < radioLength; i++) {
		    if(radioObj[i].checked) {
			    return radioObj[i].value;
		    }
	    }
	    return "";
    },
    ScrollTo: function(intValue){
   	    window.scroll(0,intValue); // horizontal and vertical scroll targets
    }
}

milouUser.init();