NAME = Battery Dependent Image

CAN_EDIT_STYLE = 1

CAN_EDIT_BACKGROUND_IMAGE = [$DEFINITION]/background.png

CAN_ADD_IMAGES = 1

BEGIN_BODY_ONLOAD

	<div id="[$DEF_name]Div" style="display: block;
	[$STYLE_background]
	[$STYLE_background-color]
	[$STYLE_height]
	[$STYLE_width]
	[$STYLE_margin]
	[$STYLE_padding]
	[$STYLE_border]
	[$STYLE_border-top]
	[$STYLE_border-bottom]
	[$STYLE_border-left]
	[$STYLE_border-right]
	[$STYLE_position_element]
	[$STYLE_transform]
	min-height: 1px;
	overflow: hidden;
	">

		<img id="[$DEF_name]Image" src="" alt="" style="display: none;
		width: 100%;
		height: 100%;
		[$STYLE_reflection]
		">

	</div>

END_BODY_ONLOAD

BEGIN_ONLOAD

  [$DEF_name]Load();

END_ONLOAD

BEGIN_DEFINITION

	var [$DEF_name]Interval 	= 1000 * [$DEF_interval];
	var [$DEF_name]IntervalID 	= 0;
	var [$DEF_name]LastShown 	= 0;

	//-------------------------------------------------------------

	function [$DEF_name]Update(obj)
	{
		var BatteryLevel;
		var BatteryDischarging = 0;
		var BatteryCharging = 0;
		var BatteryFull = 0;
		
		BatteryLevel = obj.BatteryLevel * 100;
		
		if (obj.BatteryState == 1)	BatteryDischarging = 1;
		if (obj.BatteryState == 2)	BatteryCharging = 1;
		if (obj.BatteryState == 3)	BatteryFull = 1;
		
		//---
		
		[$CONDITIONS]
		
		//---
		
		if (imgPath.length)
		{
			document.getElementById("[$DEF_name]Image").style.display = 'block';
			document.getElementById("[$DEF_name]Image").src = imgPath;
		}
		else
		{
			document.getElementById("[$DEF_name]Image").style.display = 'none';
		}
	}

	//-------------------------------------------------------------

	function [$DEF_name]Load()
	{
		var CurrentTimestamp 	= new Date();
		var CurrentTime 		= CurrentTimestamp.getTime();
		
		delete CurrentTimestamp;
		
		if ([$DEF_name]LastShown + [$DEF_name]Interval - 1000 <= CurrentTime)
		{
			var obj = {error:false, errorString:null};
			
			if (ON_DEVICE)
			{
				url = "/tmp/DeviceInfo.xml";

				var xmlReq = new XMLHttpRequest();
				xmlReq.onreadystatechange = function(e){[$DEF_name]Callback(e, xmlReq);}
				xmlReq.overrideMimeType("text/xml");
				xmlReq.open("GET", url, false); // synchronous!
				xmlReq.setRequestHeader("Cache-Control", "no-cache");
				xmlReq.setRequestHeader("wx", "385");
				xmlReq.send(null); 
			}
			else
			{
				obj.BatteryState = 0;
				obj.BatteryLevel = 0;
				
				[$DEF_name]Update(obj);
			}
			
			delete obj;

			//---
			
			var CurrentTimestamp = new Date();
			[$DEF_name]LastShown = CurrentTimestamp.getTime();
			delete CurrentTimestamp;
		}

		if ([$DEF_name]IntervalID)
		{
			clearInterval( [$DEF_name]IntervalID );
			[$DEF_name]IntervalID = 0;
		}
		
		if ([$DEF_name]Interval)
			[$DEF_name]IntervalID = setInterval([$DEF_name]Load, [$DEF_name]Interval);
	}

	//-------------------------------------------------------------

	function [$DEF_name]Callback(event, xmlReq)
	{
		if (xmlReq.readyState != 4)						return;
		if (xmlReq.status != 200 && xmlReq.status != 0)	return;
		if (!xmlReq.responseXML)						return;

		var obj = {error:false, errorString:null};

		var Database = findChild(xmlReq.responseXML, "bSE_Database");
		
		tag = findChild(Database, "BatteryState");
		obj.BatteryState = parseInt(trimWhiteSpace(tag.firstChild.data), 10);

		tag = findChild(Database, "BatteryLevel");
		obj.BatteryLevel = trimWhiteSpace(tag.firstChild.data) * 1.0;
		
		/*
		tag = findChild(Database, "Name");
		obj.Name = trimWhiteSpace(tag.firstChild.data);

		tag = findChild(Database, "Model");
		obj.Model = trimWhiteSpace(tag.firstChild.data);
		
		tag = findChild(Database, "UUID");
		obj.UUID = trimWhiteSpace(tag.firstChild.data);
		*/
		
		[$DEF_name]Update(obj);
		delete obj;
	}

END_DEFINITION

BEGIN_DEF

	DEF_name = 
	DEF_interval = 15

END_DEF

BEGIN_COND

END_COND

BEGIN_CONDITION_MENU
	
	BEGIN_CONDITION_ENTRY
		DESC = Ignore
		VAR = 
	END_CONDITION_ENTRY
	
	BEGIN_CONDITION_ENTRY
		DESC = Level (0 .. 100 in steps of 5)
		VAR = BatteryLevel
	END_CONDITION_ENTRY
	
	BEGIN_CONDITION_ENTRY
		DESC = Battery Charging (0 or 1)
		VAR = BatteryCharging
	END_CONDITION_ENTRY
	
	BEGIN_CONDITION_ENTRY
		DESC = Battery Full (0 or 1)
		VAR = BatteryFull
	END_CONDITION_ENTRY
	
	BEGIN_CONDITION_ENTRY
		DESC = Battery Discharging (0 or 1)
		VAR = BatteryDischarging
	END_CONDITION_ENTRY
	
END_CONDITION_MENU

BEGIN_STYLE

	STYLE_background = none;
	STYLE_width = 320px;
	STYLE_height = 480px;
	STYLE_padding = 0px 0px 0px 0px;
	STYLE_position_element = absolute; top: 0px; left: 0px;
	STYLE_background-color = none;
	STYLE_transform = none;
	STYLE_reflection = none;
	STYLE_margin = 0px 0px 0px 0px;
	STYLE_border = none;
	STYLE_border-top = none;
	STYLE_border-bottom = none;
	STYLE_border-left = none;
	STYLE_border-right = none;

END_STYLE
