/*
Programmer: Darryl Ballard
Date created: 2009-04-06
Last updated: 2009-04-06
Version: 1.0.0 alpha

Requires:
	Prototype 1.6
	
Version History:
*/

var GST_Flash_Messages = {
	id_to_activate:"caught",
	class_for_hover:"hover",
	hide_parent_if_zero_visible_children:true,

	handle_click:function()
	{
		this.hide();
		
		var visibleSiblings = 0;
		var siblings = this.parentNode.childElements();
		for (var i = 0; i < siblings.length; i++) {
			if (siblings[i].visible()) {
				visibleSiblings++;
			}
		}
		
		if (visibleSiblings == 0 && GST_Flash_Messages.hide_parent_if_zero_visible_children) {
			this.parentNode.hide();
		}
	},
	
	handle_mouseover:function()
	{
		this.addClassName(GST_Flash_Messages.class_for_hover);
	},

	handle_mouseout:function()
	{
		this.removeClassName(GST_Flash_Messages.class_for_hover);
	},
	
	activate:function()
	{
		var objCaught = $(GST_Flash_Messages.id_to_activate);
		
		if (objCaught) {
			// Give hover and click behavior to each child element
			var childElems = objCaught.childElements();
			for (var i = 0; i < childElems.length; i++) {
				childElems[i].observe("click", GST_Flash_Messages.handle_click);
				childElems[i].observe("mouseover", GST_Flash_Messages.handle_mouseover);
				childElems[i].observe("mouseout", GST_Flash_Messages.handle_mouseout);
			}
		}
		
		// Get all the external links
		var external_links = $$("a." + GST_External_Links.class_to_activate);
		
		for (var i = 0; i < external_links.length; i++) {
			// Only do it to links with an href
			// (No idea why this works, but it does, on FF/IE/GC)
			if (external_links[i].href) {
				external_links[i].observe("click", function(e) {
					GST_External_Links.trigger(this);
					
					// Stop event bubbling and any default browser actions
					e.stop();
				});
			}
		} // end for(i)
	} // end activate()
};

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