NAME = Mail, Calls, SMS
NEED_IFUN_SUPPORT = 1

CAN_EDIT_STYLE = 1
CAN_EDIT_STRINGS = 1

CAN_EDIT_IMAGE_1 = [$DEFINITION]/IconCalls_0.png
CAN_EDIT_IMAGE_2 = [$DEFINITION]/IconCalls_N.png

CAN_EDIT_IMAGE_3 = [$DEFINITION]/IconMail_0.png
CAN_EDIT_IMAGE_4 = [$DEFINITION]/IconMail_N.png

CAN_EDIT_IMAGE_5 = [$DEFINITION]/IconSMS_0.png
CAN_EDIT_IMAGE_6 = [$DEFINITION]/IconSMS_N.png

CAN_EDIT_BACKGROUND_IMAGE = [$DEFINITION]/background.png

BEGIN_BODY_ONLOAD

	<div id="[$DEF_name]Div" style="
	[$STYLE_background]
	[$STYLE_background-color]
	[$STYLE_color]
	[$STYLE_text-shadow]
	[$STYLE_font-family]
	[$STYLE_font-weight]
	[$STYLE_font-size]
	[$STYLE_height]
	[$STYLE_width]
	[$STYLE_margin]
	[$STYLE_padding]
	[$STYLE_font-style]
	[$STYLE_word-spacing]
	[$STYLE_text-align]
	[$STYLE_text-indent]
	[$STYLE_text-transform]
	[$STYLE_text-decoration]
	[$STYLE_line-height]
	[$STYLE_border]
	[$STYLE_border-top]
	[$STYLE_border-bottom]
	[$STYLE_border-left]
	[$STYLE_border-right]
	[$STYLE_position_element]
	[$STYLE_transform]
	min-height: 10px;
	overflow: hidden;
	">

		<span id="[$DEF_name]Text" style="display: block;
		[$STYLE_position_content]
		[$STYLE_reflection]
		">
		</span>

	</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 LocStrings 	= new Array();
		
		LocStrings['0'] = new Array("No New E-Mails", "1 New E-Mail", "[Count] New E-Mails", 
		"No Upcoming Events", "1 Upcoming Event", "[Count] Upcoming Events",
		"No New Text Messages", "1 New Text Message", "[Count] New Text Messages",
		"No Missed Calls", "1 Missed Call", "[Count] Missed Calls"
		);

		[$LOCALIZABLE_STRINGS]

		//---
		//---

	    var LocStrings_ = LocStrings['0'];
	 	
	 	if (LocStrings[LANGUAGE] != undefined) LocStrings_ = LocStrings[LANGUAGE];
	  	
		var FormatStr = "[$DEF_format]";
		var theText;
		var theStr;
		var theIcon;
		
		theText = FormatStr;
		//---
	  	{
			var Count	= obj.Mail;
			var offset	= 0;
			
		    var Value_0	= LocStrings_[offset];
		    var Value_1	= LocStrings_[offset+1];
		    var Value_N	= LocStrings_[offset+2];
			
			if (Count == 0)			{theStr = Value_0; 								theIcon = "[$DEFINITION]/IconMail_0.png";}
			else if (Count == 1)	{theStr = Value_1;								theIcon = "[$DEFINITION]/IconMail_N.png";}
			else					{theStr = Value_N.replace("[Count]", Count);	theIcon = "[$DEFINITION]/IconMail_N.png";}
			
			rep = '<img src=' + "\"" + theIcon + "\"" + ' width="[$DEF_iconSize]" alt="" align=absmiddle>';
			theText = theText.replace(/\$MAIL_ICON/g, rep);
			
			theText = theText.replace(/\$MAIL_COUNT/g, Count);
			theText = theText.replace(/\$MAIL/g, theStr);
		}
		//---
	  	{
			var Count	= obj.SMS;
			var offset	= 3;
			
		    var Value_0	= LocStrings_[offset];
		    var Value_1	= LocStrings_[offset+1];
		    var Value_N	= LocStrings_[offset+2];
			
			if (Count == 0)			{theStr = Value_0; 								theIcon = "[$DEFINITION]/IconSMS_0.png";}
			else if (Count == 1)	{theStr = Value_1;								theIcon = "[$DEFINITION]/IconSMS_N.png";}
			else					{theStr = Value_N.replace("[Count]", Count);	theIcon = "[$DEFINITION]/IconSMS_N.png";}
			
			rep = '<img src=' + "\"" + theIcon + "\"" + ' width="[$DEF_iconSize]" alt="" align=absmiddle>';
			theText = theText.replace(/\$SMS_ICON/g, rep);
			
			theText = theText.replace(/\$SMS_COUNT/g, Count);
			theText = theText.replace(/\$SMS/g, theStr);
		}
		//---
	  	{
			var Count	= obj.Call;
			var offset	= 6;
			
		    var Value_0	= LocStrings_[offset];
		    var Value_1	= LocStrings_[offset+1];
		    var Value_N	= LocStrings_[offset+2];
			
			if (Count == 0)			{theStr = Value_0; 								theIcon = "[$DEFINITION]/IconCalls_0.png";}
			else if (Count == 1)	{theStr = Value_1;								theIcon = "[$DEFINITION]/IconCalls_N.png";}
			else					{theStr = Value_N.replace("[Count]", Count);	theIcon = "[$DEFINITION]/IconCalls_N.png";}
			
			rep = '<img src=' + "\"" + theIcon + "\"" + ' width="[$DEF_iconSize]" alt="" align=absmiddle>';
			theText = theText.replace(/\$CALLS_ICON/g, rep);
			
			theText = theText.replace(/\$CALLS_COUNT/g, Count);
			theText = theText.replace(/\$CALLS/g, theStr);
		}

		//---
		
		document.getElementById("[$DEF_name]Text").style.display = 'block';
		document.getElementById("[$DEF_name]Text").innerHTML = theText;
		
		//---
		//---
		
		[$DISPOSE_LOCALIZABLE_STRINGS]
		
		delete LocStrings['0'];
		delete LocStrings;
	}
	
	//-------------------------------------------------------------

	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/WidgetInfo.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.Call 		= DEFAULT_COUNT;
				obj.SMS 		= DEFAULT_COUNT;
				obj.Mail 		= DEFAULT_COUNT;

				[$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, "Mail");
		obj.Mail = parseInt(trimWhiteSpace(tag.firstChild.data), 10);

		tag = findChild(Database, "SMS");
		obj.SMS = parseInt(trimWhiteSpace(tag.firstChild.data), 10);

		tag = findChild(Database, "Call");
		obj.Call = parseInt(trimWhiteSpace(tag.firstChild.data), 10);

		//---
		
		/*
		tag = findChild(Database, "SpaceUsed");
		obj.SpaceUsed = parseInt(trimWhiteSpace(tag.firstChild.data), 10);
		
		tag = findChild(Database, "Phone");
		obj.Phone = trimWhiteSpace(tag.firstChild.data);

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

BEGIN_DEF

	DEF_name = 
	DEF_interval = 10
	DEF_iconSize = 12
	DEF_format = [$MAIL_ICON $MAIL<br>$CALLS_ICON $CALLS<br>$SMS_ICON $SMS]

END_DEF

BEGIN_FORMAT_MENU
	
	BEGIN_FORMAT_ENTRY
		CODE = 1
		DESC = Default (All)
		FORMAT = $MAIL_ICON $MAIL<br>$CALLS_ICON $CALLS<br>$SMS_ICON $SMS
	END_FORMAT_ENTRY

	BEGIN_FORMAT_ENTRY
		CODE = 1
		DESC = Icons & Counts (All)
		FORMAT = $MAIL_ICON $MAIL_COUNT<br>$CALLS_ICON $CALLS_COUNT<br>$SMS_ICON $SMS_COUNT
	END_FORMAT_ENTRY

	BEGIN_FORMAT_ENTRY
		CODE = 1
		DESC = Icons & Counts, One Line (All)
		FORMAT = $MAIL_ICON $MAIL_COUNT $CALLS_ICON $CALLS_COUNT $SMS_ICON $SMS_COUNT
	END_FORMAT_ENTRY

	//---
	
	BEGIN_FORMAT_ENTRY
		CODE = 0
		DESC = Mail
		FORMAT = $MAIL
	END_FORMAT_ENTRY

	BEGIN_FORMAT_ENTRY
		CODE = 0
		DESC = Mail Count
		FORMAT = $MAIL_COUNT
	END_FORMAT_ENTRY
	
	BEGIN_FORMAT_ENTRY
		CODE = 0
		DESC = Mail Icon
		FORMAT = $MAIL_ICON
	END_FORMAT_ENTRY
	
	BEGIN_FORMAT_ENTRY
		CODE = 0
		DESC = Calls
		FORMAT = $CALLS
	END_FORMAT_ENTRY

	BEGIN_FORMAT_ENTRY
		CODE = 0
		DESC = Calls Count
		FORMAT = $CALLS_COUNT
	END_FORMAT_ENTRY

	BEGIN_FORMAT_ENTRY
		CODE = 0
		DESC = Calls Icon
		FORMAT = $CALLS_ICON
	END_FORMAT_ENTRY

	BEGIN_FORMAT_ENTRY
		CODE = 0
		DESC = Text Message
		FORMAT = $SMS
	END_FORMAT_ENTRY

	BEGIN_FORMAT_ENTRY
		CODE = 0
		DESC = Text Message Count
		FORMAT = $SMS_COUNT
	END_FORMAT_ENTRY

	BEGIN_FORMAT_ENTRY
		CODE = 0
		DESC = Text Message Icon
		FORMAT = $SMS_ICON
	END_FORMAT_ENTRY
		
END_FORMAT_MENU

BEGIN_STYLE

	STYLE_position_element = 0px 0px;
	STYLE_position_content = 0px 0px;
	STYLE_transform = none;
	STYLE_padding = 4px 4px 4px 4px;
	STYLE_reflection = none;
	STYLE_font-size = 12px;
	STYLE_text-align = left;
	STYLE_height = auto;
	STYLE_width = auto;
	STYLE_background-color = none;
	STYLE_margin = 0px 0px 0px 0px;
	STYLE_border = none;
	STYLE_text-shadow = 4px 4px 4px rgba(0, 0, 0, 0.75);
	STYLE_font-family = Helvetica;
	STYLE_font-weight = bold;
	STYLE_color = rgba(255, 128, 128, 1);

	STYLE_background = 
	STYLE_font-style = 
	STYLE_word-spacing = 
	STYLE_text-align = 
	STYLE_text-indent = 
	STYLE_text-transform = 
	STYLE_text-decoration = 
	STYLE_line-height = 
	STYLE_border-top = 
	STYLE_border-bottom = 
	STYLE_border-left = 
	STYLE_border-right = 

END_STYLE
