// State data
var isSiteSearchDefaultTextDisplayed = false;
var airPortListStatus = new Object();
airPortListStatus.changeAirportListVisible = false;
airPortListStatus.changeAirportListOver = false;
airPortListStatus.goToAirportBannerListVisible = false;
airPortListStatus.goToAirportBannerListOver = false;
airPortListStatus.calledIdList = ",";

// Functions
function initSearchTip(text) {

	var siteSearchText = dojo.byId("siteSearchText");
	if (siteSearchText) siteSearchText.value = text;
	var qdef = dojo.byId("qdef");
	if (qdef) qdef.value = text;
	isSiteSearchDefaultTextDisplayed = true;
}

function removeSiteSearchDefaultText() {
	if( isSiteSearchDefaultTextDisplayed ) {
		dojo.byId("siteSearchText").value = "";
	}
}

function hideAirPortList() {
	if(airPortListStatus.changeAirportListVisible && !airPortListStatus.changeAirportListOver)
		toggleAirPortList('changeAirportList',true)
	
	if(airPortListStatus.goToAirportBannerListVisible && !airPortListStatus.goToAirportBannerListOver)
		toggleAirPortList('goToAirportBannerList',false)
}

function hideAirPortListWhenClicked() {
	if(airPortListStatus.changeAirportListVisible)
		toggleAirPortList('changeAirportList',true)

	if(airPortListStatus.goToAirportBannerListVisible)
		toggleAirPortList('goToAirportBannerList',false)
}


function toggleVisibility(node) {
	if (dojo.style(node,"visibility")=="visible") {
		dojo.style(node,"visibility","hidden");
		return false;
	} else {
		dojo.style(node,"visibility","visible");
		return true;
	}
}

function toggleAirPortList(listID, toggleOption) {
	var list = dojo.byId(listID);

	var visibility = toggleVisibility(list);
	
	// Fix for Safari: Because dojo.html.toggleVisibility doesn't do it right the first time in Safari
	if( airPortListStatus.calledIdList.indexOf(","+listID+",") < 0 ) { 
		airPortListStatus.calledIdList += listID + ",";
		if( !visibility ) {
			visibility = toggleVisibility(list);
		}	
	}

	if(toggleOption)
		toggleAirPortListOption(visibility);
	if(visibility) {
		// Hide typeahead error mesage
		var autoAirportListError = dojo.byId("autoAirportListError1");
		if( null != autoAirportListError) {
			dojo.style(autoAirportListError,"display","none");
		}

		dojo.style(list,"display","block");
		window.setTimeout(function() { eval("airPortListStatus." + listID + "Visible=true"); }, 50);
	}
	else {
		dojo.style(list,"display","none");
		window.setTimeout(function() { eval("airPortListStatus." + listID + "Visible=false"); }, 50);
	}
	return false;
}

   
function toggleAirPortListOption(listVisible) {
	var optionContainer = dojo.byId("changeAirportListChoiceContainer");
	var option = dojo.byId("changeAirportListChoice");
	if(listVisible) {
		dojo.style(option,"border-top-color","#b0e1e1");
		dojo.style(option,"border-right-color","#b0e1e1");
		dojo.style(option,"border-left-color","#b0e1e1");
		dojo.style(option,"background-image","url(/resources/images/avinor/choose_airport_active.gif)");
		dojo.style(optionContainer,"background-image","url(/resources/images/avinor/choose_airport_border.gif)");
		dojo.style(option,"background-color","#f7fcfc");
        
		//Fixing overlaying layer with dropdown in IE
		if (document.all) {
			window.setTimeout(
				function() { 
					if(dojo.byId("downArrow") != null && dojo.byId("trafficBannerDeparturesInput")){					
					dojo.style("downArrow", "display", "none");   
					dojo.style("trafficBannerDeparturesInput", "display", "none");   
					}

					// Flash
					var FlashPresentation = dojo.byId("three_tier");
					if( null != FlashPresentation) {
						dojo.style(FlashPresentation,"display","none");
					}

					var slider = dojo.byId("myslider");
					
					if( null != slider) {
						dojo.style(slider,"display","none");
					}
						//Hide sticky elements 
						var tabInner4Elements = dojo.query(".tabInner4"); 
						
						for(var i=0;i<tabInner4Elements.length;i++){
							dojo.style(tabInner4Elements[i],"display","none");
					}
				
						var tabInner3Elements = dojo.query(".tabInner3");
						for(var i=0;i<tabInner3Elements.length;i++){
							dojo.style(tabInner3Elements[i],"display","none");
						}						

					// Google Map
					var GoogleMap = dojo.byId("GMapContent");
					if( null != GoogleMap) {
						dojo.style(GoogleMap,"display","none");
					}
					var slider = dojo.byId("myslider");
					if( null != slider) {
						dojo.style(slider,"display","none");
					}
					//Calendar DirectFlights
					var calendarContainer = dojo.byId("calendar-container");
					if( null != calendarContainer) {
						dojo.style(calendarContainer,"display","none");
					}

				}, 10);
		}
	}
	else {
		dojo.style(option,"border-top-color","#ffffff");
		dojo.style(option,"border-right-color","#ffffff");
		dojo.style(option,"border-left-color","#ffffff");
		dojo.style(option,"background-image","url(/resources/images/avinor/choose_airport_passive.gif)");
		dojo.style(optionContainer,"background-image","none");
		dojo.style(option,"background-color","#ffffff");
        
        //Fixing overlaying layer with dropdown in IE
		if (document.all) 
        {
			if(dojo.byId("downArrow") != null && dojo.byId("trafficBannerDeparturesInput")){					
        	dojo.style("downArrow", "display", "inline");   
			dojo.style("trafficBannerDeparturesInput", "display", "inline");   
			}

			var GoogleMap = dojo.byId("GMapContent");
			if( null != GoogleMap) {
				dojo.style(GoogleMap,"display","inline");
			}
			var slider = dojo.byId("myslider");
			if( null != slider) {
				dojo.style(slider,"display","block");
				//Show sticky elements 
				var tabInner4Elements = dojo.query(".tabInner4"); 
				for(var i=0;i<tabInner4Elements.length;i++){
					dojo.style(tabInner4Elements[i],"display","inline");
			}
				var tabInner3Elements = dojo.query(".tabInner3");
				for(var i=0;i<tabInner3Elements.length;i++){
					dojo.style(tabInner3Elements[i],"display","inline");
				}
			}
			// Flash
			var FlashPresentation = dojo.byId("three_tier");
			if( null != FlashPresentation) {
				dojo.style(FlashPresentation,"display","block");
			}
			//Calendar directFlights
			var calendarContainer = dojo.byId("calendar-container");
			if( null != calendarContainer) {
					dojo.style(calendarContainer,"display","inline");
			}

        }
	}
}

function changeNewsArchiveYear(url, formName, selectName) {    
    var formObj = document.forms[formName];    
    var selectObj = formObj[selectName];    
    location.href = url + selectObj.options[selectObj.selectedIndex].value;
}

function changeAirport(obj) {
    if (obj.options[obj.selectedIndex].value == 'avinor')
        location.href = "/" + obj.options[obj.selectedIndex].value; // key
    else
        location.href = "/lufthavn/" + obj.options[obj.selectedIndex].value; // key
}

function switchVisibleDivs(objectIdHide, objectIdShow) {
    var styleObjectHide = getStyleObject(objectIdHide);
    var styleObjectShow = getStyleObject(objectIdShow);
    if (styleObjectHide && styleObjectShow) {
        styleObjectShow.display = 'block';
        styleObjectHide.display = 'none';
    }        
}

function getStyleObject(objectId) {
  // checkW3C DOM, then MSIE 4, then NN 4.
  //
  if(document.getElementById && document.getElementById(objectId)) {
	return document.getElementById(objectId).style;
   }
   else if (document.all && document.all(objectId)) {  
	return document.all(objectId).style;
   } 
   else if (document.layers && document.layers[objectId]) { 
	return document.layers[objectId];
   } else {
	return false;
   }
}

function setCssClass(element, cssClass) {
	element.setAttribute("class", cssClass);
	element.setAttribute("className", cssClass);
}

function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            if (oldonload) {
                oldonload();
            }
            func();
        }
    }
}

function addUnLoadEvent(func) {
    var oldonunload = window.onunload;
    if (typeof window.onunload != 'function') {
        window.onunload = func;
    } else {
        window.onunload = function() {
            if (oldonunload) {
                oldonunload();
            }
            func();
        }
    }
}

dhtmlLoadScript = function (url) {
	var e = document.createElement("script");
	e.src = url;
	e.type="text/javascript";
	document.getElementsByTagName("head")[0].appendChild(e);
}

function removeDefaultText(input, defaultValue) {
	if(input.value == defaultValue)
		input.value = "";
}

function hideErrorMessage(errorFieldID) {
	var errorField = getStyleObject(errorFieldID);
		errorField.display = 'none';
}

function newWindow(href) {
    var url = href;
    var protocol = "";
    var query = "";
    var host = "";
    if (href.indexOf("://") > 0) {
        protocol = href.substring(0, href.indexOf("://")+3);
        url = href.substring(href.indexOf("://")+3, href.length);
        if (url.indexOf("/") > 0) {
            host = url.substring(0, url.indexOf("/"));            
            url = url.substring(url.indexOf("/"), url.length);
        }
    }
    if (href.indexOf("?") > 0) {
        url = url.substring(0, url.indexOf("?"));
        query = href.substring(href.indexOf("?"), href.length);
    }
 
    url = escape(unescape(url));
    
    url = protocol + host + url + query;
    window.open(url);
    return false;
}

function openJSWin(href, name, width, height, returnValue){
	var leftVal = (screen.width - width) / 2;
	var topVal = (screen.height - height) / 2;
	var winFeatures = 'width=' + width + ',height=' + height + ',left='+leftVal+',top='+topVal + ',screenX='+leftVal+',screenY='+topVal;
	winFeatures += ',location=no,menubar=no,resizable=no,scrollbars=no,status=yes';
	var jsWin = window.open(href, name, winFeatures);
	if (jsWin != null) {
		jsWin.focus();
	}
	return returnValue;
}

function setFormTarget(form, targetName){
	form.target = targetName;
}

function printPage() {
	window.print();
	return false;
}

function initPrintButtons() {
	if (typeof(noPrint) == "undefined") {
		var nodes = [];
		nodes = dojo.query(".printOption");
		for(i=0; i<nodes.length; i++) {
			nodes[i].innerHTML = "<div class=\"printOptionInner\"><a href=\"#\" onclick=\"return printPage()\" title=\"" + avinorTexts.article.print + "\">" + avinorTexts.article.print + "<\/a><\/div>";
		}
	} else {
		dojo.query(".printOption").orphan();
		dojo.query(".articleContent").orphan();
	}
}

function RIAClick(action) {
	var sAction = window.location.href + "/RIA/" + action;
	// Shell function for tracking RIA Clicks in GA and WT
	if (typeof console != "undefined") console.log(sAction);
	if (typeof pageTracker != "undefined") pageTracker._trackEvent("RIAClick", sAction);
	if (typeof dcsMultiTrack != "undefined") dcsMultiTrack("DCS.dcsuri", sAction);
}
