
var errorCount = 0;
var frameCount = 0;
var lastImageRefresh = new Date();


function SetupPic()
{
    // check the image for errors
    //if (document.campic.width < 100) {
    //    errorCount++;
    //    if (errorCount > 10) {
    //        // show error image and reset frame counter
    //        //document.campic.src = "http://www.yoics.com/images/gadget/camera-offline.png";
    //        frameCount = 0;
    //        errorCount = 0;
    //    }
    //    else {
    //        StartUpdateTimer();
    //    }
    //}
    //else {
        errorCount = 0;
        frameCount++;
        StartUpdateTimer();
        HideLoadingDiv();
        ChangeImageDiv('show');
    //}
}

function HideLoadingDiv(obj)
{
    // hide the loading div if being used
    if (typeof(loadingDiv) != 'undefined')
    {
       obj = document.getElementById('loadingDiv');
       obj.style.visibility = 'hidden';
       obj.style.display = 'none';
    }
}

function ChangeImageDiv(dir)
{
    // hide the loading div if being used
    if (dir == 'show')
    {
       obj = document.getElementById('ImageDiv');
       obj.style.visibility = 'visible';
       obj.style.display = 'block';
    }
    else 
    {
       obj = document.getElementById('ImageDiv');
       obj.style.visibility = 'hidden';
       obj.style.display = 'none';
    }
}

function ErrorPic()
{
    errorCount = errorCount + 1;

    // check for continuous errors
    if (errorCount > 20 && frameCount > 0)
    {
        // show error image and reset frame counter
        document.campic.src = "http://www.yoics.com/images/gadget/camera-offline.png";
        frameCount = 0;
    }
    else {
	    StartUpdateTimer();
	}
}

function StartUpdateTimer()
{
    // The timeout set here moderates framerate.
    if (typeof(updateRate) == 'undefined')
    {
        setTimeout("NextPic()", 500);
    }
    else
    {
        if (updateRate < 250) {
            updateRate = 1000;
        }
        setTimeout("NextPic()", updateRate);
    }
}

function NextPic()
{
	// setup onload to start the interval timer so we don't over pump the image
	if (typeof(camurl) != 'undefined' && camurl.length > 0)
	{
	   document.campic.onload=SetupPic;
	   document.campic.onerror=SetupPic;
	   document.campic.src = camurl + "?r=" + Math.random();
	   document.campic.width = imageWidth;

       //lastImageRefresh = new Date();
	}
}

function CycleImageSize(e)
{
	if (typeof(useControls) != 'undefined' && useControls == 'true')
	{
      if(e.shiftKey) {
  	    if (imageWidth == 120) {
	       imageWidth = 160;
	    }
	    else if (imageWidth == 1600) {
	       imageWidth = 240;
	    }
	    else if (imageWidth == 240) {
	       imageWidth = 320;
	    }
	    else if (imageWidth == 320) {
	       imageWidth = 480;
	    }
	    else if (imageWidth == 480) {
	       imageWidth = 640;
	    }
	    else {
	       imageWidth = 120;
	    }
      }
      else {
  	    if (imageWidth == 640) {
	       imageWidth = 480;
	    }
	    else if (imageWidth == 480) {
	       imageWidth = 320;
	    }
	    else if (imageWidth == 320) {
	       imageWidth = 240;
	    }
	    else if (imageWidth == 240) {
	       imageWidth = 160;
	    }
	    else if (imageWidth == 160) {
	       imageWidth = 120;
	    }
	    else {
	       imageWidth = 640;
	    }
	 }
  }
  else if (typeof(useControls) != 'undefined' && useControls == 'false')
  {
      openNewWindow();
  }
  
  //alert("frame=" + frameCount + " error=" + errorCount + " width=" + document.campic.width + " size=" + document.campic.size + " last=" + lastImageRefresh);
}

function openNewWindow() {
 popupWin = window.open(proxyurl,
 'open_proxy',
 'menubar, toolbar, location, directories, status, scrollbars, resizable, dependent')
}

function calcTimeDifferene(date1, date2) {
	if (isNaN(sec))
	{
		return -1;
	}
	if (sec < 0)
	{
		return -1;
	}

	var second = 1000, minute = 60 * second, hour = 60 * minute, day = 24 * hour;

	var days = Math.floor(sec / day);
	sec -= days * day;
	var hours = Math.floor(sec / hour);
	sec -= hours * hour;
	var minutes = Math.floor(sec / minute);
	sec -= minutes * minute;
	var seconds = Math.floor(sec / second);
	
    return seconds;
}