﻿// @versuib	20,01.10 - Added getCurrentImageId
// @version 14.01.10 - Added Repeat counter
// @version	06.01.10 - Changed fade mechanism. Added setFadeSpeed
// @version	08.10.09 - added lots : setHideControlsOnClick
// @version	02.06.09 - added checks for null variables
// @version	29.04.09 - added check for thumbfilename


var	yyy=100;

function	tcSlideShow()	{

	this.isLoading		=	0;
	this.active			=	0;
	this.ssiteration	=	0;
	this.currimagenum	=	0;

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

	this.onclosecallback	=	null;
	this.onloadusercallback	=	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.pausebutton		=	null;
	this.playbutton		=	null;
	this.playstatus		=	0;

	this.currasset		=	null;
	this.rolloverimgroot	=	'/resources/images/imgctrl/';

	this.imageareadisplaytype	=	null;

	this.hideControlsOnClick	=	false;
	this.imagefadespeed	=	2000;

	this.nrepeat		=	-1;
	this.yyy			=	111;
}


tcSlideShow.prototype.init	=	function( startnum, slidetime )	{

	this.imagedisptime	=	slidetime;	

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

	this.startslideshow();
}

tcSlideShow.prototype.showframe	=	function( startnum )	{

	this.currimagenum	=	startnum;

	if( this.isLoading )	{

	}

	this.playstatus		=	0;
	this.currImage();

}


tcSlideShow.prototype.start	=	function()	{
	if( !this.playstatus )	{
		this.playstatus	=	1;
//		this.startslideshow();
	}
}

tcSlideShow.prototype.stop	=	function()	{

	if( this.playstatus )	{
		this.endSlideshow();
		this.hideLoading();
		this.active	=	0;
	}
}

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

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 );
	this.playstatus	=	0;
}

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.currImage	=	function() {
	this.endCurrentImage();
	this.startNextImage();
}

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.hideimagearea	=	function()	{
	if( this.theimagearea )	{
		this.theimagearea.setStyle( 'display' ,'none' );
	}
}


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

tcSlideShow.prototype.getCurrentImageId	=	function()	{
	return	this.imgnames[ 'image' + this.currimagenum ];

}

tcSlideShow.prototype.startNextImage	=	function()	{

	if( !this.active )	{	return;	}
	this.ssiteration++;
	if( this.nrepeat != -1 && !(--this.nrepeat))	{
		this.endSlideshow();
		return;
	}
	
	imgid	=	this.getCurrentImageId();

	imgidx	=	'image' + this.currimagenum;

//	imgid	=	this.imgnames[ imgidx ];

	if( imgid )	{

		this.showLoading();
		if( this.hideControlsOnClick )	{
			this.hideControls();
		}
		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})} );
		}.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;	}

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

	this.hideLoading();

	if( this.hideControlsOnClick )	{
		this.showControls();
	}

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

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

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

	if( this.playstatus )	{
		if( this.imagedisptime )	{
			this._ssnextImage.delay( this.imagedisptime, this );
		}
	}

}

tcSlideShow.prototype._ssnextImage	=	function( )	{
	if( this.playstatus )	{
		this.nextImage();
	}
}

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

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


tcSlideShow.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;
}




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( 'height' , bounds.height);
		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');
	}
	
	this.isLoading	=	true;
}


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

tcSlideShow.prototype.hideControls	=	function()	{

	if( this.nextbutton )	{
		this.nextbutton.setStyle( 'display' ,'none');
	}	
	if( this.prevbutton )	{
		this.prevbutton.setStyle( 'display' ,'none');
	}	
	if( this.closebutton )	{
		this.closebutton.setStyle( 'display' ,'none');
	}
	
	
	/////// DO PAUSE AND PLAY
}

tcSlideShow.prototype.showControls	=	function()	{
	if( this.nextbutton )	{
		this.nextbutton.setStyle( 'display' ,'inline');
	}	
	if( this.prevbutton )	{
		this.prevbutton.setStyle( 'display' ,'inline');
	}	
	if( this.closebutton )	{
		this.closebutton.setStyle( 'display' ,'inline');
	}	



}

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);
		if( hiimg )	{
			this.prevbutton.addEvent('mouseover', this.roi.create({bind : this, arguments : [id, hiimg] }));
		}
		if( loimg )	{
			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);
		if( hiimg )	{
			this.nextbutton.addEvent('mouseover', this.roi.create({bind : this, arguments : [id,hiimg] }));
		}
		if( loimg )	{
			this.nextbutton.addEvent('mouseout', this.roi.create({bind : this, arguments : [id,loimg] }));
		}
		this.nextbutton.addEvent('click', this._nextImage.create({bind : this }));
	}
}

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

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

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

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

/* Clicked PLAY */

tcSlideShow.prototype._playSlideshow	=	function()	{	

	if( !this.playstatus )	{
		this.playstatus = 1;

		if( !this.isLoading )	{
			if( this.ssiteration )	{
				this.currimagenum++;
			}
			this.currImage();
		}
	}
}

tcSlideShow.prototype._pauseSlideshow	=	function()	{	
	this.playstatus = 0;
}


tcSlideShow.prototype._nextImage	=	function()	{

	if( !this.playstatus )	{
		if( !this.isLoading )	{
			this.nextImage();
		}
	}
	else	{
		this._pauseSlideshow();
		if( !this.isLoading )	{
			this.nextImage();
		}
	}
}

tcSlideShow.prototype._prevImage	=	function()	{

	if( !this.playstatus )	{
		if( !this.isLoading )	{
			this.prevImage();
		}
		else	{
			this._pauseSlideshow();
			if( !this.isLoading )	{
				this.nextImage();
			}
		}
	}
}

tcSlideShow.prototype._endSlideshow	=	function()	{
	this.playstatus	=	0;
	this.endSlideshow();
}


tcSlideShow.prototype.setOnLoadCallback				=	function( cb )				{	this.onloadusercallback	=	cb;				}
//tcSlideShow.prototype.init							=	function( slidetime )		{	this.imagedisptime	=	slidetime;			}
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.setThumbType					=	function( type )			{	this.thumbtype				=	type;		}
tcSlideShow.prototype.setHideControlsOnClick		=	function( bool )			{	this.hideControlsOnClick	=	bool;		}
tcSlideShow.prototype.setFadeSpeed					=	function( ms )				{	this.imagefadespeed			=	ms;		}
tcSlideShow.prototype.setRepeatCounter				=	function( cnt )				{	this.nrepeat				=	cnt;		}

tcSlideShow.prototype.setLoadingDisplay	=	function( id, imgurl )		{		
	this.loadingdisplay			=	$(id);		
	this.loadingdisplayimg		=	imgurl;
}

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

