var image_resizer	= '';
var isLoading		= true;
var line_break		= '\n<br />\n';

var timeout;
var timeout_pending	= false;
var current_cam		= null;

function clear_timeouts() {
	clearTimeout(timeout);
}

/* INIT CAMS */
function initCams(){
	//get random cam
	var rnd = Math.floor(Math.random() * cams.length);
	
	//display the cam
	displayCam(rnd);
}

/* MASTER FUNCTION TO DISPLAY CAM */
function displayCam(which){
	//set the current cam
	current_cam = which;
	
	//clear the timeouts
	clear_timeouts();

	//display the loading
	display_loading();
	
	//call the cams function
	window[cams[which].display_function](which);
	
	//set the captions
	displayCaptions(which);
}

/* AXIS CAMS */
function displayAxisCam(which){
	//get the content to display
	document.getElementById('webcam_container').innerHTML = getAxisCam(cams[which].cam);
}

/* AXIS APPLETS */
function displayAxisApplet(which){	
	//get the content to display
	document.getElementById('webcam_container').innerHTML = getAxisApplet(cams[which].cam);	
}

/* FLASH CAMS */
function displayFlashCam(which) {
	//get the flash vars
	var arVars = (typeof(cams[which].flashvars) != 'undefined') ? cams[which].flashvars : Array();
	
	//load the flash
	write_flash(cams[which].cam, 300, 240, "webcam_container", null, null, null, "high", "transparent", null, null, null, null, true, arVars);
}

function displayImageCam(which){
	//load the image
	loadImageCam(cams[which].cam,which);
}

function displayCaptions(which){
	// display the current cam's title
	document.getElementById('webcam_title').innerHTML			= (cams[which].title != null)		? cams[which].title+line_break : '';
	// display the current cam's location
	document.getElementById('webcam_location').innerHTML		= (cams[which].address != null)		? cams[which].address+line_break : '';
	
	//set the url if it is available
	if (cams[which].url != null){
		document.getElementById('webcam_caption').innerHTML		= (cams[which].title != null)		? '<a href="'+cams[which].url+'" target="_blank">'+cams[which].title+'</a>'+line_break : '';
	} else {
		document.getElementById('webcam_caption').innerHTML		= (cams[which].title != null)		? cams[which].title+line_break : '';
	}
}

function getAxisCam(BaseURL){
	// DisplayWidth & DisplayHeight specifies the displayed width & height of the image.
	// You may change these numbers, the effect will be a stretched or a shrunk image
	var DisplayWidth = "352";
	var DisplayHeight = "240";
	
	// This is the path to the image generating file inside the camera itself
	var File = "axis-cgi/mjpg/video.cgi?resolution=704x480";
	
	// No changes required below this point
	var output = "";
	
	//if its ie do the object else do the image
	if ((navigator.appName == "Microsoft Internet Explorer") &&(navigator.platform != "MacPPC") && (navigator.platform != "Mac68k")){
		// If Internet Explorer under Windows then use ActiveX 
		output = '<OBJECT ID="Player" width='
		output += DisplayWidth;
		output += ' height=';
		output += DisplayHeight;
		output += ' CLASSID="CLSID:DE625294-70E6-45ED-B895-CFFA13AEB044" ';
		output += 'CODEBASE="';
		output += BaseURL;
		output += 'activex/AMC.cab#version=3,32,14,0">';
		output += '<PARAM NAME="MediaURL" VALUE="';
		output += BaseURL;
		output += File + '">';
		output += '<param name="MediaType" value="mjpeg-unicast">';
		output += '<param name="ShowStatusBar" value="0">';
		output += '<param name="ShowToolbar" value="0">';
		output += '<param name="AutoStart" value="1">';
		output += '<param name="StretchToFit" value="1">';
		output += '<BR><B>Axis Media Control</B><BR>';
		output += 'The AXIS Media Control, which enables you ';
		output += 'to view live image streams in Microsoft Internet';
		output += ' Explorer, could not be registered on your computer.';
		output += '<BR></OBJECT>';
	} else {
		// If not IE for Windows use the browser itself to display
		theDate = new Date();
		output = '<IMG SRC="';
		output += BaseURL;
		output += File;
		output += '&dummy=' + theDate.getTime().toString(10);
		output += '" HEIGHT="';
		output += DisplayHeight;
		output += '" WIDTH="';
		output += DisplayWidth;
		output += '" ALT="Camera Image">';
	}
	
	//return the output
	return output;
}

function getAxisApplet(BaseURL){
	// DisplayWidth & DisplayHeight specifies the displayed width & height of the image.
	// You may change these numbers, the effect will be a stretched or a shrunk image
	var DisplayWidth = "352";
	var DisplayHeight = "240";

	// This is the path to the image generating file inside the camera itself
	var File = "axis-cgi/mjpg/video.cgi?resolution=CIF&compression=10";

	// No changes required below this point
	var output = "";
	var AppletDir = BaseURL + "java/ama";
	var VideoPath = "axis-cgi/mjpg/video.cgi?resolution=";
	var restOfPath = "&compression=10";
	var VideoURL = BaseURL + VideoPath;

	//setup output
	output  = '<APPLET archive="ama.jar" codeBase="';
	output += AppletDir + '"';
	output += ' code="ama.MediaApplet" height=';
	output += DisplayHeight;
	output += ' width=';
	output += DisplayWidth;
	output += '>';
	output += '<PARAM NAME="code" VALUE="ama.MediaApplet">';
	output += '<PARAM NAME="archive" VALUE="ama.jar">';
	output += '<PARAM NAME="codebase" VALUE="';
	output += AppletDir + '">';
	output += '<PARAM NAME="ama_cgi-path" VALUE="axis-cgi">';
	output += '<PARAM NAME="cache_archive" VALUE="ama.jar, ptz.jar">';
	output += '<PARAM NAME="cache_version" VALUE="1.0.0.0, 1.2.1.0">';
	output += '<PARAM NAME="ama_plugins" VALUE="ptz.PTZ">';
	output += '<PARAM NAME="type" VALUE="application/x-java-applet;version=1.4">';
	output += '<PARAM NAME="ama_url" VALUE="';
	output += VideoURL;
	output += DisplayWidth + 'x' + DisplayHeight;
	output += restOfPath +'">';
	output += '</APPLET>'

	//return the output
	return output;
}

function loadImageCam(url,which){
	// load the specified image and when it's done loading, display it
	var image	= new Image();
	
	//when image is loaded
	image.onload = function() {
		if(which == current_cam){
			//add the image container
			if(!document.getElementById('webcam_image')){
				document.getElementById('webcam_container').innerHTML	= '<img src="images/image.gif" width="300" height="240" alt="Webcam" id="webcam_image" />';
			}
			
			//put the image as the source
			document.getElementById('webcam_image').src	= image.src;
		
			//set the timeout
			timeout = setTimeout('loadImageCam("'+url+'",'+which+', false)', 500);
		}
	}
	
	//set the source of the image
	image.src	= image_resizer+url+'?'+Math.random();
}

function display_loading() {
	// display the loading swf's div container
	write_flash('flash/loading_webcam.swf', 300, 240, 'webcam_container');
	isLoading = true;
}

/*
function display_captions(title, caption, location, url) {
	// display the current cam's title
	document.getElementById('webcam_title').innerHTML			= (title != null)			? title+line_break : '';
	// display the current cam's location
	document.getElementById('webcam_location').innerHTML		= (location != null)		? location+line_break : '';
	if (url != null)
		document.getElementById('webcam_caption').innerHTML		= (caption != null)			? '<a href="'+url+'" target="_blank">'+caption+'</a>'+line_break : '';
	else
		document.getElementById('webcam_caption').innerHTML		= (caption != null)			? caption+line_break : '';
}

function display_image(image) {
	// display the image
	document.getElementById('webcam_container').innerHTML	= '<img src="images/image.gif" width="300" height="240" alt="Webcam" id="webcam_image" />';
	document.getElementById('webcam_image').src				= image.src;
}

function make2D(rows, cols) {
	// loop thru the number of rows provided and create a new array
	// for each with the number of columns set, thus creating
	// a 2d array =]
	var array	= new Array(rows);
	
	for (i=0; i<array.length; ++i) {
		array[i] = new Array(cols);
	}
	
	return array;
}*/
