/**
 * @author POP webdev [tw+cn]
 * @classDescription Project-specific class to manage an expanding effect of a list of promos.
 * @return {Object}	Returns an new instance.
 * This class depends on Prototype v1.6 and Scriptaculous effects.
 */
var PromoExpando = Class.create ({
	initialize: function(elPromoList, options)
	{
		this.options = Object.extend({
			expandedStyle: {width: '490px'}, 
			contractedStyle: {width: '169px'},
			contractedBGImageClassName: 'pe_bg_contracted'
		}, options || {});

		this.promoList = $(elPromoList);
		if(!this.promoList){return;} // element check
		
		this.promoListID = this.promoList.identify();

		this.promos = this.promoList.select('li');
		this.promos.each(function(promo){
			// get the original calculated width
			promo._originalWidth = promo.getWidth() -1;//  -1 = prototype/scriptaculous adds 1px border to elements calculated width, need to subtract -1px)
		});

		this._activeIndex = -1;

		// Attach an event handler to the ul
		this.promoList.observe('mouseover', this.__ListOver.bindAsEventListener(this));
		this.promoList.observe('mouseout', this.__ListOut.bindAsEventListener(this));

		// create selector/style pair objects to pass to transform
		var oContractLi = {};
		oContractLi['ul#' + this.promoListID + ' li:not([class~=selected])'] = this.options.contractedStyle;
		var oContractBG = {};
		oContractBG['ul#' + this.promoListID + ' li:not([class~=selected]) div.' + this.options.contractedBGImageClassName] = 'opacity: 1.0';
		var oExpandLi = {};
		oExpandLi['ul#' + this.promoListID + ' li.selected'] = this.options.expandedStyle;
		var oExpandBG = {};
		oExpandBG['ul#' + this.promoListID + ' li.selected div.' + this.options.contractedBGImageClassName] = 'opacity: 0';

		this.fxScopeID = 'scope_' + this.promoListID;
		var boundFxCancel = this._cancelEfxInQueue.bind(this);
		
		// store transformations (multiple effects in parallel than can be played)
		this.transformation = new Effect.Transform(
			[
				oContractLi,
				oContractBG,
				oExpandLi,
				oExpandBG
			], {
					queue: {position:'front', scope: this.fxScopeID, limit: 1}, 
					duration: 0.55, 
					transition: Effect.Transitions.sinoidal,
					fps: 50,
					//delay: .1,
					beforeStart: boundFxCancel
				}
		);
		
		var oResetLi = {};
		oResetLi['ul#' + this.promoListID + ' li'] = this.options.contractedStyle; // this width will be overwritten with it's unique slot width before effect setup
		var oResetBG = {};
		oResetBG['ul#' + this.promoListID + ' li div.' + this.options.contractedBGImageClassName] = 'opacity: 1.0';
		
		this.resetTansformation = new Effect.Transform(
			[	
				oResetLi,
				oResetBG
			], { 
					duration: 0.55,
					fps: 50,
					queue: {position:'end', scope: this.fxScopeID, limit:2},
					beforeSetup:function(fxInstance){
						fxInstance.effects.each(function(fx){
							// overwrite contracted width with original width for li elements
							if(fx.element._originalWidth){
								fx.style.set('width', fx.element._originalWidth + 'px');
							}
						});
					}
				}
		);

		// set initial state
		this.resetPromos();
	},

	__ListOver: function(e){
		var element = Event.findElement(e, 'li');
		if(element){
			var promoIndex = this.promos.indexOf(element);
			this._activeIndex = promoIndex;
			
			// add/remove selected class
			for(var i=0, len = this.promos.length; i < len; i++){
				var idx = this.promos.indexOf(this.promos[i]);
				(idx > -1 && idx == promoIndex) ? this.promos[i].addClassName('selected'): this.promos[i].removeClassName('selected');
			}
			
			this.expandPromo(promoIndex);
		}
	},

	__ListOut: function(e){
		this._activeIndex = -1;
		this.resetPromos.bind(this).delay(.1);
	},

	expandPromo: function(index){
		//console.log('expanding');
		this.transformation.play();
	},

	resetPromos: function(){
		if(this._activeIndex != -1){return;}

		//console.log('resetting');
		this.resetTansformation.play();
	},
	
	_cancelEfxInQueue: function(fxInstance){
		// console.log('cancelling');
		Effect.Queues.get(this.fxScopeID).invoke('cancel');
	}

});

/**
 * @author POP webdev [tw]
 * @classDescription Extends Scroller, by adding pagination.
 * @return {Object}	Returns an new instance.
 * This class depends on Scroller, Prototype v1.6, and Scriptaculous effects.
 */
var PromoScroller = Class.create(Scroller, {
	initialize: function($super, elFrame, elPrev, elNext, options){
		$super(elFrame, options);
		
		this.lnkPrev = elPrev;
		this.lnkNext = elNext;
		this.lnkPrev.observe('click', this.__previousClick.bindAsEventListener(this));
		this.lnkNext.observe('click', this.__nextClick.bindAsEventListener(this));
		
		//this._slideInOnInit.bind(this).defer();
		this._slideInOnInit.bind(this).delay(1);
	},
	
	_slideInOnInit: function(){
		this.moveToFirst();
		this.adjustPagingLinks();
	},
	
	__previousClick: function(e){
		e.stop();
		this.moveToPrevious();
		this.adjustPagingLinks();
	},

	__nextClick: function(e){
		e.stop();
		
		this.moveToNext();
		this.adjustPagingLinks();

	},

	adjustPagingLinks: function(){
		var pageCount = this.getSlideCount();
		var nextIndex = this.currentSlideIndex +1;

		var bDisableNext = (nextIndex) >= pageCount;
		if(bDisableNext){
			this.lnkNext.addClassName('disabled');
			this.lnkNext.setOpacity(0.25);
		}
		else{
			this.lnkNext.removeClassName('disabled');
			this.lnkNext.setOpacity(1);
		}
				
		var prevIndex = this.currentSlideIndex -1;
		var bDisablePrev = (prevIndex < 0);
		if(bDisablePrev){
			this.lnkPrev.addClassName('disabled');
			this.lnkPrev.setOpacity(0.25);
		}else{
			this.lnkPrev.removeClassName('disabled');
			this.lnkPrev.setOpacity(1);
		}
	}
	
});


/* firebugx.js    */
/******************/
// prevent errors from console calls to browsers that don't provide firebug's debugging console.
if (!window.console || !console.firebug)
{
    var names = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml",
    "group", "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"];

    window.console = {};
    for (var i = 0; i < names.length; ++i)
        window.console[names[i]] = function() {}
}
