(function($) {
/* Plugin Name:		
	 * Biber Intro Carousel
	 * Programmed by:	Bircan Tuner
	 * Programmed for:	biber Ltd. of Istanbul, Turkiye.
	 * Licensed under:	GPL
	 * 
	 * version:			1.0
	 * 
	 * 
	 * http://www.biberltd.com
	 */
	$.fn.biberintrocarousel = function(options) {
		
	var defaults = {
		autoscroll: 3,
		offset: 1,
		start: 1,
		scroll_animation: 0.1,
		scroll_direction: 'forward',
		scroll_mode: 'circular',
		animation: 'fade',
		show_buttons: true,
		show_player: true,
		show_switcher: true,
		button_play_HTML: 'play',
		button_pause_HTML: 'pause'
	};	
	/*
	* Merge user definedoptions with defaults.
	*/
	var options = $.extend(defaults, options);
	
	return this.each(function() {  
    	var container = $(this);
		create(container, options);
	}); 
	
	}
	
 	/*
     * Creates the carousel and the navigation.
     */
	create = function(container, options) {
		this.container = container;
		this.options = options;
		this.list = null;
		this.listsize = null;
		this.navigation = null;
		this.switcher = null;
		this.buttonNext = null;
		this.buttonPrev = null;
		this.visible = null;
		this.timer = null;
		
		this.list = $(container).find('>ul, div>ul');
		this.list.addClass('intro_carousel_list');
		this.listsize = $(list).find('>li').length;	
		
		if(!this.list.parent().hasClass("intro_carousel_content")) {
			this.list.wrap('<div></div>').parent().addClass('intro_carousel_content');			
		}

		if(this.listsize > 1 && (this.options.show_player || this.options.show_switcher || this.options.show_buttons)) {
			
			this.navigation = this.list.after('<div></div').next();
			this.navigation.addClass('intro_carousel_navigation');
			
			/*
     		 * Creates switcher wrap (switcher and buttons).
     		 */
			if(this.options.show_switcher || this.options.show_buttons) {
				
				this.navigation.append('<div class="intro_carousel_switcher_wrap"></div');
				this.switcher_wrap = this.navigation.find('.intro_carousel_switcher_wrap');
				
				/*
 			 	 * Creates buttons.
 			 	 */
				if(this.options.show_buttons) {
					this.switcher_wrap.append('<div class="previous"></div>');
					this.buttonPrev = this.navigation.find('.previous');
					this.switcher_wrap.append('<div class="next"></div>');
					this.buttonNext = this.navigation.find('.next');
				}
				
				/*
				 * Creates switcher
				 */
				if(this.options.show_switcher) {
					if(this.options.show_buttons) {
						this.switcher = this.buttonPrev.after('<ul style="display:none"></ul>').next();
						this.switcher.addClass('intro_carousel_switcher');
						var i = null;
						for(i = 0; i < this.listsize; i++) {
							this.switcher.append('<li><a href="#"></a></li>');
						}
					}
					else {
						this.switcher_wrap.append('<ul class="intro_carousel_switcher"></ul>');
						this.switcher = this.navigation.find('ul.intro_carousel_switcher');
					}
				}
				
				this.switcher_wrap.append('<div class="clear"></div>');
				
				if(this.options.show_player && this.options.autoscroll != 0) {
					this.navigation.append('<div class="intro_carousel_player"><a href="#"></a></div>');
					this.buttonPlayer = this.navigation.find('.intro_carousel_player>a');
					
					this.buttonPlayer.addClass('pause');						
					this.buttonPlayer.html(this.options.button_pause_HTML);
					
				}
								
			}
		}
		
		setup();
	}
	
	setup = function() {
		
		this.animating = false;
		
		this.visible = this.options.start;
		var width = 0;
		
		if(this.listsize > 0) {
			var i = this.options.offset;
			var j = this.options.offset;
			var li = this.list.children('li');
			var switcher_li = this.switcher.children('li');
			
			li.each(function() {
				format(this, i++);
				width += $(this).width();
			});
			
			switcher_li.each(function() {
				formatSwitcher(this, j++);
			}); 
			this.list.css('width', width);	
		}
		
		this.buttonNext.bind('click', function(event){
			stopAuto();
			togglePlayer();
			forward();
			event.preventDefault();
		});

		this.buttonPrev.bind('click', function(event){
			stopAuto();
			togglePlayer();
			backward();
			event.preventDefault();
		});
		
		var self = this;
		
		if(this.options.show_player) {
			
			this.buttonPlayer.bind('click', function(event){
				
				if(self.options.show_player) {
					if($(this).hasClass('play')) {
						$(this).removeClass('play');
						$(this).addClass('pause');
						$(this).html(self.options.button_pause_HTML);
						startAuto();
					}
					else if($(this).hasClass('pause')) {
						$(this).removeClass('pause');
						$(this).addClass('play');
						$(this).html(self.options.button_play_HTML);
						stopAuto();
					}	
				}	
				event.preventDefault();
			});	
		}
		
		startAuto();
	}
	
	/*
	 * This function moves the carousel forwards.
	 */
	forward = function() {
		if(this.animating) {
			return;
		}
		
		animate(move(this.visible + 1));
		
	}
	
	/*
	 * This function moves the carousel backwards.
	 */
	backward = function() {
		if(this.animating) {
			return;
		}
		
		animate(move(this.visible - 1));
		
	}
	
	move = function(index) {
		if(this.animating) {
			return;
		}
		
		return(pos(index));
	}
	
	/*
	 * This function gives the position of the carousel item
	 */
	pos = function(index) {
		if(this.animating) {
			return;
		}
		
		var pos = 0;
		this.templist = null;
		
		var forward = index > this.visible;
		
		if(this.options.scroll_mode == 'circular' && (index > this.listsize || index == 0)) {
			
			this.templist = this.list.html();
			this.list.find('.intro_carousel_item').addClass('remove');
			this.list.css('width', this.list.width() * 2);
			
			if(forward) {
				this.list.append(this.templist);
			}
			else {
				this.list.prepend(this.templist);
			}
		}
		
		this.visible = index > this.listsize ?  1 : ((index != 0) ? index : this.listsize);
		
		this.listsize = this.list.find('>li').length;	// Dont forget to update
		
		pos = -((index - 1) * (this.list.width() / this.listsize));
		
		if(pos > 0) {
			pos -= this.list.width() / 2;
			this.list.css("left", pos - (this.list.width() / this.listsize));
		}
		
		return pos;

	}
	
	animate = function(pos) {
			
		this.animating = true;
		var self = this;	
		
		var animateCallback = function() {
			if(self.templist != null) {
				$('.remove').remove();
			
				pos = -((self.visible - 1) * (self.list.width() / self.listsize));
				
				self.list.css('left', pos);	
				self.listsize = self.list.find('>li').length;
				self.list.css('width', self.list.width() / 2);
				self.templist == null;	
			}
			self.animating = false;
	
			$("a.selected").removeClass('selected');
			$("a[introcarouselindex = '"+self.visible+"']").addClass('selected');
			
			this.list = null;
			this.list = $(container).find('>ul, div>ul');
			
			if(self.timer != null) {
				startAuto();
			} 
		}	

		if(this.options.animation == 'slideHorizontal') {
			this.list.animate({left: pos}, this.options.scroll_animation * 1000, 'swing', animateCallback);	
		}	
		
		else if(this.options.animation == 'fade') {
			this.list.fadeOut().animate({left: pos}, this.options.scroll_animation * 1000, 'swing', animateCallback).fadeIn();	
		}
		
		else if(this.options.animation == 'slideVertical') {
			this.list.slideUp().animate({left: pos}, this.options.scroll_animation * 1000, 'swing', animateCallback).slideDown();	
		}
	}
	
	startAuto = function(s) {
		
		if(s != undefined) {
			this.options.auto = s;
		}
		
		if(this.options.autoscroll == 0) {
			return stopAuto();
		}
		
		this.timer = setTimeout(function(){
			this.options.scroll_direction == 'forward' ? forward() : backward();
		}, this.options.autoscroll * 1000);
	}
	
	stopAuto = function() {
		clearTimeout(this.timer);
		this.timer = null;
	}
	
	/*
	 * This function formats all of the list items in the carousel and sets index for them.
	 */
	format = function(li, i) {
	
		$(li).addClass('intro_carousel_item').attr('introcarouselindex', i);
		
	}
	
	formatSwitcher = function(li, i) {
		var self = this;
		
		if(this.visible == i) {
			$(li).find("a").addClass("selected");
		}
		
		$(li).find("a").attr('introcarouselindex', i).append(i).bind('click', function(event){
			if(self.animating) {
				return;
			}
			stopAuto();	
			togglePlayer();
			animate(move(parseInt($(this).attr('introcarouselindex'))));
			event.preventDefault();
		});
	}
	
	togglePlayer = function() {
		if(this.options.show_player) {
			if(this.buttonPlayer.hasClass('pause')) {
				this.buttonPlayer.removeClass('pause');
				this.buttonPlayer.addClass('play');
				this.buttonPlayer.html(self.options.button_play_HTML);
			}	
		}	
		else {
			return;
		}
	}
	
})(jQuery);