	// Test to see that the window is in the correct frameset 
//	if (window.name != "main") { top.location.replace("");}
	
	// Pop-up Windows
	var winname='viewer';
	var defwidth=480;
	var defheight=320;
	var winfeatures = 'scrollbars,resizable';
	function popup(url,win,size,height,features) {
        	if (win) { winname=win; }
        	if (features) { winfeatures=features; }
        	if (size) { defwidth=size; defheight=size; }
        	if (height) { defheight=height; }
        	winfeatures = "width="+defwidth+",height="+defheight+","+winfeatures;
        	if (winname) { newwin = window.open(url, winname, winfeatures); }
        	else { newwin = window.open(url, winfeatures); }
        	newwin.focus();
	}

	//	Production Script
	var isIE = (navigator.userAgent.indexOf( "MSIE" ) > 0 ) ? true : false;
	var imageSrc, 
		previousImage, 
		updatePeriod, 
		now, 
		nextDue, 
		timerPeriod, 
		remoteWin, 
		timerID,
		intervalID = null;	
	var imageLoaded = false;
	var imgbuffer = new Image();
	var downloadStart = new Date().getTime();
	var firstDownloadStart = downloadStart;
	var numberofDownloads = 20;
	var downloadExpire;
	var lastSuccess = downloadStart;	
	var isOwner = true;
	var clientVersion = '6.0';
	
	function getClientVersion() {
		return clientVersion;
	}

	function setIsOwner( ownerStatus ) {
		isOwner = ownerStatus;
	}
	
	function getIsOwner() {
		return isOwner;
	}
	
	function onBeforeLoad() {
		if(!getIsOwner()) {
			if( !isRemoteWin() && getIntop() ) {
				getIntop().displayWebCamStatus();
			}
		}
	}
	
	function getIntop() {
		return top.frames[0];
	}
	
	function camInit(period) {
		onBeforeLoad();
		updatePeriod = period*1000;
		downloadStart = new Date().getTime();
		firstDownloadStart = downloadStart;
		lastsuccess = downloadStart;
		downloadExpire = downloadStart+(numberofDownloads-1)*updatePeriod;
		imageSrc = document.refrimage.src;
		if( imageSrc.indexOf( "?" ) > 0 ) {
			imageSrc = imageSrc.substr( 0, imageSrc.indexOf( "?" ) );
		}
		imgbuffer.onload =  function () { imageLoad();  };
		imgbuffer.onerror = function () { imageError(); };
		startTimer();
		if( checkForClock() )
			startClockUpdates();
	}

	function launchRemote(w,h,htmlfile) {
		var klugedWidth = w + 150;
		var klugedHeight = h + 150;
		remoteWin=window.open(htmlfile,"","resizable=yes,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=auto,copyhistory=0,width=" + klugedWidth + ",height=" + klugedHeight + "");
		remoteWin.creator=self;
	}

	function startTimer() {
		if (timerID != null) {
			window.clearTimeout(timerID);
			timerID = null;
		}
		
		nextDue = lastSuccess + updatePeriod;
		now = new Date().getTime();
		timerPeriod = Math.max((nextDue-now),0);

		if (timerPeriod > 0) {
			if (now>downloadExpire) window.clearInterval(intervalID);
			else
				timerID = window.setTimeout("timerExpire()", timerPeriod);
		} else { 
			timerExpire();
		}
	}

	function timerExpire() {
		timerID = null;
		getImage();
	}

	function getImage() {
		imageLoaded = false;
		downloadStart = new Date().getTime();
		imgbuffer.src = imageSrc + "?" + downloadStart ;
	}

	function imageLoad() {
		document.refrimage.src = imgbuffer.src;
		imageLoaded = true;
		lastSuccess = downloadStart;
		startTimer();
	}

	function imageError() {
		window.setTimeout("getImage()", 120*1000);
	}
	
	function checkForClock(){
		if(this.document.clock && this.document.clock.time ){
			return true;
		}
		return false;
	}
	
	function startClockUpdates() {
		intervalID = window.setInterval("updateClock()", 100);
	}

	function updateClock() {
		now = new Date().getTime();
		var timeLeft = Math.ceil((nextDue - now)/1000);
		var value = (timeLeft > 0) ? timeLeft : "-";
		if( document.clock.time ) {
			if (value != document.clock.time.value) {	// update if changed
				document.clock.time.value = value;
			}
		}
	}
	
	function isRemoteWin() {
		var retval = false;
		if( getUrl() ) {
			if( getUrl().toLowerCase().indexOf( 'remote' ) >= 0 ) {
				retval = true;
			}
		}
		return retval;		
	}
	
	function getUrlStatus() {
		var retval = "";
		if( getUrl() != null ) {
			if( getUrl().toLowerCase().indexOf( '_on' ) < 0 && 
				getUrl().toLowerCase().indexOf( '_off' ) < 0 ) {
				return retval;
			} else {
				retval = ( getUrl().toLowerCase().indexOf( '_on' ) < 0 ) 
				? 'off' : 'on';
			}
		}
		return retval;
	}
	
	function getUrl() {
		return this.location.href;
	}
	
	function getBaseUrl() {
		var retval = null;
		if( getUrl() ) {
			var lastSlash = getUrl().toLowerCase().lastIndexOf( '/' );
			if( lastSlash < 0 ) {
				lastSlash = getUrl().toLowerCase().lastIndexOf( '\\' );
			}
			var prefix = getUrl().toLowerCase().substr( 0, lastSlash + 1 );
			retval = ( prefix!=null ) ? prefix : '/';
		}
		return retval;
	}
	

