

// Slider swap for the thumbnail gallery
var SliderSwap = Class.create();
Object.extend(Object.extend(SliderSwap.prototype, AC.ContentSwap.prototype), {

	lastSelectorIndex: 0,
	cs1IsAnimating: false,
	cs2IsAnimating: false,
	swapContent: function(selectorIndex) {

		if(this.cs1IsAnimating == false && this.cs2IsAnimating == false) {

			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, -998, { duration: 0.5, queue: {position:'end', scope:'cs1'}, afterFinish: this.resetFlag.bind(this,'cs1IsAnimating') });
					new Effect.MoveBy(content, 0, -998, 
						{ duration: 0.5, queue: {position:'end', scope:'cs2'}, beforeStart: function() {content.style.left = '998px'}, afterFinish: this.resetFlag.bind(this,'cs2IsAnimating') }
					);
				} else {
					new Effect.MoveBy(lastContent, 0, 998, { duration: 0.5, queue: {position:'end', scope:'cs1'}, afterFinish: this.resetFlag.bind(this,'cs1IsAnimating') });
					new Effect.MoveBy(content, 0, 998,
						{ duration: 0.5, queue: {position:'end', scope:'cs2'}, beforeStart: function() {content.style.left = '-998px'}, afterFinish: this.resetFlag.bind(this,'cs2IsAnimating') }
					);
				}
				this.lastSelectorIndex = selectorIndex;

			} else {
				this.resetFlag('cs1IsAnimating');
				this.resetFlag('cs2IsAnimating');
			}
		}

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

// initialize function
function galleryInit() {
	Element.addClassName('main', 'hasjs');
	new SliderSwap('gallerythumb', 'gallerycontent', 'click');
}
Event.observe(window, 'load', galleryInit, false);
