/*  BannerController (requires Prototype javascript framework, scriptaculous optional for effects)
 *  Jeff Weakland
 *  11/15/2010
 *  Version 1.0
 *--------------------------------------------------------------------------*/

var BannerController = Class.create({
	initialize: function (initObj) {		
		/** required initialization parameters **/
		this._bannerArray = initObj.bannerArray;
		
		/** optional initialization parameters **/
		this._bannerDelay = initObj.delay != null ? initObj.delay : 5000;
		this._autoStart = initObj.autoStart != null ? initObj.autoStart : true;
		this._enableEffects = initObj.enableEffects != null ? initObj.enableEffects : false;
		this._effectDuration = initObj.effectDuration != null ? initObj.effectDuration : 0.7;
					
		/** private for internal use only **/
		this._activeButton = null;
		this._activeBanner = null;
		this._activeBannerInfo = null;		
		this._currentBannerPosition = 0;
		this._timer = null;
									
		document.observe('dom:loaded', this._initBanner.bind(this));																				
	},
	
	_initBanner: function() {
		this._activeBannerInfo = this._bannerArray.first();
		this._activeButton = $(this._activeBannerInfo.keys().first());
		this._activeBanner = $(this._activeBannerInfo.get(this._activeButton.id));		
				
		if (this._autoStart) {			
			this._bindMouseEvents();
			this._autoUpdateBanner();			
		}
	},
	
	_switchTab: function(button) {											
		clearTimeout(this._timer);		
		if (this._activeButton.id != $(button).id) {								
			this._doChangeBanner(button);
		}												
	},

	_doChangeBanner: function(button) {				
		this._activeButton.removeClassName('selected');						
		var buttonObj = $(button);
		var bannerInfo = this._getBannerInfo(buttonObj.id);
		var bannerObj = $(bannerInfo.get(buttonObj.id));
		
		buttonObj.addClassName('selected');						
		this._activeButton = buttonObj;
				
		this._hideBanner(this._activeBanner);				
		this._showBanner(bannerObj);
		
		this._activeBanner = bannerObj;																																				
	},
	
	_hideBanner: function(bannerObj) {
		if (this._enableEffects) {
			try {
				// cancel any effects currently being rendered
				var queue = Effect.Queues.get('hideQueue');
				queue.each(function(effect) { 
					if (effect.element.id != bannerObj.id) {
						effect.cancel(); 
						effect.element.hide();
						//console.log('cancelled hiding ' + effect.element.id);
					}
				});
				
				bannerObj.fade({
					duration: this._effectDuration, 					
					queue: { position: 'front', scope: 'hideQueue' }
				});				
			}catch(e) {
				// scriptaculous probably isn't loaded show just hide the banner without effects
				bannerObj.hide();
			}			
		}else {
			bannerObj.hide();
		}
	}, 
	
	_showBanner: function(bannerObj) {
		if (this._enableEffects) {
			try {
				// cancel any effects currently being rendered
				var queue = Effect.Queues.get('showQueue');
				queue.each(function(effect) { 
					if (effect.element.id != bannerObj.id) {
						effect.cancel();
						//console.log('cancelled showing ' + effect.element.id);
					}
				});
												
				bannerObj.appear({					 
					duration: this._effectDuration,										
					queue: { position: 'front', scope: 'showQueue' }
				});
			}catch(e) {
				// scriptaculous probably isn't loaded show just hide the banner without effects
				bannerObj.show();
			}			
		}else {
			bannerObj.show();
		}
	},

	_autoUpdateBanner: function() {		
		this._activeBannerInfo = this._getNextBannerInfo();
		var nextButton = this._activeBannerInfo.keys().first();
		
		var _self = this;
		this._timer = setTimeout(function() { _self._doChangeBanner(nextButton); _self._autoUpdateBanner(); }, this._bannerDelay);												
	},		

	_getBannerInfo: function(key) {
		var bannerInfo = null;
		var i=0;
		var _self = this;
		this._bannerArray.each(function(item) {				
			if (item.keys().first() == key) {
				bannerInfo = item;
				_self._currentBannerPosition = i;
				$break;
			}
			i++;
		});
		return bannerInfo;
	},		

	_getNextBannerInfo: function() {			
		if (this._currentBannerPosition == this._bannerArray.size()-1) {
			this._currentBannerPosition = 0;
		}else {
			this._currentBannerPosition += 1;
		}			
		return this._bannerArray[this._currentBannerPosition];
	},
		
	_resumeAutoUpdateBanner: function(currentPosition) {	
		this._currentBannerPosition = currentPosition;
		this._autoUpdateBanner();		
	},
	
	_bindMouseEvents: function() {
		var _self = this;		
		this._bannerArray.each(function(item, position) {
			var objId = item.keys().first();			
			var buttonObj = $(objId);
			
			buttonObj.observe('mouseenter', function() {				
				_self._switchTab(buttonObj);				
			});
			buttonObj.observe('mouseleave', function() {				
				_self._resumeAutoUpdateBanner(position);				
			});
			
			var bannerId = item.values().first();
			var bannerObj = $(bannerId);
			
			bannerObj.observe('mouseenter', function() {				
				clearTimeout(_self._timer);				
			});
			bannerObj.observe('mouseleave', function() {				
				_self._resumeAutoUpdateBanner(position);							
			});						
		});
	}
	
});

