// Copyright (c) 2001 Industrial Video & Control Co. LLC.  All rights reserved. 
//======================================================================================================================
//        Predefined Scripts Necessary to use the system  - Javascript Modifications should not be necessary be beow this line.
//========================================================================================================================


tempImage = new Image();
tempImage2 = new Image();
tempImage2.src = "/IVC/images/errors/temporarilyDown.jpg";
updateCount = 0;

presetnumber = 1;
panpresetnumber = 1;

// Make sure this is the top page
//if (top.location == self.location) {top.location.href="index.htm"}

// Other variables - Do not modify.


panImageName = new Array(numberOfPanoramas);
for (i=1; i<= numberOfPanoramas; i++) {
	panImageName[i] = parent.baseurl + "/IVC/images/panorama/panimage" + channel + "-" + i +".jpg";	
}


extension = new Date(); extension = extension.getTime();	
count = framesPerStream;

function initPageTimer() {
	time=setTimeout("initPageScript();",2000);	
}

function getCookie(NameOfCookie)
{
    if (document.cookie.length > 0) {              
       begin = document.cookie.indexOf(NameOfCookie+"=");       
       if (begin != -1) {           
         begin += NameOfCookie.length+1;       
         end = document.cookie.indexOf(";", begin);
         if (end == -1) end = document.cookie.length;
         return unescape(document.cookie.substring(begin, end));
         } 
       }
    return null;
}

function setCookie(NameOfCookie, value) 
{
  document.cookie = NameOfCookie + "=" + escape(value) + "; path=/";
}

function initPage() {

	if (getCookie("CamNum") != null) {
		parent.cameraNumber = getCookie("CamNum")
	} else {
		//channel = 1;
		channel = parent.cameraNumber
	}

	if (useChannelDropDownBox == 1) {
		//document.selectionForm.cameraSelectionList.selectedIndex= parent.cameraNumber - 1;
		channel = parent.cameraNumber;

		// JDM-1 use mapping
		document.selectionForm.cameraSelectionList.selectedIndex = ReverseSWITCHarray[channel] - 1;
	}

	// Look for the appropriate control type

	if (!controlType) {
		// Use default control routine
		movebaseurl=baseurl + "/control/" + channel
	} else 	{
		// Use specified control routine

		// MERGE
		movebaseurl=baseurl + "/" + parent.controlType + "/" + channel
		//movebaseurl=baseurl + "/" + controlType + "/" + channel

		if (returnType != "") parent.sidebar.location= movebaseurl + '/cmd=mng&return=' + returnType;	
	}


	baseImagePath = baseurl + "/video/" + channel
	queueRequestURL = "/queue1.htm"

	singleImagePath = baseImagePath + '/1/' + 'fps=' + maxFramesPerSecond;
	multiImagePath  = baseImagePath + '/' + framesPerStream + '/fps=' + maxFramesPerSecond;



	alertflag = 0;
	window.status = "Live streaming video...";   // Sets status bar in browser


	// Make sure Variables are defined
	script_doNotUseVideo = "" + parent.main.doNotUseVideo;


	if (script_doNotUseVideo == "undefined") {
		time=setTimeout("setPageProperties()",1000);
		firstImage();
	} 


//	if (updateImageAfterMove == 1) {
//
//		// MERGE
//		if (channel == 0) return;
//
//		document.mainimage.onload = "";
//		document.mainimage.src =  baseImagePath + '/1/';
//	}

}

function setPageProperties() {

	count = framesPerStream;
	scriptName="";
		
	if (queuingEnabled == 1) {
		parent.queueframe.location = queueRequestURL;
	}

//	// Load Presets into Pull Down Menu if '1'
//	if (usePresetDropDownBox == 1 && useChannelDropDownBox == 1) {
//		scriptName = baseurl + '/info/' + parent.cameraNumber + '/presetlist&cameralist/' + extension;
//
//	} else if (usePresetDropDownBox == 1) {
//		scriptName = baseurl +  '/info/' + parent.cameraNumber + '/presetlist';
//
//	} else
 	if (useChannelDropDownBox == 1) {
		scriptName = baseurl + '/info/' + parent.cameraNumber + '/cameralist';
	}


	if (scriptName != "") {
		//text = "<SCRIPT LANGUAGE='JavaScript' SRC='" + scriptName + "'></SCRIPT>"
		//parent.infoframe.document.write("<HTML><head>" + text + "</head><body>getting-info</body></HTML>\n\n");
		parent.infoframe.location = scriptName;
	}


	// Rename Links
	for (Count = 0; Count < document.links.length; Count ++) {
		
		Ret = document.links[Count].href;

		if (Ret == 'http://127.0.0.1/point_click_link/') {
			pointClickLink = Count;
		}
		
		
		else if (Ret == 'http://127.0.0.1/first_imagemap_link' ) {
			firstImageMapLink = Count;
		} 
		
		else if (Ret == 'http://127.0.0.1/panorama_link_1') 
			document.links[Count].href = movebaseurl + "/quiet=1&cmd=pan&panpresetnumber=1&iheight=" + document.panimage1.height + "&iwidth=" + document.panimage1.width 		
		else if (Ret == 'http://127.0.0.1/panorama_link_2') 
			document.links[Count].href = movebaseurl + "/quiet=1&cmd=pan&panpresetnumber=2&iheight=" + document.panimage2.height + "&iwidth=" + document.panimage2.width 		
		else if (Ret == 'http://127.0.0.1/panorama_link_3') 
			document.links[Count].href = movebaseurl + "/quiet=1&cmd=pan&panpresetnumber=3&iheight=" + document.panimage3.height + "&iwidth=" + document.panimage3.width 		

	}               
			


	// Update the Point & Click Reference				
	document.links[pointClickLink].href=movebaseurl + "/quiet=1&cmd=point&iheight=" + document.mainimage.height + "&iwidth=" + document.mainimage.width

	
	updateControlPanelLinks();	
	  	
}


function enlargePopup() {
	remote = window.open("enlarge.htm", "IVCRemote", "top=1,width=600,height=460,resizable=yes,scrollbars=0");
	document.mainimage.src=document.mainimage.src;
	//remote.location.href = "enlarge.htm";
}



function goToPreset( ){
	index = document.selectionForm.presetSelectionList.selectedIndex;
	presetnumber = document.selectionForm.presetSelectionList.options[index].value;
	movedata=movebaseurl + "/quiet=1&cmd=dir&dir=p&presetnumber=" + presetnumber;
	parent.sidebar.location=movedata; 
	
	document.selectionForm.presetSelectionList.selectedIndex=0;
	document.selectionForm.presetSelectionList.options[0].text='Going to ' + document.selectionForm.presetSelectionList.options[index].text;
	id = setTimeout("displayPresetText()",2000);
}



function setOption(){
	index = document.selectionForm.optionSelectionList.selectedIndex;
	selectionnumber = document.selectionForm.optionSelectionList.options[index].value;
	
	if (selectionnumber == 1) {
//		enlargePopup();
		alert("Enlarge - This pops up a large screen image.");
	}
	if (selectionnumber == 2) {
//		if (updateImageAfterMove == 0) {
//			updateImageAfterMove = 1;
//			update();
//		} else {
//			updateImageAfterMove = 0;
//			initPage();
//		}
		alert("Toggle - This switches to on-demand single images.");

	}
	if (selectionnumber == 3) {
		alert("View Manager - This accesses the system configuration pages.");		
	}
	if (selectionnumber == 4) {
		alert("Four Cam View - This displays four streams of video.");
	}
	if (selectionnumber == 5) {
		alert("View Video - This plays previously saved videos and/or snapshots.");
//		parent.main.location = baseurl + "/IVC/EVM/ARCH-index.htm";
	}

/*
	if (selectionnumber == 6) {
		updatePan(3);
	}

	if (selectionnumber == 7) {
		// Normal Light
		parent.sidebar.location = movebaseurl  + "/cmd=video&function=VISCA-8101043900FF";
		document.mainimage.src = document.mainimage.src;
	}

	if (selectionnumber == 8) {
		// Low Light
		parent.sidebar.location = movebaseurl  + "/cmd=video&function=VISCA-8101043903FF";
		document.mainimage.src = document.mainimage.src;

		setTimeout("LowLight();" ,2000);
	}

	if (selectionnumber == 9) {
		// Dark
		parent.sidebar.location = movebaseurl  + "/cmd=video&function=VISCA-8101043903FF";
		document.mainimage.src = document.mainimage.src;

		setTimeout("DarkLight();" ,2000);	
	}

	if (selectionnumber == 10) {
		maxFramesPerSecond = 0;
		singleImagePath = baseImagePath + '/1/' + 'fps=' + maxFramesPerSecond;
		multiImagePath  = baseImagePath + '/' + framesPerStream + '/fps=' + maxFramesPerSecond;
		firstImage();
	}


	if (selectionnumber == 11) {
		maxFramesPerSecond = 1;
		singleImagePath = baseImagePath + '/1/' + 'fps=' + maxFramesPerSecond;
		multiImagePath  = baseImagePath + '/' + framesPerStream + '/fps=' + maxFramesPerSecond;
		firstImage();
	}


	if (selectionnumber == 12) {
		maxFramesPerSecond = 2;
		singleImagePath = baseImagePath + '/1/' + 'fps=' + maxFramesPerSecond;
		multiImagePath  = baseImagePath + '/' + framesPerStream + '/fps=' + maxFramesPerSecond;
		firstImage();
	}

	if (selectionnumber == 13) {
		maxFramesPerSecond = 3;
		singleImagePath = baseImagePath + '/1/' + 'fps=' + maxFramesPerSecond;
		multiImagePath  = baseImagePath + '/' + framesPerStream + '/fps=' + maxFramesPerSecond;
		firstImage();
	}


	if (selectionnumber == 14) {
		maxFramesPerSecond = 4;
		singleImagePath = baseImagePath + '/1/' + 'fps=' + maxFramesPerSecond;
		multiImagePath  = baseImagePath + '/' + framesPerStream + '/fps=' + maxFramesPerSecond;
		firstImage();
	}

	if (selectionnumber == 15) {
		maxFramesPerSecond = 5;
		singleImagePath = baseImagePath + '/1/' + 'fps=' + maxFramesPerSecond;
		multiImagePath  = baseImagePath + '/' + framesPerStream + '/fps=' + maxFramesPerSecond;
		firstImage();
	}
*/


	document.selectionForm.optionSelectionList.selectedIndex=0;

}

function updatePan(preset) {
	
 	managerbaseurl = movebaseurl + "/cmd=pan&function=update&return=refresh&panpresetnumber="  + preset ;
	parent.sidebar.location=managerbaseurl;
	document.mainimage.src = document.mainimage.src;
}


function displayPresetText(text){
	text = "Preset Views";
	document.selectionForm.presetSelectionList.options[0].text=text
}


function savePicture(){
	alert("This function will save a picture to disk.");
	document.mainimage.src = document.mainimage.src;
}



function saveVideo(){
	alert("This function will save video to disk for future playback.");
	document.mainimage.src = document.mainimage.src;
}       




// The update function is called by movecam after each movecam call is made and completed

function update(){
	updateCount ++;
	extension = new Date(); extension = extension.getTime();
	if (updateImageAfterMove == 1) {
		regulateBandwidth = 0;
		document.mainimage.onload = "";
		document.mainimage.src = baseImagePath + '/1/' + extension + updateCount;
		document.mainimage.src = document.mainimage.src;
	} else {
		document.mainimage.src = document.mainimage.src;
	}

}


function switchChannel(){

	index = document.selectionForm.cameraSelectionList.selectedIndex;
	selectionnumber = document.selectionForm.cameraSelectionList.options[index].value;

	// JDM-1
	selectionnumber = SWITCHarray[selectionnumber];
	
	if (selectionnumber > 0) {	
		parent.cameraNumber = selectionnumber;	
		document.location = document.location;
		setCookie("CamNum", selectionnumber);
	} else {
		channel = getCookie("CamNum");
		if (channel == null) { channel = 1; }
		document.selectionForm.cameraSelectionList.selectedIndex = ReverseSWITCHarray[channel];
	}
}

function displayPanoramas() {
	document.write('<div align="center"><table border="0" cellpadding="2" cellspacing="0">');
	for (i = 1; i <= numberOfPanoramas; i++) {
		document.write('<tr><td align="center"><a href="http://127.0.0.1/panorama_link_' + i +  '" target="sidebar">');
		document.write('<IMG SRC="' + panImageName[i] + '?' + extension + '" align="center" valign="top" name="panimage' + i + '" ISMAP border="0"></a></td></tr>');
	}
	document.write('</table></div>');
}

function displayMiniPanorama() {
	document.write('<div align="center"><table border="0" cellpadding="2" cellspacing="0">');
	for (i = 1; i <= 1; i++) {
		document.write('<tr><td align="center"><a href="http://127.0.0.1/panorama_link_' + i +  '" target="sidebar">');
		document.write('<IMG SRC="' + panImageName[i] + '?' + extension + '" align="center" valign="top" width="280" name="panimage' + i + '" ISMAP border="0"></a></td></tr>');
	}
	document.write('</table></div>');
}


// ================================================================================
//        Video Functions - Client Pull
// ================================================================================

function firstImage() {

	if (navigator.appName == 'Netscape' && regulateBandwidth != 1) {
		document.mainimage.onload = "";
		document.mainimage.src = multiImagePath + "/" + extension;
	} else {
	
	extension = new Date(); extension = extension.getTime();	
	document.mainimage.onload = pullTheImage;
	document.mainimage.src = singleImagePath; 
	}
}

function pullTheImage() {
	
	if (maxFramesPerSecond != 0) {
		delay = Math.round((1 / maxFramesPerSecond) * 1000);
		//time=setTimeout("pullTheImageScript();",delay);		
		pullTheImageScript();
	} else {
		pullTheImageScript();
	}
}

function pullTheImageScript(){

   count--;
	//document.countform.count.value = count;


   extension = new Date(); extension = extension.getTime(); extension = extension + count;
	document.mainimage.src = singleImagePath + "/" + extension;



	if (count > 0) {
		//window.status = "Frames left in stream --> " + count;
		window.status = "Receiving Live Video...";
	} else {
		window.status = "Receiving unlimited frames...";
	}
	
	if (count < 0 && framesPerStream > 0) {
		window.status = "Video stream finished - Refresh screen to restart video";
		document.mainimage.onload = '';
		if (alertflag == 0) {
			alert("Video stream finished - Refresh screen to restart video");
			alertflag = 1;
		}
	}
}
