/**
 * @original author Stéphane Roucheray 
 * @strongly modified by Felix Manea (www.ever.ro)
 * @extends $
 */


(function($) {

	$.fn.carousel = function(options){
		if(typeof(options) == "object"){
			return this.each(function() {
				if(typeof(options.op) == 'undefined') new $carousel(this, options);
				else{
					switch(options.op){
						case 'jump':
							var carousel = $(this).data('carousel');
							if(carousel){
								carousel.jumpto(options.jumpTo, true);
							}
						break;
					}
				}
			});
		}
	};

	// Default configuration properties.
	var defaults = {
			autoPlay: 1,
			autoTimer: 2000,
			buttonNext: null,
			buttonPrev: null,
			buttonPlay: null,
			buttonStop: null,
			buttonTogglePlay: null,
			buttonTogglePlayText: {play: 'play', stop: 'stop'},
			captionBox: null
	};

	$.carousel = function(e, o){
		var self = this;
		// Add a reverse reference to the DOM object
		$(e).data("carousel", this);


		this.options = $.extend({}, defaults, o || {});
		this.buttonNext = $(this.options.buttonNext)?$(this.options.buttonNext):null;
		this.buttonPrev = $(this.options.buttonPrev)?$(this.options.buttonPrev):null;
		this.buttonPlay = $(this.options.buttonPlay)?$(this.options.buttonPlay):null;
		this.buttonStop = $(this.options.buttonStop)?$(this.options.buttonStop):null;
		this.buttonTogglePlay = $(this.options.buttonTogglePlay)?$(this.options.buttonTogglePlay):null;
		this.list = $(e);
		this.sliderList = $(e).children()[0];
		this.timer = null;
		
		if(this.sliderList) {
			this.increment = $(this.sliderList).children().outerWidth("true");
			this.elmnts = $(this.sliderList).children();
			this.imgs = $(this.elmnts).children('img');
			this.numElmts = this.elmnts.length;
			this.sizeFirstElmnt = this.increment;
			this.shownInViewport = Math.round($(e).width() / this.sizeFirstElmnt);
			this.firstElementOnViewPort = 1;
			this.isAnimating = false;
			
			for (i = 0; i < this.shownInViewport; i++) {
				$(this.sliderList).css('width',(this.numElmts+this.shownInViewport)*this.increment + this.increment + "px");
				$(this.sliderList).append($(this.elmnts[i]).clone());
			}
		}
		
		this.funcPrev   			= function() { self.prev(true); };
		this.funcNext   			= function() { self.next(true); };
		this.funcPlay   			= function() { self.play(true, 0); };
		this.funcStop  				= function() { self.stop(); };
		this.funcTogglePlay   = function() { self.toggleplay(0); };
			
		if(this.options.buttonPrev != null) $(this.options.buttonPrev).click(this.funcPrev);
		if(this.options.buttonNext != null) $(this.options.buttonNext).click(this.funcNext);
		if(this.options.buttonPlay != null) $(this.options.buttonPlay).click(this.funcPlay);
		if(this.options.buttonStop != null) $(this.options.buttonStop).click(this.funcStop);
		if(this.options.buttonTogglePlay != null) $(this.options.buttonTogglePlay).click(this.funcTogglePlay);
		
		this.options.autoPlay = this.options.autoPlay?0:1; 
		this.toggleplay();
		this.showcaption();
	}

	// Create shortcut for internal use
	var $carousel = $.carousel;

	$carousel.fn = $carousel.prototype = {
			carousel: '0.0.1'
	};

	$carousel.fn.extend = $carousel.extend = $.extend;

	$carousel.fn.extend({
		next: function(stop) {
			var self = this;
			if(stop) this.stop();			
			else this.play(false);
			if (!this.isAnimating) {
				if (this.firstElementOnViewPort > this.numElmts) {
					this.firstElementOnViewPort = 2;
					$(this.sliderList).css('left', "0px");
				}
				else {
					this.firstElementOnViewPort++;
				}
				$(this.sliderList).animate({
					left: "-=" + this.increment,
					y: 0,
					queue: true
				}, "swing", function(){self.isAnimating = false;});
				this.isAnimating = true;
			}
			this.showcaption();
		},

		prev: function(stop) {			
			var self = this;
			if(stop) this.stop();			
			else this.play(false);
			if (!this.isAnimating) {
				if (this.firstElementOnViewPort == 1) {
					$(this.sliderList).css('left', "-" + this.numElmts * this.sizeFirstElmnt + "px");
					this.firstElementOnViewPort = this.numElmts;
				}
				else {
					this.firstElementOnViewPort--;
				}
				
				$(this.sliderList).animate({
					left: "+=" + this.increment,
					y: 0,
					queue: true
				}, "swing", function(){self.isAnimating = false;});
				this.isAnimating = true;
			}
			this.showcaption();
		},
		
		play: function(start, customTimer) {
			if(this.options.buttonTogglePlay != null)	$(this.options.buttonTogglePlay).text(this.options.buttonTogglePlayText.stop);

			var self = this;
			if(this.options.autoPlay == 0 && start) this.options.autoPlay = 1;
			if(this.options.autoPlay == 0) return;

			if(this.timer != null) return;
			if(typeof(customTimer) == 'undefined') customTimer = this.options.autoTimer;
			
			this.timer = setTimeout(function() { clearTimeout(self.timer); self.timer = null; self.next(false);}, customTimer);
		},
		
		stop: function() {
			if(this.options.buttonTogglePlay != null)	$(this.options.buttonTogglePlay).text(this.options.buttonTogglePlayText.play);
			
			this.options.autoPlay = 0;

			if(this.timer == null) return;

			clearTimeout(this.timer);
			this.timer = null;
		},
		
		toggleplay: function(customTimer) {
			if(this.options.autoPlay == 0){
				if(typeof(customTimer) == 'undefined') customTimer = this.options.autoTimer;
				this.play(true, customTimer);
			}
			else{
				this.stop();
			}
		},
		
		jumpto: function(imgNum, stop) {
			var self = this;
			if(stop) this.stop();			
			else this.play(false);
			//this.firstElementOnViewPort = imgNum;
			if (!this.isAnimating) {
				if (imgNum > this.numElmts || imgNum < 1) {
					imgNum = 1;
					$(this.sliderList).css('left', "0px");
				}
				//alert(imgNum+' '+this.firstElementOnViewPort);
				var customIncrement = imgNum - this.firstElementOnViewPort;
				this.firstElementOnViewPort = imgNum;
				$(this.sliderList).animate({
					left: "-=" + customIncrement * this.increment,
					y: 0,
					queue: true
				}, "swing", function(){self.isAnimating = false;});
				this.isAnimating = true;
			}
			this.showcaption();
		},
		
		showcaption: function(imgNum) {
			if(typeof(imgNum) == 'undefined') imgNum = this.firstElementOnViewPort;
			//alert(imgNum+' '+this.imgs[imgNum]+' '+$(this.imgs[imgNum]).attr('alt'));
			if(this.options.captionBox == null || !(this.imgs[imgNum - 1])) return;
			document.getElementById(this.options.captionBox.substr(1)).innerHTML = $(this.imgs[imgNum - 1]).attr('alt')==''?'&nbsp;':$(this.imgs[imgNum - 1]).attr('alt');
		}
	});
								
})(jQuery);
