var maxPositions = 4;
var actPosition = Array();
var pageString = "";

var actContentID = 0;

var backgroundImageAspectRatio = 0.72; //testvalue

var boxStatus = 0;
var oldYCoordinate = 0;

function createPageString() {
	//add displayed boxes, actual background, rootline, flash and movie information
	
	//displayed boxes:
	for(var i = 0; i < maxPositions; i++) {
		pageString += actPosition+"|";	
	}

	//background:
	//document.getElementById("backgroundImage").src = backg;

	//flash:
	//document.getElementById("flashfileobject").value

	//movie:
	//document.getElementById("moviefileobject").value

	//rootline:
	//
}

function loadPageString() {

}

function hideElement(ident,pos) {
	if (document.getElementById(ident)) {
		document.getElementById(ident).style.visibility = "hidden";
		actPosition[pos] = -1;
	}
}

function showElement(ident,pos) {
	if (document.getElementById(ident)) {
		document.getElementById(ident).style.visibility = "inherit";
		actPosition[--pos] = ident;
	}
}

function showSubElement(ident) {
	if (document.getElementById(ident)) {
		document.getElementById(ident).style.visibility = "inherit";
		actContentID = ident;
	}
}

function hideSubElement(ident) {
	if (actContentID != 0) {
		if (document.getElementById(actContentID)) {
			document.getElementById(actContentID).style.visibility = "hidden";
			actContentID = 0;
		}
	}
}

function getElementPosition(id) {
	var pos = 0;
	while (!(document.getElementById("i"+pos+":"+id) || (pos > maxPositions))) {
		pos++;
	}
	if (pos > maxPositions) {
		//error case (activate only for testing
		//alert("pos "+pos+" too large");
	}
	return pos;
}

function getElementIdent(id) {
	var pos =  getElementPosition(id);
	return "i"+pos+":"+id;
}



function hideGtPosition(pos) {
	for(i = pos; i < maxPositions; i++) {
		hideElement(actPosition[i],i);
	}
}

function setLinkToRootLine(obj,ident, debug) {
	//remove all "on" classes for this node
	if (debug) {
		alert(obj+" "+ident);
	}
	var node = document.getElementById(ident+"_links");
	if (node) {
		var children = node.childNodes;	
		for (var i = 0; i < children.length; i++) { 
			if (children.item(i).nodeType == 1) {
				if (debug) {
					alert("old classname"+children.item(i).className);
				}
				children.item(i).className = "";
			}
		}
	}

	//define clickd node as "on"
	if (obj) {
		
		if (debug) {
			alert("new classname"+obj.parentNode.className);
		}
		obj.parentNode.className = "on";
	}
}


function openLink(pid,id,obj, backg) {	
	if (id instanceof Array) {
		openLinkInternal(pid,id[0],obj,backg, true);
		for (var i = 1; i < id.length; i++) {
			openLinkInternal(pid,id[i],obj,backg, false);
		}
	} else {
		openLinkInternal(pid,id,obj,backg, true);
	}
}

function openSubLink(pid,id,obj, backg, flash, movie) {

	//change background if requested
	if (backg && backg != "") {
		document.getElementById("backgroundImage").src = backg;
	}
	
	if (flash && flash != "") {
		document.getElementById("moviecontainer").style.visibility = "visible";
		document.getElementById("moviecontainer").style.display = "block";
		document.getElementById("moviecontainer").src="flash.php?t=f&f="+flash;
		document.getElementById("moviecontainer").location="flash.php?t=f&f="+flash;

		//alert(document.getElementById("moviecontainer").src);
	} else 	if (movie && movie != "") {
		//alert(movie);
		document.getElementById("moviecontainer").style.visibility = "visible";
		document.getElementById("moviecontainer").style.display = "block";
		document.getElementById("moviecontainer").src = "flash.php?t=m&f="+movie;
		document.getElementById("moviecontainer").location="flash.php?t=m&f="+movie;

		//alert(document.getElementById("moviecontainer").src);

	} else {
		document.getElementById("moviecontainer").location = "about:blank";
		document.getElementById("moviecontainer").style.visibility = "hidden";
		document.getElementById("moviecontainer").style.display = "none";
	}

	//reset rootline (only if obj is set).
	if (obj != null) {
		setLinkToRootLine(obj,"i4:"+pid, false);	
	}

	//hide actual subtext if one is visisble
	hideSubElement();
	//show linked subtext
	showSubElement("i5:"+id);
}

function openLinkInternal (pid,id,obj,backg, hideOther) {
	var ppos = getElementPosition(pid);
	var ipos = getElementPosition(id);
	var pident = getElementIdent(pid);
	var ident = getElementIdent(id);

	//change background if requested
	if (backg && backg != "") {
		document.getElementById("backgroundImage").src = backg;
	}

	//rest moviecontainer if set
	document.getElementById("moviecontainer").location = "about:blank";
	document.getElementById("moviecontainer").style.visibility = "hidden";
	document.getElementById("moviecontainer").style.display = "none";
	
	//reset rootline
	setLinkToRootLine(obj,pident);

	if (hideOther) {
		//hide all boxes with position greater than this position
		hideGtPosition(ppos);
	}

	//show linked box
	showElement(getElementIdent(id),ipos);
}

function showHideBoxCoordinates(event) {
	var maxBottom = 216;
	var size = getWindowSize();
	var bottom = size[1]- event.clientY;

	if (bottom > maxBottom) {
		hideBox();
	} else {
		showBox();
	}

	oldYCoordinate = bottom;
}


function hideBoxTimer(event) {
	boxStatus = 0;
	//window.setTimeout("hideBox()",500);
	return false;
}

function hideBox() {
	//alert("hide");
	//if (boxStatus == 0) {
		var box = document.getElementById("innerBox");
		box.style.visibility = "hidden";
	//}
}


function showBox() {
	//alert("show");
	boxStatus = 1;
	var box = document.getElementById("innerBox");
	if (box.style.visibility != "visible") {
		box.style.visibility = "visible";
	}
}


function getUrlParameter( name ) {
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp( regexS );
	var results = regex.exec( window.location.href );
	if( results == null ) {
    		return "";
	} else {
		return results[1];
	}
}

function loadPageByParameter() {
	//alertSize();
	//FUTURE: page loading by get parameter
	//alert(getUrlParameter('id'));
}

function initPage() {
	//loadPageByParameter();
	resizeImage_window();
	window.onresize = function() { resizeImage_window() }
}

function resizeImage() {
	var size = getWindowSize(); 
	var width = size[0]-20;
	
	document.getElementById("backgroundImage").style.width = (width)+"px";
	document.getElementById("backgroundImage").style.height = (width*backgroundImageAspectRatio)+"px";

	document.getElementById("moviecontainer").style.height = ((width)/2)*0.75+"px";
	//alert(document.getElementById("moviecontainer").style.height);
}

function resizeImage_window() {
	var size = getWindowSize(); 
	var width = size[0]-20;
	var height = size[1]-70;
	
	var newheight = (width*0.72);
	if(newheight > height) {
		document.getElementById("backgroundImage").style.width = (height*1.38)+"px";
		document.getElementById("backgroundImage").style.height = height+"px";
	}
	else {
		document.getElementById("backgroundImage").style.width = (width)+"px";
		document.getElementById("backgroundImage").style.height = (width*0.72)+"px";
	}

	document.getElementById("moviecontainer").style.height = ((width)/2)*0.75+"px";
}

function getWindowSize() {
  var myWidth = 0, myHeight = 0;
  var ret = new Array()
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  ret[0] = myWidth;
  ret[1] = myHeight;
  return ret;

  //window.alert( 'Width = ' + myWidth );
  //window.alert( 'Height = ' + myHeight );
}

