/* 
	For marked blocks, adds specified class on rollover, and link behaviour if blocks contain links.

	Requires glow.dom (uses simple event handling without glow.events at present).

	created: 2008-10-03 <a href="mailto:Michael.Taylor1@bbc.co.uk">Mike Taylor</a>
*/

(function(){

	if (!window.glow)
		return;

// MAINTAINED VARIABLES:
	var dbg = false; // set true to open only one at a time

// CODE TO EXECUTE:
	glow.ready(function() {
		addLinks('.blockLink', 'blockLinkHover');

// for IE6 only (to get hover on non-anchor elements):	
//		addLinks('.nonLinkHover', 'hover');
		if (glow.env.ie && glow.env.ie < 8) {
			addLinks('.submit-btn', 'hover');
			addLinks('.submit_button', 'hover');
			addLinks('.submit-as-link', 'hover');
			addLinks('.gallery-page .page-nav-btn', 'hover');
			addLinks('.gallery-extras .display-options .submit-btn', 'hover');
			addLinks('#filter .med-btn', 'hover');	
			addLinks('#filter .categories-btn', 'hover');	
			addLinks('#filter .subject-btn', 'hover');	
		}
	});


// FUNCTIONS:
/* 
	addLinks: enhancement to add link behaviour to whole block containing the link.
	Search for all blocks with the given class. 
	Find the first link in the block, save the target and set the onclick 
	event handler for the block to respond by navigating to that link.
*/
	function addLinks(
		sWrapperSelector /* selector for wrapping elements containing link */
		, sHoverClass /* class added here to block to simulate link-hover */
	) 
	{
		var ns_wrappers = glow.dom.get(sWrapperSelector) 
			;

		ns_wrappers.each(function(i) {

			var ns_block = ns_wrappers.slice(i,i+1) // containing block
				, ns_a = ns_block.get('a') // first anchor in block
				;

//			alert(ns_block[0].nodeName + ": " + ns_block[0].className);

			this.onmouseover = function() { glow.dom.get(this).addClass(sHoverClass) };
			this.onmouseout = function() { glow.dom.get(this).removeClass(sHoverClass) };

			if (!ns_a.length) return; // this bit applies only if anchors are found

			this.linkTarget = ns_a.attr('href');
			this.onclick = function() { window.location = this.linkTarget };

		});
	}
	//-----


})(); // call immediately

// end of script
