﻿// @version	02.06.09 - added checks for null variables
// @version	29.04.09 - added check for thumbfilename

var	yyy=100;

function	tcSlideShow()	{

	this.active			=	0;
	this.currimagenum	=	0;

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

	this.onclosecallback	=	null;

	this.imgcnt			=	0;
	this.imgnames		=	null;
	this.loadingdisplay	=	null;
	this.loadingdisplayimg	=	null;
	this.imagedisptime	=	0;
	this.thumbtype		=	null;
	this.closebutton	=	null;
	this.nextbutton		=	null;
	this.prevbutton		=	null;
	
	this.currasset		=	null;
	this.rolloverimgroot	=	'/resources/images/imgctrl/';

	this.imageareadisplaytype	=	null;

	this.yyy			=	111;
}

tcSlideShow.prototype.startslideshow	=	function()	{
	this.showimagearea();
	this.imagebuffer.empty();
	this.startNextImage();
}

tcSlideShow.prototype.endSlideshow	=	function()	{
	if( this.theimagearea )	{
		this.theimagearea.fade([0]);
	}
	this.destroy.delay(1000, this );
}

tcSlideShow.prototype.destroy	=	function()	{

	this.currasset	=	undefined;
	this.imagebuffer.empty();
	this.theimage.empty();
	this.currimagenum		=	0;
	this.hideimagearea();
	if( this.onclosecallback )	{	
		this.onclosecallback.delay(10, this );
	}
}


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


tcSlideShow.prototype.showimagearea	=	function()	{
	if( this.theimagearea )	{

//		this.imageareadisplaytype	=	this.theimagearea.getStyle( 'display');
		this.theimagearea.setStyle( 'display' ,'inline');
		
	}
}


tcSlideShow.prototype.startNextImage	=	function()	{

	if( !this.active )	{	return;	}

	imgidx	=	'image' + this.currimagenum;
	imgid	=	this.imgnames[ imgidx ];

	if( imgid )	{

		this.showLoading();
		var	aurl	=	'/code/ajaxcall.php?module=images&action=getimagebyid&id=' + imgid + '&ttype=' + this.thumbtype;

		var myShowSlide = function( jsonObj ) {

			// If there is a thumbtype AND the thumbfilename is OK then display that image
			if( this.thumbtype && jsonObj.thumbfullfilename)	{	
				filename	=	jsonObj.thumbfullfilename;
			}
			else	{
				filename	=	jsonObj.fullfilename;
			}

			title		=	jsonObj.title;
			comments	=	jsonObj.comments;
			alttext		=	jsonObj.alttext;

			this.currasset = new Asset.image( filename, { id: imgidx, title: comments, name : title, onload: this.onloadcallback.create({bind : this})} );
//			this.showLoading();

		}.create({bind : this});

		var request = new Request.JSON({
			url: aurl,
			onComplete: function( jsonObj ) {
				myShowSlide( jsonObj.TC_ASSET );
			}
		}).send();
	}
}

// OK the image has loaded ...

tcSlideShow.prototype.onloadcallback	=	function	(e)	{

	if( !this.active )	{	return;	}

	this.hideLoading();
	this.imagebuffer.empty();
	this.imagebuffer.setOpacity( 0 );
	e.inject( this.imagebuffer );
	this.imagebuffer.fade([1]);

	if( this.imagedisptime )	{
		this.nextimage.delay( this.imagedisptime, this );
	}

}

tcSlideShow.prototype.roi	=	function( iid, fn )	{

	if( fn )	{
		if( fn.substring(0,1) != '/' )	{
			$(iid).src= this.imgroot + fn; 
		}
		else	{
			$(iid).src= fn; 
		}
	}
}

// Call to go to the next image
tcSlideShow.prototype.nextimage	=	function() {
	this.endCurrentImage();
	if( ++this.currimagenum >= this.imgcnt )	{	this.currimagenum=0;	}
	this.startNextImage();
}

tcSlideShow.prototype.previmage	=	function()	{
	this.endCurrentImage();
	if( --this.currimagenum < 0)	{	this.currimagenum=this.imgcnt-1;	}
	this.startNextImage();
}

tcSlideShow.prototype.endCurrentImage	=	function()	{

//	if( this.theimagearea )	{
//		this.theimagearea.setOpacity( 0 );
//	}

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


tcSlideShow.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( 'background' ,'url('+this.loadingdisplayimg+') no-repeat');
		this.loadingdisplay.setStyle( 'background-position' , left + 'px ' + top + 'px');
//		this.loadingdisplay.setStyle( 'background' ,'url('+this.loadingdisplayimg+') no-repeat center center');
	}
}

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

tcSlideShow.prototype.setCloseButton	=	function( id, hiimg, loimg, closecallback )	{

	this.closebutton		=	$(id);

	if( this.closebutton )	{
		if( hiimg != undefined )	{
			this.closebutton.addEvent('mouseover', this.roi.create({bind : this, arguments : [id,hiimg] }));
		}
		if( loimg != undefined )	{
			this.closebutton.addEvent('mouseout', this.roi.create({bind : this, arguments : [id,loimg] }));
		}
		this.onclosecallback	=	closecallback;
		if( this.theimagearea )	{
			this.closebutton.addEvent( 'click', this.endSlideshow.create({bind : this }));
		}
	}
}

tcSlideShow.prototype.setPrevButton	=	function( id, hiimg, loimg )	{

	if($(id))	{
		this.prevbutton		=	$(id);
		this.prevbutton.addEvent('mouseover', this.roi.create({bind : this, arguments : [id, hiimg] }));
		this.prevbutton.addEvent('mouseout', this.roi.create({bind : this, arguments : [id, loimg] }));
		this.prevbutton.addEvent('click', this.previmage.create({bind : this }));
	}
}

tcSlideShow.prototype.setNextButton	=	function( id, hiimg, loimg )	{

	if($(id))	{
		this.nextbutton		=	$(id);
		this.nextbutton.addEvent('mouseover', this.roi.create({bind : this, arguments : [id,hiimg] }));
		this.nextbutton.addEvent('mouseout', this.roi.create({bind : this, arguments : [id,loimg] }));
		this.nextbutton.addEvent('click', this.nextimage.create({bind : this }));
	}
}


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

tcSlideShow.prototype.setImageData	=	function( namearray, cnt )	{
	this.imgnames	=	namearray;
	this.imgcnt		=	cnt;
}

tcSlideShow.prototype.init	=	function( slidetime )	{
	this.imagedisptime	=	slidetime;
}

tcSlideShow.prototype.start	=	function( restart , startnum )	{

	this.active			=	1;
	this.currimagenum	=	0;
	if( startnum )	{
		this.currimagenum	=	startnum;
	}
	if( this.theimagearea )	{
		this.theimagearea.fade([1]);
	}

	if( restart || !this.imagedisptime )	{
		this.startslideshow();
	}
	else	{
		this.startslideshow.delay( this.imagedisptime, this);
	}
}

tcSlideShow.prototype.stop	=	function()	{
	this.endSlideshow();
	this.hideLoading();
	this.active	=	0;
}
