// $Id: $

var RandomPicsStart_Instances = new Array();

function RandomPicsStartPage() {
	var clock = null;
	var id = null;
	var max_pic_idx = null;
	var old_idx = -1;
	var start_delay = -1;
	var max_delay = -1;
	var img_id = null;
	var img_link_id = null;
	var fx = null;
	var link_title = null;
	var preload_imgs = null;
	var pic_array = null;
	var width = -1;
	var height = -1;
	
	this.randomizer = function(pic_array,start_delay,max_delay,img_id,img_link_id,link_title,width,height) {
		if (this.id == null) {
			if (!RandomPicsStart_Instances) {
				RandomPicsStart_Instances = new Array();
			}
		
			this.id = RandomPicsStart_Instances.length;
			RandomPicsStart_Instances[this.id] = this;
		}

		this.pic_array=pic_array;
		this.max_pic_idx = pic_array.length-1;
		this.start_delay = start_delay;
		this.max_delay = max_delay;
	
		this.width = width;
		this.height = height;
			
		this.img_id = img_id;
			
		this.img_link_id = img_link_id;
		this.link_title = link_title;
		
		this.preload_imgs = new Array();
		
		var new_src_name;
		
/*		
		for (i=0; i<=max_pic_idx; i++) {
			this.preload_imgs[i] = new Image();
			
			this.preload_imgs[i].onerror = function() { console.log("image file '" +pic_array[i]+ "' could not be loaded!"); pic_array[i]=""; };	
			
			new_src_name = "image.php?image="+pic_array[i];
			
			if (this.width>-1){
				new_src_name += "&width="+this.width;
			}
			
			if (this.height>-1){
				new_src_name += "&height="+this.height;
			}
			
			new_src_name += "&dontcache=true";
			
			this.preload_imgs[i].src=new_src_name;
		}
*/
		
		this.fx = new MyEffects(this.img_id);
		
		this.old_idx = -1;
		
		this.setClock();
	}
	
	this.setClock = function() {
		if (this.max_pic_idx>0) {						   	
		   	window.setTimeout('RandomPicsStart_Instances['+this.id+'].doClock()', Math.round(Math.random()*this.max_delay) + this.start_delay);
		}
	}

	this.doClock = function() {
		var new_idx=this.old_idx;
		var new_src_name;
		
		
		do {
			do {
				new_idx=Math.floor(Math.random() * this.max_pic_idx);
				
				if (new_idx!=this.old_idx){
					if (this.pic_array[new_idx]==""){
						nex_idx=this.old_idx;
					}
				}
			} while (new_idx==this.old_idx);
			
			new_src_name="image.php?image="+this.pic_array[new_idx];
			
			if (this.width>-1){
				new_src_name += "&width="+this.width;
			}
			
			if (this.height>-1){
				new_src_name += "&height="+this.height;
			}
			
			new_src_name += "&dontcache=true";
//			console.log(new_src_name);
		} while (this.fx.start_effect('x-fade','',new_src_name,2000) == false);
		
		this.old_idx=new_idx;
		
		if (this.img_link_id != "") {
			document.getElementById(this.img_link_id).href="Javascript:popImage('"+this.pic_array[new_idx]+"','"+this.link_title+"','"+this.img_id+"')";
		}

		this.setClock();
	}	
}
