var Rotator = function(options){
	
	var defaults = {
		rotator : $('.slider'),
		rotator_item_selector : 'li',
		selected_class : 'selected',
		switcher : $('.slider_navigation'),
		switcher_item_selector : 'td',
		switcher_item_selected_class : 'selected',
		left_quote_selector : '.left_quote',
		right_quote_selector : '.right_quote',
		marker_selector : '.rotator-marker',
		auto_rotate : true,
		auto_scroll : false,
		slider_timeout : 10000
	}

	this.options = $.extend({}, defaults, options ? options : {});
	
	/* === Setup === */
	this.rotator = this.options.rotator;
	this.rotator_items = this.rotator.find(this.options.rotator_item_selector);
	
	this.selected_class = this.options.selected_class;
	this.switcher_selected_class = this.options.switcher_item_selected_class;
	
	this.rotator_selected_item = this.rotator_items.filter('.' + this.selected_class);
	
	this.rotator_items_amount = this.rotator_items.size();

	this.switcher = this.options.switcher;
	
	this.switcher_items = this.switcher.find(this.options.switcher_item_selector);
	this.switcher_selected_item = this.switcher_items.filter('.' + this.switcher_selected_class);

	
	this.left_quote = this.switcher.find(this.options.left_quote_selector);
	this.right_quote = this.switcher.find(this.options.right_quote_selector);
	this.has_quotes = ( this.left_quote.length || this.right_quote.length ) ? true : false;
	
	this.marker = this.switcher.find(this.options.marker_selector);
	this.markerClass = this.marker.attr('class');
	this.has_marker = ( this.marker.length ) ? true : false;
	
	this.slider_timeout = this.options.slider_timeout;
	
	this.auto_rotate = this.options.auto_rotate;
	this.auto_scroll_allowed = this.options.auto_scroll;
	this.auto_scroll = false; // Don't auto scroll on auto rotation

	/* == Aux vars == */
	this.is_animate = false;
	this.timeout_array = (typeof(this.slider_timeout) === "object") ? true : false;

	/* == Get them! == */
	for( var i = 0; i < this.rotator_items_amount; i++ ){
		new Rotator_item(
			this.rotator_items.eq(i),
			this.switcher_items.eq(i),
			i,
			this
		);
	}

	this.init();
};

Rotator.prototype = {
	init: function(){
		var that = this;

		$(window).resize(function(){
			that.quotes_animation( that.switcher_selected_item, 0 );
		});

		this.selectedIndex = 0;
		
		if(this.auto_rotate) {
			this.init_cycle( (this.timeout_array) ? this.slider_timeout[0] : this.slider_timeout );
		}
		
		this.quotes_animation( that.switcher_selected_item, 0 );
		this.update_marker ( that.switcher_selected_item );
	},

	switching: function(slider_item, switcher_item, switcher_index){
		if( !this.is_animate && this.rotator_selected_item[0] !== slider_item[0] ){
			this.is_animate = true;
			this.selectedIndex = switcher_index;
			
			if(this.timeoutId) {
				window.clearTimeout(this.timeoutId);
			}

			this.update_marker ( switcher_item, 'normal' );
			this.slide_animation(slider_item, switcher_item);
			
			
			this.quotes_animation(switcher_item, 'normal');
			
		}
		
	},
	
	cycle: function(){
		
		var iCycleNext = (this.selectedIndex < this.rotator_items_amount - 1 ) 
		? this.selectedIndex + 1 
		: 0;
		
		this.switching($(this.rotator_items[iCycleNext]), $(this.switcher_items[iCycleNext]), iCycleNext);
	},
	
	init_cycle: function(timeout){
		this.timeoutId = window.setTimeout($.proxy(this.cycle, this), timeout);	
	},
	
	/**
	 * @function scrollUp
	 * @description Scrolls up to show the control band.
	 * 
	 */
	scrollUp: function(){
		if(!$('html, body').is(':animated') && $(window).scrollTop() > this.rotator.offset().top){
			$.scrollTo(this.rotator, 1000);
		}
	},

	slide_animation: function(slider_item, switcher_item){
		var that = this;

		this.rotator_selected_item.after(slider_item);
		
		this.switcher_selected_item.removeClass(that.switcher_selected_class);
		this.switcher_selected_item = switcher_item.addClass(that.switcher_selected_class);
	
		this.rotator_selected_item.animate(
			{
				marginLeft: '-100%'
			},
			function(){
				that.rotator.append( that.rotator_selected_item.removeClass(that.selected_class).css('marginLeft', '') );
				that.rotator_selected_item = slider_item;

				that.is_animate = false;
				if(that.auto_rotate){
					that.init_cycle( (that.timeout_array) ? that.slider_timeout[that.selectedIndex] : that.slider_timout );
				}
				if(that.auto_scroll){
					that.scrollUp()
				}
			}
		);
	},

	quotes_animation: function(switcher_item, time){
		if(this.has_quotes){
			var
				link = switcher_item.find('.pseudo'),
				link_left = Math.round(link.position().left);
	
			this.left_quote.animate({left: link_left + 2}, time);
			this.right_quote.animate({left: link_left + link[0].offsetWidth + 35} , time);
		}
	},
	
	update_marker: function(switcher_item, time){
		if(this.has_marker){
			var
				link = (typeof switcher_item === 'undefined') ? this.switcher_selected_item : switcher_item,
				link_top = Math.round(link.position().top),
				link_height = link.outerHeight();
			
			this.marker.attr("class", this.markerClass + ' ' + link.attr('class'));
			
			this.marker.css ({
				marginTop: link_top,
				height: link_height + 10
			});
			
			//Fix IE7 behavior
			this.marker[0].innerHTML = this.marker[0].innerHTML;
		}
	},
	
	stop: function(){
		window.clearTimeout(this.timeoutId);
		this.auto_rotate = false;
	}
};


var Rotator_item = function(slider_item, switcher_item, number, Rotator){
	this.slider_item = slider_item;
	this.switcher_item = switcher_item;
	this.index = number;
	this.link = this.switcher_item.find('.pseudo');
	this.Rotator = Rotator;

	this.init();
};


Rotator_item.prototype = {
	init: function(){
		var that = this;
		this.link.click(function(){
			that.Rotator.auto_rotate = false;
			that.Rotator.auto_scroll = true;
			that.Rotator.switching(that.slider_item, that.switcher_item, that.index);
		});
	}
};
