var layer;
var tempX;
var tempY;
var IE;
var validCord = '';
var validCord2 = '';

var siteURL;

function trackMouse() {
	
	IE = document.all?true:false;
	if (!IE) document.captureEvents(Event.MOUSEMOVE)
	document.onmousemove = getMouseXY;
	tempX = 0;
	tempY = 0;
}

function getMouseXY(e) {
	if (IE) { // grab the x-y pos.s if browser is IE
		tempX = event.clientX + document.body.scrollLeft;
		tempY = event.clientY + document.body.scrollTop;
	}
	else {  // grab the x-y pos.s if browser is NS
	tempX = e.pageX;
	tempY = e.pageY;
	}  
	if (tempX < 0){tempX = 0;}
	if (tempY < 0){tempY = 0;}  
	//alert( "x=" + tempX + " y =" + tempY );
	
	var cords = new Array();
	var cords2 = new Array();
	
	var remove1 = 0;
	var remove2 = 0;
	
	if ( validCord.length > 4 )
	{
		cords = validCord.split( ';' );
		counter = 0;
		
		if ( cords.length <= 2 )
		{
	
		}
		else
		{
			// 815;215;120;74
			//alert( validCord );
			xMIN = cords[0];
			xMAX = parseInt( cords[0]) + parseInt( cords[2] );
			yMIN = cords[1]; // y
			yMAX = parseInt( cords[1] ) + parseInt( cords[3] ) + 20;
			
			//alert( "xmin=" + xMIN + " xmax=" + xMAX + " ymin=" + yMIN + " ymax=" + yMAX );
			//validCord = '';

			if ( tempX > xMIN && tempX < xMAX && tempY > yMIN && tempY < yMAX )
			{
				//OK
			}
			else
			{
				//removeMenu();
				remove1 = 1;
			}
		}
		
	}

	if ( validCord2.length > 4 )
	{
		cords2 = validCord2.split( ';' );
		counter = 0;
		
		if ( cords2.length <= 2 )
		{
	
		}
		else
		{
			// 815;215;120;74
			//alert( validCord );
			xMIN = cords2[0];
			xMAX = parseInt( cords2[0]) + parseInt( cords2[2] );
			yMIN = cords2[1]; // y
			yMAX = parseInt( cords2[1] ) + parseInt( cords2[3] ) + 20;
			
			//alert( "xmin=" + xMIN + " xmax=" + xMAX + " ymin=" + yMIN + " ymax=" + yMAX );
			//validCord = '';

			if ( tempX > xMIN && tempX < xMAX && tempY > yMIN && tempY < yMAX )
			{
				
			}
			else
			{
				//removeMenu();
				remove2 = 1;
			}
		}
		
	}

	//alert( remove1 );
	
	
	//alert( "remove1=" + remove1 + " remove2=" + remove2 );
	
	
	if ( remove1 == 1 && ( remove2 == 1 || validCord2 == '' ) )
	{
		removeMenu();	
	}

	
	


}

function setSiteURL( server )
{
	siteURL = server;
}


function getSiteURL()
{
	return siteURL;
}

function removeMenu()
{
	//removeList( divID );
	validCord = '';
	validCord2 = '';
	
	div = document.getElementById( 'toppmeny' );
	
	div.innerHTML = '';
	div.style.display = 'none';

	div = document.getElementById( 'submenu' );
	
	div.innerHTML = '';
	div.style.display = 'none';


}

function attachMenu( divID, nodeID, type, sa)
{
	/*	
	if(type==1)
	{
		subb = document.getElementById( 'submenu' );
		subb.style.display = 'none';

		layer = document.getElementById( 'toppmeny' );

		div = document.getElementById( divID );
		
		if ( type == 1 )
			layer.style.backgroundColor	= "#646464";
		else
			layer.style.backgroundColor	= "#de892a";
		
		//var newlayer = layer.cloneNode( true );
		//newlayer.setAttribute('id', 'newtoppmeny');

		
		//div.appendChild( newlayer );
	
		
		var x = findPosX( div );
		var y = findPosY( div );
		
		validCord = x + ";" + y;
		
		newY = y+22;
		newX = x+2;
		
		layer.style.left = newX + "px";
		layer.style.top = newY + "px";
		
		layer.style.display='inline';
		
		
		myFunction = 'topsearch';

		poststr = "&nodeID=" + encodeURI( nodeID ) + "&type=" + type + "&sa=" + sa;
		
		postRequest( "/" + sa + "/layout/set/ajax/vztlibrary/submenu", poststr);	
	
	}
	*/
}

var myFunction = '';
var http_request = false;
var poststr;
   
function postRequest(url, parameters) {
  http_request = false;
  if (window.XMLHttpRequest) { // Mozilla, Safari,...
	 http_request = new XMLHttpRequest();
	 if (http_request.overrideMimeType) {
		// set type accordingly to anticipated content type
		//http_request.overrideMimeType('text/xml');
		http_request.overrideMimeType('text/html');
	 }
  } else if (window.ActiveXObject) { // IE
	 try {
		http_request = new ActiveXObject("Msxml2.XMLHTTP");
	 } catch (e) {
		try {
		   http_request = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e) {}
	 }
  }
  if (!http_request) {
	 alert('Kan ikke opprette XMLHTTP instansen. Bytt browser');
	 return false;
  }
  
  http_request.onreadystatechange = hentPHP;
  http_request.open('POST', url, true);
  http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  http_request.setRequestHeader("Content-length", parameters.length);
  http_request.setRequestHeader("Connection", "close");
  http_request.send(parameters);
}

function hentPHP() {
	
  if ( http_request.readyState == 4 ) {
	if ( http_request.status == 200 ) 
	 {
		//alert(http_request.responseText);
		result = http_request.responseText;
		
		if ( myFunction == 'topsearch' )
		{
			var mylayer = document.getElementById( 'toppmeny' );
			mylayer.innerHTML = result;
			
			validCord = validCord + ";" + mylayer.clientWidth + ";" + mylayer.clientHeight;
			
			//alert( validCord );

			//mylayer.innerHTML = "jejejejeje";

		}
		
		if ( myFunction == 'getChildren' )
		{
			var mylayer = document.getElementById( 'submenu' );
			mylayer.innerHTML = result;		

			validCord2 = validCord2 + ";" + mylayer.clientWidth + ";" + mylayer.clientHeight;

		}
		
	} 
	 else 
	 {
		//alert( 'Problemer med å finne fila det postes til.' );
	 }
  }
}

function findPosX(obj)
{
var curleft = 0;
if(obj.offsetParent)
	while(1) 
	{
	  curleft += obj.offsetLeft;
	  if(!obj.offsetParent)
		break;
	  obj = obj.offsetParent;
	}
else if(obj.x)
	curleft += obj.x;
return curleft;
}

function findPosY(obj)
{
var curtop = 0;
if(obj.offsetParent)
	while(1)
	{
	  curtop += obj.offsetTop;
	  if(!obj.offsetParent)
		break;
	  obj = obj.offsetParent;
	}
else if(obj.y)
	curtop += obj.y;
return curtop;
}


function VZTfixHeight()
{
	var left = document.getElementById( 'left' );
	var right = document.getElementById( 'right' );
	
	leftheight 	= left.clientHeight;
	rightheight	= right.clientHeight;
	
	//alert ( "left = " + leftheight + " right = " + rightheight );
	
	if ( leftheight > rightheight )
		right.style.height = leftheight + 'px';
	else
	{
		document.getElementById( 'fixheight' ).style.height = parseInt( rightheight - 36) + 'px';
		//alert( parseInt( rightheight - 36) + 'px' );
	}
	
}

var xxx = 0;

function VZTfixHeightFull()
{
	var left = document.getElementById( 'left' );
	var right = document.getElementById( 'right' );
	
	var rightline = document.getElementById( 'rightline' );
	
	leftheight 	= left.clientHeight;
	rightheight	= right.clientHeight;
	
	//alert ( "left = " + leftheight + " right = " + rightheight );
	
	if ( leftheight > rightheight )
	{
		right.style.height = leftheight + 'px';
		rightline.style.height = parseInt( leftheight - 34 ) + 'px';
	}
	else
	{
		if ( xxx == 0 )
		{
			document.getElementById( 'fixheight' ).style.height = parseInt( rightheight - 36) + 'px';
			rightline.style.height = parseInt( rightheight - 34) + 'px';
		}
		//alert( parseInt( rightheight - 36) + 'px' );
	}
	

	xxx++;
	if ( xxx == 1 ) 
		setTimeout ( "VZTfixHeightFull()", 300 );
}

function swapImg( path )
{
	document.getElementById( 'imgtoswap' ).src = path;
	document.getElementById( 'video' ).style.display = 'none';
}

function getChildren( nodeid, id, type, sa )
{
		layer = document.getElementById( 'submenu' );

		div = document.getElementById( "sub" + id );
		
		if ( type == 1 )
			layer.style.backgroundColor	= "#646464";
		else
			layer.style.backgroundColor	= "#de892a";
		
		//var newlayer = layer.cloneNode( true );
		//newlayer.setAttribute('id', 'newtoppmeny');

		
		//div.appendChild( newlayer );
	
		var x = findPosX( div );
		var y = findPosY( div );
		
		newY = y;
		newX = x+115;
		
		validCord2 = newX + ";" + newY;
		
		layer.style.left = newX + "px";
		layer.style.top = newY + "px";
		
		layer.style.display='inline';


		myFunction = 'getChildren';
		
		poststr = "&nodeID=" + encodeURI( nodeid ) + "&sa=" + sa;
		postRequest( "/" + sa + "/layout/set/ajax/vztlibrary/subsubmenu", poststr);	
		
}

function popupx( dir )
{
	var url = "http://www.zeropex.com" + dir;
	var testwindow= window.open ( url, "mywindow","resizable=0,menubar=0,location=1,status=0,scrollbars=1,width=450,height=400");
	testwindow.moveTo(100,200);
}

function calculate()
{

	/* FLOW START */
	var useflow;
	var flowx 		= document.getElementById( 'flow' );
	var flow 		= flowx.value.replace(/,/, ".");
	var flowtype 	= document.getElementById( 'flowtype' );
	
	var selectedflowtype = flowtype[flowtype.selectedIndex].value;
	
	if ( selectedflowtype == 1 )
		useflow = flow;
	else if ( selectedflowtype == 2 )
		useflow = parseFloat( ( flow / 3600)*1000 );
	else if ( selectedflowtype == 3 )
		useflow = parseFloat( ( flow / 86400 ) * 1000 );
	else if ( selectedflowtype == 4 )
		useflow = parseFloat( ( flow * 3.78541178 ) / 60 );
	else if ( selectedflowtype == 5 )
		useflow = parseFloat( ( flow * 3.78541178 ) / 3600 );
	else if ( selectedflowtype == 6 )
		useflow = parseFloat( ( flow * 3.78541178 ) / 86400 );
	
	/* FLOW END */
	
	/* PRESSURE START */
	var usepressure;
	var pressurex 		= document.getElementById( 'pressure' );
	var pressure 		= pressurex.value.replace(/,/, ".");
	var pressuretype 	= document.getElementById( 'pressuretype' );
	var selectedpressuretype = pressuretype[pressuretype.selectedIndex].value;

	if ( selectedpressuretype == 1 )
		usepressure = pressure;
	else if ( selectedpressuretype == 2 )
		usepressure = parseFloat( pressure * 0.0689475729 );
	
	/* PRESSURE END */
	
	/* COST START */
	var costx	 		= document.getElementById( 'cost' );
	var cost 			= costx.value.replace(/,/, ".");
	var costtype 		= document.getElementById( 'costtype' );
	var selectedcosttype = costtype[costtype.selectedIndex].value;
	/* COST END */

	/* EFFIENCY */
	var efficiency		= document.getElementById( 'efficiency' ).value;
	
	
	/* OUTPUT */
	var netpower = parseFloat( usepressure * useflow * 0.0981 * efficiency );
	var production = parseFloat( netpower * 8760 );
	var str = "<table border=0><tr><td>Net Power</td><td align=right>" + makeStr( parseInt( netpower ) ) + " </td><td align=left>&nbsp;kW</td></tr>";
	str+= "<tr><td>Annual Production &nbsp;&nbsp;&nbsp;&nbsp;</td><td align=right>" + makeStr( parseInt( production ) ) + " </td><td align=left>&nbsp;kWh</td></tr>";
	str+= "<tr><td>Value of Electricity &nbsp;&nbsp;&nbsp;&nbsp;</td><td align=right>" + makeStr( parseInt( production * cost ) ) + " </td><td align=left>&nbsp;" + selectedcosttype +  "</tr></table>"; 
	
	document.getElementById( 'output' ).innerHTML = str;
	
}

function makeStr( input )
{
	
	var theString = input + "";
	var counter = theString.length; 

	var newStr = '';
	var i = 0; 
	for (counter ;counter > 0 ;counter -- ) { 
		newStr = newStr + theString.substring(counter-1,counter);
		i++;
		if ( i % 3 == 0 )
			newStr = newStr + " ";
	} 
	
	newerStr = '';
	counter = newStr.length;
	for (counter ;counter > 0 ;counter -- ) { 
		newerStr = newerStr + newStr.substring(counter-1,counter);
	} 
	
	return newerStr;
	
	
	//alert( theString + " - " + newStr );

}
