var imgloaders = new Array();

//Class imgloader (int id, String pv_src, String div_id)
//Contains information of a specific image loader ajax module.
var imgloader = function (id, pv_src, div_id) {
	this.id = id;
	this.div_id = div_id;
	this.div = function ()
				 {
					return document.getElementById(this.div_id);
				 }
	
	//Initialize progress bar
	//This is the default function, can be replaced by custom module
	this.progress_bar_initialize = function ()
				{
					if (this.div())
					{
						var div_innerHTML = document.getElementById("text_imgload_progress_bar_html_code").value;
						while (div_innerHTML.indexOf("{?id?}") != -1)
						{
							div_innerHTML = div_innerHTML.replace("{?id?}", this.id);
						}
						this.div().innerHTML = div_innerHTML;				
						
						//If custom module defined its own initialization, run it now.
						if (this.progress_bar_initialize_custom)
						{
							this.progress_bar_initialize_custom();
						}
					} else {
						window.setTimeout("imgloaders["+this.id+"].progress_bar_initialize()", 500);
					}
				}
				
	this.progress_bar_initialize();

	this.pi_src_pagevalue_length = "./page_infos/pi_page_value_length.xml&page_value="+pv_src+"&var_name=imgload_"+this.id+"_page_value_length&script=imgloaders["+this.id+"].first_img()";
	this.pi_src_imgload = "./page_infos/pi_imgload.xml&page_value="+pv_src+"&imgloader="+this.id;

	this.ajax_div = new layoutObj.ajax_div(0, "div_imgload_ajax_empty", this.pi_src_pagevalue_length);

	this.completed_count = 0;
	this.img_count = 0;

	this.next_img = function ()
				 {
					this.completed_count++;
					
					this.progress_bar_update();
					if (this.completed_count < this.img_count)
					{
	
						//this.ajax_div.set_page_value_limits (this.current_id, this.current_id);
	
						//this.ajax_div.sync();
					} else {
					    this.finished_func();
					}
				 }

	this.first_img = function ()
				 {
					this.img_count = layoutObj.global["imgload_"+this.id+"_page_value_length"];
					this.ajax_div.page_info_url=this.pi_src_imgload;
					this.ajax_div.sync();
				 }

	this.finished_html_code = "";
	this.finished_func = function ()
				 {
				 }
}


function create_imgloader(pv_src, div_id, finished_html_code)
{
	 var id = imgloaders.length;
	 
     imgloaders[id] = new imgloader(id, pv_src, div_id);
     imgloaders[id].finished_html_code = finished_html_code;
	 
	 //fetch img_count
	 imgloaders[id].ajax_div.sync();
	 
     imgloaders[id].finished_func = function ()
     {
		 var codeTimeout = "";
		 
		 if (imgloaders[id].finished_html_code != "")
		 {
			 codeTimeout = codeTimeout + "imgloaders["+this.id+"].div().innerHTML = imgloaders["+this.id+"].finished_html_code; opcGradient(imgloaders["+this.id+"].div(), 0, 100, 5, 25);";
		 }
		 
		 if (codeTimeout != "")
		 {
			 opcGradient(this.div(), 100, 0, -5, 25);
			 window.setTimeout(codeTimeout, 525);
		 }
		 
		 if (imgloaders[id].finished_func_custom)
		 {
			 imgloaders[id].finished_func_custom();
		 }
     }
}
