var Timeout = 500;
var loading=1;
var expired=0;
var qualityi=0;
var quality=0;

newPic = new Image();

var PicLoaded;

//Get the user specified view mode
if (forceviewmode == -1) {
  viewmode = getCookie("viewmode") || 2;
} else {
  viewmode = forceviewmode;
}
//Disable Java modes if they aren't supported
if (!(navigator.javaEnabled())) { viewmode = 1; }
setCookie("viewmode", viewmode);
if (viewmode == 2) { viewmode = 0; }
if (forcequality == -1) {
  qualityi = parseInt(getCookie("quality") || 3) - 1;
} else {
  qualityi = forcequality;
}

var PageInited = 0;
var AppletInited = 0;
if (viewmode == 1) { AppletInited = 1; }

function PageInit() {
  PageInited = 1;
  if ((PageInited == 1) & (AppletInited == 1)) { Init(); }
}

function notifyAppletInit() {
  AppletInited = 1;
  if ((PageInited == 1) & (AppletInited == 1)) { Init(); }
}

function Init() {
  document.controls.reset();
//  zoom(1);
  document.controls.viewmodeopt.selectedIndex=viewmode;
  setquality(qualityi);
  document.controls.quallist.selectedIndex = qualityi;
  loading=0;
  if (viewmode == 0) document.camimage.CamStart();

  if (ptzenabled == 1) {
    document.getElementById('ptzscrolls').style.display='';
    document.getElementById('ptzcontrols').style.display='';
  } else {
    document.getElementById('ptzscrolls').style.display='none';
    document.getElementById('ptzcontrols').style.display='none';
  }

  //Stop requesting live image after duration
  if (duration != 0) { window.setTimeout("expireSession()", (duration + 5) * 1000); }
}

function SetStatus() {
 PicLoaded=1;
}

function loadNewPic()
{
    var u=0;
    uniq = new Date();
    uniq = uniq.getTime();
    if (viewmode == 0) return;

    if (PicLoaded==1) {
     document.images.camimage.src = newPic.src;
     newPic.src = MakeURL();
     PicLoaded=0;
     newPic.onload=SetStatus;
     newPic.onerror=SetStatus;
     u=1;
    }
    if (u==1) {
     window.status="Live video... " + uniq;
    }
    if (expired == 0) {
      window.setTimeout("loadNewPic()", Timeout);
    }
}

function firstPic()
{
   if (viewmode == 0) return;
   newPic.onload = SetStatus;
   newPic.onerror= SetStatus;
   newPic.src = MakeURL();
   document.images.camimage.onload='';
   window.setTimeout("loadNewPic()", Timeout);
}

function expireSession() {
  expired=1;
  if (viewmode == 0) document.camimage.CamStop();
}

function MakeURL() {
var URL;

  uniq = new Date();
  uniq = uniq.getTime();

  URL = "/image.jpg?feed=";
  if (quality != 0) { URL = URL + "&quality=" + quality; }
  URL = URL + "&uniq=" + uniq;
  return URL;
}

function MakeMJPEGURL() {
var URL;

  URL = "video.mjpg?feed=";
  if (quality != 0) { URL = URL + "&quality=" + quality; }
  return URL;
}

function setquality(newquality) {
  setCookie("quality", newquality + 1);

  if (newquality == 0) { //Highest
    quality = 0;
  } else if (newquality == 1) { //High
    quality = 70;
  } else if (newquality == 2) { //Medium
    quality = 48;
  } else if (newquality == 3) { //Low
    quality = 20;
  } else { //Lowest
    quality = 8;
  }
  if (viewmode == 0) document.camimage.SetStreamSource(MakeMJPEGURL());
}

var zl=3;

function notifyResChange(width, height){
  owidth = width;
  oheight = height;
  document.camimage.width=width;
  document.camimage.height=height;

  document.images.panimage.width=width;
  document.images.panimage.src = "/ptz/gradiant.gif?orientation=0&size=" + width;
  document.images.tiltimage.height=height;
  document.images.tiltimage.src = "/ptz/gradiant.gif?orientation=1&size=" + height;
}

function zoom(zlevel) {
  if (viewmode != 1) {
	document.camimage.zoom(zlevel);
	return;
  }

  if ((zlevel == 2) && (zl > 1)) zl--; //Out
  if (zlevel == 1) { zl = 3; } //native
  if ((zlevel == 0) && (zl < 5)) zl++; //In

  w=owidth; h=oheight;

  if (zl == 1) { w = w / 4; h = h / 4; }
  if (zl == 2) { w = w / 2; h = h / 2; }
//  if (zl == 3) { w = w; h = h; }
  if (zl == 4) { w = w * 2; h = h * 2; }
  if (zl == 5) { w = w * 4; h = h * 4; }

  document.images.camimage.width=w;
  document.images.camimage.width=w;

  document.images.panimage.width=w;
  document.images.panimage.src = "/ptz/gradiant.gif?orientation=0&size=" + w
  document.images.tiltimage.height=h;
  document.images.tiltimage.src = "/ptz/gradiant.gif?orientation=1&size=" + h
}

function forcemode(mode) {
  if (mode == 0) { mode = 2; }
  setCookie("viewmode", mode);
  if (mode == 2) { mode = 0; }
  document.location="?mode=" + mode;
}

function panscrollclick(click) {
  if (navigator.appName=='Netscape') {
    rx = click.clientX - document.controls.panimage.x;
  } else {
    rx = click.offsetX;
  }

  mins = parseInt(rx * (360/(document.controls.panimage.width-1)));
  mins = (mins - 180);
  ptzmove('to&pan=' + mins);
}

function tiltscrollclick(click) {
  if (navigator.appName=='Netscape') {
    rx = click.clientY - document.controls.tiltimage.y;
  } else {
    rx = click.offsetY;
  }

  mins = parseInt(rx * (360/document.controls.tiltimage.height));
  mins = -(mins - 180);
  ptzmove('to&tilt=' + mins);
}

function ptzmove(location) {
  uniq = new Date();
  uniq = uniq.getTime();
  cmd = new Image();
  cmd.src = "/ptz/control.htm?move=" + location + "&uniq=" + uniq
}

function ptzzoom(action) {
  uniq = new Date();
  uniq = uniq.getTime();
  cmd = new Image();
  cmd.src = "/ptz/control.htm?zoom=" + action + "&uniq=" + uniq
}

function ptzangle(click) {
var angle = 0;

  if (click.ctrlKey) {
	angle = 1;
  } else if (click.shiftKey) {
    angle = 25;
  } else {
    angle = 5;
  }
  
  return "&angle=" + angle;
}