var ajax; // Stores reference for xmlhttp object

var refreshtime = 100; // x100 ms
var secs;              // Used by the timer 
var timerID = null;    
var timerRunning = false;
var delay = 100;       // Total refresh time = refreshtime * delay



// Get XmlHttp object depending on browser (IE old/new, Mozilla, Opera, Safari)
function GetXmlHttp()
{
  var xmlHttp;
  try
  {
    // Firefox, Opera 8.0+, Safari, IE 7(?)
    xmlHttp=new XMLHttpRequest();
  }
    catch (e)
    {
      // Internet Explorer
      try
      {
        xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      }
        catch (e)
        {
          try
          {
            xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
          }
            catch (e)
            {
              alert("Could not create AJAX object! Automatic refreshing will NOT work!");
              return false;
            }
        }
    }
  return xmlHttp;
}


// Builds and sends ajax request for data update
function StartRefresh()
{
  var devidvalue=encodeURIComponent(document.getElementById("devid").value)
  var datevalue=encodeURIComponent(document.getElementById("date").value)
  var parameters="devid="+devidvalue+"&date="+datevalue;
  ajax.open("POST", "ajax.php", true)
  ajax.onreadystatechange=DoRefresh;   
  ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
  ajax.send(parameters)
  
  
}

function alertarr(arr)
{
  var s = '';
  
  for(name in arr)
  {
    s = s + name + ' = "' + arr[name] + '"\r\n';
  }
  
  alert(s);
}


// Callback when data is received
function DoRefresh()
{
  var data,container;
  
  
  if (ajax.readyState!=4) return false;  // Should only listen for complete requests.
  
                                                     
  data = eval('(' + ajax.responseText + ')');  // Compiles JSON object received from server
  
  container = document.getElementById('container');  
   
  // Remove Old text
  while (container.hasChildNodes())
    container.removeChild(container.firstChild);
	
  
    
  // Create a new element for each text
  for(name in data)
  {
    var arr = data[name];
    var type;
    var style = '';
    var bgimg, bgh, bgw;
	    
    var newnode = document.createElement('div');
      
    // Check if data is text, or link to an image, and act accordingly
    
    
    switch (arr['type'])
    {
    
      case 'text':
        newnode.innerHTML=arr['data'];  
        style = 
          'background-color:'+ arr['color'] + ';    \
           width:' + arr['width'] + 'px;            \
           height:' + arr['fontsize'] + 'px;        \
           vertical-align:middle;                   \
           font-weight:'+arr['bold'] + ';           \
           font-size:' + arr['fontsize'] + 'px;     \
           text-align:right;                        \
           padding-right:4px;                       \
           padding-bottom:2px;                      \
          ';
        if (arr['padding'] == 1)
           style +=
             '                                        \
             border-style:solid;                      \
             border-width:thin;                       \
             border-color:#404040;                    \
             ';

//           border: thin solid rgb(60,60,60);        \        
        break;
      
      case 'image':
        //Non-IE positioning workaround
        // HACK, HACK, HACK!!!!
        if (navigator.appName != 'Microsoft Internet Explorer')
          newnode.innerHTML='<img src="plcimg/'+arr['data']+'" alt="'+html_entity_decode(arr['hint'])+'" style="position:absolute;left:0px;top:-2px;" />';
        else
          {
          //newnode.setAttribute('alt', html_entity_decode(arr['hint'])); // Show hint on mouseover    
          newnode.innerHTML='<img src="plcimg/'+arr['data']+'" alt="'+html_entity_decode(arr['hint'])+'" />';
          }
        break;
    }
          
//    newnode.setAttribute('onmouseover', "javascript:showhint('" + arr['hint'] +"',this);"); // Show hint on mouseover    
    newnode.setAttribute('title', html_entity_decode(arr['hint'])); // Show hint on mouseover    
     
   bgimg = document.getElementById("bgimg");     
   bgh = bgimg.height;         
   bgw = bgimg.width;              
   if (arr['y'] < 0) arr['y'] = parseInt(arr['y'])+ parseInt(bgh);
   if (arr['x'] < 0) arr['x'] = parseInt(arr['x'])+ parseInt(bgw);
   

           
    var style = style +
     '                                \
       font-family:tahoma;            \
       position:absolute;             \
       left:'+arr['x']+'px;           \
       top:'+arr['y']+'px;           \
       color:black;                   \
       margin:0px;                    \
     ';
     

     
    // IE setAttribute('style', ...) bug workaround
    // HACK, HACK, HACK!!!
    if (navigator.appName == 'Microsoft Internet Explorer') newnode.style.setAttribute('cssText', style);
      else newnode.setAttribute('style',style);  
  
    
    container.appendChild(newnode);
  }
}

function InitializeTimer()
{
    // Set the length of the timer, in seconds
    secs = refreshtime + 1;
    StopTheClock();
    onTimer();
}

function StopTheClock()
{
    if(timerRunning)
        clearTimeout(timerID)
    timerRunning = false
}

function onTimer()
{
    secs = secs - 1;

    if (secs==0)
    {
        // We reached timeout, start a refresh
        StartRefresh();
        secs = refreshtime;
    }
    
    // Display remeaining time for refresh
    a=Math.ceil(secs/(1000/delay));
    if (a<10) {x='0'+a} else {x=a;}
    document.getElementById('refresh').value = 'Refresh in '+ x ;
    timerRunning = true;
    timerID = self.setTimeout("onTimer()", delay);
}


// variable initialization
ajax = GetXmlHttp();
timerID = null;
timerRunning = false;
delay = 100;


