// Function retrieved from: http://cass- hacks.com/articles/code/js_url_encode_decode/
function URLEncode (clearString) {
  var output = '';
  var x = 0;
  clearString = clearString.toString();
  var regex = /(^[a-zA-Z0-9_.]*)/;
  while (x < clearString.length) {
    var match = regex.exec(clearString.substr(x));
    if (match != null && match.length > 1 && match[1] != '') {
    	output += match[1];
      x += match[1].length;
    } else {
      if (clearString[x] == ' ')
        output += '+';
      else {
        var charCode = clearString.charCodeAt(x);
        var hexVal = charCode.toString(16);
        output += '%' + ( hexVal.length < 2 ? '0' : '' ) + hexVal.toUpperCase();
      }
      x++;
    }
  }
  return output;
}

function extractPageName(hrefString)
{
	var arr = hrefString.split('/');
	return  (arr.length < 2) ? hrefString : arr[arr.length-1].toLowerCase();
}
 
function setActiveMenu(arr, crtPage)
{
	for (var i=0; i < arr.length; i++)
	{
		if(extractPageName(arr[i].href) == crtPage)
		{
			if (arr[i].parentNode.tagName != "DIV")
			{
				arr[i].className = "current";
				//arr[i].parentNode.className = "current";
			}
		}
	}
}
 
function setPage(elmID)
{
	hrefString = document.location.href ? document.location.href : document.location;
 
	if (document.getElementById(elmID) !=null )
	setActiveMenu(document.getElementById(elmID ).getElementsByTagName("a"), extractPageName(hrefString));
}

// handle nav selection - lots of nice chaining :-)
function selectNav() {
  $(this)
    .parents('ul:first') // find the first UL parent
      .find('a') // find all the A elements
        .removeClass('selected') // remove from all
      .end() // go back to all A elements
    .end() // go back to 'this' element
    .addClass('selected');
}


$(document).ready(function() {
     $(".menuitem").hover(
         function () {
             $(this).css({
                 'background-image' : 'url(images/on.png)'
             });
         },
         function (){
             $(this).css({
                 'background-image' : 'url(images/off.png)'
             });
         }
     );
    
    $("#navigation").find('a').click(function() {
        $(this)
            .parents('ul:first') // find the first UL parent
                .find('a') // find all the A elements
                    .removeClass('current') // remove from all
                .end() // go back to all A elements
            .end() // go back to 'this' element
        .addClass('current');
    });
    
    
setPage("leftNav");
//setPage ("menuUL");

});

