/**********************************
		  GLOBAL VARIABLES
***********************************/
// pre-cache art files and sizes for widget styles and spacers
// (all images must have same height/width)
var collapsedWidget = new Image(20, 16);
collapsedWidget.src = "/micons/oplus.gif";
var collapsedWidgetStart = new Image(20, 16);
collapsedWidgetStart.src = "/micons/oplusStart.gif";
var collapsedWidgetEnd = new Image(20, 16);
collapsedWidgetEnd.src = "/micons/oplusEnd.gif";
var expandedWidget = new Image(20, 16);
expandedWidget.src = "/micons/ominus.gif";
var expandedWidgetStart = new Image(20, 16);
expandedWidgetStart.src = "/micons/ominusStart.gif";
var expandedWidgetEnd = new Image(20, 16);
expandedWidgetEnd.src = "/micons/ominusEnd.gif";
var nodeWidget = new Image(20, 16);
nodeWidget.src = "/micons/onode.gif";
var nodeWidgetEnd = new Image(20, 16);
nodeWidgetEnd.src = "/micons/onodeEnd.gif";
var emptySpace = new Image(20, 16);
emptySpace.src = "/micons/oempty.gif";
var chainSpace = new Image(20, 16);
chainSpace.src = "/micons/ochain.gif";

// miscellaneous globals
var widgetWidth = "20";
var widgetHeight = "16";
var currState = "";
//var displayTarget = "contentFrame";
var displayTarget = "_self";

/**********************************
		   DATA COLLECTIONS
***********************************/
var expansionState = "";
// constructor for outline item objects
function outlineItem(text, uri) {
	this.text = text;
	this.uri = uri;
}
var olData = {
	childNodes: [
		{item:new outlineItem("Home", "/index.html"), 
			childNodes: [
			{item:new outlineItem("Dorling", "/dorling/dorling.html")}, 
			{item:new outlineItem("Gallery", "/gallery")}
			]
		},
		{item:new outlineItem("Examples", "/examples.html"), 
			childNodes: [
			{item:new outlineItem("Tool", "/tool")}, 
			{item:new outlineItem("Screenshots", "/screenshots.html")}
			]
		},
		{item:new outlineItem("Help", "/help.html"), 
			childNodes: [
			{item:new outlineItem("<i>English</i>", "/help.html")}, 
			{item:new outlineItem("<i>Deutsch</i>", "/help_de.html")}, 
			{item:new outlineItem("<i>Fran&ccedil;ais</i>", "/help/help_fr.pdf")},
			{item:new outlineItem("<i>Italiano</i>", "help_it.html")}, 
			{item:new outlineItem("Own Data", "/help/h_data.html")}, 
			{item:new outlineItem("EPS", "/help/h_ps.html")}
			]
		},
		{item:new outlineItem("Documentation", "/docu.html"), 
			childNodes: [
			{item:new outlineItem("Colorschemes", "/docu/colorschemes.html")}, 
			{item:new outlineItem("Error Codes", "/msgcodes.html")}, 
			{item:new outlineItem("shp2psc", "/shp2psc.html")}
			]	
		},
		{item:new outlineItem("Downloads", "/downloads.html")},
		{item:new outlineItem("Support, Contact", "/support.html"),
			childNodes: [
			{item:new outlineItem("Feedback", "/feedback")}, 
			{item:new outlineItem("Donation", "/support.html#don")}
			]	
		}
	]
	};

		  


/**********************************
  TOGGLE DISPLAY AND ICONS
***********************************/
// invert item state (expanded to/from collapsed)
function swapState(currState, currVal, n) {
	var newState = currState.substring(0,n);
	newState += currVal ^ 1 // Bitwise XOR item n;
	newState += currState.substring(n+1,currState.length);
	return newState;
}

// retrieve matching version of 'minus' images
function getExpandedWidgetState(imgURL) {
	if (imgURL.indexOf("Start") != -1) {
		return expandedWidgetStart.src;
	}
	if (imgURL.indexOf("End") != -1) {
		return expandedWidgetEnd.src;
	}
	return expandedWidget.src;
}

// retrieve matching version of 'plus' images
function getCollapsedWidgetState(imgURL) {
	if (imgURL.indexOf("Start") != -1) {
		return collapsedWidgetStart.src;
	}
	if (imgURL.indexOf("End") != -1) {
		return collapsedWidgetEnd.src;
	}
	return collapsedWidget.src;
}

// toggle an outline mother entry, storing new state value;
// invoked by onclick event handlers of widget image elements
function toggle(img, blockNum) {
	var newString = "";
	var expanded, n;
	// modify state string based on parameters from IMG
	expanded = currState.charAt(blockNum);
	currState = swapState(currState, expanded, blockNum);
	// dynamically change display style
	if (expanded == "0") {
		document.getElementById("OLBlock" + blockNum).style.display = "block";
		img.src = getExpandedWidgetState(img.src);
	} else {
		document.getElementById("OLBlock" + blockNum).style.display = "none";
		img.src = getCollapsedWidgetState(img.src);
	}
}

function expandAll() {
	var newState = "";
	while (newState.length < currState.length) {
		newState += "1";
	}
	currState = newState;
	initExpand();
}

function collapseAll() {
	var newState = "";
	while (newState.length < currState.length) {
		newState += "0";
	}
	currState = newState;
	initExpand();
}

/*********************************
   OUTLINE HTML GENERATION
**********************************/
// apply default expansion state from outline's header
// info to the expanded state for one element to help 
// initialize currState variable
function calcBlockState(n) {
	// get default expansionState data
	var expandedData = (expansionState.length > 0) ? expansionState.split(",") : null;
	if (expandedData) {
		for (var j = 0; j < expandedData.length; j++) {
			if (n == expandedData[j] - 1) {
				return "1";
			}
		}
	}
	return "0";
}

// counters for reflexive calls to drawOutline()
var currID = 0;
var blockID = 0;
// generate HTML for outline
function drawOutline(ol, prefix) {
	var output = "";
	var nestCount, link, nestPrefix, lastInnerNode;
	prefix = (prefix) ? prefix : "";
	for (var i = 0; i < ol.childNodes.length ; i++) {
		nestCount = (ol.childNodes[i].childNodes) ? ol.childNodes[i].childNodes.length : 0;
		output += "<div class='OLRow' id='line" + currID++ + "'>\n";
		if (nestCount > 0) {
			output += prefix;
			output += "<img id='widget" + (currID-1) + "' src='" + ((i == ol.childNodes.length-1 && blockID != 0) ? collapsedWidgetEnd.src : (blockID == 0) ? collapsedWidgetStart.src : collapsedWidget.src);
			output += "' height=" + widgetHeight + " width=" + widgetWidth;
			output += " title='Click to expand/collapse nested items.' onClick='toggle(this," + blockID + ")'>&nbsp;";
			link =	(ol.childNodes[i].item.uri) ? ol.childNodes[i].item.uri : "";
			if (link) {
				output += "<a href='" + link + "' class='itemTitle' title='" + 
				link + "' target='" + displayTarget + "'>" ;
			} else {
				output += "<a class='itemTitle' title='" + link + "'>";
			}
			output += "<span style='position:relative; top:-3px; height:11px'>" + ol.childNodes[i].item.text + "</span></a>";
			currState += calcBlockState(currID-1);
			output += "<span class='OLBlock' blocknum='" + blockID + "' id='OLBlock" + blockID++ + "'>";
			nestPrefix = prefix;
			nestPrefix += (i == ol.childNodes.length - 1) ? 
					   "<img src='" + emptySpace.src + "' height=" + widgetHeight + " width=" + widgetWidth + ">" :
					   "<img src='" + chainSpace.src + "' height=" + widgetHeight + " width=" + widgetWidth + ">"
			output += drawOutline(ol.childNodes[i], nestPrefix);
			output += "</span></div>\n";
		} else {
			output += prefix;
			output += "<img id='widget" + (currID-1) + "' src='" + ((i == ol.childNodes.length - 1) ? nodeWidgetEnd.src : nodeWidget.src);
			output += "' height=" + widgetHeight + " width=" + widgetWidth + ">";
			link =	(ol.childNodes[i].item.uri) ? ol.childNodes[i].item.uri : "";
			if (link) {
				output += "&nbsp;<a href='" + link + "' class='itemTitle' title='" + 
				link + "' target='" + displayTarget + "'>";
			} else {
				output += "&nbsp;<a class='itemTitle' title='" + link + "'>";
			}
			output += "<span style='position:relative; top:-3px; height:11px'>" + ol.childNodes[i].item.text + "</span></a>";
			output += "</div>\n";
		}
	}
	return output;
}

/*********************************
	 OUTLINE INITIALIZATIONS
**********************************/
// expand items set in expansionState var, if any
function initExpand() {
//	alert("init " + currState + " " + currState.length);
	for (var i = 0; i < currState.length; i++) {
//		alert(i + ": OLBlock" + i + "." + document.getElementById("OLBlock" + i));			
//FIXME: notloesung mit test auf null
		if (document.getElementById("OLBlock" + i) != null) {
		if (currState.charAt(i) == 1) {
				document.getElementById("OLBlock" + i).style.display = "block";
			} else {
				document.getElementById("OLBlock" + i).style.display = "none";
			}
		}
		}
	}

// initialize first time -- invoked onload
function initExpMenu(xFile) {
	// wrap whole outline HTML in a span
	var olHTML = "<span id='renderedOL'>" + drawOutline(olData) + "</span>";
	// throw HTML into 'content' div for display
	document.getElementById("content").innerHTML = olHTML;
	initExpand();
}

/*********************************
	 COOKIES
**********************************/

function setState() {
	var expDate = getExpDate(180, 0, 0);
	setCookie("currState", currState, expDate);
//	alert("set " + currState + "/" + expDate);
}

function getState() {
	currState = getCookie("currState");
	if (currState == null) currState = "00000000000000000000";
	currState = currState.substring(0, 15);
//	alert("get " + currState);
}

