/*
* @author: stephanie trimble
*
*/                      

if (typeof(AC) == "undefined") { AC = {}; }

AC.ContentSwap = Class.create();
AC.ContentSwap.prototype = {

	selectorList: null,
	contentList: null,
	contentSelectorHash: null,
	eventStr: null,

	initialize: function(selectorClass, contentClass, eventStr, options) {
	
		this.eventStr = eventStr;
		this.options = options || {};
		
		// get lists of selectors and content in order
		this.selectorList = document.getElementsByClassName(selectorClass);
		this.contentList = document.getElementsByClassName(contentClass);
		
		this.setMouseover();
	},
	
	setMouseover: function() {
		for(var i=this.selectorList.length-1; i >= 0; i--) {
			Event.observe(this.selectorList[i], this.eventStr, this.swapContent.bind(this, i), false);
		}
	},
	
	swapContent: function(selectorIndex) {
		var selector = this.selectorList[selectorIndex];
		var content = this.contentList[selectorIndex];
		
		// add 'on' class
		//if(!Element.hasClassName(selector, 'active')) Element.addClassName(selector, 'active');
		//if(!Element.hasClassName(content, 'active')) Element.addClassName(content, '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(Element.hasClassName(this.contentList[i], 'active')) Element.removeClassName(this.contentList[i], 'active');
			}
		}
		
		if (typeof(this.options.onSwap) == 'function') {
			this.options.onSwap(selector, content);
		}
		
		return false;
	}
}



// local
function init() {
	new SliderSwap('swapnav', 'swapcontent', 'click', {
		onSwap: function(nav, content) {
			var linkText = nav.down().innerHTML;
			//AC.Tracking.trackClick({prop3: linkText}, this, 'o', document.title + 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) {
		
			this.cs1IsAnimating = true;
			this.cs2IsAnimating = true;
			
			var lastSelected = this.lastSelectorIndex;
			var newSelection = lastSelected;
			if(selectorIndex > 0)
				newSelection ++;
			else 
				newSelection --;
			if(newSelection < 0)
				newSelection=this.contentList.length - 1;
			else if (newSelection == this.contentList.length)
				newSelection = 0;
				
			var content = this.contentList[newSelection];
		
			var lastContent = this.contentList[this.lastSelectorIndex];
		
			if(selectorIndex > 0) {
				//move to the right
				new Effect.MoveBy(lastContent, 0, -300, { duration: 1.5, queue: {position:'end', scope:'cs1'}, afterFinish: this.resetFlag.bind(this,'cs1IsAnimating') });
				new Effect.MoveBy(content, 0, -300, 
					{ duration: 1.5, queue: {position:'end', scope:'cs2'}, beforeStart: function() {content.style.left = '300px'}, afterFinish: this.resetFlag.bind(this,'cs2IsAnimating') }
				);
			} else {
				new Effect.MoveBy(lastContent, 0, 300, { duration: 1.5, queue: {position:'end', scope:'cs1'}, afterFinish: this.resetFlag.bind(this,'cs1IsAnimating') });
				new Effect.MoveBy(content, 0, 300,
					{ duration: 1.5, queue: {position:'end', scope:'cs2'}, beforeStart: function() {content.style.left = '-300px'}, afterFinish: this.resetFlag.bind(this,'cs2IsAnimating') }
				);		
			}

			this.lastSelectorIndex = newSelection;
		}
		
		return false;
	},

	resetFlag: function(flagName) {
		if(flagName == 'cs1IsAnimating') this.cs1IsAnimating = false;
		if(flagName == 'cs2IsAnimating') this.cs2IsAnimating = false;
	}

});



Event.observe(window, 'load', init, false);

