//
// jamescam.js version 1.0.1
//
// JavaScript webcam script. It shows the reload seconds, can be
// paused, disables after a timeout period, and can manage more than
// one cam image in a single webpage.
//
// Copyright (c) 2005 James Reynolds
// All Rights Reserved.
//
// Permission to use, copy, modify, and distribute this software and 
// its documentation for any purpose and without fee is hereby granted,
// provided that the above copyright notice appears in all copies and
// that both that copyright notice and this permission notice appear
// in supporting documentation, and that the name James Reynolds not be
// used in advertising or publicity pertaining to distribution of the
// software without specific, written prior permission. This software
// is supplied as is without expressed or implied warranties of any
// kind.
//
// In other words, feel free to use or copy at will, but I wrote it
// first and you have to give me credit.  And I must say it is pretty
// cool too.
//
// Instructions.  For each webcam, put this text in your webpage where
// you want it to appear (and in a script tag, naturally).
//
//  webCam(image_url, width, height, unique_name, refreshtime, text_class, disable_after, paused, disabled_time){
//
// Replace "image_url" with the url of the image to load, the "width"
// with the image width, and "height" w/ its height.
//
// Replace "unique_name" with a unique name for the webcam. If you
// multiple webcams and they have the same unique name, I doubt
// this script will work right.  Unique name is used for the name
// of the image, the id of several hidden fields, an anchor link,
// and the image alt.
//
// Replace "refreshtime" with the image refresh seconds.
//
// Replace "text_class" with the css class of the webcam text.
//
// Replace "disable_after" with the total seconds for reloads. If you
// don't want this, specify -1.
//
// Replace paused with the default paused state.  This is actually if
// You want an external script to set the state of the cam.  For
// example, you can include a second javascript file that sets the
// paused variable.  If this is set to 1, then the message states
// that the webcam is disabled because the image hasn't been updated
// since "disabled_time".  For an example of this, see the jamescam
// at http://james.magnusviri.com/webcam.
//
// v 1.0.1 - put <p></p> around text

function webCam(image_url, width, height, unique_name, refreshtime, text_class, disable_after, paused, disabled_time){
  document.write('<a name="'+unique_name+'"></a>');
  document.write('<img src="'+image_url+'" name="'+unique_name+'" alt="'+unique_name+'" height="'+height+'" width="'+width+'">')
  document.write('<form>');
  document.write('<p><span class="'+text_class+'" id="'+unique_name+'_timer_text"></span></p>');
  document.write('<input type="hidden" readonly id="'+unique_name+'_timer_value" value="'+refreshtime+'">');
  document.write('<input type="hidden" readonly id="'+unique_name+'_disable_value" value="'+disable_after+'">');
  document.write('<input type="hidden" readonly id="'+unique_name+'_paused" value="'+paused+'">');
  document.write('</form>');

  if ( paused == 1) {
    var message = 'Reloads disabled because image has not changed since '+disabled_time+'. ';
    message += '<a href="#'+unique_name+'" onclick="restart(\''+image_url+'\', \''+unique_name+'\', \''+refreshtime+'\', \''+disable_after+'\');">Reload anyway</a>.';
    document.getElementById(unique_name+'_timer_text').innerHTML = message;
  } else {
    countDown(image_url, unique_name, refreshtime, disable_after);
  }
}

function refreshCam(image_url, unique_name){
  timey = new Date() ; timey = "?" + timey.getTime()
  document.images[unique_name].src = image_url + timey;
}

function unpause(image_url, unique_name, refreshtime, disable_after) {
  document.getElementById(unique_name+'_paused').value = 0;
  countDown(image_url, unique_name, refreshtime, disable_after);
}

function restart(image_url, unique_name, refreshtime, disable_after) {
  document.getElementById(unique_name+'_disable_value').value = disable_after;
  document.getElementById(unique_name+'_paused').value = 0;
  countDown(image_url, unique_name, refreshtime, disable_after);
}

function countDown(image_url, unique_name, refreshtime, disable_after) {
  var current_disable = document.getElementById(unique_name+'_disable_value').value;
  var current_timer = document.getElementById(unique_name+'_timer_value').value;    
  var current_paused = document.getElementById(unique_name+'_paused').value;

  if ( current_disable == 0 ) {

    var message = 'To save bandwidth, reloads have stopped. ';
    message += '<a href="#'+unique_name+'" onclick="restart(\''+image_url+'\', \''+unique_name+'\', \''+refreshtime+'\', \''+disable_after+'\');">Restart</a>';
    document.getElementById(unique_name+'_timer_text').innerHTML = message;

  } else if ( current_paused == 1 ) {

    var message = "<font color='grey'>Will reload in "+(current_timer)+" seconds."
    if ( current_disable >= 0 ) { message += " Disables in "+current_disable+" seconds."; }
    message += "</font>";
    message += ' <a href="#'+unique_name+'" onclick="unpause(\''+image_url+'\', \''+unique_name+'\', \''+refreshtime+'\', \''+disable_after+'\');">Unpause</a>';
    document.getElementById(unique_name+'_timer_text').innerHTML = message;

  } else {

    if ( current_timer < 1 ) {

      refreshCam(image_url, unique_name);

      var message = "Will reload in "+refreshtime+" seconds.";
      if ( current_disable >= 0 ) { message += " Disables in "+current_disable+" seconds."; }
      message += ' <a href="#'+unique_name+'" onclick="document.getElementById(\''+unique_name+'_paused\').value = 1;">Pause</a>';
      document.getElementById(unique_name+'_timer_text').innerHTML = message;

      document.getElementById(unique_name+'_timer_value').value = refreshtime-1;
      if ( current_disable >= 0 ) { document.getElementById(unique_name+'_disable_value').value = current_disable-1; }

    } else {

      var message = "Will reload in "+current_timer+" seconds.";
      if ( current_disable >= 0 ) { message += " Disables in "+current_disable+" seconds."; }
      message += ' <a href="#'+unique_name+'" onclick="document.getElementById(\''+unique_name+'_paused\').value = 1;">Pause</a>';
      document.getElementById(unique_name+'_timer_text').innerHTML = message;

      document.getElementById(unique_name+'_timer_value').value = current_timer-1;
      if ( current_disable >= 0 ) { document.getElementById(unique_name+'_disable_value').value = current_disable-1; }

    }

    setTimeout('countDown("' + image_url + '", "' + unique_name + '", "' + refreshtime + '", "' + disable_after + '")', 1000);

  }
}

