/*****************************************
Author: Ritchie A.
Date: 02/03/07
Description:
- Open new browser windows (externalLinks)
- Back Button (externalLinks)
- sfHover for IE
- HideSelects for IE 5 & 6
*******************************************/


// addLoadEvent, for queuing window.onload init functions
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		};
	};
};

//Opens New Window based on rel name
function externalLinks() {
	if (!document.getElementsByTagName) return;
 		var anchors = document.getElementsByTagName("a");
 		for (var i=0; i<anchors.length; i++) {
  	 	var anchor = anchors[i];
  	 if (anchor.getAttribute("href") &&
       	anchor.getAttribute("rel") == "external")
     	anchor.target = "_blank";
 		}
	}
addLoadEvent(externalLinks);

//Back button is created based on id name - #back
function backLink(){
	var p=document.getElementById('back');//change this to whatever div name you like
		if(p && document.all ){
			var newa=document.createElement('a');
			newa.setAttribute('href','#');
			newa.appendChild(document.createTextNode('back to previous page'));
			newa.onclick=function(){history.back();return false} //for ie
		    p.replaceChild(newa,p.firstChild);
		  	}
		else if (p && document.getElementById ){
			var newa=document.createElement('a');
			newa.setAttribute('href','#');
			newa.appendChild(document.createTextNode('back to previous page'));
			newa.onclick=function(){window.back();return false} //for gecko based browsers
		    p.replaceChild(newa,p.firstChild);	
		}
}
addLoadEvent(backLink);

//Standard sfHover code - additional !document.all to target only IE browsers
menu_sfHover = function() {	
	if(!document.all || !document.createTextNode){return;}
	var sfEls = document.getElementById("primary-nav").getElementsByTagName("LI")
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}	
addLoadEvent(menu_sfHover);


//Hide Select - hides select field from specific subnav.   
menu_hideShow={
	triggerClass:'hide', /*change this according to the class name on your submenu links*/
	init:function(){
		/*Check to see if DOM = IE*/
		if(!document.all || !document.createTextNode){return;}
		/*Find Nav ID*/
		var menu = document.getElementById("primary-nav")
		/*Find a href*/
		var allLinks = menu.getElementsByTagName('a');
		for(var i=0;i<allLinks.length;i++){
		/*if trigglerclass=hide then active the functions below*/
		if(!cssjs('check',allLinks[i],menu_hideShow.triggerClass)){continue;} 
		allLinks[i].onmouseover=function() { 
			hideSelects();
			}
		allLinks[i].onmouseout=function() { 
			showSelects();
		}
		}
	}
	}
addLoadEvent(menu_hideShow.init);	

//DOM checker	
init:function(){
		if(!document.getElementById || !document.createTextNode){return;}
	}
//CSS Checker
function cssjs(a,o,c1,c2){
		switch (a){
			case 'swap':
				o.className=!cssjs('check',o,c1)?o.className.replace(c2,c1):o.className.replace(c1,c2);
			break;
			case 'add':
				if(!DOMhelp.cssjs('check',o,c1)){o.className+=o.className?' '+c1:c1;}
			break;
			case 'remove':
				var rep=o.className.match(' '+c1)?' '+c1:c1;
				o.className=o.className.replace(rep,'');
			break;
			case 'check':
				var found=false;
				var temparray=o.className.split(' ');
				for(var i=0;i<temparray.length;i++){
					if(temparray[i]==c1){found=true;}
				}
				return found;
			break;
		}
	}
//Hide Select Field
 function hideSelects() {
	if ((document.all)&&(navigator.appVersion.indexOf("MSIE 6.")!=-1)||(navigator.appVersion.indexOf("MSIE 5.")!=-1)) /* browser detection for IE5/IE6*/
	var selects = document.getElementsByTagName("SELECT");
	for (var i=0; i<selects.length; i++) {
	var oneSelect = selects[i];
	oneSelect.style.visibility = 'hidden';
  	}
  }
//Show select field
  function showSelects() {
   if ((document.all)&&(navigator.appVersion.indexOf("MSIE 6.")!=-1)||(navigator.appVersion.indexOf("MSIE 5.")!=-1)) /* browser detection for IE5/IE6*/
   var selects = document.getElementsByTagName("SELECT");
   for (var i=0; i<selects.length; i++) {
    var oneSelect = selects[i];
    oneSelect.style.visibility = 'visible';
   }
  }
  


