/**
  Stokersoft Javascript engine.
  Copyright 2010 Stokersoft - www.stokersoft.com
*/

var pages = new Array("lastupdate.html", "values.html", "averages.html", "eventlog.html");
var pageTargets = new Array("lastupdate", "skvalues", "averages", "eventlog");
var currentPage = 0;


/**
 Get current path and file.
*/
function xtractFile(data){
  var m = data.match(/(.*)[\/\\]([^\/\\]+\.\w+)$/);
  if (m==null || m.length<2) {
    return {path: data, file: ""};
  }
  return {path: m[1], file: m[2]};
}


/**
 Get the XMLHttp object from most browsers.
*/
function getXMLHttp() {
  var xmlhttp=false;
  /* running locally on IE5.5, IE6, IE7 */                                              
  if(location.protocol=="file:"){
   if(!xmlhttp)try{ xmlhttp=new ActiveXObject("MSXML2.XMLHTTP"); }catch(e){xmlhttp=false;}
   if(!xmlhttp)try{ xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }catch(e){xmlhttp=false;}
  }                                                                                
  /* IE7, Firefox, Safari, Opera...  */
  if(!xmlhttp)try{ xmlhttp=new XMLHttpRequest(); }catch(e){xmlhttp=false;}
  /* IE6 */
   if(typeof ActiveXObject != "undefined"){
     if(!xmlhttp)try{ xmlhttp=new ActiveXObject("MSXML2.XMLHTTP"); }catch(e){xmlhttp=false;}
     if(!xmlhttp)try{ xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }catch(e){xmlhttp=false;}
   }
   /* IceBrowser */
   if(!xmlhttp) try{ xmlhttp=createRequest(); }catch(e){xmlhttp=false;}
   return xmlhttp;
}


/**
 Load a text based document (url), into a division (divname) and specify if it
 should be done acynchronously.   
*/
function loadDoc(url, divName, async) {
  docBase = document.location.href;  
  ans = xtractFile(docBase);
  docBase = ans.path + '/';
  url = docBase + url + '?' + Date.parse(new Date().toString());  

  xmlhttp=getXMLHttp();
  xmlhttp.open("GET",url, async);
  if (async) {
    xmlhttp.onreadystatechange = function() {
      if(xmlhttp.readyState==4) {
        document.getElementById(divName).innerHTML=xmlhttp.responseText;
      }
    } 
  }
  xmlhttp.send(null);
  if (!async) {
    document.getElementById(divName).innerHTML=xmlhttp.responseText;    
  }
}

/**
 Load all text based documents immediately.
*/
function initDocs() {
  for (i=0; i<pages.length; i++) {
    loadDoc(pages[i], pageTargets[i], false);  
  }
}


/**
 Reloads the documents one at a time in a round robin fashion.
*/
function reloadDocs() {
  if (currentPage>=pages.length) {
    currentPage = 0;
  }  
  
  loadDoc(pages[currentPage], pageTargets[currentPage], true);
  currentPage++;
}


/**
 Udates the image (name) to a img with a specified id (id)
*/
function updateImage(id, name) {
  var newImg = new Image();
  newImg.onload = function () {
      var theImg = document.getElementsByName(id)[0];
      theImg.src = name;
  }
  newImg.src = name;
  theImg = null;
  newImg = null;
}


/**
 Reload the all of the images.
*/
function reloadImage() {
  if(document.images) {
    updateImage('systemgfx', 'system.png?' + Date.parse(new Date().toString()));
  }
}

/**
 Start the automatic update of images and text.
*/
function start() {
  initDocs();
  setInterval(reloadImage,5000);
  setInterval(reloadDocs,5000);
}

