// For all generic helper functions see global.js

addLoadEvent(function() {
	init('start');  // init main navigation tab
});

/*
addLoadEvent(function() {
	var currentPage = window.location.href;
	if (currentPage.indexOf('ypsilon.net') > 0) {
		all_layers_off();
		activate("menue_offers");
	}
	else {
		init('start');
	}
	//window.setTimeout("if (!alreadyrunflag){showLocationsFromPriceList();}", 0)
});
*/



function getNodeByName(fromNode, name) {
	if (fromNode) {
		for (i=0; i<fromNode.childNodes.length; i++){
			if (fromNode.childNodes[i].nodeName == name || fromNode.childNodes[i].nodeName == name.toUpperCase()) {
				return fromNode.childNodes[i];
			}
		}
	}
	
	return null;
}

function showLocationsFromPriceList() {
	var currentPage = window.location.href;
	if (currentPage.indexOf('pricelist') > 0) {
		var texthead = document.getElementById("texthead");
		if (texthead) {			
			var children = texthead.childNodes;
			var nodes = [];
			for(var i=0; i<children.length; i++) {
				if((children[i].nodeType == 3 || children[i].nodeType == 4) && 
					children[i].nodeValue.match(/^\s*$/)) continue; //it's an empty text node
					
				if (children[i].id.match(/texthead/)) {					
					var loc = children[i].innerHTML;
					nodes.push(loc.substring(0, loc.indexOf(",")));
				}
			}
			
			var title = document.getElementById("title");
			if (title) {
				var title_h3 = getNodeByName(title, "h3");
				var title_h2 = getNodeByName(title, "h2");
				var dest = "";
				if (nodes.length > 0 && nodes.length % 2 == 0) {
					if (nodes.length == 2) {
						dest = nodes[1];
					} else if (nodes.length == 4) {
						if (nodes[0] == nodes[nodes.length - 1]) {	// return ticket
							dest = nodes[1];
						} else {
							dest = nodes[nodes.length - 1];
						}					
					}
				}
				
				if (title_h3 && title_h2 && dest && dest.replace(/\s/g, "") != "") {
					title_h2.innerHTML += " nach " + dest;
					var alink = getNodeByName(title_h3, "a");
					if (alink) {
						var lk1 = alink.innerHTML.substring(0, alink.innerHTML.indexOf("..."));
						var lk2 = alink.innerHTML.substring(alink.innerHTML.indexOf("..."));
						alink.innerHTML = lk1 + "nach " + dest + " " + lk2;
					}
				}
			}
		}
		//alert("texthead");
	}
}

function showLocationsCalled()
{
	var logo = document.getElementById("logo");
	var spanTag = document.createElement("span");
	spanTag.id = "onFnDone";
	if (logo && logo.parentNode) {
		logo.parentNode.insertBefore(spanTag, logo);
	}
}

var alreadyrunflag=0 //flag to indicate whether target function has already been run
function getLocationsLoaded(guard) {
	if (document.getElementById("onFnDone")) return;
	
	var texthead = document.getElementById("texthead");
	if (texthead) {
		showLocationsFromPriceList();
		showLocationsCalled();
	} else {
		if (guard < 10) {
			window.setTimeout(function() { getLocationsLoaded(guard + 1); }, 50);
		}
	}
}

window.setTimeout(function() { getLocationsLoaded(0); }, 70);
/*if (document.addEventListener)
  document.addEventListener("DOMContentLoaded", function(){alreadyrunflag=1; showLocationsFromPriceList();}, false);
else if (document.all && !window.opera){
  document.write('<script type="text/javascript" id="contentloadtag" defer="defer" src="javascript:void(0)"><\/script>');
  var contentloadtag=document.getElementById("contentloadtag");
  contentloadtag.onreadystatechange=function(){
    if (this.readyState=="complete"){
      alreadyrunflag=1;
      showLocationsFromPriceList();
    }
  }
}*/
