// Html Menu Output Layer
// Recurity-Labs 2007
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';

	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);
	}
}
