// local
function init() {
	new SliderSwap('swapnav', 'swapcontent', 'click', {
		onSwap: function(nav, content) {
			var linkText = nav.down().innerHTML;
			AC.Tracking.trackClick({prop3: linkText}, this, 'o', AC.Tracking.pageName() + linkText);
		}
	});
}


var SliderSwap = Class.create();
Object.extend(Object.extend(SliderSwap.prototype, AC.ContentSwap.prototype), {
	lastSelectorIndex: 0,

	cs1IsAnimating: false,
	cs2IsAnimating: false,

	swapContent: function(selectorIndex, evt) {
		if (evt) Event.stop(evt);

		if (this.cs1IsAnimating == false && this.cs2IsAnimating == false) {
			//if(selectorIndex == 2) Effect.Appear('herotitle', { duration: 0.25 });
			//if(selectorIndex != 2) Effect.Fade('herotitle', { duration: 0.25 });
		
			//this.cs1IsAnimating = true;
			//this.cs2IsAnimating = true;
			
			var selector = this.selectorList[selectorIndex];
			var content = this.contentList[selectorIndex];
		
			// add 'on' class
			if (!Element.hasClassName(selector, 'active')) Element.addClassName(selector, 'active');
			// remove 'on' class from all other selectors and content areas
			for(var i=this.selectorList.length-1; i >= 0; i--) {
				if(i != selectorIndex) {
					if(Element.hasClassName(this.selectorList[i], 'active')) Element.removeClassName(this.selectorList[i], 'active');
				}
			}

			if (selectorIndex != this.lastSelectorIndex) {
				var lastContent = this.contentList[this.lastSelectorIndex];
		
				if(selectorIndex > this.lastSelectorIndex) {
					new Effect.MoveBy(lastContent, 0, -992, { duration: 0.5, queue: {position:'end', scope:'cs1'}, afterFinish: this.resetFlag.bind(this,'cs1IsAnimating') });
					new Effect.MoveBy(content, 0, -992, 
						{ duration: 0.5, queue: {position:'end', scope:'cs2'}, beforeStart: function() {content.style.left = '992px'}, afterFinish: this.resetFlag.bind(this,'cs2IsAnimating') }
					);
				} else {
					new Effect.MoveBy(lastContent, 0, 992, { duration: 0.5, queue: {position:'end', scope:'cs1'}, afterFinish: this.resetFlag.bind(this,'cs1IsAnimating') });
					new Effect.MoveBy(content, 0, 992,
						{ duration: 0.5, queue: {position:'end', scope:'cs2'}, beforeStart: function() {content.style.left = '-992px'}, afterFinish: this.resetFlag.bind(this,'cs2IsAnimating') }
					);		
				}
				this.lastSelectorIndex = selectorIndex;
				
			} else {
				this.resetFlag('cs1IsAnimating');
				this.resetFlag('cs2IsAnimating');
			}
			
			if (typeof(this.options.onSwap) == 'function') {
				this.options.onSwap(selector, content);
			}
		}
		
		return false;
	},

	resetFlag: function(flagName) {
		if(flagName == 'cs1IsAnimating') this.cs1IsAnimating = false;
		if(flagName == 'cs2IsAnimating') this.cs2IsAnimating = false;
	}

});



Event.observe(window, 'load', init, false);
