var FL_SimpleSlideshow = Class.create();

FL_SimpleSlideshow.prototype = {
	/*
		Important: AOL uses it's own instance of this function
	*/
	initialize: function(params) {
		this.instance = flssInstances.length;
		flssInstances[this.instance] = this;
		flssCurrentIndexes[this.instance] = -1;
		this.params = (params) ? params : {};
		this.slidesClass = (this.params.slidesClass) ? this.params.slidesClass : 'flssItem';
		this.captionsClass = (this.params.captionsClass) ? this.params.captionsClass : 'flssCaption';
		this.doNotOutputStyles = (this.params.doNotOutputStyles) ? this.params.doNotOutputStyles : false;
		this.fade = (typeof this.params.fade == 'undefined') ? true : this.params.fade;
		this.duration = (this.params.duration) ? this.params.duration : 1;
		this.firstSlide = (this.params.firstSlide) ? this.params.firstSlide : 0;
		this.prevLinkID = (this.params.prevLinkID) ? this.params.prevLinkID : 'flssPrevLink';
		this.nextLinkID = (this.params.nextLinkID) ? this.params.nextLinkID : 'flssNextLink';
		this.firstLinkID = (this.params.firstLinkID) ? this.params.firstLinkID : 'flssFirstLink';
		this.lastLinkID = (this.params.lastLinkID) ? this.params.lastLinkID : 'flssLastLink';
		this.statusElID = (this.params.statusElID) ? this.params.statusElID : 'flssStatus';
		this.captionElID = (this.params.captionElID) ? this.params.captionElID : 'flssCaption';
		this.prevLinkEl = $(this.prevLinkID);
		this.nextLinkEl = $(this.nextLinkID);
		this.firstLinkEl = $(this.firstLinkID);
		this.lastLinkEl = $(this.lastLinkID);
		this.statusEl = $(this.statusElID);
		this.captionEl = $(this.captionElID);
		this.hideTotalPagesInStatus = (this.params.hideTotalPagesInStatus) ? this.params.hideTotalPagesInStatus : false;
		this.linkDisabledClassName = (this.params.linkDisabledClassName) ? this.params.linkDisabledClassName : 'flssLinkDisabled';
		if (this.params.linkDisabledClassName) {
			this.params.doNotOutputStyles = true;
		}
		this.els = document.getElementsByClassName(this.slidesClass);		
		this.init();
	},
	init: function() {
		this.writeStyles();
		this.prevLinkEl.href = "javascript:flssInstances[" + this.instance + "].previous();";
		this.nextLinkEl.href = "javascript:flssInstances[" + this.instance + "].next();";
		if (this.firstLinkEl) this.firstLinkEl.href = "javascript:flssInstances[" + this.instance + "].first();";
		if (this.lastLinkEl) this.lastLinkEl.href = "javascript:flssInstances[" + this.instance + "].last();";
		this.hideAllButFirst();
	
	},
	get: function(forward, indx) {
		var els = this.els;
		var noOfItems = els.length;	
		var no_forward = (flssCurrentIndexes[this.instance] >= (noOfItems - 1));
		var no_back = (flssCurrentIndexes[this.instance] <= 0);
		var back = !forward;				
		if (typeof indx == 'undefined' && ((forward && no_forward) || (back && no_back))) return;
		// increment counter (forward or backwards)
		var prev_item = flssCurrentIndexes[this.instance];
		if (typeof indx != 'undefined') {
			flssCurrentIndexes[this.instance] = indx;
		} else if (forward) {
			flssCurrentIndexes[this.instance] ++;
		} else {
			flssCurrentIndexes[this.instance] --;
		}				
		var no_forward = (flssCurrentIndexes[this.instance] >= (noOfItems - 1));
		var no_back = (flssCurrentIndexes[this.instance] <= 0);
		if (no_forward) {
			this.addClass($(this.nextLinkID), this.linkDisabledClassName);
			if (this.lastLinkEl) this.addClass($(this.lastLinkEl), this.linkDisabledClassName);
		} else {
			this.removeClass($(this.nextLinkID), this.linkDisabledClassName);
			if (this.lastLinkEl) this.removeClass($(this.lastLinkEl), this.linkDisabledClassName);
		}				
		if (no_back) {
			this.addClass($(this.prevLinkID), this.linkDisabledClassName);
			if (this.firstLinkEl) this.addClass($(this.firstLinkEl), this.linkDisabledClassName);
		} else {
			this.removeClass($(this.prevLinkID), this.linkDisabledClassName);
			if (this.firstLinkEl) this.removeClass($(this.firstLinkEl), this.linkDisabledClassName);
		}
		var outgoingEl;
		if (flssPrevItems[this.instance]) {
			outgoingEl = flssPrevItems[this.instance];
		}
		var incomingEl = els[flssCurrentIndexes[this.instance]];		
		if (!this.fade) {
			if (outgoingEl) {
				outgoingEl.style.display = "none"; // hide previous item
			}
			incomingEl.style.display = "block"; // show new current item
		} else {
			var queue = Effect.Queues.get('main');
			queue.each(function(effect) { effect.cancel(); });
			
			if (outgoingEl) {
				new Effect.Fade(outgoingEl, { duration: 0, queue: { position: 'end', scope: 'main' } });
				//outgoingEl.style.display = "none"; // hide previous item
			}
			//incomingEl.style.display = "block"; // show new current item
			new Effect.Appear(incomingEl, { duration: this.duration	, queue: { position: 'end', scope: 'main' } });
		}
		//	update previous
		flssPrevItems[this.instance] = els[flssCurrentIndexes[this.instance]];
		// update the status element
		if ($(this.statusEl)) {
			if (this.hideTotalPagesInStatus) {
				$(this.statusEl).innerHTML = (parseInt(flssCurrentIndexes[this.instance]) + 1);
			} else {
				$(this.statusEl).innerHTML = (parseInt(flssCurrentIndexes[this.instance]) + 1) + '/' + this.els.length;
			}
		}
		// update the caption element 
		// :NEW: 080422 - if an element exists with a class of flssCaption (or the
		// class-name specified in 'captionsClass', above), then use the inner html
		// from this element, otherwise use the 'title' attribute...
		var captionEls = document.getElementsByClassName(this.captionsClass);
		if (captionEls.length > 0) {
			$(this.captionEl).innerHTML = captionEls[flssCurrentIndexes[this.instance]].innerHTML;
		} else if ($(this.captionEl) && incomingEl.title) {
			$(this.captionEl).innerHTML = unescape(incomingEl.title);
		} else if ($(this.captionEl)) {
			$(this.captionEl).innerHTML = '';
		}
	},
	previous: function() {
		return this.get(false);
	},
	next: function() {
		return this.get(true);
	},
	first: function() {
		return this.get(true, 0);
	},
	last: function() {
		return this.get(true, (this.els.length - 1));
	},
	writeStyles: function() {
		if (this.doNotOutputStyles) return;
		document.write('<style>\
			.hidden {\
				display: none;\
			}\
			.' + this.linkDisabledClassName + ' {\
				color: silver;\
			}\
		<\/style>');
	},
	addClass: function(el, className) {
		var classes=el.className.toString();
		if ((" " + classes + " ").indexOf(" " + className + " ") == -1) {
			var newClasses = (classes == "") ? className : classes + " " + className;
			el.className=newClasses;
		}
	},
	removeClass: function(el, className) {
		var classes=el.className.toString().split(" ");
		var newClasses = new Array();
		for (var i = 0; i < classes.length; i++) {
			if (classes[i] != className) {
				newClasses[newClasses.length] = classes[i];
			}
		}
		el.className = newClasses.join(" ");
	},
	hideAllButFirst: function() {
		for (var i = 0; i < this.els.length; i ++) {
			this.els[i].style.display = 'none';
		}
		this.get(true, this.firstSlide);
	}
}

var flssInstances = [];
var flssCurrentIndexes = [];
var flssPrevItems = [];
var flssTimeouts = [];

