﻿// @04.03.09 : Fixed problems with 'next'
var	mainimagestr	=	'mainimage';
var imagebufferstr	=	'imagebuffer';
var	loadingDisplayStr	=	'loadingdisplay';

//var	currimage		=	null;
var	currimagenum	=	0;
//var	theimage		=	null;
var	imagebuffer		=	null;
var	imagedisptime	=	3500;
var	playstatus		=	1;
var	hasstarted		=	0;
var	thumbtype		=	105;
var	isLoading		=	false;
var	dispDir			=	1;

window.addEvent('domready', function() {
	isLoading				=	false;
	currimagenum			=	1;
	dispDir					=	1;

	setupControlButton( 'playbutton' );
	setupControlButton( 'backbutton' );
	setupControlButton( 'pausebutton' );
	setupControlButton( 'forwardbutton' );
	
	fireEvent('nextimage', "xxx", imagedisptime);
});

function	dobuttonclick( ids )		{	
	if( ids == 'playbutton' )	{
		resumeSlideshow();
	} else
	if( ids == 'forwardbutton' )	{
		nextSlide();
	} else	if( ids == 'pausebutton' )	{
		pauseSlideshow();
	} else	if( ids == 'backbutton' )	{
		previousSlide();
	}
}

function	setupControlButton( ids )	{
	var		but = $(ids);
	if(but)	{
		but.addEvent('mouseover', function()	{ roi( ids, ids + '_hi.png'); }	);
		but.addEvent('mouseout', function()		{ roi( ids, ids + '.png'); });
		but.addEvent('click', function()		{ dobuttonclick( ids ); });
	}
}

function	roi(iid, fn)	{
	$(iid).src='/resources/images/slideshow/' + fn;
}

window.addEvent('nextimage', function() {

	if( playstatus )	{
		if( !hasstarted )	{
			$(imagebufferstr).empty();
			hasstarted	=	1;
		}
		else	{
			$(mainimagestr).setOpacity( 0 );
			var	clone	=	$(imagebufferstr).clone();
			clone.setProperty('id', mainimagestr);
			clone.replaces( $(mainimagestr) );
			if( dispDir )	{
				setNextImage();
			}
			else	{
				setPrevImage();
			}
		}
		if( playstatus == 2 )	{
			playstatus	=	0;
		}
		zoomimage( currimagenum );
	}
	
});

function	setThumbType( tt )	{
	thumbtype	=	tt;
}

function	zoomimage( cin )	{

	if( imgnames['image' + cin] )	{
	
		isLoading	=	true;
		showLoading('ssimg');
		var	aurl	=	'/code/ajaxcall.php?module=registration&action=getthumbimage&id=' + imgnames['image' + cin] + '&thumbtype=' + thumbtype;

		var showSlide = function( jsonObj ) {
			filename	=	jsonObj.fullfilename;
			title		=	jsonObj.title;
			comments	=	jsonObj.comments;
			alttext		=	jsonObj.alttext;
			var newasset = new Asset.image( filename, { id: 'image' + cin, title: comments, name : title, onload: newimageloadcomplete} );
		};

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


function	newimageloadcomplete( e )	{

	hideLoading();
	imagebuffer				=	$(imagebufferstr);

	imagebuffer.empty();
	imagebuffer.setOpacity( 0 );
	
	e.set('id','ssimg');
	e.inject( imagebuffer );
	var el = new Element('div', {'class': 'article_item'}).inject(imagebuffer);
//	var ic = new Element('div', {'html': 'Image ' + (currimagenum+1) + ' of ' + imgcnt, 'class': 'imagecountdisplay'}).inject(imagebuffer);
	var title = new Element('h3', {'html': e.name}).inject(el);
	var comments = new Element('p', {'class': 'article_bulk', 'html': e.title}).inject(el);
	e.title	=	e.name;
	
	imagebuffer.fade([1]);
	isLoading	=	false;

	icd	=	$('imagecountdisplay');
	if( icd )	{
		icd.empty();
		var ic = new Element('span', {'html': (currimagenum+1) + '/' + imgcnt}).inject(icd);
	}
	
	window.fireEvent('nextimage', "xxx", imagedisptime );
}

function	pauseSlideshow()	{	
	playstatus = 0;
}

function	resumeSlideshow()	{	
	if( !isLoading )	{
		if( playstatus == 0)	{
			window.fireEvent('nextimage', "xxx", 1 );
		}
		dispDir	=	1;
		playstatus = 1;
	}
}
function	nextSlide()		{
	dispDir	=	1;
	if( !isLoading )	{
		playstatus = 2;
		window.fireEvent('nextimage', "xxx", 1 );
	}
}
function	previousSlide()		{
	dispDir	=	0;
	if( !isLoading )	{
		playstatus = 2;
		window.fireEvent('nextimage', "xxx", 1 );
	}
}
function	setNextImage()	{	if( ++currimagenum >= imgcnt )	{	currimagenum=0;	}	}
function	setPrevImage()	{	if( --currimagenum < 0)			{	currimagenum=imgcnt-1;	}	}

function	showLoading( forimgid )	{	

	var		ld	=	$(loadingDisplayStr);
	if( forimgid )	{

		var	s	=	$(forimgid);
		if( s )	{
			var bounds= s.getCoordinates();
			
			//22 is half width/height of the loading image
			var	left = ((bounds.width/2)-22);
			var	top = ((bounds.height/2)-22);
			ld.setStyle('background-position', left + 'px ' + top + 'px');
			ld.setOpacity( 0.5 );

		}
	}
	ld.setStyle('display', 'block');	
}

function	hideLoading()	{	
	$(loadingDisplayStr).setStyle('display', 'none');		
}

