﻿
/**
 * tcZizzer
 *
 * @version		1.06		26.11.10		-	changes
 * @version		1.05		24.11.10		-	changes
 * @version		1.04		25.05.10		-	Changed to mousedown. Added getFullWidth
 *
 * @param		String		The main outer container that holds the scroll target
 * @param		String		Scroll Target Name - This is container contining the Slide Containers
 * @param		String		Slide Container Name - this is the outer div containing each of the slides
 * @param		String		Slide Class Name - This is the outer container for each slide
 * @param		String		Item Class Name - Class for each item itself
 * @param		Function	Click on Slide callback function
 * @param		Boolean		Allow overscroll (adds on the width of the display to allow spaces after the last slide)
 *
 * Implements tcArrowScrollerTarget
 */

function	tcZizzer( maincontainerName, scrollTargetName, slideContainerName, eachitemcontainer,eachitemclass ,ccb, allowOverscroll )	{

	this.maincontainerName		=	maincontainerName;
	this.scrollTargetName		=	scrollTargetName;
	this.slideContainerName		=	slideContainerName;
	this.display_width			=	$(this.slideContainerName).getSize().x;
	this.allSlides				=	null;
	this.width_of_all_images	=	0;
	this.eachitemcontainer			=	eachitemcontainer;
	this.eachitemclass			=	eachitemclass;
	this.clickCallback			=	ccb;
	this.clickActive			=	true;
	this.hasClicked				=	false;
	this.currentActiveSlide		=	0;
	if( $(this.slideContainerName))	{
		var	vwid	=	this.initZdisplay();
		if( allowOverscroll )	{
			vwid += this.getVisibleWidth();
		}
		this.width_of_all_images	=	vwid;
		$(this.slideContainerName).setStyle('width', vwid);

	}
}

tcZizzer.prototype.initZdisplay	=	function()	{
	var	vwid	=	0;
	var	i = 0;
	this.allSlides		=	Array();

	$$('.' + this.eachitemcontainer).each(
		function( imgele )	{
			var	ela	=	imgele.getElements('.' + this.eachitemclass);
			el		=	ela[0];
			x	=	el.getSize().x;
			ob	=	new	tcZSlideDisplay( imgele, i, this, vwid );
			vwid += x;
			this.allSlides[i++]	=	ob;
		}.bind(this)
	);

	return		vwid;
}


tcZizzer.prototype.getSlideElement	=	function( n )	{
	e = this.allSlides[n];
	if( !e )	{	return	null;	}
	return	e.getImageElement();
}

tcZizzer.prototype.getScrollTargetName	=	function( n )			{		return	this.scrollTargetName;	}
tcZizzer.prototype.getSlide		=	function( n )			{		return	this.allSlides[n];		}
tcZizzer.prototype.getSlideWidth	=	function( n )			{		return	this.getSlideElement(n).getWidth();		}
tcZizzer.prototype.getSlideCoords	=	function( n )			{		return	this.getSlideElement(n).getCoordinates($(this.maincontainerName));		}
tcZizzer.prototype.setClickActive	=	function( bool )		{		this.clickActive = bool;	}
tcZizzer.prototype.isClickActive	=	function()			{		return	this.clickActive;	}
tcZizzer.prototype.getHasClicked	=	function()			{		return	this.hasClicked;	}
tcZizzer.prototype.getNumSlides		=	function( n )			{		return	this.allSlides.length;		}
tcZizzer.prototype.rhsOffScreen		=	function( currslideidx )	{		return( (this.width_of_all_images - this.getSlideCoords( currslideidx ).left) > this.display_width  );	}
tcZizzer.prototype.partiallyOffRhsScreen=	function( currslideidx )	{		return( this.getSlideCoords( currslideidx ).right > this.display_width  );	}
tcZizzer.prototype.partiallyOffLhsScreen=	function( currslideidx )	{		return( ( this.getSlideCoords( currslideidx ).left) < (-this.display_width) );	}
tcZizzer.prototype.getPixelsOffRhs	=	function()			{		return	this.width_of_all_images - this.display_width;	}
tcZizzer.prototype.getFullWidth		=	function()			{		return	this.width_of_all_images;	}
tcZizzer.prototype.getVisibleWidth	=	function()			{		return $(this.scrollTargetName).getSize().x;}
tcZizzer.prototype.getCurrentActiveSlide=	function()			{		return	this.currentActiveSlide;	}
tcZizzer.prototype.setHasClicked	=	function()			{		this.hasClicked	=	true;	}
tcZizzer.prototype.getNumSlides		=	function()			{		return	this.allSlides.length;	}
tcZizzer.prototype.clickedOnChild	=	function( target )	{
	if( this.isClickActive() )	{
		this.currentActiveSlide	=	target.idx;
		if( this.clickCallback )	{
			this.setClickActive( false );
			this.clickCallback( target );
		}	
	}
}

tcZizzer.prototype.scrollTo		=	function( n, oncomplete )	{
	var ele=this.getSlideElement(n);
	if( ele )	{
		x =	new Fx.Scroll( this.getScrollTargetName());
		x.addEvent('complete',oncomplete );
		x.toElement( ele )
	}
}



tcZizzer.prototype.scrollToLocation		=	function( x, y )	{
		x =	new Fx.Scroll( this.getScrollTargetName()).set( x,y);
}

tcZizzer.prototype._scrollComplete	=	function( el )	{
}

tcZizzer.prototype.isOffScreen	=	function()	{
	if(this.xoffset < this.container.getX())	{ return	-1;}
	if(this.xoffset + 96 > this.container.getY())	{ return	1;}
	return	0;
}

tcZizzer.prototype.getFirstSlideOffscreen	=	function(dir)	{

	if( dir == 'right')	{
		for(i=0; i<this.getNumSlides(); i++)	{
			if(this.partiallyOffRhsScreen(i))	{
				return	i;
			}
		}
	}
	else	{
		for(i=this.getNumSlides()-1; i>=0; i--)	{
			if(this.partiallyOffLhsScreen(i))	{
				return	i+1;
			}
		}
		return	0;
	}
	return	-1;
}

tcZizzer.prototype.reset		=	function()	{
	x	=	new Fx.Scroll( this.getScrollTargetName() ).set(0);
}


//tcZizzer.prototype.getFirstElementOffRHS()	=	function() {
//	return 1;
//}

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

function	tcZSlideDisplay( imgele, idx, container )	{
	this.imgele			=	imgele;
	this.idx			=	idx;
	this.container			=	container;
	this.imgele.addEvent( 'mousedown', this._clickedOnFrame.bind( this ) );

}

tcZSlideDisplay.prototype.getImageElement	=	function()	{
	return	this.imgele;
}

tcZSlideDisplay.prototype.getIndex			=	function()	{
	return	this.idx;
}

tcZSlideDisplay.prototype._clickedOnFrame	=	function()	{
	this.container.setHasClicked();
	this.clickedOnFrame();
}

tcZSlideDisplay.prototype.clickedOnFrame	=	function()	{
	this.container.clickedOnChild( this );
}
