

var please_wait_thumb_h = "pw_thumb_h.jpg";
var please_wait_image_h = "pw_large_h.jpg";

var please_wait_thumb_v = "pw_thumb_v.jpg";
var please_wait_image_v = "pw_large_v.jpg";

// function load_get_pw_tbn(){ return please_wait_thumb; }
// function load_get_pw_img(){ return please_wait_image; }

// please_wait_image = "images/huge.jpg";

var	listObj = null;

function cListElm(url)
{
	cListElm.prototype.set_default = function(url)
	{
		// set some default value
		this.prev = null;
		this.next = null;
		this.url = url;
		this.status = 'nl';
		this.src = url;
		this.img = img = new Image();
		this.callback = null;
		this.lParam = 0;
		
		// set the handler 'onload' event
		if( browser == 'ie' )	img.attachEvent("onload", load_handler_ie);
		else
		if( browser == 'opera' )img.attachEvent("onload", load_handler_moz);
		else					img.addEventListener("load", load_handler_moz, false);
	}
	
	this.set_default( url );

	cListElm.prototype.set_next = function( n ){ this.next = n; }
	cListElm.prototype.set_prev = function( p ){ this.prev = p; }
	cListElm.prototype.set_src = function( s ){ this.src = s; }
	cListElm.prototype.apply_src = function(){ this.img.src = this.src; }
	cListElm.prototype.set_status = function( s ){ this.status = s; }
	cListElm.prototype.set_callback = function( f, p ){ this.callback = f; this.lParam = p; }
	
	cListElm.prototype.get_next = function( ){ return this.next; }
	cListElm.prototype.get_prev = function( ){ return this.prev; }
	cListElm.prototype.get_src = function( ){ return this.img.src; }
	cListElm.prototype.get_img_status = function( ){ return this.img.complete; }
	cListElm.prototype.get_status = function( ){ return this.status; }
	cListElm.prototype.reply = function(){ if( this.callback != null ) this.callback(this.src, this.lParam); }
}


/* ----------------------------------------------------

*/
function list_count( )
{
	if( (obj = listObj ) == null ) return 0;
	var	cnt = 1;
	while( ( obj = obj.get_next() ) != null )	cnt++;
	return cnt;
}


/* ----------------------------------------------------

*/
function list_get_last( obj )
{
	while( ( o = obj.get_next() ) != null ) obj = o;
	return obj;
}


/* ----------------------------------------------------

*/
function list_add( url, cb, lp)
{
	var obj = new cListElm(url);
	
	if( listObj == null ) listObj = obj;
	else
	{
		p = list_get_last( listObj );
		p.set_next(obj);
		obj.set_prev(p);
	}

	obj.set_callback(cb, lp);
}

function list_del_obj(obj)
{
	prev = obj.prev;
	next = obj.next;	

	if( prev != null )	prev.next = next;
	else				list = next;
	
	if( next != null )	next.prev = prev;

	return	next;
}

/* ----------------------------------------------------
	Disregard the entry argument; wouldn't work with explorer :(
	On top of that, the img.complete flag is not set by explorer before he calls this function but after!
	Therefore, the only solution is to start a small timer and to check the flag later on...	
*/
function	load_handler_ie(data)
{
	id = setTimeout(post_handler_ie, 1);
}

function	post_handler_ie()
{
	var obj = listObj;
	
//	if( obj == null ) return;
	do
	{			
		if( obj.get_img_status() == true && obj.status != 'loaded' )
		{		
			obj.set_status( 'loaded' );
			obj.reply();
		}
	}while( ( obj = obj.get_next() ) != null );
}

/* ----------------------------------------------------

*/
function	load_handler_moz(data)
{
	var obj = listObj;

	// src = new String( data.currentTarget.src );
	src = data.currentTarget.src;

//	dbg = document.getElementById("debug");	
//	dbg.value += "handler: " + src + " \r\n";

	do
	{		
		if( obj.img.src == src )
		{			
			obj.set_status('loaded');
			obj.reply();			
		}
	}while( ( obj = obj.get_next() ) != null );
}


/* ----------------------------------------------------
	Pre-load the "please wait" image & thumbnail
*/
function	load_preload()
{
	load_push( please_wait_thumb_h );
	load_push( please_wait_image_h );

	load_push( please_wait_thumb_v );
	load_push( please_wait_image_v );
}


/* ----------------------------------------------------
	Reset the list of thumbnail objects
*/
function	load_reset()
{
	var obj = listObj;
	do
	{
		delete obj.img;

		copy_obj = obj;
			
		obj = obj.get_next()
		
		delete	copy_obj;
		
	}while( obj != null );

	list = null;
}


/* ----------------------------------------------------
	Add a reference to the list of thumbnail objects
*/
function	load_push( url )
{
	list_add( url, null, 0);	
}

/* ----------------------------------------------------
	Add several references to the list of thumbnail objects
*/
function	load_push_array( ar )
{
	for( i in ar )	list_add( ar[i], null, 0);
}

/* ----------------------------------------------------

*/
function	load_isloaded( url )
{
	var obj;
	
	if( (obj = listObj) != null )
	{
		do
		{
			if( obj.src == url && obj.status == 'loaded' ) return true;
		}while( ( obj = obj.get_next() ) != null );
	}
	return false;
}

/* ----------------------------------------------------

*/
function	load_add_ref( url, funct, lParam)
{
	var obj;
	if( (obj = listObj) != null )
	{
		do
		{
			if( obj.src == url ) return;
		}while( ( obj = obj.get_next() ) != null );
	}

	list_add( url, funct, lParam);
	
//	dbg = document.getElementById("debug");	
//	dbg.value += "Adding: " + url + " id= " + lParam + " \n\r";
}

/* ----------------------------------------------------
	Start loading the thumbnail objects
*/
function	load_process()
{
	var obj;
	
	if( (obj = listObj) == null ) return;
	do
	{
		obj.apply_src();	
	}while( ( obj = obj.get_next() ) != null );
}

/* ----------------------------------------------------
	Start loading the thumbnail objects
*/
function	load_process_ex()
{
	var obj;
	
	if( (obj = listObj) == null ) return;
	do
	{
		if( obj.get_status() == 'nl' )
		{
			obj.set_status( 'loading' );
			obj.apply_src();	
			break;
		}
	}while( ( obj = obj.get_next() ) != null );
}

/* ----------------------------------------------------

*/
var limit_it = 0;
function checker()
{
	dbg = document.getElementById("debug");	

	dbg.value += list_count() + " element\n\r";
	
	var 	obj;
	if( (obj = listObj) != null )
	{
		do
		{
			dbg.value += "Status: " + obj.get_status() + "\n\r";
		}while( ( obj = obj.get_next() ) != null );
	
	}
	
	if( limit_it++ < 7 )	timerId = setTimeout(checker, 200);

	load_process_ex();
}


/* ----------------------------------------------------

*/
function load_test()
{
	var dbg = document.getElementById("debug");	

	dbg.value = "Test (browser: " + browser + " )\n\r";

	load_preload();
		
	checker();
	
	var table2 = [["url", 5, 7], ["url", 1, 4]];

	table = new Array();
	t = new Array();
	t.push("url");	t.push("w");	t.push("h");	
	t2 = new Array();
	t2.push("url");	t2.push("w");	t2.push("h");	

	table.push(t);
	table.push(t2);

	for( i in table2 )	dbg.value += table2[i] + "\n\r";

//	load_process_ex();
	
/*
	var this_img = new Image();
	this_img.attachEvent("onload", load_handler_ie);
	this_img.src = please_wait_thumb;
*/
}

