/** The setTimeout fix for method calls is based on the explanation here: http://alexle.net/archives/169 **/
var globalScope = new Array();

function EtcCam(camTimer,camName,camImage) {
	this.camTimer = camTimer;
	this.camName = camName;
	this.camImage = camImage;
	this.timer = this.camTimer;
}	
EtcCam.prototype.updateImage = function() {
	
	/** This is used as a unique idenifier in the setTimeout method call fix below **/
	this.uniqueId =  this.camName;
	
	//console.log(this.timer);
	if (this.timer == 0) {

		if (window.XMLHttpRequest) { // Mozilla, Safari, ...
		    http_request = new XMLHttpRequest();
			if (http_request.overrideMimeType) {
                	http_request.overrideMimeType('text/xml');
			}
		} else if (window.ActiveXObject) { // IE
			try {
				http_request = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    http_request = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
            }
		}
		
        if (!http_request) {
            alert('Giving up :( Cannot create an XMLHTTP instance');
            return false;
        }
        /**
        * These are not required for the programatic post needed to regenerate the image.
        */
		//http_request = new XMLHttpRequest();
		//http_request.overrideMimeType('text/xml');
		//http_request.onreadystatechange = arbitraryFunction();
		http_request.open('POST', location.href, true);
		http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

		http_request.send('mode=update_img&camname='+this.camName);

		//alert(camTimer);
		var cam_img = document.getElementById('etc_cam_'+this.camName);
		var now_time = new Date();

		var cam_url = "/"+this.camImage;
		cam_url += "?dt="+now_time.getTime().toString(10);

		cam_img.src = cam_url;
		this.timer = this.camTimer;

	}
	document.getElementById(this.camName+'_reload_timer').innerHTML = this.timer;

	this.timer--;
	
	if( document.all ) {
	    /* A.3.1.1 - make a reference to the current object and saved in the global scope array
	 * to use later to correct the scope.
	     */
	globalScope[ this.uniqueId ] = this;
	setTimeout( 'ieIntervalHandler("' + this.uniqueId + '","updateImage")', 1000 );
	} else {
	  /* A.3.2 - Mozilla */
	  this.timerAction = setTimeout(function(EtcCam) { EtcCam.updateImage(); }, 1000, this);
	}
	
}

function ieIntervalHandler( id, strFunc ) {
	/* D.1 - correct the scope then make the call */
	var scope = globalScope[id];
	eval( "scope." + strFunc + "()" );
}