/* START Menu JavaScript */
var debug = false;
var menu, geom;
function quitit() {return true;}
function init() {
	geom = eval(layout['geometry']);
	if(!initRenderer()) return false;
	window.onresize = resize;
	var rootParentNode = new Object
	rootNode = document.getElementById('menuRootNode');
	rootNode.childNum = 1;
	rootNode.siblingNodeCount = 1;
	run(rootNode,rootParentNode);
	}
	
function resize() {
	//Need to have an intermediate instance -> Firefox fix
	menu.resize();
}
function initRenderer() {
	
//detect valid rendering method and make it the default one
	var mySvgMenu = new svgMenu('svgMenu');
	var myHtmlMenu = new htmlMenu('htmlMenu');
	debugMsg(mySvgMenu.supported);
	if (mySvgMenu.supported) {
		menu = mySvgMenu;
		myHtmlMenu.hide();
		return true;
	} else {
		//Opera delay fix
		if (window.opera) {
			window.setTimeout('init()',10); 
			return false;
		} else {
			menu = myHtmlMenu;
			mySvgMenu.hide();
			return true;
		}
	}
}
	
function debugMsg(msg) {
	if (debug) document.getElementById('debugMsg').innerHTML += (msg + '<br/>');
}

function run(currentDataNode, parentDataNode) {
	debugMsg('_______________________________________');
	var coordinates = geom(currentDataNode, parentDataNode);
	var x = coordinates[0];
	var y = coordinates[1];
	currentDataNode.param = coordinates[2];
	var text = currentDataNode.firstChild.firstChild.data;
	var href = currentDataNode.getElementsByTagName('a')[0].href;
	var title = (currentDataNode.getElementsByTagName('a')[0].attributes.title) ? currentDataNode.getElementsByTagName('a')[0].attributes.title.value : '';
	var childDataNodes = getChildNodesByTagName(currentDataNode, 'li');
	var childDataNodeCount = childDataNodes.length;
	debugMsg('currentNode:' + text + ' childDataNodes: ' + childDataNodeCount + ' parentLinkNode: ' + parentDataNode.linkNode + ' title: ' + title);
	currentDataNode.linkNode = menu.addNode(currentDataNode, parentDataNode.linkNode, x, y, text, href, title);
	debugMsg('LinkNode: ' + currentDataNode.linkNode)
	if(childDataNodeCount > 0) {
		for (var i = 0; i < childDataNodeCount; i++) {
			childDataNodes[i].childNum = i;
			childDataNodes[i].siblingNodeCount = childDataNodeCount;
			run(childDataNodes[i], currentDataNode);
		}
	}
}

function getChildNodesByTagName(element, tagname) {
	var elements = new Array();
	if (element) {
		var childNodes = element.getElementsByTagName(tagname);
		for (var i = 0; i < childNodes.length; i++) {
			if(childNodes[i].parentNode.parentNode == element) {
				elements[elements.length] = childNodes[i];
			}
		}
	}
	return elements;
}

snowflake = function (currentDataNode, parentDataNode) {
	parentAngle = parentDataNode.param;
	parentNodeCount = parentDataNode.siblingNodeCount;
	siblingNodeCount = currentDataNode.siblingNodeCount;
	currentChildNum = currentDataNode.childNum
	debugMsg('Snowflake - currentDataNode: ' + currentDataNode + ' parentAngle: ' + parentAngle + ' siblingNodeCount: ' + siblingNodeCount + ' currentChildNum: ' + currentChildNum);
	if (menu.root) {
		debugMsg('Root Node');
		var x = layout['renderWidth'] / 2;
		var y = layout['renderHeight'] / 2;
		var angle = 0;
		currentDataNode.arrowLength = layout['arrowLength']/ layout['arrowLengthChange'];
	}else{
		debugMsg('Normal node');
		var spread = (2 * Math.PI) / Math.sqrt(parentNodeCount);
		if(siblingNodeCount > 0) {
			var angleStep = spread / siblingNodeCount;
			var angle = currentChildNum * angleStep + parentAngle - (spread / 2) + (.5* angleStep);
			currentDataNode.arrowLength = parentDataNode.arrowLength * layout['arrowLengthChange'];
			var x = Math.cos(angle) * currentDataNode.arrowLength;
			var y = Math.sin(angle) * currentDataNode.arrowLength;

		}
	}
	return [x,y,angle];
}
tree = function (currentDataNode, parentDataNode) {
	parentWidth = parentDataNode.param;
	parentNodeCount = parentDataNode.siblingNodeCount;
	siblingNodeCount = currentDataNode.siblingNodeCount;
	currentChildNum = currentDataNode.childNum
	if (menu.root) {
		var x = layout['renderWidth'] / 2;
		var y = layout['logoHeight'] / 2;
		var currentWidth = layout['renderWidth'] - layout['maxTextWidth'];
		currentDataNode.arrowLength = layout['arrowLength'];
	} else {
		parentDataNode.parentDataNode
		var currentWidth = parentWidth / siblingNodeCount;
		if(siblingNodeCount > 0) {
			var x = (currentWidth * currentChildNum) - (parentWidth / 2) +  (currentWidth / 2);
			currentDataNode.arrowLength = parentDataNode.arrowLength * layout['arrowLengthChange'];			
			var y = currentDataNode.arrowLength;
		}
	}
	return [x,y,currentWidth];
}

/* END Menu JavaScript */

/* START Menu Output for JavaScript menu */
var htmlMenu = function() {
	this.root = true;
	//Fix since IE doesnt support floating point for pixel values
	this.ieFactor = 20;
	this.element = document.getElementById('htmlMenu');
	this.height = layout['renderHeight']
	this.width = layout['renderWidth']
	this.element.className='javascriptMenu';
	this.image = document.getElementById('logo');
	this.image.style.position='absolute';
	this.image.style.left =  '-' +((layout['logoWidth'] / 2) / this.ieFactor) + 'em';
	this.image.style.top =  '-' +((layout['logoHeight'] / 2) / this.ieFactor) + 'em';
	this.image.style.width = layout['logoWidth'] / this.ieFactor + 'em';
	this.image.style.height = layout['logoHeight'] / this.ieFactor + 'em';
	
	// WGR - upgrade footer from simple to Javascript
	document.getElementById('footer').setAttribute('class', 'javascriptFooter');
	document.getElementById('footer').setAttribute('className', 'javascriptFooter');

	this.resize();
}
htmlMenu.prototype.hide = function() {
	debugMsg('Hiding Html Menu');
	this.element.style.display = 'none';
}
htmlMenu.prototype.addNode = function(linkNode, parentNode, x, y, text, href) {
	linkNode.style.position = 'absolute';
	linkNode.firstChild.style.top = ((-linkNode.firstChild.offsetHeight / 2 )/ this.ieFactor) + 'em';
	linkNode.firstChild.style.left = ((-linkNode.firstChild.offsetWidth / 2 )/ this.ieFactor) + 'em';
	linkNode.style.top = y / this.ieFactor + 'em';
	linkNode.style.left = x  / this.ieFactor + 'em';
	linkNode.firstChild.style.fontSize = layout['textFontSize'] / this.ieFactor + 'em';
	if (!this.root) {
		this._drawArrow(linkNode, x, y)
		if (layout['boxShadow']) { 
			shadow = linkNode.firstChild.cloneNode(true);
			shadow.style.position = 'absolute';
			linkNode.firstChild.style.top = (-linkNode.firstChild.offsetHeight / 2 / this.ieFactor) + 'em';
			linkNode.firstChild.style.left = (-linkNode.firstChild.offsetWidth / 2 / this.ieFactor) + 'em';
			shadow.style.top = ((-linkNode.firstChild.offsetHeight +30) / 2 / this.ieFactor) + 'em';
			shadow.style.left =  ((-linkNode.firstChild.offsetWidth +30) / 2 / this.ieFactor) + 'em';
			shadow.style.opacity = '.3';
			shadow.style.filter="Alpha(opacity=40, finishopacity=20, style=2)";
			shadow.style.background = '#000000';
			shadow.style.border = 'none';
			linkNode.insertBefore(shadow,linkNode.firstChild);
		}
	} else {
		this.root = false;
	}
	return linkNode;
}
htmlMenu.prototype.resize = function () {
	scale = (layout['renderScaleToWindowWidth']) ? this._browserWidth() / this.width : 1;
	this.element.style.fontSize = (this.ieFactor * scale) + 'px';
	this.element.style.width = (this.width * scale) + 'px';
	this.element.style.height = (this.height * scale) + 'px';
}
htmlMenu.prototype._browserWidth = function () {
  var myWidth = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
  } else if( document.documentElement && ( document.documentElement.clientWidth) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
  } else if( document.body && ( document.body.clientWidth) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
  }
  return myWidth;
}
htmlMenu.prototype._drawArrow = function(container,x,y) {
	var length = Math.sqrt((x*x + y*y))
	var pixelNum = length * layout['htmlArrowPixelDensitiy'];
	var pixelStep = length / pixelNum;
	var dx = x / length;
	var dy = y / length;
	for (currentPixel = 1;currentPixel<=pixelNum;currentPixel++) {
		pixel = document.createElement('div');
		pixel.style.left = (currentPixel * pixelStep * dx - x) / this.ieFactor + 'em';
		pixel.style.top = (currentPixel * pixelStep * dy - y) / this.ieFactor + 'em';
		pixel.style.backgroundColor = layout['arrowColor'];
		pixel.style.width = layout['arrowWidth'] / this.ieFactor + 'em';
		pixel.style.height = layout['arrowWidth'] / this.ieFactor + 'em';
		pixel.style.overflow = 'hidden';
		pixel.style.position = 'absolute';
		container.appendChild(pixel);
	}
}

/* END Menu Output for JavaScript menu */

/* START Menu JavaScript backend for SVG*/
	
var svgMenu = function(id) {
	this.element = document.getElementById(id);
	this.supported = window.svgSupported
	if (!this.supported) return false;
	this.root = true;
	this.height = layout['renderHeight'];
	this.width = layout['renderWidth'];
	window.SVGsetLayout(layout);
	this.resize();
} 

svgMenu.prototype.addNode = function(currentDataNode, parentLinkNode, x, y, text, href,tooltip) {
	var currentNode = window.SVGaddNode(parentLinkNode, x, y, text, href,tooltip);
	if (this.root) {this.root = false};
	return currentNode;
}

svgMenu.prototype.resize = function() {
	if(!this.supported) return;
	var scale = (layout['renderScaleToWindowWidth']) ?  this._browserWidth() / this.width : 1;
	this.element.style.width = (this.width * scale + 'px');
	this.element.style.height = (this.height * scale + 'px');
	window.SVGsetDimension((this.width * scale),(this.height * scale));
	window.SVGsetScale(scale,scale);
}

svgMenu.prototype._browserWidth = function () {
  var myWidth = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
  } else if( document.documentElement && ( document.documentElement.clientWidth) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
  } else if( document.body && ( document.body.clientWidth) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
  }
  return myWidth;
}

svgMenu.prototype.hide = function() {
	debugMsg('Hiding SVG Menu');
	debugMsg(this.element);
	this.element.style.display = 'none';
}

var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();

/* END Menu JavaScript backend for SVG*/

/* START Snowflake */
// As some css properties are not correctly supported by firefox use this file for setting the style of the menu
var layout = new Object;
// ________________________________________________
// _________ Renderer Setup _______________________
layout['geometry'] = 'snowflake';
//Size of the SVG Canvas to use;
layout['renderHeight'] = 700;
layout['renderWidth'] = 1200;
//Enable scaling of the SVG to the actual browser width;
layout['renderScaleToWindowWidth'] = true;
//Interval for Animations (msec)
layout['animInterval'] = 100;
layout['logoWidth'] = 384;
layout['logoHeight'] = 240;
//_________________________________________________
//____________ Node settings ______________________
//font size of the text in node boxes
layout['textFontSize'] = 12;
//node text css style
layout['textFontStyle'] = 'fill:#000000; font-family:Verdana, Arial, Helvetica;';
//line height for multi line text
layout['textLineHeight'] = 15;
//width after which text will be wrapped
layout['maxTextWidth'] = 150;
//Distance of arrow heads from the outer border of node boxes
layout['boxMargin'] = 20;
//Distance between text and node box
layout['boxPadding'] = 5;
//Fill style of the node box
layout['boxFill'] = 'url(#darkBlue)'
//Enable node shadow
layout['boxShadow'] = true;
//Enable rounded node boxes
layout['boxCornerRadius'] = 5;
//background box css style 
layout['boxStyle'] = 'stroke-width: 1.5px; stroke:#5588DD;';
//____________________________________________________________
//____________ Arrow settings ________________________________
//starting length of connecting arrows
layout['arrowLengthChange'] = 0.65;
layout['arrowLength'] = 215;
//Ratio of the arrow length for child nodes
layout['arrowColor'] = '#000000';
layout['arrowWidth'] = '2';
//Densitiy of pixels used for the html arrows
layout['htmlArrowPixelDensitiy'] = .25;

//_____________________________________________________________
//______________ Tooltip Settings _____________________________
layout['tooltipWidth'] = 100;
layout['tooltipOffset'] = 35;
layout['tooltipCornerRadius'] = 10;
layout['tooltipFill'] = 'url(#lightYellow)';
layout['tooltipTipFill'] = '#DFD0B0'
layout['tooltipOpacity'] = .9;
layout['tooltipFontSize'] = 15;
layout['tooltipBoxStyle'] = 'font-family:Verdana, Arial, Helvetica,';
//Fadeing time in msec
layout['tooltipFadeTime'] = 1000;

/* END Snowflake */
