var navFrame, fbFrame;
var arDestinations = new Array();

function initHome() {
	top.mainContainer = document.getElementById('maincontainer');
	updateSize();
	if (document.getElementById('language_selector')) ls = new LanguageSelector();
	FBfill(true);
	BBfill(true);
	setTriggersHome();
	setOtherKLMSites();
}
function initLocalHome() {
	setTriggersHome();
	setOtherKLMSites();
}
function init() {
	top.mainContainer = document.getElementById('maincontainer');
	if (document.getElementById('language_selector')) ls = new LanguageSelector();
	FBfill();
	BBfill();
	updateSize();
	setTriggers();
	setOtherKLMSites();
}
function FBfill (home) {
	if (document.getElementById('jffp_login')) {
		fbFrame = new RPCFrame(window);
		if (home)
			fbFrame.setLocation('flyingblueLoggedIn.html', FBfillItHome)
		else
			fbFrame.setLocation('flyingblueLoggedIn.html', FBfillIt)
	}
}
function FBfillIt () {
	document.getElementById('jffp_login').innerHTML = fbFrame.getHTMLById('content').innerHTML;
	toggleMilesPlus();
	try {
		if (bluebiz != undefined) checkBB();
	} catch (e) {}
}
function FBfillItHome () {
	document.getElementById('jffp_login').innerHTML = fbFrame.getHTMLById('content').innerHTML;
	document.getElementById('bottom').innerHTML = '';
}

function BBfill (home) {
	if (document.getElementById('bb_login')) {
		fbFrame = new RPCFrame(window);
		if (home)
			fbFrame.setLocation('BlueBizLogIn.html', BBfillItHome)
		else
			fbFrame.setLocation('BlueBizLogIn.html', BBfillIt)
	}
}
function BBfillIt () {
	document.getElementById('bb_login').innerHTML = fbFrame.getHTMLById('content').innerHTML;
	toggleMilesPlus();
	try {
		if (bluebiz != undefined) checkBB();
	} catch (e) {}
}
function BBfillItHome () {
	document.getElementById('bb_login').innerHTML = fbFrame.getHTMLById('content').innerHTML;
	document.getElementById('bottom').innerHTML = '';
}

function loadNav() {
	if (document.getElementById('navigation')) {
		navFrame = new RPCFrame(window);
		navFrame.setLocation('navigation.html', initNavigation);
		clearInterval(n);
	}
	else {
		return false;
	}
}
function initNavigation () {
	new Navigation('navigation', 'navopen', navFrame.getHTMLById('navcontent').innerHTML);
}
function isChild(ancestor, candidate) {
	while (candidate && candidate != ancestor.parentNode) {
		if (candidate == ancestor) return true;
		candidate = candidate.parentNode;
	}
	return false;
}
function createContextFunction(context, method, method2) {
	return (function(x){
		method = (method == "post") ? method2 : method;
		eval("context."+method+"(x)");
		return false;
	});
}
function initSpecials() {
	var entries = document.getElementsByTagName('div');
	for (var i = 0; i < entries.length; i++) {
		if (entries[i].className == 'specials') {
			var specials = entries[i].getElementsByTagName('li');
			for (var j = 0; j < specials.length; j++) {
				specials[j].link = specials[j].getElementsByTagName('a')[0].href;
				specials[j].onmouseover = function () {this.className = "over"}
				specials[j].onmouseout = function () {this.className = ""};
				specials[j].onclick = function () {document.location.href = this.link};
			}
		}
	}
}
function initSpecials_mp() {
	var entries = document.getElementsByTagName('div');
	for (var i = 0; i < entries.length; i++) {
		if (entries[i].className == 'specials_jffp') {
			var specials = entries[i].getElementsByTagName('li');
			for (var j = 0; j < specials.length; j++) {
				specials[j].link = specials[j].getElementsByTagName('a')[0].href;
				specials[j].onmouseover = function () {this.className = "over"}
				specials[j].onmouseout = function () {this.className = ""};
				specials[j].onclick = function () {document.location.href = this.link};
			}
		}
	}
}
function setTriggers() {
	var entries = document.getElementsByTagName('div');
	for (var i = 0; i < entries.length; i++) {
		if (entries[i].className.indexOf('TRIG') != -1) {
			entries[i].outline = entries[i].getElementsByTagName('div')[0];
			// exclude specials trigger from this behaviour
			if(entries[i].outline.className.indexOf('specials') == -1) {
				entries[i].link = entries[i].getElementsByTagName('a')[0];
				entries[i].onmouseover = function (e) {
					this.outline.oldClassName = this.outline.className;
					this.outline.className += "over";
				}
				entries[i].onmouseout = function (e) {
					this.outline.className = this.outline.oldClassName;
				}
				entries[i].onclick = function () {
					if (this.className.indexOf('newWin') != -1) {
						if (this.className.indexOf('KANA') !=-1) {
							window.open(this.link,'kana','width=400,height=300');
						}
						else if (this.className.indexOf('LSW') !=-1) { //local shop window
							window.open(this.link,'lsw','width=800,height=600,toolbar=yes,menubar=yes,location=yes,scrollbars=yes,resizable=yes,status=yes');
						}
						else {
							window.open(this.link);
						}
						return false;
					}
					else {
						document.location.href = this.link;
					}
				}
			}
		}
	}
}
function setTriggersHome() {
	var entries = document.getElementsByTagName('div');
	for (var i = 0; i < entries.length; i++) {
		if (entries[i].className.indexOf('TRIG') != -1) {
			entries[i].link = entries[i].getElementsByTagName('a')[0];
			entries[i].onclick = function () {
				if (this.className.indexOf('newWin') != -1) {
					if (this.className.indexOf('KANA') !=-1) {
						window.open(this.link,'kana','width=400,height=300');
					}
					else if (this.className.indexOf('LSW') !=-1) { //local shop window
						window.open(this.link,'lsw','width=800,height=600');
					}
					else {
						window.open(this.link);
					}
					return false;
				}
				else {
					document.location.href = this.link;
				}
			}
			entries[i].onmouseover = function (e) {
				this.className += " TRIGOVER";
			}
			entries[i].onmouseout = function (e) {
				this.className = this.className.replace(" TRIGOVER","");
			}
		}
	}
}
function setPromos() {
	var entries = document.getElementsByTagName('div');
	for (var i = 0; i < entries.length; i++) {
		if (entries[i].className.indexOf('nonair') != -1) {
			entries[i].outline = entries[i].getElementsByTagName('span')[0];
			entries[i].link = entries[i].getElementsByTagName('a')[0];
			entries[i].onclick = function () {document.location.href = this.link};
			entries[i].onmouseover = function (e) {
				this.outline.oldClassName = this.outline.className;
				this.outline.className += "over";
			}
			entries[i].onmouseout = function (e) {
				this.outline.className = this.outline.oldClassName;
			}
		}
	}
}

function toggleMilesPlus() {
	var miles = document.getElementById('mpopen');
	var milesplusblock = document.getElementById('jffpcontentcontainer');
	var seealsoblock = document.getElementById('seealso');
	milesplusblock.style.display = "block";
	miles.height = milesplusblock.offsetHeight;
	milesplusblock.style.display = "none";
	var milesclose = document.getElementById('bottom');
	miles.onclick = function() {
		if (milesplusblock.style.display == 'none') {
			milesplusblock.style.display = 'block';
			if (seealsoblock) seealsoblock.style.top = (this.height - 35) + "px";
		} else {
			milesplusblock.style.display = 'none';
			if (seealsoblock) seealsoblock.style.top = "0";
		}
	}
	milesclose.onclick = function() {
		if (milesplusblock.style.display == 'none') {
			milesplusblock.style.display = 'block';
			if (seealsoblock) seealsoblock.style.top = (this.height - 35) + "px";
		} else {
			milesplusblock.style.display = 'none';
			if (seealsoblock) seealsoblock.style.top = "0";
		}
	}
	if (top.FBopen) {
		milesplusblock.style.display = 'block';
		if (seealsoblock) seealsoblock.style.top = (miles.height - 35) + "px";
	}
}
function addEventHandler(o, eventName, handler) {
	if (o.addEventListener) {
		o.addEventListener(eventName, handler, true);
	} else if (o.attachEvent) {
		o.attachEvent("on"+eventName, handler);
	}
}
function removeEventHandler(o, eventName, handler) {
	if (o.removeEventListener) {
		o.removeEventListener(eventName, handler, true);
	} else {
		o.detachEvent("on"+eventName, handler);
	}
}
function calculateTop(object) {if (object) return object.offsetTop + calculateTop(object.offsetParent); else return 0;}
function calculateLeft(object) {if (object) return object.offsetLeft + calculateLeft(object.offsetParent); else return 0;}

function scaleFrame() {
	var f = document.getElementById('extFrame');
	var top;
	top = calculateTop(f);
	f.style.height = getWindowHeight() - top + "px";
	document.body.style.overflow = "hidden"; //for firefox/mozilla
	document.body.parentNode.style.overflow="hidden"; //for ie
}
function getWindowHeight() {
var windowHeight=0;
	if (typeof(window.innerHeight)=='number') {
		windowHeight=window.innerHeight;
	} else {
		if (document.documentElement&&document.documentElement.clientHeight) {
			windowHeight=document.documentElement.clientHeight;
		} else if (document.body&&document.body.clientHeight) windowHeight=document.body.clientHeight;
	}
	return parseInt(windowHeight);
}
function setOtherKLMSites() {
	var s = document.getElementById('otherKLMsites');
	if (s) {
		s.onchange = function () {
			document.location.href = this.options[this.selectedIndex].value;
		}
	}
}
function generateSitemap () {
	var counter = 1;
	var copy;
	var entries = document.getElementById('navigation').getElementsByTagName('ul')[0].getElementsByTagName('li');
	for (var i = 1; i < entries.length; i++) {
		if (entries[i].parentNode.className == 'level1') {
			copy = entries[i].cloneNode(true);
			if (copy.getElementsByTagName('ul')[0].getElementsByTagName('ul').length > 0) {
				copy.className += ' hassubsub';
			}
			document.getElementById('map' + counter).getElementsByTagName('ul')[0].appendChild(copy);
			counter++;
		}
	}
}
function logOutJFFP () {
	deleteCookie('jffp');
	document.location.href = '/';
}
function checkBB () {
	if (getCookie('klmcomcaas') == 'b2b') {
		document.getElementById('loginbar').style.display = 'none';
		document.getElementById('loggedinbar').style.display = 'block';
	} else {
		document.getElementById('loginbar').style.display = 'block';
		document.getElementById('loggedinbar').style.display = 'none';
	}
}
function checkLoggedin () {
	if (getCookie('jffp')) {
		if (getCookie('jffp').length > 10) {
			return true;
		}
	}
	return false;
}
function checkLogin () {
	if (getCookie('jffp')) {
		if (getCookie('jffp').length > 10) {
			document.getElementById('IN').style.display = 'block';
		} else {
			document.getElementById('OUT').style.display = 'block';
		}
	} else {
		document.getElementById('OUT').style.display = 'block'
	}
}
function initConditions() {
	var entries = document.getElementsByTagName('a');
	for (var i = 0; i < entries.length; i++) {
		if (entries[i].className == 'CONDITIONS') {
			entries[i].layer = entries[i].parentNode.getElementsByTagName('div')[0];
			entries[i].container = entries[i].parentNode.parentNode.parentNode.parentNode.parentNode;
			entries[i].layer.closer = entries[i].layer.getElementsByTagName('div')[0];
			entries[i].layer.closer.container = entries[i].container;
			entries[i].layer.closer.onclick = function () {
				this.parentNode.style.display = 'none';
				//this.container.style.position = 'static';
			}
			entries[i].onclick = function () {
				closeOthers();
				//this.container.style.position = 'relative';
				this.layer.style.display = 'block';
				var high = this.layer.offsetHeight;
				var newY = 0 - high + 'px';
				this.layer.style.marginTop=newY;
			}
		}
	}
}
function closeOthers () {
	var entries = document.getElementById('content').getElementsByTagName('div');
	for (var i = 0; i < entries.length; i++) {
		if (entries[i].className == 'conditionslayer')
			entries[i].style.display = 'none';
		if (entries[i].className == 'webawards')
			entries[i].style.position = 'static';
	}
}
function initWABanner () {
	var wa = document.getElementById('webawards');
	if (wa) {
		wa.link = wa.getElementsByTagName('a')[0].href;
		wa.onclick = function () {document.location.href = this.link};
	}
}

function showBluebiz() {
	var bbcheck = document.getElementById('bluebiz');
	if (!bbcheck.checked) {
		document.getElementById('bluebiznr').style.display = 'none';
	}
	else {
		document.getElementById('bluebiznr').style.display = 'block';
	}
}
function emptyBluebiz() {
	var nr = document.getElementById('bluebiznr');
	nr.value = '';
}
function fillinBluebiz() {
	var nr = document.getElementById('bluebiznr');
	nr.value = 'Enter your code (NL1234567890)';
}

addClass=function(obj,cName) { 
	removeClass(obj,cName); 
	return obj.className+=(obj.className.length>0?' ':'')+cName; 
}

removeClass=function(obj,cName) {
	return obj.className=obj.className.replace(new RegExp("^"+cName+"\\b\\s*|\\s*\\b"+cName+"\\b",'g'),''); 
}
