/*
Programmer: Darryl Ballard
Date created: 2008-10-07
Last updated: 2009-03-16
Version: 1.1

Requires:
	Prototype 1.6
	
Version History:
	2009-03-16: Added the class "scripted" to any clickbox element once it's
		activated.  Added windows status text changing on hover for the clickbox
		matching the link's path.

	2008-12-23: 1.0.0 released?
	
	2008-10-07: Started?
*/

var GST_Click_Boxes = {
	class_to_activate:"GST_Click_Box",
	
	class_for_hiding_child_link:"hide_link",
	
	activate:function()
	{
		var all_links = $$("a");
		
		for (var i = 0; i < all_links.length; i++) {
			var click_box_parent = all_links[i].up("." + GST_Click_Boxes.class_to_activate);
			
			if (click_box_parent != undefined) {
				// Mark this click box as activated
				click_box_parent.addClassName("scripted");
				
				// Optionally hide the link
				if (click_box_parent.hasClassName(GST_Click_Boxes.class_for_hiding_child_link)) {
					all_links[i].hide();
				}
				
				// Disable the click event of the link
				all_links[i].observe("click", function(e) {
					e.preventDefault();
					return false;
				});
				
				// Give the click box some behavior
				click_box_parent.observe("click", function() {
					var child_link = this.down("a");
					
					if (child_link != undefined) {
						//alert("Should I go to " + child_link.href);
						
						// Check for offsite link behavior
						if (typeof GST_External_Links == "undefined") {
							// Open in current tab/window
							window.location = child_link.href;
						} else {
							// Ask GST_External_Links to handle it
							GST_External_Links.trigger(child_link);
						}
						
						/*
						if (this.hasClassName("offsite")) {
							// Open in a new tab/window
							window.open(child_link.href, "externalWindow");
						} else {
							// Open in current tab/window
							window.location = child_link.href;
						}
						*/
					}
				});
				
				click_box_parent.observe("mouseover", function() {
					var child_link = this.down("a");

					if (child_link) {
						// Because the href attribute on links in IE returns the full path and not the actual
						// content of the href attribute, use just the href for IE
						if (child_link.getAttribute("href").match(/^http/)) {
							window.status = child_link.getAttribute("href");
						} else {
							// Now, assuming that the real href is available, try to create the full path the
							// link will go to.  If base is unavailable, just show the href.
							var bases = document.getElementsByTagName("base");
							if (bases.length > 0) {
								window.status = bases[0].getAttribute("href") + child_link.getAttribute("href");
							} else {
								window.status = child_link.getAttribute("href");
							}
						}
					}
				});
				
				click_box_parent.observe("mouseout", function() {
					window.status = "";
				});
			}
		} // end for(i)
	} // end activate()
};

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

