soundManager.url = '/swf-files/';
soundManager.flashVersion = 8;
soundManager.useFlashBlock = false;

soundManager.onload = function() {
  // soundManager is ready to use.
  // createSound() / play() etc. can now be called
  soundManager.createSound({ id:'perri', url:'anutheroneforperri.mp3' });
}
soundManager.onerror = function() {
  // Oh no! No sound support.
  // Maybe configure your app to ignore sound calls.
  // (SM2 calls will silently return false after this point.)
}

var removacountup = 0;
var removatimeout = null;
var lastcount = 0;

function removameter()
{
  // defeat browser caching for revision changes!
  var rev_no = 112;
  loadXMLDoc('latest_removal_xml.php?&rev_no='+rev_no);
  if (removacountup >= 0) removacounter();
}

function removacounter()
{
  var counter = document.getElementById("removameter_count");

  if (removacountup < 0)
  {
    // kill the timer
    clearTimeout(removatimeout);
    // try again one minute later
    removatimeout = setTimeout("removameter()", 60000);
  }
  else if (removacountup > 400000)
  {
    // kill the timer
    clearTimeout(removatimeout);
    counter.innerHTML = ". . . . . . .";
    // try again one minute later
    removatimeout = setTimeout("removameter()", 60000);
  }
  else
  {
    //removacountup += Math.floor(Math.random() * 3333);
    removacountup += 2000;
    var rand1 = 100 + Math.floor(Math.random() * 899);
    var rand2 = 100 + Math.floor(Math.random() * 899);
    counter.innerHTML = "8," + rand1 + "," + rand2;
    removatimeout = setTimeout("removacounter()", 60);
  }
}

/*
<response>
  <action>done</action>
  <count><?php echo $count; ?></count>
  <image-src><?php echo $imagesrc; ?></image-src>
  <image-time><?php echo $imagetime; ?></image-time>
</response>
*/
function removameter_update() 
{
  // iff req state is "complete" - all other states are tossed out and ignored
  if (xml_http_req.readyState == 4) 
  {
    // iff "OK"
    if (xml_http_req.status == 200) 
    {
      removacountup = -1;

      var response = xml_http_req.responseXML.documentElement;
      
      var action = response.getElementsByTagName('action')[0].firstChild.data;
      var count = response.getElementsByTagName('count')[0].firstChild.data;
      var imagesrc = response.getElementsByTagName('image-src')[0].firstChild.data;
      var imagetime = response.getElementsByTagName('image-time')[0].firstChild.data;
      
      if (count != lastcount)
      {
        document.getElementById("removameter_count").innerHTML = count;
        document.getElementById("removameter_photo").src = imagesrc;
        document.getElementById("removameter_time").innerHTML = imagetime;

        // play sound
        soundManager.play('perri');

        // keep the last count number
        lastcount = count;
      }
    } 
    // else ignore any errors
  }
}

// url must contain a URL that returns an xml document with two child elements:
// action - the name of a callback javascript function
// result - the value of the xml response that will be returned (as an argument) to the callback javascript function
//          (value is an XML node object)
function loadXMLDoc(url) 
{
  // branch for native XMLHttpRequest object
  if (window.XMLHttpRequest) 
  {
    xml_http_req = new XMLHttpRequest();
    xml_http_req.onreadystatechange = removameter_update;
    xml_http_req.open("GET", url, true);
    xml_http_req.send(null);
  } 
  // branch for IE/Windows ActiveX version
  else if (window.ActiveXObject) 
  {
    xml_http_req = new ActiveXObject("Microsoft.XMLHTTP");
    if (xml_http_req) 
    {
      xml_http_req.onreadystatechange = removameter_update;
      xml_http_req.open("GET", url, true);
      xml_http_req.send();
    }
  }
}

