/**
 For Static Image Refresh
*/
$().ready(function() {
	if(document.getElementById("cam_pic") != null) {
	        imagerefresher();
	}
});

function imagerefresher(){
                var d = new Date();
                var timerobj;
                var cam_wait = 15000;
                var imagesrc = document.getElementById("cam_pic").src;
                document.images.cam_pic.src = imagesrc + "?" + Math.random() + d.getTime();
                timerobj = setTimeout("imagerefresher()",cam_wait);
}

/**
 For Video Webcam
 Copy from the AXIS webcam javascript library
*/
function video(imagepath,host,still_pic,alttext,vid)
{
  var resolution = 0
  var width = 0
  var height = 0
  
  if (imagepath.indexOf("resolution=") != -1) {
    var resStart = imagepath.indexOf("resolution=")
    var resStop = imagepath.indexOf("&", resStart)
    if (resStop == -1) resStop = imagepath.length
    resolution = imagepath.substring(resStart + 11, resStop);
    if (resolution.indexOf('x') != -1) {
      width = parseInt(resolution.substring(0, resolution.indexOf('x')));
      height = parseInt(resolution.slice((resolution.indexOf('x') + 1)));
    }
  }

  if (!(width > 0 && height > 0)) {
      width = parseInt("320");
      height = parseInt("240");
  } else {
      width = width * parseInt("1");
      height = height * parseInt("1");
  }
  resolution = width + 'x' + height;

  var width_height = 'width="' + width + '" height="' + height + '"';
  var use_java = 0;
  var use_flash = 0;
  var use_still = 0;
  var use_mjpeg = 0;
  var viewer = "still";
  var use_quicktime = 0;

  if ((navigator.appName == "Microsoft Internet Explorer") && (navigator.platform != "MacPPC") && (navigator.platform != "Mac68k")) {
    viewer = "flash";
  } else {
    viewer = "mjpeg";
  }

  if (viewer.indexOf("flash") != -1) {
    use_flash = 1;
  }
//  if (viewer.indexOf("quicktime") != -1) {
//    use_quicktime = 1;
//  }
  if (viewer.indexOf("mjpeg") != -1) {
    use_mjpeg = 1;
  }
  if (viewer.indexOf("still") != -1) {
    use_still = 1;
  }

// Older Safari
  var agent = navigator.userAgent.toLowerCase();
  if (agent.indexOf("applewebkit/") != -1) {
    var pos = agent.indexOf("applewebkit/") + 12
    var webKitVersion = parseInt(agent.substring(pos, agent.indexOf(" ", pos)))
    if ((webKitVersion < 416)) {
      use_java = 1;
      use_flash = 0;
      use_still = 0;
    }
  }

// Catch all
  if ((viewer.indexOf("still") != -1) || !(use_flash || use_java || use_quicktime|| use_mjpeg)) {
      use_still = 1;
  }

/*
use_flash = 0;
use_mjpeg =  0;
use_java =  0;
use_quicktime = 0;
use_still = 1;
alert(viewer);
*/

  if (use_flash) {
    var view_NeedFlashPluginTxt = "You need a Shockwave Flash plugin, get it from:"
    document.write('<EMBED src="http://'+ host + '/axis-cgi/mjpg/video.swf?resolution=' + resolution +'&camera=1&vcamid='+ vid +'" ' +
    'quality=low bgcolor=#000000 ' + width_height +
    ' TYPE="application/x-shockwave-flash" swLiveConnect=TRUE' +
    ' PLUGINSPAGE="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash">' +
    '</EMBED>' +
    '<NOEMBED>' + view_NeedFlashPluginTxt +
    '<a href="http://www.macromedia.com/shockwave/download/">Macromedia</a>.' +
    '</NOEMBED><br>');
  }

  if (use_java) {
    var playerWidth = width;
    var playerHeight = height + 105;
    if (playerWidth < 300)
      playerWidth = 300;

    if (imagepath.indexOf("http") != -1) {
      var addrEnd = imagepath.indexOf("/", 8);
      var addr = "http://" + host + "/" + imagepath.substring(0, addrEnd);
    } else {
      var addr = "http://"+ host +"/";
    }

    // get the rotation from parhand
    var rotation = 180;

    document.write('<APPLET archive="ama.jar" codeBase="http://' + host + '/java/ama" code="ama.MediaApplet" width="' + playerWidth + '" height="' + playerHeight + '">');
    document.write('<PARAM NAME="code" VALUE="ama.MediaApplet">');
    document.write('<PARAM NAME="archive" VALUE="ama.jar">');
    document.write('<PARAM NAME="codebase" VALUE="http://' + host + '/java/ama">');
    document.write('<PARAM NAME="ama_cgi-path" VALUE="axis-cgi">');
    document.write('<PARAM NAME="cache_archive" VALUE="ama.jar">');
    document.write('<PARAM NAME="cache_version" VALUE="1.1.0.0">');
    document.write('<PARAM NAME="ama_plugins" VALUE="">');
    document.write('<PARAM NAME="type" VALUE="application/x-java-applet;version=1.4">');
    document.write('<PARAM NAME="ama_url" VALUE="' + addr +
	    '/axis-cgi/mjpg/video.cgi?camera=1&resolution=' + width + 'x' + height + '&rotation=' + rotation + '">');
    document.write("Your browser does not support Java")
    document.write("</APPLET><br>");
  }


  if(use_mjpeg) {
	  document.write('<img height="240" width="320" src="http://' + host + '/mjpg/video.mjpg?resolution=' + resolution +'" alt="'+alttext + '" name="cam_pic_video" id="cam_pic_video"/>');
	  document.write("<br />");
  }

  if (use_still) {
	picturepath = "/cam-images/"+still_pic;
        document.write('<img id="cam_pic" name="cam_pic" alt="'+alttext + '"  src="' + picturepath+ '" />');
	document.write('<p class="tc">This page refreshes every 15 seconds.</p>');
	imagerefresher();
  }
}

