/*
Programmer: Darryl Ballard
Date created: 2009-01-27
Last updated: 2009-01-27
Version: 1.0.0

Requires:
	Prototype 1.6
	
Version History:
*/

var GST_Crossfader = {
	class_to_activate:"GST_Crossfader",

	debug:false,
	
	handle_delay:function()
	{
		GST_Crossfader.debug.send("Timeout expired; starting the interval.");
		this.gst_cf_timer = setInterval(GST_Crossfader.handle_interval.bind(this), 50);
	},
	
	handle_interval:function()
	{
		// "this" refers to the list
		
		// Decrement the opacity of the primary item
		this.gst_cf_primary.gst_cf_opacity -= 0.03;
		GST_Crossfader.debug.send("Decremented primary's opacity to " + this.gst_cf_primary.gst_cf_opacity);
		
		// Check the primary item's new opacity
		if (Math.round(100.0 * this.gst_cf_primary.gst_cf_opacity) / 100.0 > 0) {
			// Still slightly visible, set the opacity of the element
			this.gst_cf_primary.setOpacity(this.gst_cf_primary.gst_cf_opacity);
		} else {
			// Primary item is invisible
			
			// Keep a temporary reference to the primary item
			var tempRef = this.gst_cf_primary;
			
			// Choose a new primary
			this.gst_cf_primary = this.gst_cf_secondary;
			this.gst_cf_primary.show();
			this.gst_cf_primary.style.zIndex = "100";
			this.gst_cf_primary.gst_cf_opacity = 1.0;
			this.gst_cf_primary.setOpacity(this.gst_cf_primary.gst_cf_opacity);
			
			// Choose a new secondary
			var next = this.gst_cf_secondary.next("li");
			if (next) {
				// Use the next list item
				this.gst_cf_secondary = next;
			} else {
				// Go back to the first list item
				this.gst_cf_secondary = this.firstDescendant();
			}
			GST_Crossfader.debug.send("New secondary: " + this.gst_cf_secondary);
			this.gst_cf_secondary.show();
			this.gst_cf_secondary.style.zIndex = "99";
			this.gst_cf_secondary.gst_cf_opacity = 1.0;
			this.gst_cf_secondary.setOpacity(this.gst_cf_secondary.gst_cf_opacity);

			// Hide the old primary
			tempRef.hide();
			
			// Start the delay between images
			GST_Crossfader.debug.send("Stopping the interval and starting a new timeout.");
			clearInterval(this.gst_cf_timer);
			this.gst_cf_timer = undefined;
			this.gst_cf_delayTimeout = setTimeout(GST_Crossfader.handle_delay.bind(this), 5000);
		}
	},
	
	activate:function()
	{
		GST_Crossfader.debug = $("debug");
		if (!GST_Crossfader.debug) {
			GST_Crossfader.debug = new Object();
		}
		GST_Crossfader.debug.send = function(strText) {
			// "this" refers to the debug object (an Object or an element)
			
			if (this.firstChild) {
				this.firstChild.nodeValue = this.firstChild.nodeValue + "\n" + strText;
			}
		}
		
		GST_Crossfader.debug.send("Starting.");
		
		// Get all crossfader lists
		var lists = $$('.' + GST_Crossfader.class_to_activate);
		GST_Crossfader.debug.send("Found " + lists.length + " list(s)");
		
		for (var i = 0; i < lists.length; i++) {
			//lists[i].style.position = "relative";
			
			var items = lists[i].select("li");
			GST_Crossfader.debug.send("Found " + items.length + " item(s) in list " + i);

			// Ignore lists with 1 item
			if (items.length <= 1) {
				GST_Crossfader.debug.send("Ignoring list " + i + " because it has only 1 item.");
			} else {
				for (var j = 0; j < items.length; j++) {
					// Make the item positionable
					//items[j].absolutize();
					
					// Hide images past the second
					if (j > 1) {
						items[j].hide();
						items[j].gst_cf_opacity = 0.0;
						items[j].setOpacity(items[j].gst_cf_opacity);
					}
				}

				// Set the 0th item as the first to show and the 1st item as next in line
				lists[i].gst_cf_primary = items[0];
				items[0].style.zIndex = "100";
				items[0].gst_cf_opacity = 1.0;
				items[0].setOpacity(items[0].gst_cf_opacity);
				lists[i].gst_cf_secondary = items[1];
				items[1].style.zIndex = "99";
				items[1].gst_cf_opacity = 1.0;
				items[1].setOpacity(items[1].gst_cf_opacity);
				
				// Start with a delay
				if (true) {
					lists[i].gst_cf_delayTimeout = setTimeout(GST_Crossfader.handle_delay.bind(lists[i]), 5000);
					GST_Crossfader.debug.send("Initial timeout started.");
				}
				
				// Set up a timer of this list
				//lists[i].gst_cf_timer = setInterval(GST_Crossfader.handle_interval.bind(lists[i]), 50);
			}
		}
	} // end activate()
};

// Require that Prototype is available
if (typeof Prototype == "undefined") {
	alert("GST Crossfader 1.0 requires the Prototype javascript framework.\n\nPlease check that it is included.");
} else {
	document.observe("dom:loaded", GST_Crossfader.activate);
}

