var currImg=0;
var ImgArray = new Array(dataArray.length/2);
var timerID=null;
var ip=null;
var mayStart=true;
var period=500;
var invPeriod=2;



/**
 *
 * get the current command (in the getUrl)
 *
 **/
function getCommand(){
	var jstring = window.location.search.split("=")
	var cmd="";
	if ((jstring.length==2) && (jstring[0]=="?cmd"))
	cmd=jstring[1]; else cmd="last4";
	return(cmd);
}
/**
 *
 * get the current html file name (in the getUrl)
 *
 **/
function getHtmlFile(){
	var url=window.location.href
	var htmlFile="";
	if (url.lastIndexOf("/")!=-1){
		htmlFile=url.substr(url.lastIndexOf("/")+1);
	}else{
		htmlFile=url;
	}
	if (htmlFile.indexOf("?")!=-1){
		htmlFile=htmlFile.substr(0,htmlFile.indexOf("?"));
	}
	return(htmlFile);
}
/**
 *
 * get a string containing an img tag with the requested img
 *
 **/
function getImageTag(i,width,height){
	return('<img id="animImage" width="' + width + '" height="' + height + '" src="' + dataArray[i] + '" alt="" border="0" />');
}
/**
 *
 * get a string containing 4 images contained in a table
 *
 **/
function getLast4Images(){
	var jstring;
	var k=(dataArray.length/2)-4;
	var i;
	
	jstring = '<table border="0" cellspacing="5">';
	for (i=0;i<2;i++){
		jstring += '<tr>';
		for (j=0;j<2;j++){
			jstring += '<td>';
			if (k > 0){
				jstring += '<a href="./'+getHtmlFile()+'?cmd=img' + (2*k) + '" target="_top">';
				jstring += getImageTag(2*k,256,256*785/920);
				jstring += '</a>';
			}
			else{
				jstring += '&nbsp;';
			}
			k++;
			jstring += '</td>';
		}
		jstring += '</tr>';
	}
	jstring += '</table>';
	return(jstring);
}
/**
 *
 * main imagewrite function
 *
 **/
function writeImage(){
	var jstring;
	var cmd=getCommand();
	
	if ((cmd=="")||(cmd=="anim")){
		jstring = getImageTag(0,920,785);
	}else if (cmd=="last4"){
		jstring = getLast4Images();
	}else if (cmd.substring(0,3)=="img"){
		jstring = getImageTag(cmd.substring(3),920,785);
	}
	document.writeln(jstring);
}
/**
 *
 * stop the animation
 *
 **/
function stopAnim(){
	if (timerID!=null){
		clearTimeout(timerID);
		timerID=null;
	}
	//	if (ip.aImages[i]) delete ip.aImages[i];
}
/**
 *
 * cross browser getElementById (Mozilla Ugh!, MSIE)
 *
 **/
function getMyElementById(elementId) {
	var z;
  
	if ( document.getElementById )
		z = document.getElementById(elementId);
	else if ( document.layers )
		z = document.layers[elementId];
	else if ( document.all )
		z = document.all.item(elementId);
	return(z);
}
/**
 *
 * sets the date in the timebox
 *
 **/
function setDateInTimerBox(dateString) {
	var z = getMyElementById("timerBox");
	z.innerHTML=dateString;
}
/**
 *
 * main animation function
 *
 **/
function animate(imageNr){
	var index=imageNr;
	var timedFunction;
	
	currImg=imageNr;
	document.images['animImage'].src=ip.aImages[index].src;
	setDateInTimerBox(dataArray[index*2+1]);
	
	index++;
	if (index >= (dataArray.length/2)) index=0;
	timedFunction='animate(' + index + ')';
	timerID=setTimeout(timedFunction,period);
}
/**
 *
 * starts the animation
 *
 **/
function startAnim(){
	if (mayStart){
		if (timerID==null){
			animate(currImg);
		}else{
			alert('animation already started, please stop first!');
		}
	}else{
		alert('not all images are loaded yet, please wait!');	
	}
}
/**
 *
 * initialisation script
 *
 **/
function initScript(){
	ip = new ImagePreloader(dataArray,onPreload);
}
/**
 *
 * changes the pace eh!
 *
 **/
function preStrPad(jstring,size){
	while (jstring.length < size) jstring="0"+jstring;
	return(jstring);
}
function changeSpeed(speedSign){
	if (speedSign < 0){
		// decrease speed
		if (period < 1000){
			// current speed higher than 1 fps
			invPeriod--;
			period=1000/invPeriod;
		}else{
			// current speed lower than 1 fps
			period+=1000;
			invPeriod=1000/period;
		}
	}else{
		// increase speed
		if (period <= 1000){
			// current speed higher than 1 fps
			invPeriod++;
			period=1000/invPeriod;
		}else{
			// current speed lower than 1 fps
			period-=1000;
			invPeriod=1000/period;
		}
	}
	setSpeed(period);
}

function setSpeed(speed){
	var z = getMyElementById("speedBox");
	period=speed;
	invPeriod=1000/period;
	z.innerHTML=invPeriod.toString().substring(0,4)+" fps";
	createCookie('radarRefreshrate', period, 365*2, '');
}




/**
 *
 * advances one frame etc...
 *
 **/
function advanceFrame(sence){
	stopAnim();
	currImg+=sence;
	if (currImg<0) currImg=(dataArray.length/2)-1;
	if (currImg==(dataArray.length/2)) currImg=0;
	document.images['animImage'].src=ip.aImages[currImg].src;
	setDateInTimerBox(dataArray[currImg*2+1]);
}
/*******************************************************************************
 *
 * the imagepreloader part, please stay of of this!
 *
 **/
function ImagePreloader(dataArray, call_back){
   this.call_back = call_back;
   this.nLoaded = 0;
   this.nProcessed = 0;
   this.aImages = new Array;
   this.nImages = (dataArray.length/2);
	for ( var i = 0; i < (dataArray.length/2); i++ )
      this.preload(dataArray[i*2]);
}
ImagePreloader.prototype.preload = function(image){
   var oImage = new Image;
	this.aImages.push(oImage);
   oImage.onload = ImagePreloader.prototype.onload;
   oImage.onerror = ImagePreloader.prototype.onerror;
   oImage.onabort = ImagePreloader.prototype.onabort;

   oImage.oImagePreloader = this;
   oImage.bLoaded = false;
   oImage.src = image;
}
ImagePreloader.prototype.onComplete = function(){
   this.nProcessed++;
   setPgb(this.nProcessed);
   if ( this.nProcessed == this.nImages ){
		this.call_back(this.aImages, this.nLoaded);
	}
}
ImagePreloader.prototype.onload = function(){
   this.bLoaded = true;
   this.oImagePreloader.nLoaded++;
   this.oImagePreloader.onComplete();
}
ImagePreloader.prototype.onerror = function(){
   this.bError = true;
   this.oImagePreloader.onComplete();
}
ImagePreloader.prototype.onabort = function(){
   this.bAbort = true;
   this.oImagePreloader.onComplete();
}
function onPreload(aImages,nImages){
//	ImgArray=ip.aImages;
}
function setPgb(nLoaded) {
	var progress=parseInt((100*nLoaded)/(dataArray.length/2));
	var z = getMyElementById("progressbar");
  	if (z!=null){
  		z.style.width = progress + '%';
		z.innerHTML=parseInt(progress)+"%";
	}
	if (nLoaded==(dataArray.length/2)) mayStart=true;
}
/******************************************************************************/

