var MainPromo = {
	currentPromoElement:null,
	currentNavElement:null,
	count : 0,
	settings:{
		parent : 'promo-navigator-wrapper',
		navIDFormat : 'promoNav',
		promoNavs : 'promo-navs',
		promoBoxes : 'promo-navs-boxes',
		promoClass : '.primary-promo',
		navClass : 'promo-nav',
		selectedClass : 'selected',
		hoverClass : 'hover',
		navWidth : 51,
		effect : {transition: 'bounce:out'},
		rotateDelay : 8000
	},
	rotating: true,
	rotateLimit: 2,
	initialize:function(){
		this.currentPromoElement = $$(this.settings.promoClass)[0];
		this.count = $$(this.settings.promoClass).length;
		
		var weightedIndex = -1;
		$$(this.settings.promoClass).each(function(el,index){
			if ($chk(el.get('weight'))){
				weightedIndex = index;
			}
		});
		if (weightedIndex==-1){
			//weightedIndex = $random(0,this.count-1);
			weightedIndex = 0;
		}
		
		this.rotateCountdown = this.rotateLimit;
		var spacer = new Element('div',{'class':'promo-nav-spacer'	});
		for (var loop=0;loop<this.count;loop++){
			var div = new Element('div',{
				'class':this.settings.navClass,
				'id':this.settings.navIDFormat+loop,
				'events':{
					'mouseover':function(){
									if (this.hasClass(MainPromo.settings.selectedClass)){
										return;
									}
									this.addClass(MainPromo.settings.hoverClass);
								},
					'mouseout':function(){
									if (this.hasClass(MainPromo.settings.selectedClass)){
										return;
									}
									this.removeClass(MainPromo.settings.hoverClass);
								},
					'click':function(force, rotation){
								if (force!=true && !rotation){
									MainPromo.rotating = false;
								}
								if (this.hasClass(MainPromo.settings.selectedClass)){
									return;
								}
								(function(){MainPromo.currentNavElement.removeClass(MainPromo.settings.selectedClass);}).delay(0);
								MainPromo.change($$(MainPromo.settings.promoClass)[parseInt(this.id.substring(8,9))]);
								MainPromo.currentNavElement = this;
								MainPromo.currentNavElement.removeClass(MainPromo.settings.hoverClass);
								MainPromo.currentNavElement.addClass(MainPromo.settings.selectedClass);
								(function(){
								}).delay(force?0:500);
								
								// SDC Code
								var promoDisplay ="";
								if (force == true || rotation){
									promoDisplay = "DisplayRandom";
								}else{
									promoDisplay = "DisplaySelect"
								}
								promoUpIdx = parseInt(this.id.substring(8,9));
								promoUpElem = $$(MainPromo.settings.promoClass)[promoUpIdx];
								promoUpNbr = promoUpIdx+1;
								promoLinkName = promoUpElem.get('id');
								// TEMPORARY:  Only send beacon for priority mail (first) banner
								if (promoUpIdx == 0){
								_tag2.dcsMultiTrack('DCS.dcsuri','/ed','WT.z_section','Carousel','WT.z_link',promoLinkName,'WT.z_event',promoDisplay);
								// Add Rotation Limiter
								if (rotation && promoUpIdx == 0){
									MainPromo.rotateCountdown = MainPromo.rotateCountdown - 1;
									if ( MainPromo.rotateCountdown == 0){
										MainPromo.rotating = false;
										_tag2.dcsMultiTrack('DCS.dcsuri','/ed','WT.z_section','Carousel','WT.z_link','N/A','WT.z_event','RotateLimitMet-' + MainPromo.rotateLimit );
									}
								}
								}
							}
				}
			});
			if (loop!=0){
				$(this.settings.promoBoxes).adopt(new Element('div',{'class':'promo-nav-spacer'	}));
			}
			$(this.settings.promoBoxes).adopt(div);
		}
		this.currentNavElement = $(this.settings.navIDFormat+0);
		$(this.settings.promoNavs).setStyle('width',this.settings.navWidth*this.count+(this.count-1));
		$(this.settings.parent).setStyles({
			'left':(537-(this.settings.navWidth*this.count+18+(this.count-1)))/2
		});
		this.buffer.init();
		this.showNavigator();
		
		if (weightedIndex>-1){
			$$('.promo-nav')[weightedIndex].fireEvent('click',true);
		}
		
		this.rotate.periodical(MainPromo.settings.rotateDelay,this);
	},
	rotate : function(){
		if (!MainPromo.rotating){
			$clear(MainPromo.rotate);
			return;
		}
		//alert(this.rotating);
		
		var id = MainPromo.currentNavElement.id.substring(8,100);
		id++;
		id = (id%MainPromo.count);
		$$('.promo-nav')[id].fireEvent('click',[false,true]);
	},
	buffer : {
		bufferID : 'primary-promo-buffer',
		bufferObj : null,
		init : function(){
			this.bufferObj = $(this.bufferID);
			this.bufferObj.set('morph',MainPromo.settings.effect);
			this.bufferObj.setStyles({
				'opacity':'0',
				'visibility':'visible'
			});
		},
		hide : function(){
			this.bufferObj.fade('out');
		},
		show : function(){
			this.bufferObj.fade('in');
		}
	},
	showNavigator : function(force){
		$(this.settings.parent).tween('top',246,216);
	},
	change:function(element,force){
		MainPromo.buffer.show();
		(function(){
			MainPromo.currentPromoElement.removeClass(MainPromo.settings.selectedClass);
			MainPromo.currentPromoElement = element;
			MainPromo.currentPromoElement.addClass(MainPromo.settings.selectedClass);
			MainPromo.loadImage();
			if (element.hasClass('promo2')) MainPromo.animate(element);
			MainPromo.buffer.hide();
		}).delay(force?0:1000);
	},
	animate:function (element){
		var t = new PromoTemplate2(element);
		t.animate();
		return;
	},
	loadImage : function(){
		var img = this.currentPromoElement.getElement('div.promo-background img');
		var path = img.get('path');
		if (path!='_'){
			img.set('src',path);
			img.set('path','_');
		}
	}

}

PromoTemplate2 = new Class({
	Implements : [Chain],
	effects : [],
	duration: 1000,
	initialize: function(el){
		el.getElement('.promo-content-wrapper').setStyle('height','0px');
		this.chain(
			function(){
				var rule1 = el.getElement('.top-rule');
				rule1.set('tween',{duration:'long',onComplete:(function(){this.callChain();}).bind(this)});
				rule1.set('opacity',0.35);
				
				var rule2 = el.getElement('.bottom-rule');
				rule2.set('tween',{duration:'long'});
				rule2.set('opacity',0.35);
				
				rule1.tween('top',0,246/2);
				rule2.tween('top',246,246/2);
			},
			function(){
				el.getElement('.promo-content-wrapper').setStyles({
					'visibility':'visible'
				});
				this.callChain();
			},
			function(){
				var rule1 = el.getElement('.top-rule');
				rule1.set('tween',{duration:this.duration,onComplete:(function(){this.callChain();}).bind(this)});
				
				var rule2 = el.getElement('.bottom-rule');
				rule2.set('tween',{duration:this.duration});
				
				var rule3 = el.getElement('.promo-content-wrapper');
				rule3.set('morph',{duration:this.duration});
				
				var rule4 = el.getElement('.promo-text');
				rule4.set('morph',{duration:this.duration});
			
				var rule5 = el.getElement('.black-background-bar');
				rule5.set('morph',{duration:this.duration});
			
				rule1.tween('top',90);
				rule2.tween('top',150);
				var effects = {
					'height':[0,59]
					,'top':[246/2,91]
				}
				rule3.morph(effects);
			},
			function(){}
		);
	},
	animate : function(){
		this.callChain();
	}
});

window.addEvent('domready',function(){
	MainPromo.initialize();
});
