﻿/**
 * @version		1.11		01.04.10		Comment out unused thumb
 * version		1.1			21.12.09		Changed fade speed
 */

function	tcImageLoader()	{

	this.isLoading		=	0;

	this.imagebuffer	=	null;
	this.theimagearea	=	null;
	this.theimage		=	null;

	this.onloadusercallback	=	null;

	this.loadingdisplay	=	null;
	this.loadingdisplayimg	=	null;

//	this.thumbtype		=	null;
	this.currasset		=	null;
}


tcImageLoader.prototype.init	=	function()	{

	if( this.theimagearea )	{
		this.theimagearea.fade([1]);
	}
	this.showimagearea();
	this.imagebuffer.empty();
	this.isLoading	=	false;
}

/* =========================================================== */

tcImageLoader.prototype.loadImage		=	function( fname )	{
	this.endCurrentImage();
	filename	=	fname;
	this.currasset = new Asset.image( filename, { onload: this.onloadcallback.create({bind : this})} );
}

tcImageLoader.prototype.onloadcallback	=	function	(e)	{

	if( this.onloadusercallback )	{
		this.onloadusercallback(e);
	}

	this.hideLoading();

	this.imagebuffer.empty();
	this.imagebuffer.setOpacity( 0 );
	e.inject( this.imagebuffer );

	var	myFx	=	new Fx.Tween(this.imagebuffer, {duration : 1500});
	myFx.start('opacity', 0, 1);
	var	myFx2	=	new Fx.Tween(this.theimage, {duration : 1500});
	myFx2.start('opacity', 1, 0);


//	this.imagebuffer.fade([1]);
//	this.theimage.fade([0]);
}

tcImageLoader.prototype.hideimagearea	=	function()	{
	if( this.theimagearea )	{
		this.theimagearea.setStyle( 'display' ,'none' );
	}
}

tcImageLoader.prototype.showimagearea	=	function()	{
	if( this.theimagearea )	{
		this.theimagearea.setStyle( 'display' ,'inline');
		
	}
}

tcImageLoader.prototype.endCurrentImage	=	function()	{

	this.theimage.setOpacity( 0 );
	var	clone	=	this.imagebuffer.clone();
	clone.setProperty( 'id', this.theimage.id );
	clone.replaces( this.theimage );
	this.theimage	=	clone;
}


tcImageLoader.prototype.showLoading	=	function()	{
	if( this.loadingdisplay )	{
	
		this.loadingdisplay.setStyle( 'display' ,'inline');
		this.loadingdisplay.fade([0.7]);

		if( this.currasset )	{
			var bounds	= this.currasset.getCoordinates();
		}
		else	{
			var bounds	= this.loadingdisplay.getCoordinates();
		}

		var	wh		=	16;		// Loading symbol w/h divided by 2
		var	left = ((bounds.width/2)-wh);
		var	top = ((bounds.height/2)-wh);

		this.loadingdisplay.setStyle( 'height' , bounds.height);
		this.loadingdisplay.setStyle( 'background' ,'url('+this.loadingdisplayimg+') no-repeat');
		this.loadingdisplay.setStyle( 'background-position' , left + 'px ' + top + 'px');
	}
	
	this.isLoading	=	true;
}


tcImageLoader.prototype.hideLoading	=	function()	{
	if( this.loadingdisplay )	{
		this.loadingdisplay.setStyle( 'background' ,'none');
		this.loadingdisplay.setStyle( 'display' ,'none');
	}
	this.isLoading	=	false;
}


tcImageLoader.prototype.setOnLoadCallback				=	function( cb )				{	this.onloadusercallback	=	cb;				}
tcImageLoader.prototype.setImage						=	function( id )				{	this.theimage				=	$(id);		}
tcImageLoader.prototype.setImageArea					=	function( id )				{	this.theimagearea			=	$(id);		}
tcImageLoader.prototype.setImageBuffer					=	function( id )				{	this.imagebuffer			=	$(id);		}
//tcImageLoader.prototype.setThumbType					=	function( type )			{	this.thumbtype				=	type;		}
tcImageLoader.prototype.setLoadingDisplay				=	function( id, imgurl )		{	this.loadingdisplay			=	$(id);		
																						this.loadingdisplayimg		=	imgurl;		}
