//-----------------------------------//
// MARQUEE WIDGET CLASS DEFINITION
//-----------------------------------//
(function(scope, name) {
	//create a base class
	var $ = window[scope], c = function(wrapper, options) {
		if(this.__construct) {
			this.__construct(wrapper, options || {}); //call the construct
		}
	};
	
	/* Base Class Definition
	-------------------------------------*/
	c.prototype = (function() {
		var p =  c.prototype;  //scope
		
		/* Private Properties
		-------------------------------------*/
		var _this = null;
		
		/* Construct
		-------------------------------------*/
		p.__construct = function(wrapper, options) {
			this.wrapper = $(wrapper);
			options = this.getDefaultOptions(options);
			_this = this;
			
			this.left		= this.wrapper.find(options.leftHandle);
			this.right		= this.wrapper.find(options.rightHandle);
			this.items 		= this.wrapper.find(options.items);
			this.strip 		= this.wrapper.find(options.strip);
			
			this.range		= options.range;
			this.start		= options.start;
			
			this.disableClass	= options.disableClass;
			
			//size per items
			this.outerWidths = [];
			
			this.isAnimating = false;
			
			this.items.each(function() {
				_this.outerWidths.push($(this).outerWidth(true));
			});
			
			//listen to handles
			this.left.click(_eventScrollLeft);
			this.right.click(_eventScrollRight);
			
			//listen to item click
			this.items.click(options.itemClickAction);
		};
		
		/* Public Methods
		-------------------------------------*/
		p.getDefaultOptions = function(options) {
			options 				= options || {};
			options.range 			= options.range || 5;
			options.start 			= options.start || 0;
			options.strip			= options.strip || '.marquee-center ul';
			options.items 			= options.items || 'li';
			options.leftHandle 		= options.leftHandle || '.left-arrow';
			options.rightHandle 	= options.rightHandle || '.right-arrow';
			options.disableClass	= options.disableClass || 'disabled';
			options.itemClickAction	= options.itemClickAction || function(e) {};
			return options;
		};
		
		p.moveToItem = function(index) {
			//if the initial end point is less than 0 
			if(index < 0) {
				//make the initial end point to be 0
				index = 0;
			} else if(index > (_this.outerWidths.length - 1)) {
				//do nothing because the current frame is already in the max frame
				return;
			}
			
			//add the width of each point leading up to the end point
			var position = 0;
			for(var i = 0; i < index; i ++) {
				position -= _this.outerWidths[i];
			}
			
			//start animating only if it 
			//is not currently animating
			if(!_this.isAnimating) {
				//set flag to true
				_this.isAnimating = true;
				//move the strip to that position
				_this.strip.animate({left:position}, 'fast', function() {
					//set flag to false
					_this.isAnimating = false;
					//make the end point the new start point
					_this.start = index;
					
					//now calc the disabled class
					var min = _this.start - _this.range;
					var max = _this.start + _this.range;
					//if the initial end point is less than 0 
					if(min < 0) {
						//set the left handle to disabled
						_this.left.addClass(_this.disableClass);
					} else {
						//unset the left handle to disabled
						_this.left.removeClass(_this.disableClass);
					} 
					
					if(max > (_this.outerWidths.length - 1)) {
						//set the right handle to disabled
						_this.right.addClass(_this.disableClass);
					} else {
						//unset the right handle to disabled
						_this.right.removeClass(_this.disableClass);
					}
				});
			}
		};
		
		/* Private Methods
		-------------------------------------*/
		var _eventScrollLeft = function(e) {
			//stop default actions
			e.stopPropagation();
			e.preventDefault();
			
			//use current start minus the range as the initial end point. 
			_this.moveToItem(_this.start - _this.range);
		};
		
		var _eventScrollRight = function(e) {
			//stop default actions
			e.stopPropagation();
			e.preventDefault();
			
			//use current start plus the range as the initial end point. 
			_this.moveToItem(_this.start + _this.range);
		};
				
		return p;
	})();
	
	//This is the jQuery adapter
	//so that anytime this method is
	//called it will create a new instance
	//of our actual class
	$.fn.extend(new function() {
		this[name] = function(options, returnInstance) {
			returnInstance = returnInstance || false;
			var instance = new c(this, options || {});
			if(returnInstance) {
				return instance;
			}
			return this;
		}
	});
})('jQuery', 'marquee');