/**
 * @version $Id: carousel.js 92 2010-06-30 08:11:51Z paulinad $
 * @package frontend
 * @author paulinad
 */

/**--------------------------------------------------------------------
*	karuzela wzorowana na na http://www.queness.com/post/923/create-a-simple-infinite-carousel-with-jquery
*---------------------------------------------------------------------*/
	
	var carouselClass = new Class({
		
		animateDuration: 300,
		elementsCount: 0,
		elemsVis: 0,
		
		initialize: function( elem, elemsVis, module ){
			if( typeof( module ) == 'undefined' )
			{
				module = 'carousel';
			}
			this.elemsVis = elemsVis;
			this.carouselTimeout = 0;
			this.carousel = elem;
			this.slidesObj = this.carousel.getElements( '.'+module+'_slides' )[0];//$$( '#'.this.carouselId+' .carousel_slides ' );
			this.ulObj = this.slidesObj.getElements( 'ul' )[0];
			var elements = this.ulObj.getChildren( 'li' );
			this.elementsCount = elements.length;
			this.item_width = elements[0].offsetWidth;
			this.left_value = 0;
			if( this.elementsCount > this.elemsVis )
			{
				this.left_value = this.item_width*(-1)-5;
				this.moveToBegin();
			}
			//this.scroll = new Fx.Tween( this.ulObj, {property: 'left', onComplete: this.doNothing.bind ,duration: this.animateDuration });
			//this.scroll = new Fx.Style( this.ulObj, {property: 'left', onComplete: this.doNothing.bind ,duration: this.animateDuration });
			this.scroll = new Fx.Styles( this.ulObj, { duration: this.animateDuration, transition: Fx.Transitions.quadOut } );
			
			var prevButtons = this.carousel.getElements( 'a.'+module+'_prev' );
			for( var i = 0; i < prevButtons.length; i++ )
			{
				prevButtons[i].addEvent('click', this.clickPrev.bind(this) );
			}
			
			var nextButtons = this.carousel.getElements( 'a.'+module+'_next' );
			for( var i = 0; i < nextButtons.length; i++ )
			{
				nextButtons[i].addEvent('click', this.clickNext.bind(this) );
			}
		},
		
		doNothing: function(){
			return;
		},
		
		clickNext: function( event )
		{
			event = new Event( event ).stop();
			//this.scroll.cancel();
			//if( this.elementsCount < this.elemsVis+2 )
			if( this.elementsCount <= this.elemsVis ){ return; }
			{
				this.moveToEnd();
				this.ulObj.setStyle( 'left', this.left_value+this.item_width );//
				var left_indent = parseInt( this.ulObj.getStyle( 'left' ) )-this.item_width;
				//this.scroll = new Fx.Tween( this.ulObj, { property: 'left', duration: this.animateDuration } );
				this.scroll = new Fx.Styles( this.ulObj, { duration: this.animateDuration, transition: Fx.Transitions.quadOut } );
			}
			/*else
			{
				var left_indent = parseInt(this.ulObj.getStyle('left')) - this.item_width;
				this.scroll = new Fx.Tween( this.ulObj, {property: 'left', onComplete: this.moveToEnd.bind(this), duration: this.animateDuration });
			}
			*/
			this.scroll.start( { 'left': left_indent } );
			return false;
		},
		
		clickPrev: function( event )
		{
			event = new Event(event).stop();
			//this.scroll.cancel();
			if( this.elementsCount <= this.elemsVis ){ return; }
			/*if( this.elementsCount == ( this.elemsVis+1 ) )
			{
				var left_indent = parseInt( this.ulObj.getStyle( 'left' ) )+this.item_width;
				//this.scroll = new Fx.Tween( this.ulObj, { property: 'left', onComplete: this.moveToBegin.bind( this ), duration: this.animateDuration } );
				this.scroll = new Fx.Styles( this.ulObj, { duration: this.animateDuration, transition: Fx.Transitions.quadOut } );
			}
			else*/
			{
				//this.moveToBegin();
				//this.ulObj.setStyle( 'left', this.left_value-this.item_width );
				var left_indent = parseInt( this.ulObj.getStyle( 'left' ) )+this.item_width;
				//var left_indent = 0;
				//this.scroll = new Fx.Tween( this.ulObj, {property: 'left', duration: this.animateDuration });
				this.scroll = new Fx.Styles( this.ulObj, { duration: this.animateDuration, transition: Fx.Transitions.quadOut, onComplete: this.moveToBegin.bind( this ) } );//
			}
			//this.scroll.start( left_indent );
			this.scroll.start( { 'left': left_indent } );
			//this.moveToBegin.delay( this.animateDuration, this );
			return false;
		},
		
		moveToEnd: function(){
			this.ulObj.getFirst( 'li' ).inject( this.ulObj, 'bottom' );
			this.ulObj.setStyle( 'left', this.left_value );
		},
		
		moveToBegin: function(){
			//alert( this.left_value );
			this.ulObj.getLast( 'li' ).inject( this.ulObj, 'top' );
			this.ulObj.setStyle( 'left', this.left_value );
			//this.ulObj.setStyle( 'left', -this.item_width );
		}
	
	});
	carouselClass.implement(new Events);
	carouselClass.implement(new Options);

	window.addEvent('domready', function() {
		if( $('mainCatalogCarousel') )
		{
			var carousel1 = new carouselClass( $('mainCatalogCarousel'), 1, 'carousel' );
		}
	});

	window.addEvent('domready', function() {
		if( $('mainPageCarousel') )
		{
			var carousel1 = new carouselClass( $('mainPageCarousel'), 3, 'maincarousel' );
		}
	});


