<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:widget="http://www.netvibes.com/ns/"  >
  <head>


    <meta name="apiVersion" content="0.0" />
    <meta name="author" content="Peer 2 World" />
    <meta name="website" content="http://www.weezo.net" />
    <meta name="description" content="Weezo server monitoring Widget" />
    <meta name="version" content="0.3" />
    <meta name="keywords" content="weezo, monitoring, photo, music, video, sharing, remote" />
    <meta name="screenshot" content="http://www.weezo.net/widget/screenshot280.png" />
    <meta name="thumbnail" content="http://www.weezo.net/widget/screenshot120.png" />

    <meta name="debugMode" content="false" />

    <title>Weezo Monitor</title>
    <link rel="icon" type="image/png"  href="http://www.weezo.net/favicon.ico" />

    <widget:preferences>
      <preference name="account" type="text" label="Weezo Account / Compte Weezo"
        defaultValue="" />
      <preference name="monitorWidgetAccessCode" type="text" label="Access Code / Code d'acces (Cf. configuration)"
        defaultValue=""/>

    </widget:preferences>

    <style type="text/css">
		.nv-thumbnailedList {width:100%;}
		#std{width:10%;padding-right:10px;border-right:0px solid #AAF}
		#utd{width:100%;padding-left:10px;text-align:left;font:10px underline bold;acolor:#CCC;vertical-align:top; font-family:arial}
		#serverState{font-size:16px !important}
		#nbConnected{font-size:10px !important; font-family:arial}
		#connected{width:100%;vertical-align:top;border:0px}
		#connected td{border:0px; font-family:arial}
		.icon{width:16px;height:16px;margin-right:4px;vertical-align:middle;cursor:hand}
    </style>
    <script type="text/javascript">
    var widgetId=Math.floor(10000*Math.random());
    widgetId='';

    // document.getElementById()
    function dgi(i){return document.getElementById(i+widgetId)}

    // Widget Item
	var weezoUWA = {}
	weezoUWA.data = null;

	// Weezo site URL
	var siteURL; var localSiteURL=false;
	widget.siteURL=false;
	widget.localSiteURL=false;
	
	var i; var output; var bodyInitialized=false; var xmlRequestError; var nextRefresh;
	widget.myIP=false;

    // XML response processing
    weezoUWA.dataProcessor = function (xhr){
    	try{window.clearTimeout(widget.connectionErrorTimeout);} catch(err){};
    	if(!xhr) return;
		var xmlRoot=xhr.documentElement;
		if(xmlRoot==null) {
    		if(xmlRequestError) xmlRequestError();
			return;
		}
		// Initialize body structure
		if(!bodyInitialized) initBody();

		//if(debugJS) alert('Async response : ' + xhr.responseText);
		var action;
		var content;
		for(var i=0;i<xmlRoot.childNodes.length;i++) if(xmlRoot.childNodes[i].nodeType==1){
			if(xmlRoot.childNodes[i].firstChild) content=xmlRoot.childNodes[i].firstChild.nodeValue; else content='';
			action=xmlRoot.childNodes[i].nodeName;
			// JSAction
			if(action=="JSAction") {
				eval(((xmlRoot.childNodes[i].getAttribute('commitBuffer'))?geckoBuffer:content));
			}
			// innerHTML
			if(action=="innerHTML"){
				if(xmlRoot.childNodes[i].getAttribute('add')) add=true; else add=false;
				if(xmlRoot.childNodes[i].getAttribute('nodeId')){
					if(dgi(xmlRoot.childNodes[i].getAttribute('nodeId'))) dgi(xmlRoot.childNodes[i].getAttribute('nodeId')).innerHTML=((add)?dgi(xmlRoot.childNodes[i].getAttribute('nodeId')).innerHTML:'')+((xmlRoot.childNodes[i].getAttribute('commitBuffer'))?geckoBuffer:content);
				}
				if(xmlRoot.childNodes[i].getAttribute('nodeName')){
					if(document.getElementsByName(xmlRoot.childNodes[i].getAttribute('nodeName')).length==0) alert('Problem: node name not found');
					else for(var o=0;o<document.getElementsByName(xmlRoot.childNodes[i].getAttribute('nodeName')).length;o++)
						document.getElementsByName(xmlRoot.childNodes[i].getAttribute('nodeName'))[o].innerHTML=((add)?document.getElementsByName(xmlRoot.childNodes[i].getAttribute('nodeName'))[o].innerHTML:'')+((xmlRoot.childNodes[i].getAttribute('commitBuffer'))?geckoBuffer:content);
				}
				if(xmlRoot.childNodes[i].getAttribute('nodeTagName')){
					for(var o=0;o<document.getElementsByTagName(xmlRoot.childNodes[i].getAttribute('nodeTagName')).length;o++)
						document.getElementsByTagName(xmlRoot.childNodes[i].getAttribute('nodeTagName'))[o].innerHTML=((add)?document.getElementsByTagName(xmlRoot.childNodes[i].getAttribute('nodeTagName'))[o].innerHTML:'')+((xmlRoot.childNodes[i].getAttribute('commitBuffer'))?geckoBuffer:content);
				}
			}
			// Add data to gecko buffer
			if(action=="addToBuffer") geckoBuffer+=content;

			// Clear buffer
			if(xmlRoot.childNodes[i].getAttribute('commitBuffer')) geckoBuffer="";
		}
    }

    // Update connected users information
    widget.updateInformation=function(){
    	// Timeout Error
    	xmlRequestError=function(){
    		xmlRequestError=undefined;
    		UWA.Data.getXml('http://weezo.net/widget/monitorWidget.php?error=connection&lng='+widget.lang.substr(0,2)+'&account='+widget.getValue('account'),weezoUWA.dataProcessor);
    	}
    	
		UWA.Data.getXml(widget.siteURL+'/widgets/monitor.php?monitorWidgetAccessCode='+widget.getValue('monitorWidgetAccessCode')+'&lng='+widget.lang,weezoUWA.dataProcessor);
		widget.connectionErrorTimeout=window.setTimeout('xmlRequestError()',12000);
    }
    function setNextRefresh(sec){nextRefresh=window.setTimeout('weezoWidget.updateInformation()',sec*1000);}
    function clearRefresh(){window.clearTimeout(nextRefresh);}

    // Set server state icon
    function setServerIcon(serverIconURL,onclickURL){
    	dgi('std').innerHTML=((onclickURL!=undefined)?'<a href="'+onclickURL+'" target="_blank">':'') + '<img alt="" src="'+serverIconURL+'">'+((onclickURL!=undefined)?'</a>':'');
    }

    // Set server state caption
    function setServerState(state){
    	dgi('serverState').innerHTML=state
    }

    // Display an error message
    function setMessage(message, isError){
    	if(message.indexOf(widget.getValue('monitorWidgetAccessCode'))!=-1) {
    		message=message.substr(0,message.length-widget.getValue('monitorWidgetAccessCode').length);
    		// Workaround for V1.0.8
    		setServerIcon("http://www.weezo.net/widget/Err.png");
    		isError=true;
    	}
    	if(isError=undefined || !isError) dgi('nbConnected').innerHTML='<span>'+message+'</span>'; else dgi('nbConnected').innerHTML='<span style="color:red">'+message+'</span>';
    }

    // Display number of connected users
    function setConnectedNb(message){
    	dgi('nbConnected').innerHTML=message
    	// Workaround for V1.0.8
    	setServerIcon("http://www.weezo.net/widget/On.png",'http://www.weezo.net/'+widget.getValue('account'));
    	dgi('serverState').href='http://www.weezo.net/'+widget.getValue('account');
    }

    /**
    * Display connected users list
    */
    function setConnected(list){
    	output='';
    	if(!widget.localSiteURL) widget.localSiteURL=widget.siteURL;
    	for(i=0;i<list.length;i++){
    		if(i%2==0) output+='<tr>';
			output+='<td><img class="icon" src="'+widget.localSiteURL+'/'+list[i][2]+'"/>'+list[i][0]+' ('+list[i][1]+')</td>';
    		if(i%2!=0) output+='</tr>';
    	}
    	if(i%2!=0) output+='</tr>';
    	if(navigator.appName=='Microsoft Internet Explorer') dgi('connected').outerHTML='<table id="connected'+widgetId+'" class="connected">'+output+'</table>';
    	else dgi('connected').innerHTML=output;
    	widget.callback('onUpdateBody');
    }

    // Initialize body structure
	function initBody(){
		//alert(widget.lang)
		widget.setBody('<table id="mt'+widgetId+'" class="nv-thumbnailedList" style="text-align:left !important; height:70px;display:block"><tr class="item odd">'+
		'<td id="std'+widgetId+'" class="std"><img src="http://www.weezo.net/widget/loading.gif" alt="..."/></td>'+
		'<td id="utd'+widgetId+'" class="utd">' +
			'<h3 style="padding-left:0;margin-left:0"><a href="javascript:void" id="serverState'+widgetId+'" class="serverState"></a></h3>' +
			'<div id="nbConnected'+widgetId+'" class="nbConnected"></div>' +
			'<table id="connected'+widgetId+'" class="connected"></table>' +
		'</td></tr></table>');
		setMessage(_('Loading...'),false);
		widget.callback('onUpdateBody');
		bodyInitialized=true;
	}
    // onRefresh
	widget.onRefresh=function(){
		clearRefresh();
		if(widget.getValue('account')) widget.setTitle('Weezo - '+widget.getValue('account'));
		// Wait until myIP script has run
		if(widget.myIP==false){
			window.setTimeout("weezoWidget.onRefresh()",100);
			return;
		}
		UWA.Data.getXml('http://weezo.net/widget/monitorWidget.php?userName='+widget.getValue('account')+'&lng='+widget.lang+'&monitorWidgetAccessCode='+((widget.getValue('monitorWidgetAccessCode')!='')?'1':'')+'&myIP='+widget.myIP,weezoUWA.dataProcessor);
	}

	// onResize
	widget.onResize = function() {}

	// onLoad
	widget.onLoad = function() {
		// Init body layout
		initBody();

		// Set Widget name
		if(widget.getValue('account')) widget.setTitle('Weezo - '+widget.getValue('account')); else widget.setTitle('Weezo');

		// Save widget object in document context
		snode = document.createElement("script");
	    snode.setAttribute("type", "text/javascript");
	    snode.setAttribute("charset", "utf-8");
	    snode.innerHTML='var weezoWidget=true;function updateInformation(){weezoWidget.updateInformation()}';
	    document.getElementsByTagName("head").item(0).appendChild(snode);
	    weezoWidget=this;

	    // init client IP
		snode = document.createElement("script");
	    snode.setAttribute("type", "text/javascript");
	    snode.setAttribute("charset", "utf-8");
	    snode.setAttribute("src", "http://www.weezo.net/widget/monitorWidget.php?getMyIP=1&widgetVersion=0.3");
	    snode.setAttribute("id", "yopla");
	    document.getElementsByTagName("head").item(0).appendChild(snode);
	    widget.onRefresh();
	}
    </script>

	</head>
	<body height="80" style="height:80px">
	<p>...</p>
	</body>
</html>