// created by Adam Ashby 11/28/11
// last modified: 12/1/11


function bioMaxMeter(DIV,XML)
{
	var timer
	var xmlhttp
	var xmlDoc
	var TotalRunHours = String;
	var kWhrsProduced = String;
	var CommisionedDate = String;
	var Site_id = String;
	Site_id = '';
	var CurrentDate = String;
	var CurrentTime = String;
	var ThermalRunHours = String;
	var EngineRunHours = String;
	var sInt;
	var count = Number;
	var num = 0;
	var string = "";
	var div
	var bo = 1;
	var blinkOn = false;

	var parentDiv = document.getElementById(DIV); 
	
	var bg = document.createElement("div");
    bg.id ="biomax-bg"; 
    parentDiv.appendChild(bg);
	
	var site = document.createElement("div");
    site.id ="biomax-site"; 
	//site.innerHTML = "Dixon Ridge Farms";
    bg.appendChild(site);
	
	var update = document.createElement("div");
    update.id ="biomax-update"; 
	//update.innerHTML = "As of: 9:45 : 1/24/11";
    bg.appendChild(update);
	
	var totalrunhours = document.createElement("div");
    totalrunhours.id ="biomax-totalrunhours"; 
	totalrunhours.className ="biomax-numbers"; 
	totalrunhours.align = "right";
    bg.appendChild(totalrunhours);
	
	var elecrunhours = document.createElement("div");
    elecrunhours.id ="biomax-elecrunhours"; 
	elecrunhours.className ="biomax-numbers"; 
	elecrunhours.align = "right";
    bg.appendChild(elecrunhours);
	
	var thermrunhours = document.createElement("div");
    thermrunhours.id ="biomax-thermrunhours";
	thermrunhours.className ="biomax-numbers"; 
	thermrunhours.align = "right";
    bg.appendChild(thermrunhours);
	
	var totalenergy = document.createElement("div");
    totalenergy.id ="biomax-totalenergy";
	totalenergy.className ="biomax-numbers"; 
	totalenergy.align = "right";
    bg.appendChild(totalenergy);
	
	var cursor = document.createElement("div");  
    cursor.id ="biomax-cursor"; 
    bg.appendChild(cursor);
	
	var btn = document.createElement("div");  
    btn.id ="biomax-btn"; 
    bg.appendChild(btn);
	
	var popup = document.createElement("div");  
    popup.id ="biomax-popup"; 
    bg.appendChild(popup);
	
	btn.onclick = function(){ popup.style.visibility = "visible"};
	popup.onclick = function(){ popup.style.visibility = "hidden"};
	
	var pholder = document.createElement("div");  
    pholder.id ="biomax-pholder"; 
    parentDiv.appendChild(pholder);
	
	var photo = document.createElement("div"); 
    photo.id ="biomax-photo"; 
    pholder.appendChild(photo);
	
	var tagbox = document.createElement("div"); 
    tagbox.id ="biomax-tagbox"; 
	tagbox.style.marginTop = '254px';
    pholder.appendChild(tagbox);
	
	var tagbg = document.createElement("div");
    tagbg.id ="biomax-tagbg"; 
    tagbox.appendChild(tagbg);
	
	var tag = document.createElement("div"); 
    tag.id ="biomax-tag";
    tagbox.appendChild(tag);
	
	start();
	
	function start()
	{
		//alert('start');
		tag.style.zoom = '1';
    	tag.style.filter= 'alpha(opacity=0)';
    	tag.style.opacity= '0';
		$('#biomax-tagbox').animate({ bottom: "66px"}, 500, 'swing',function() { tagIn() });
	}
	
	function tagIn()
	{
		//alert('tagIn');
		timer = setTimeout(fadeOut,5500);
		$('#biomax-tag').animate({ opacity: 1}, 500, 'linear',function() { getXML(); });
	}
	
	
	function fadeOut()
	{
		ifError();
		$('#biomax-pholder').animate({ opacity: 0}, 700, 'linear',function() { typeNext();  pholder.style.visibility = "hidden" });
	}
	
	function ifError()
	{
		if(Site_id.length == 0)
		{
			site.innerHTML = "ERROR!";
			site.className = "biomax-site-error";
			xmlhttp.abort();
		} else {
			site.className = "biomax-site-normal";
		}
	}
	
	function fadeIn()
	{
		pholder.style.visibility = "visible";
		$('#biomax-pholder').animate({ opacity: 1}, 700, 'linear',function() { start(); resetTxt()  });
	}
	
	function getXML ()
	{
		//alert('getXML')
		xmlhttp = null;
		xmlhttp = new XMLHttpRequest();
		xmlhttp.open("GET",XML,true);
		xmlhttp.onreadystatechange = onSumResponse;
	 	xmlhttp.send();
		
		/*xmlhttp.open("GET",XML,false);
		xmlhttp.send();
		xmlDoc = xmlhttp.responseXML;

		
		for (var i = 0; xmlDoc.getElementsByTagName("Terminal").length > i; i++)
		{
			var v = i;
			parseXML(i);
		}*/
		
	}
	
	function onSumResponse() 
	{	
		//alert(xmlhttp.statusText);
		
		if(xmlhttp.readyState == 4 && xmlhttp.statusText == 'OK')
		{
			if (xmlhttp.responseXML == null) xmlDoc = stringToXML(xmlhttp.responseText); else xmlDoc = xmlhttp.responseXML;

			//alert(xmlDoc);
			
			for (var i = 0; xmlDoc.getElementsByTagName("Terminal").length > i; i++)
			{
				var v = i;
				parseXML(i);
			}
		}
	}
	
	function stringToXML(strXML)
	{
		if (window.ActiveXObject)
		{
			var doc=new ActiveXObject("Microsoft.XMLDOM");
			doc.async="false";
			doc.loadXML(strXML);
		}
		// code for Mozilla, Firefox, Opera, etc.
		else
		{
			var parser=new DOMParser();
			var doc=parser.parseFromString(strXML,"text/xml");
		}// documentElement always represents the root node
		return doc;
	}
	
	function parseXML(Number)
	{
		//alert(xmlDoc.getElementsByTagName("Name")[Number].childNodes[0].nodeValue);
		
		switch(xmlDoc.getElementsByTagName("Name")[Number].childNodes[0].nodeValue)
		{
			case 'Total Run Hours':
				TotalRunHours = xmlDoc.getElementsByTagName("Value")[Number].childNodes[0].nodeValue;
				break;
			case 'kWhrs Produced':
				kWhrsProduced = xmlDoc.getElementsByTagName("Value")[Number].childNodes[0].nodeValue;
				break;
			case 'Commisioned Date':
				CommisionedDate = xmlDoc.getElementsByTagName("Value")[Number].childNodes[0].nodeValue;
				break;
			case 'Site_id':
				Site_id = xmlDoc.getElementsByTagName("Value")[Number].childNodes[0].nodeValue;
				site.innerHTML = Site_id;
				site.className = "biomax-site-normal";
				break;
			case 'Current Time':
				CurrentTime = xmlDoc.getElementsByTagName("Value")[Number].childNodes[0].nodeValue;
				break;
			case 'Current Date':
				CurrentDate = xmlDoc.getElementsByTagName("Value")[Number].childNodes[0].nodeValue;
				break;
			case 'Thermal Run Hours':
				ThermalRunHours = xmlDoc.getElementsByTagName("Value")[Number].childNodes[0].nodeValue;
				break;
			case 'Engine Run Hours':
				EngineRunHours = xmlDoc.getElementsByTagName("Value")[Number].childNodes[0].nodeValue;	
				break;
		}
	}
	
	function cursorBlink()
	{
		if(blinkOn){
			if (bo == 0) $('#biomax-cursor').animate({ opacity: 65}, 300, 'linear',function() { bo=1; cursorBlink() });
			if (bo == 1) $('#biomax-cursor').animate({ opacity: 0}, 300, 'linear',function() { bo=0; cursorBlink()  });
		} else {			
			$('#biomax-cursor').animate({ opacity: 65}, 300, 'linear');
		}
	}
	
	function typeNext()
	{
		switch(num)
		{
			case 0:
				string = "As of: " + CurrentTime.toString() + " : " + CurrentDate.toString();;
				div = update;
				cursor.className ="biomax-cursor0";
				break;
			case 1:
				string = TotalRunHours;
				div = totalrunhours;
				break;
			case 2:
				string = EngineRunHours;
				div = elecrunhours;
				break;
			case 3:
				string = ThermalRunHours;
				div = thermrunhours;
				break;
			case 4:
				string = kWhrsProduced;
				div = totalenergy;
				break;	
		}
		
		count = 0;
		
		if(Site_id.length == 0) 
		{
			$('#biomax-tagbox').animate({ bottom: "-4px"}, 1);
			cursor.className ="biomax-cursor0";
			blinkOn = true; 
			cursorBlink();
			timer = setTimeout(fadeIn,8000);
		} else  {
			sInt = setInterval(writeIt,34);
		}
	}
	
	function writeIt() 
	{
		if (num == 0) {
			$('#biomax-cursor').animate({ left: '+=5',}, 1, 'linear'); 
		}
		
		div.innerHTML = string.substring(0,count);
		count++;
		
		if(count > string.length) 
		{
			clearInterval(sInt);
			num++;
			//if (num == 1) {blinkOn = true; cursorBlink()};
			if(num < 5) timer = setTimeout(typeNext,900);
			if(num > 4) num = 0;
			
			switch(num)
			{
				case 0:
					cursor.className ="biomax-cursor5"
					blinkOn = false;
					//$('#biomax-tag').animate({ opacity: 0}, 1);
					$('#biomax-tagbox').animate({ bottom: "-4px"}, 1,function() { bo=1; cursorBlink() });
					timer = setTimeout(fadeIn,8000);
					break;
				case 1:
					blinkOn = true; 
					cursorBlink();
					cursor.className ="biomax-cursor1";
					$('#biomax-cursor').animate({ left: '0',}, 1, 'linear');
					break;
				case 2:
					cursor.className ="biomax-cursor2"
					break;
				case 3:
					cursor.className ="biomax-cursor3"
					break;
				case 4:
					cursor.className ="biomax-cursor4"
					break;	
			}
			
			
		}
	}
	
	function resetTxt()
	{
		blinkOn = false;
		cursor.className ="biomax-cursor5"
		popup.style.visibility = "hidden"
		update.innerHTML = "";
		totalrunhours.innerHTML = "";	
		elecrunhours.innerHTML = "";	
		thermrunhours.innerHTML = "";	
		totalenergy.innerHTML = "";
	}
	
	
}



