(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 _this, p =  c.prototype;  //scope
		
		/* Private Properties
		-------------------------------------*/
		/* Construct
		-------------------------------------*/
		p.__construct = function(wrapper, options) {
			_this = this;
			
			this.wrapper = $(wrapper);
			
			options = this.getDefaultOptions(options);
			
			//options = this.getDefaultOptions(options);
			this.tabs = this.wrapper.find(options.tabs).click(_eventShow);
			
			if(options.play) {
				this.hover 				= false;
				this.fadeInAnimation 	= false;
				this.fadeOutAnimation 	= false;
				
				this.wrapper.hover(function() {
					_this.hover = true;
				}, function() {
					_this.hover = false;
				});
				
				this.active = options.start;
				
				//start playing now
				setInterval(function() {
					if(_this.hover == false) {
						var activeQueue = _this.active + 1;
						if(activeQueue >= _this.tabs.size()) {
							activeQueue = 0;
						}
						
						var activeTab = _this.tabs.eq(_this.active);
						var activeContent = _getTarget(activeTab);
						var queueTab = _this.tabs.eq(activeQueue);
						var queueContent = _getTarget(queueTab);
						
						_this.fadeOutAnimation = _fadeOut(activeContent.get(0), function() {
							activeContent.addClass('hide');
							activeTab.removeClass('selected');
							_this.fadeOutAnimation = false;
						}, 50);
						
						_this.fadeInAnimation = _fadeIn(queueContent.removeClass('hide').get(0), function() {
							_this.active = activeQueue;
							queueTab.addClass('selected');
							_this.fadeInAnimation = false;
						}, 50);
					}
				}, options.interval);
			}
		};
		
		/* Public Methods
		-------------------------------------*/
		p.getDefaultOptions = function(options) {
			options = options || {};
			
			options.tabs 		= options.tabs || '.panelNav li';
			options.play 		= options.play == false ? false : true;
			options.interval 	= options.interval || 8000;
			options.start		= options.start || 0;
			
			return options;
		};
		
		
		/* Private Methods
		-------------------------------------*/
		var _getTarget = function(tab) {
			tab = $(tab);
			if(tab.get(0).tagName.toLowerCase() != 'a') {
				tab = tab.find('a');
			}
			
			return $(tab.attr('href'));
		};
		
		/**
		 * Fade in animation effect
		 *
		 * @param Node
		 * @param string callback function
		 * @param int speed
		 * @return int the interval id
		 */
		var _fadeIn = function(el, callback, speed) {
			var opacity = 0;
			callback = callback || function() {};
			
			el.style.opacity = 0;
			el.style.filter = 'alpha(opacity=0)';
			
			speed = speed || 10;
			
			var interval = setInterval(function() {
				el.style.opacity = opacity/10;
				el.style.filter = 'alpha(opacity=' + opacity*10 + ')';
				opacity ++;
				if(opacity == 11)
				{
					clearInterval(interval);
					callback();
				}
				
			}, speed);
			
			return interval;
		};
		
		/**
		 * Fade out animation effect
		 *
		 * @param Node
		 * @param function callback
		 * @param int speed
		 * @return int the interval id
		 */
		var _fadeOut = function(el, callback, speed) {
			var opacity = 10;
			callback = callback || function() {};
			
			speed = speed || 10;
			
			el.style.opacity = 100;
			el.style.filter = 'alpha(opacity=100)';
			
			var interval = setInterval(function() {
				el.style.opacity = opacity/10;
				el.style.filter = 'alpha(opacity=' + opacity*10 + ')';
				opacity --;
				
				if(opacity == -1)
				{
					clearInterval(interval);
					callback();
				}
			}, speed);
			
			return interval;
		};
		
		var _eventShow = function(e) {
			e.preventDefault();
			
			//stop animation
			if(_this.fadeInAnimation) {
				clearInterval(_this.fadeInAnimation);
			}
			
			if(_this.fadeOutAnimation) {
				clearInterval(_this.fadeOutAnimation);
			}
			
			_this.tabs.each(function() {
				//hide all content and fix opacity
				_getTarget(this).addClass('hide').get(0).style.opacity = 1;
				_getTarget(this).addClass('hide').get(0).style.filter = 'alpha(opacity=100)';
				
				//deslect all tabs
				$(this).removeClass('selected');
			});
			
			//show the content
			_getTarget(this).removeClass('hide');
			
			//add class of selected
			$(this).addClass('selected');
			
			_this.active = _this.tabs.index(this);
		};
		
		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) {
			new c(this, options);
			return this;
		}
	});
	
})('jQuery', 'banner');

/* Driver
-------------------------------------*/
jQuery(function() {
 jQuery('#banner .video').banner();
});
