// Fade swapper
var FadeSwap = Class.create();
Object.extend(Object.extend(FadeSwap.prototype, AC.ContentSwap.prototype), {
	cs1IsAnimating: false,
	cs2IsAnimating: false,
	lastSelectorIndex: false,

	initialize: function(wrapper, selectors, contents, eventStr) {

 		this.title = 'Apple - Back to School';
		this.eventStr = eventStr;
		this.wrapper = wrapper;
		this.lastContent = null;
		
		// get lists of selectors and content in order
		this.selectorList = selectors;
		this.contentList = contents;

		for (var i=0; i<this.contentList.length; i++) {
			this.contentList[i].style.display = 'none';

			var name = this.selectorList[i];
			name = Element.down(name, 'a');
			this.selectorList[i].url = name.href;
			while (name.firstDescendant()) name = name.firstDescendant();
			this.selectorList[i].name = name.innerHTML.unescapeHTML();
		}
		
		this.setMouseover();
	},

	setMouseover: function() {
		for(var i=this.selectorList.length-1, selector; selector = this.selectorList[i]; i--) {
			Event.observe(selector, this.eventStr, this.swapContent.bindAsEventListener(this, i), false);
		}
	},

	swapContent: function(evt, selectorIndex) {
		if(evt) Event.stop(evt);
		var selector = this.selectorList[selectorIndex];
		var content = this.contentList[selectorIndex];
		// set the document.title to make click tracking a breeze

		// track this click
		if (evt) this.trackClick(selectorIndex);

		// if we aren't already animating
		if (this.cs1IsAnimating == false && this.cs2IsAnimating == false) {

			// set the first anim flag
			this.cs1IsAnimating = true;
			this.cs2IsAnimating = true;
			
			// set the 'on' class
			if (!Element.hasClassName(selector, 'active')) Element.addClassName(selector, 'active');

			// if this is the first one
			if (!this.lastSelectorIndex && typeof(this.lastSelectorIndex) == 'boolean') {
				if(!AC.Detector.isiPhone()) {					
					this.afterFade(content);
				} else {
					content.style.display = 'block';
					
					this.resetFlag('cs1IsAnimating');
					this.resetFlag('cs2IsAnimating');
				}

			} else if (selectorIndex != this.lastSelectorIndex) {
				// set some more temporary vars
				var lastSelector = this.selectorList[this.lastSelectorIndex];
				this.lastContent = this.contentList[this.lastSelectorIndex];

				// remove the 'on' class
				if (Element.hasClassName(lastSelector, 'active')) Element.removeClassName(lastSelector, 'active');
		
				// swap
				if(!AC.Detector.isiPhone()) {
					new Effect.Fade(this.lastContent, { duration:.1, queue:{position:'end', scope:'cs1'},
						afterFinish:this.afterFade.bind(this, content)
					});
				} else {
					this.lastContent.style.display = 'none';
					content.style.display = 'block';
					
					this.resetFlag('cs1IsAnimating');
					this.resetFlag('cs2IsAnimating');
				}
			} else {
				this.resetFlag('cs1IsAnimating');
				this.resetFlag('cs2IsAnimating');
			}

			// set the last content index now that we're done doing everything
			this.lastSelectorIndex = selectorIndex;
		}
	},

	afterFade: function(content) {
		this.resetFlag('cs1IsAnimating');
		new Effect.Appear(content, { duration:.1, queue:{position:'end', scope:'cs2'},
			//beforeStart:this.fixHeight.bind(this, content),
			afterFinish:this.afterAppear.bind(this, content)
		});
	},

	fixHeight: function(content) {
		if(this.lastContent) {
			var difference = this.lastContent.getHeight() - content.getHeight();
			this.wrapper.morph({
				height:this.lastContent.getHeight() - difference + 'px'
			}, { duration:.1, queue:{position:'front', scope:'cs2'} });
		} else {
			this.wrapper.morph({ 
				height:content.getHeight() + 'px'
			}, { duration:.1, queue:{position:'front', scope:'cs2'} });
		}
		
	},

	afterAppear: function(content) {
		this.resetFlag('cs2IsAnimating');
		content.style.opacity = '';
	},

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

	trackClick: function(selectorIndex) {
		var url = this.selectorList[selectorIndex].down('a').href;
		var title = this.title+' - '+this.selectorList[selectorIndex].down('a').innerHTML;
		
		// click tracking
		AC.Tracking.trackPage({
			pageName: title,
			prop4: url,
			prop6: s.getQueryParam('cp')+': '+title,
			pageURL: url,
			referrer: s.fl(s.wd.location ? s.wd.location : '', 255)
		});
	}

});
