/*!
 * Tiny Carousel 1.9
 * http://www.baijs.nl/tinycarousel
 *
 * Copyright 2010, Maarten Baijs
 * Dual licensed under the MIT or GPL Version 2 licenses.
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.opensource.org/licenses/gpl-2.0.php
 *
 * Date: 01 / 06 / 2011
 * Depends on library: jQuery
 */

(function($)
{
	$.tiny = $.tiny ||
	{};

	$.tiny.carousel =
	{
		options :
		{
		    start : 1, // where should the carousel start?
		    display : 1, // how many blocks do you want to move at 1 time?
		    axis : 'x', // vertical or horizontal scroller? ( x || y ).
		    controls : true, // show left and right navigation buttons.
		    pager : false, // is there a page number navigation present?
		    pager2 : false, // is there a page number navigation present?
		    interval : true, // move to another block on intervals.
		    intervaltime : 3000, // interval time in milliseconds.
		    rewind : false, // If interval is true and rewind is true it will play in reverse if the last slide is reached.
		    animation : true, // false is instant, true is animate.
		    duration : 300, // how fast must the animation move in ms?
		    callback : null, // function that executes after every move.
		    eventtype : 'click',
		    title : false,
		    animationtype : 'slide',
		    skipslide : true
		}
	};

	$.fn.tinycarousel = function(options)
	{
		var options = $.extend(
		{}, $.tiny.carousel.options, options);
		this.each(function()
		{
			$(this).data('tcl', new Carousel($(this), options));
		});
		return this;
	};
	$.fn.tinycarousel_start = function()
	{
		$(this).data('tcl').start();
	};
	$.fn.tinycarousel_stop = function()
	{
		$(this).data('tcl').stop();
	};
	$.fn.tinycarousel_move = function(iNum)
	{
		$(this).data('tcl').move(iNum - 1, true);
	};
	
	$.fn.tinycarousel_reinit = function()
	{
		$(this).data('tcl').reinit();
	};

	function Carousel(root, options)
	{
		var oSelf = this;
		var oViewport, oContent, oPages, oBtnNext, oBtnPrev, oPager, oPager2, oTitle;
		var iPageSize, iSteps, iCurrent, oTimer, bPause, bForward = true, bAxis;

		function initialize()
		{
			bAxis = options.axis == 'x';
			oViewport = $('.viewport:first', root);
			oContent = $('.overview:first', root);
			oPages = oContent.children();
			oBtnNext = $('.next:first', root);
			oBtnPrev = $('.prev:first', root);
			oPager = $('.pager:first', root);
			oPager2 = $('.pager2:first', root);
			oTitle = $('.title:first', root);
		
			iPageSize = bAxis ? $(oPages[0]).outerWidth(true) : $(oPages[0]).outerHeight(true);
			var iLeftover = Math.ceil(((bAxis ? oViewport.outerWidth() : oViewport.outerHeight()) / (iPageSize * options.display)) - 1);
			iSteps = Math.max(1, Math.ceil(oPages.length / options.display) - iLeftover);
			iCurrent = Math.min(iSteps, Math.max(1, options.start)) - 2;
			oContent.css(bAxis ? 'width' : 'height', (iPageSize * oPages.length));
			oSelf.move(1);
			setEvents();
			return oSelf;
		}
		;
		function setEvents()
		{
			if (options.controls && oBtnPrev.length > 0 && oBtnNext.length > 0)
			{
				oBtnPrev.click(function()
				{
					oSelf.move(-1);
					return false;
				});
				oBtnNext.click(function()
				{
					oSelf.move(1);
					return false;
				});
			}
			if (options.interval)
			{
				root.hover(oSelf.stop, oSelf.start);
			}
			if (options.pager && oPager.length > 0)
			{
				if (options.eventtype == 'hover')
				{
					$('a', oPager).hover(setPager);
				}
				else
				{
					$('a', oPager).click(setPager);
				}
				if (options.pager2)
				{
					oPager2.html((iCurrent + 1) + '/' + iSteps);
				}
			}
		}
		;
		function setButtons()
		{
			if (options.controls)
			{
				oBtnPrev.toggleClass('disable', !(iCurrent > 0));
				oBtnNext.toggleClass('disable', !(iCurrent + 1 < iSteps));
			}
			if (options.pager)
			{
				var oNumbers = $('.pagenum', oPager);
				oNumbers.removeClass('active');
				$(oNumbers[iCurrent]).addClass('active');
			}
		}
		;
		function setTitle()
		{
			if (options.title)
			{
				titleCurr = $('img', oPages[iCurrent]).attr('alt');
				if (titleCurr)
				{
					oTitle.html('<a href="' + $('a:first', oPages[iCurrent]).attr('href') + '">' + titleCurr + '</a>');
				}
			}
		}
		;
		function setPager(oEvent)
		{
			if ($(this).hasClass('pagenum'))
			{
				oSelf.move(parseInt(this.rel), true);
			}
			return false;
		}
		;
		function setTimer()
		{
			if (options.interval && !bPause)
			{
				clearTimeout(oTimer);
				oTimer = setTimeout(function()
				{
					iCurrent = iCurrent + 1 == iSteps ? -1 : iCurrent;
					bForward = iCurrent + 1 == iSteps ? false : iCurrent == 0 ? true : bForward;
					oSelf.move(bForward ? 1 : -1);
				}, options.intervaltime);
			}
		}
		;
		this.reinit = function()
		{
			initialize();
			//alert(oPager)
			//alert(setPager)
			//$('a', oPager).hover(setPager);
		};
		
		this.stop = function()
		{
			clearTimeout(oTimer);
			bPause = true;
		};
		this.start = function()
		{
			bPause = false;
			setTimer();
		};
		this.move = function(iDirection, bPublic)
		{
			iPrev = iCurrent;
			iCurrent = bPublic ? iDirection : iCurrent += iDirection;
			//alert(iPrev + " " + iCurrent)
			if (iPrev > -1 && options.animationtype == 'slide')
			{
				if (iCurrent - iPrev > 0)
				{
					oContent.css('left', "-" + ((iCurrent - 1) * (iPageSize * options.display)) + "px");
				}
				else if (iCurrent != iPrev)
				{
					oContent.css('left', "-" + ((iCurrent + 1) * (iPageSize * options.display)) + "px");
				}
			}
			
			if(iCurrent > -1 && iCurrent < iSteps)
			{
				var oPosition = {};
				oPosition[bAxis ? 'left' : 'top'] = -(iCurrent * (iPageSize * options.display));	
				oContent.animate(
					oPosition,
					{
						queue: false,
						duration: options.animation ? options.duration : 0,
						complete: function()
						{
							if(typeof options.callback == 'function')
							{
								options.callback.call(this, oPages[iCurrent], iCurrent);
							}
						}
					}
				);
				setButtons();
				setTimer();
			}
			/*
			if (iCurrent > -1 && iCurrent < iSteps)
			{
				var oPosition =
				{};
				oPosition[bAxis ? 'left' : 'top'] = -(iCurrent * (iPageSize * options.display));
				if (options.animationtype == 'slide')
				{
					oContent.animate(oPosition,
					{
					    queue : false,
					    duration : options.animation ? options.duration : 0,
					    complete : function()
					    {
						    if (typeof options.callback == 'function')
							    options.callback.call(this, oPages[iCurrent], iCurrent);
					    }
					});
				}
				else if (options.animationtype == 'fadeout')
				{
					oContent.stop(true).css('opacity', '0').delay(200).css('left', "-" + (iCurrent * (iPageSize * options.display)) + "px").animate(
					{
					    queue : false,
					    opacity : 1,
					    duration : 5000
					});
				}
				else
				{
					oContent.css('left', "-" + (iCurrent * (iPageSize * options.display)) + "px");
				}
				setButtons();
				setTimer();
				setTitle();
			}
			*/
		};
		return initialize();
	}
	;
})(jQuery);

