/*
Programmer: Darryl Ballard
Date created: 2008-10-07
Last updated: 2010-05-13
Version: 2.0.0

Requires:
	Prototype 1.6
	
Version History:
	2.0.0, 2010-05-13: Made it completely different; no more global
		GST_Click_Boxes, more use of Prototype (.down()), more efficient for
		loops. Removed link-within-the-clickbox hiding, as that's CSS's job.
	
	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?
*/

// Require that Prototype is available
if (typeof Prototype != "undefined") {
	document.observe("dom:loaded", function() {
		var class_to_activate = "GST_Click_Box";
		var old_status_text;
		
		var get_full_href = function(elem) {
			var attr_href = elem.getAttribute("href");
			var ret = attr_href;
			
			// True for absolute hrefs (http://example.com, /foo.txt, mailto:foo@example.com, etc.)
			var href_is_absolute = attr_href.match(/^\/|^[A-Za-z]+:/);
			
			if (!href_is_absolute) {
				// Prepend the base href to create a full href
				var bases = document.getElementsByTagName("base");
				if (bases.length > 0) {
					ret = bases[0].getAttribute("href") + attr_href;
				}
			}
			
			return ret;
		};
		
		var handle_clickbox_click = function(e) {
			var descendant_link = this.down("a");
			if (descendant_link) {
				//window.location = descendant_link.href;
				window.location = get_full_href(descendant_link);
			}
		};
		
		var handle_clickbox_mouseover = function() {
			// When hovering a clickbox, change the statusbar text to the link location
			var descendant_link = this.down("a");
			
			if (descendant_link) {
				//old_status_text = window.status;
				window.status = get_full_href(descendant_link);
			}
		};
		
		var handle_clickbox_mouseout = function() {
			// When leaving a clickbox, change the statusbar text back to whatever
			//window.status = old_status_text ? (old_status_text + "") : "";
			window.status = "";
		};
		
		// Init
		var allClickboxes = $$("." + class_to_activate);
		var i, il;
		var descendantLink;
		for (i = 0, il = allClickboxes.length; i < il; i++) {
			// Only do anything if there's a descendant link in the clickbox
			descendantLink = allClickboxes[i].down("a");
			if (descendantLink) {
				// Flag the clickbox as scripted (conditionally style stuff within, etc.)
				allClickboxes[i].addClassName("scripted");
				
				// Set up event handlers
				allClickboxes[i].observe("click", handle_clickbox_click);
				allClickboxes[i].observe("mouseover", handle_clickbox_mouseover);
				allClickboxes[i].observe("mouseout", handle_clickbox_mouseout);
			}
		}
	});
}

