/*

© 2008 Ben Jones / e-motion design

Usage: Include this script
Then call
setupLink( 'abc' , 'http://google.com' );
- this links element with ID='abc' to google
or
setupLink( 'def' );
- which links element 'def' to the first link found within itself

*/

// Have the browser load the passed URL

function openURL( URL ) {
 window.location = URL;
}

// Set the cursor to be the hand (or normal cursor)
function setMouseStatus( elementId , cursorHand ) {
 // Get the element object
 e = getElementsByClassName(document,"div", elementId )[0];
 // Set the cursor style appropriately
 if ( cursorHand ) {
  // Mouse-over code here
  e.style.cursor = 'pointer';
 } else {
  // Mouse-out code here
  e.style.cursor = 'default';
 }
}

// Link the element
function setupLink( elementId , URL ) {
 // Get the element object
 e = getElementsByClassName(document,"div", elementId )[0];
 // Get the URL
 if ( URL == null ) {
  // If we weren't passed a URL, get the first A-HREF inside the element
  fe = e.getElementsByTagName('a')[0];
  if ( fe != null ) {
   URL =  fe.href;
  } else {
	 // If there are none inside, set to #
   URL = '#';
  }
 }
 // Set the click and mouseover/out behaviours
 e.onclick = new Function( "openURL('" + URL + "')" );
 e.onmouseover = new Function( "setMouseStatus( '" + elementId + "', true )" );
 e.onmouseout = new Function( "setMouseStatus( '" + elementId + "' , false )" );
}




function getElementsByClassName(oElm, strTagName, oClassNames){
	var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	var arrRegExpClassNames = new Array();
	if(typeof oClassNames == "object"){
		for(var i=0; i<oClassNames.length; i++){
			arrRegExpClassNames.push(new RegExp("(^|\\s)" + oClassNames[i].replace(/\-/g, "\\-") + "(\\s|$)"));
		}
	}
	else{
		arrRegExpClassNames.push(new RegExp("(^|\\s)" + oClassNames.replace(/\-/g, "\\-") + "(\\s|$)"));
	}
	var oElement;
	var bMatchesAll;
	for(var j=0; j<arrElements.length; j++){
		oElement = arrElements[j];
		bMatchesAll = true;
		for(var k=0; k<arrRegExpClassNames.length; k++){
			if(!arrRegExpClassNames[k].test(oElement.className)){
				bMatchesAll = false;
				break;
			}
		}
		if(bMatchesAll){
			arrReturnElements.push(oElement);
		}
	}
	return (arrReturnElements)
}