// JavaScript Document

// include CSS file
document.write('<link rel="stylesheet" type="text/css" href="styles/menu.css" />');


// IE 6.0 Flicker Bug fix
try {
  document.execCommand('BackgroundImageCache', false, true);
} catch(e) {}


// global to store the current page mainmenu item
var SLCTD_MM_ITEM;
// global to store the current page submenu caption
var SLCTD_SM_CAPTION;
// global to store the current page submenu bullet
var SLCTD_SM_BULLET;


/*
Params:
	itemId: The specified mainmenu item id.
Desc:
	Sets the specified mainmenu item to be selected permanently
*/
function mmMakeSelected(itemId) {
	// check if specified item exist
	if (SLCTD_MM_ITEM = document.getElementById(itemId)) {
		//set selected blurb to be the default 'home' blurb
		SLCTD_MM_ITEM.className = "item_down";
	}
}

/*
Params:
	itemId: The specified mainmenu item id.
	blurId: The specified blurb item id.
Desc:
	Switches the mainmenu item and shows the blurb item.
*/
function mmOver(itemId, blurbId) {
	// check if specified item exists
	if(menuItem = document.getElementById(itemId))
	{
		
		// activate specified item if it is not for the current page
		if (menuItem != SLCTD_MM_ITEM) {
			menuItem.className = "item_over";
		}
	
		var def_blrb = document.getElementById('default_blurb');
		
		// check if blurb exists, i.e. HOMEPAGE ONLY
		if (blurbItem = document.getElementById(blurbId)) {
			// show selected blurb
			blurbItem.style.visibility = "visible";
			// hide default blurb
			def_blrb.style.visibility = "hidden";
		}
	}
}

/*
Params:
	itemId: The specified mainmenu item id.
	blurId: The specified blurb item id.
Desc:
	Unswitches the mainmenu item and hides the blurb item.
*/
function mmOut(itemId, blurbId) {
	// check if specified item exists
	if(menuItem = document.getElementById(itemId))
	{
		// unactivate specified item if it is not for the current page
		if (menuItem != SLCTD_MM_ITEM) {
			menuItem.className = "item";
		}
	
		var def_blrb = document.getElementById('default_blurb');
		
		// check if blurb exists, i.e. HOMEPAGE ONLY
		if (blurbItem = document.getElementById(blurbId)) {
			// hide specified blurb
			blurbItem.style.visibility = "hidden";
			// hide default blurb
			def_blrb.style.visibility = "visible";
		}
	}
}

/*
Params:
	captionId: The specified submenu caption id.
	itemId: The specified mainmenu item id.
Desc:
	Sets the specified mainmenu item to be selected permanently.
*/
function smMakeSelected(captionId, bulletId) {
	// check if specified submenu caption exists
	if (SLCTD_SM_CAPTION = document.getElementById(captionId)) {	
		//set caption to be selected on pageload
		SLCTD_SM_CAPTION.className = "caption_down";
	}
	// check if specified submenu bullet exists
	if (SLCTD_SM_BULLET = document.getElementById(bulletId)) {	
		//set bullet to be selected on pageload
		SLCTD_SM_BULLET.className = "bullet_down";
	}
}


/*
Params:
	captionId: The specified submenu caption id.
	bulletId: The specified submenu bullet id.
Desc:
	Switches the color of the submenu bullet.
*/
function smOver(captionId, bulletId) {
	// check if submenu caption exists
	if(captionItem = document.getElementById(captionId)) {
		// change back color of caption if it is not for the current page
		if (captionItem != SLCTD_SM_CAPTION) {
			captionItem.className = "caption_over";
		}	
	}
	// check if submenu bullet exists
	if(bulletItem = document.getElementById(bulletId)) {
		// change color of bullet if it is not for the current page
		if (bulletItem != SLCTD_SM_BULLET) {
			bulletItem.className = "bullet_over";
		}		
	}
}

/*
Params:
	captionId: The specified submenu caption id.
	bulletId: The specified submenu bullet id.
Desc:
	Unswitches the color of the submenu bullet.
*/
function smOut(captionId, bulletId) {
	// check if submenu caption exists
	if(captionItem = document.getElementById(captionId)) {
		// change back color of caption if it is not for the current page
		if (captionItem != SLCTD_SM_CAPTION) {
			captionItem.className = "caption";
		}	
	}
	// check if submenu bullet exists
	if(bulletItem = document.getElementById(bulletId)) {
		// change back color of bullet if it is not for the current page
		if (bulletItem != SLCTD_SM_BULLET) {
			bulletItem.className = "bullet";
		}	
	}
}

function getAllChildren(e) {
  // Returns all children of element. Workaround required for IE5/Windows. Ugh.
  return e.all ? e.all : e.getElementsByTagName('*');
}

document.getElementsBySelector = function(selector) {
  // Attempt to fail gracefully in lesser browsers
  if (!document.getElementsByTagName) {
    return new Array();
  }
  // Split selector in to tokens
  var tokens = selector.split(' ');
  var currentContext = new Array(document);
  for (var i = 0; i < tokens.length; i++) {
    token = tokens[i].replace(/^\s+/,'').replace(/\s+$/,'');;
    if (token.indexOf('#') > -1) {
      // Token is an ID selector
      var bits = token.split('#');
      var tagName = bits[0];
      var id = bits[1];
      var element = document.getElementById(id);
      if (tagName && element.nodeName.toLowerCase() != tagName) {
        // tag with that ID not found, return false
        return new Array();
      }
      // Set currentContext to contain just this element
      currentContext = new Array(element);
      continue; // Skip to next token
    }
    if (token.indexOf('.') > -1) {
      // Token contains a class selector
      var bits = token.split('.');
      var tagName = bits[0];
      var className = bits[1];
      if (!tagName) {
        tagName = '*';
      }
      // Get elements matching tag, filter them for class selector
      var found = new Array;
      var foundCount = 0;
      for (var h = 0; h < currentContext.length; h++) {
        var elements;
        if (tagName == '*') {
            elements = getAllChildren(currentContext[h]);
        } else {
            elements = currentContext[h].getElementsByTagName(tagName);
        }
        for (var j = 0; j < elements.length; j++) {
          found[foundCount++] = elements[j];
        }
      }
      currentContext = new Array;
      var currentContextIndex = 0;
      for (var k = 0; k < found.length; k++) {
        if (found[k].className && found[k].className.match(new RegExp('\\b'+className+'\\b'))) {
          currentContext[currentContextIndex++] = found[k];
        }
      }
      continue; // Skip to next token
    }
    // Code to deal with attribute selectors
    if (token.match(/^(\w*)\[(\w+)([=~\|\^\$\*]?)=?"?([^\]"]*)"?\]$/)) {
      var tagName = RegExp.$1;
      var attrName = RegExp.$2;
      var attrOperator = RegExp.$3;
      var attrValue = RegExp.$4;
      if (!tagName) {
        tagName = '*';
      }
      // Grab all of the tagName elements within current context
      var found = new Array;
      var foundCount = 0;
      for (var h = 0; h < currentContext.length; h++) {
        var elements;
        if (tagName == '*') {
            elements = getAllChildren(currentContext[h]);
        } else {
            elements = currentContext[h].getElementsByTagName(tagName);
        }
        for (var j = 0; j < elements.length; j++) {
          found[foundCount++] = elements[j];
        }
      }
      currentContext = new Array;
      var currentContextIndex = 0;
      var checkFunction; // This function will be used to filter the elements
      switch (attrOperator) {
        case '=': // Equality
          checkFunction = function(e) { return (e.getAttribute(attrName) == attrValue); };
          break;
        case '~': // Match one of space seperated words 
          checkFunction = function(e) { return (e.getAttribute(attrName).match(new RegExp('\\b'+attrValue+'\\b'))); };
          break;
        case '|': // Match start with value followed by optional hyphen
          checkFunction = function(e) { return (e.getAttribute(attrName).match(new RegExp('^'+attrValue+'-?'))); };
          break;
        case '^': // Match starts with value
          checkFunction = function(e) { return (e.getAttribute(attrName).indexOf(attrValue) == 0); };
          break;
        case '$': // Match ends with value - fails with "Warning" in Opera 7
          checkFunction = function(e) { return (e.getAttribute(attrName).lastIndexOf(attrValue) == e.getAttribute(attrName).length - attrValue.length); };
          break;
        case '*': // Match ends with value
          checkFunction = function(e) { return (e.getAttribute(attrName).indexOf(attrValue) > -1); };
          break;
        default :
          // Just test for existence of attribute
          checkFunction = function(e) { return e.getAttribute(attrName); };
      }
      currentContext = new Array;
      var currentContextIndex = 0;
      for (var k = 0; k < found.length; k++) {
        if (checkFunction(found[k])) {
          currentContext[currentContextIndex++] = found[k];
        }
      }
      continue; // Skip to next token
    }
    // If we get here, token is JUST an element (not a class or ID selector)
    tagName = token;
    var found = new Array;
    var foundCount = 0;
    for (var h = 0; h < currentContext.length; h++) {
      var elements = currentContext[h].getElementsByTagName(tagName);
      for (var j = 0; j < elements.length; j++) {
        found[foundCount++] = elements[j];
      }
    }
    currentContext = found;
  }
  return currentContext;
}

String.prototype.trim=function(){
    return this.replace(/^\s*|\s*$/g,'');
}

function findCoord(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

