dojo.provide("Carousel");

dojo.declare("Carousel", null, 
{
// parameters
	containerQuery : '> :first-child',
	disabledClass : 'scrollDisabled',
	childrenQuery : '> DIV',
	prev : '',
	next : '',
	duration: 400,
	easing : null,

	srcNodeRef: null,

	containerNode : null,
	visibleWidth : 0,

	prevNode: null,
	nextNode: null,
	prevC: null,
	nextC: null,

	pager : false,
	pagerQuery : '> A',
	pagerContainer : null,
	pagerChildren : [],
	pagerSelectedClass : 'selected',
	
	_pagerSelected : 0,

	childrenNodes : [],
	childrenWidth : 0,
	childMarginBox : {},
	visibleChildren : 0,

	enabled : true,

	_anim : null,
	_createFirst : true,
	_createLast : false,
	_currentIndex: 0,
	_currentPosition: 0,
	_zeroPosition: 0,

	
	constructor : function(params, srcNodeRef)
	{
		dojo.mixin(this, params);
		
	// contents 
		
		this.srcNodeRef = dojo.byId(srcNodeRef);
		
		this.containerNode = dojo.query(this.containerQuery, this.srcNodeRef)[0];

		this.childrenNodes = this._getChildren();
		
		if (this.childrenNodes.length > 0) 
		{
			this.childMarginBox = dojo.marginBox(this.childrenNodes[0]);

			this.childrenWidth = this.childrenNodes.length * this.childMarginBox.w;

			dojo.style(this.containerNode, { 'width' : this.childrenWidth+'px' } );
			
			this.visibleWidth = dojo.marginBox(this.srcNodeRef).w;
//			this.visibleWidth = dojo.style(this.srcNodeRef, 'width');
			
			this.visibleChildren = this.visibleWidth / this.childMarginBox.w;
		}

	// controls 
		
		if (this.next) this.nextNode = dojo.byId(this.next);
		if (this.prev) this.prevNode = dojo.byId(this.prev);

		this.pagerContainer = this.pager ? dojo.byId(this.pager) : false;
		
		
		if (this.pagerContainer) this.pagerChildren = dojo.query(this.pagerQuery, this.pagerContainer);

		
		this.enabled = this.childrenWidth > this.visibleWidth;

		if (this.enabled) 
		{
			if (this.next && this.prev)
			{
				this.prevC = dojo.connect(this.prevNode, "onclick", this, "slideLeft");
				this.nextC = dojo.connect(this.nextNode, "onclick", this, "slideRight");
	
				if (this.disabledClass)
				{
					dojo.removeClass(this.prevNode, this.disabledClass);
					dojo.removeClass(this.nextNode, this.disabledClass);
				}
			}

			this._createLast = this.childrenNodes > this.visibleChildren;
			
			if (this.pagerContainer) 
			{
				dojo.removeClass(this.pagerContainer, this.disabledClass);
				
				dojo.forEach(this.pagerChildren, function(item) 
				{
					dojo.connect(item, "onclick", this, "slideTo")
				}, this);
			}
		}
		else 
		{
			if (this.next) dojo.destroy(this.prevNode);
			if (this.prev) dojo.destroy(this.nextNode);
			
			if (this.pagerContainer) dojo.destroy(this.pagerContainer);
		}
	},

	slideLeft : function(e)
	{
		if (this._createFirst)
		{
			children = this._getChildren();
			
			var clonePosition = this.childrenNodes.length - 1;
			
			var lastchild = children[clonePosition];
// console.log(lastchild);
			dojo.place(lastchild, this.containerNode, 'first');

			dojo.style(this.containerNode, { 'left' : dojo.style(this.containerNode, 'left')-this.childMarginBox.w+'px' } );
		}
		else 
		{
			this._currentIndex--;
		}

		if (this.pagerContainer) 
		{
			dojo.removeClass(this.pagerChildren[this._currentPosition], this.pagerSelectedClass);
		}
		
		if (this._currentPosition > 0) this._currentPosition--; else this._currentPosition = this.childrenNodes.length-1;
		
		if (this._createFirst) 
		{
			this._zeroPosition++;
			
			if (this._zeroPosition > this.childrenNodes.length - 1) this._zeroPosition = 0; 
		}
		
		this._createFirst = true;
		this._createLast = false;
		
//		console.log(this._currentPosition);

		this.slide(e);
	},

	slideRight : function(e)
	{
		if (this._createLast)
		{
			var children = this._getChildren(), firstchild = children[0];
// console.log(firstchild);
			dojo.place(firstchild, this.containerNode, 'last');

			dojo.style(this.containerNode, { 'left' : dojo.style(this.containerNode, 'left')+this.childMarginBox.w+'px' } );
		}
		else
		{
			this._currentIndex++;
		}

		if (this.pagerContainer) 
		{
			dojo.removeClass(this.pagerChildren[this._currentPosition], this.pagerSelectedClass);
		}
		
		if (this._currentPosition < this.childrenNodes.length-1) this._currentPosition++; else this._currentPosition = 0;
		
		if (this._createLast) 
		{
			this._zeroPosition--;
			
			if (this._zeroPosition < 0) this._zeroPosition = this.childrenNodes.length - 1; 
		}
		
		this._createLast = true;
		this._createFirst = false;
		
//		console.log(this._currentPosition, this._zeroPosition);
		
		this.slide(e);
	},
	
	slideTo : function(e)
	{
		var link = e.currentTarget, pageIndex = 0;
		
		if (dojo.hasClass(link, this.pagerSelectedClass)) return dojo.stopEvent(e);
		
		var page = dojo.filter(this.pagerChildren, function(item,i) 
		{ 
			var found = item.href == link.href; 
			if (found) pageIndex = i; 
			return found; 
		})[0];
		
//		if (dojo.this.childrenNodes[this._currentPosition].href == link.href) return false;
		
		dojo.removeClass(this.pagerChildren[this._currentPosition], this.pagerSelectedClass);
		
		
		this._currentPosition = pageIndex;
		
		this._currentIndex = Math.abs(this._zeroPosition - pageIndex);
		
		if (pageIndex == 0) 
		{ 
			this._createFirst = true;
			this._createLast = false;
		}
		else if (pageIndex == this.childrenNodes.length-1) 
		{
			this._createFirst = false;
			this._createLast = true;
		}
		
		this.slide(e);
	},
	
	slide : function(e)
	{
		dojo.stopEvent(e);
		
//		console.log(this._currentIndex, this._currentPosition, this._zeroPosition);
		
		if (this.pagerContainer) 
		{
			dojo.addClass(this.pagerChildren[this._currentPosition], this.pagerSelectedClass);
		}
		
		if (this._anim) this._anim.gotoPercent(1, false);
		
		this._anim = this.getSlideAnim( - this._currentIndex * this.childMarginBox.w );
		
		this._anim.play();
	},

/**
 * custom metoda pro animaci
 * 
 * returns @dojo._Anim
 */
	getSlideAnim : function(endpx)
	{
//		console.info(endpx);
		
		var self = this;
		
		return dojo.animateProperty(
		{
			node: self.containerNode,
			duration: self.duration,
			properties: {
				left: { end: endpx, unit:"px" }
			},
			easing: self.easing
		});
	},

	_getChildren : function()
	{
//		var queryChildren = this.childrenQuery!='' ? this.childrenQuery : '> div';

		return dojo.query(this.childrenQuery, this.containerNode);
	}
});
