// ==|JS Common Lib |=================================================== //
// ==|Author : Tom Wilson|============================================== //
// ~~|Inital Functions|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //

// ++|FUNCTIONS IN LIB|+++++++++++++++++++++++++++++++++++++++++++++++++ //

// >>|unObJS()|========================================================= //
// ~~|Desc: This boy appends JS to HTML unobtrusivly, No messy JS.|~~~~~ //

// ++|END: FUNCTIONS IN LIB|++++++++++++++++++++++++++++++++++++++++++++ //




// ~~|UnObtrusive Check|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //
// ~~|This function allows you appened Javascript to Elements without |~ //
// ~~|_going into HTML|~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //

function unObJS() {
  //makes sure there is tags around.
  if (!document.getElementsByTagName) return false;
  
  //Puts all Link tags into an Array
  var links=document.getElementsByTagName("a");
  
  //Loop for Rollover Class
  //Loops through the array to find
  for (var i=0; i < links.length; i++) {
  	
  //Search for A tags with a specific Class name
	if (links[i].className.match("rollover")) {
  
  // If the link is there, creates a function on that element.
  // The Function.
  		//Pre Loads the Images
		var varImgPath = links[i].firstChild.src;
		if (varImgPath) {
			var strImg = varImgPath.substring(varImgPath.lastIndexOf('/') + 1);
			var fileExt = strImg.substring(strImg.lastIndexOf('.'));
			var fileName = strImg.replace(fileExt, '');
	  		ImagePre = new Image(1,1)
			ImagePre.src = "/images/img/" + fileName + "_ovr" + fileExt;
		}		
		//Creates Rollover Function to Swap Images
	  links[i].onmouseover=function() {
			var varImgPath = this.firstChild.src;
			if (varImgPath) {
				var strImg = varImgPath.substring(varImgPath.lastIndexOf('/') + 1);
				var fileExt = strImg.substring(strImg.lastIndexOf('.'));
				var fileName = strImg.replace(fileExt, '');
				this.firstChild.src = "/images/img/" + fileName + "_ovr" + fileExt;
			}
			//alert("/images/img/" + fileName + "_ovr" + fileExt);
			return false;
      }
		
		//Creates Rollover Function to Swap Images Back
	  links[i].onmouseout=function() {
			var varImgPath = this.firstChild.src;
			if(varImgPath) {
				var strImg = varImgPath.substring(varImgPath.lastIndexOf('/') + 1);
				var fileExt = strImg.substring(strImg.lastIndexOf('.'));
				var fileName = strImg.replace("_ovr" + fileExt, '');
				this.firstChild.src = "/images/img/" + fileName + fileExt;
			}
			//alert("/images/img/" + fileName + "_ovr" + fileExt);
			return false;
      }
    }
  }

  
  //Loop for Popups at the bottom
  var popups=document.getElementsByTagName("LI");
  //Loops through the array to find
  for (var i=0; i < popups.length; i++) {
  //Search for A tags with a specific Class name
	if (popups[i].className.match("PopHere")) {
  // If the link is there, creates a function on that element.
  // The Function.
	   popups[i].onmouseover=function() {
			var me = this; me.timer = setTimeout(function() { timeout_temp(me) }, 1000);
	   }
	   popups[i].onmouseout=function() {
			clearTimeout(this.timer);
			divBlock = this.getElementsByTagName("div");
			divBlock[0].style.display = "none";
			return false;
      }
    
	 }
  }
}
function timeout_temp(el) {
			divBlock = el.getElementsByTagName("div");
			divBlock[0].style.display = "block";
			return false;
}

function setHighlighting(oElement, setting)  {
    var style = '1px solid rgb(204, 204, 204)';
    if (setting === true) {
        style = '1px solid rgb(255, 204, 0)';
    } 
    oElement.style['border'] = style;
}

function setErrorHighlighting(sElement, setting)  {
    var oElement = document.getElementById(sElement);
    var style = '1px solid rgb(204, 204, 204)';
    if (setting === true) {
        style = '1px solid red';
    } 
	if (oElement && oElement.type != 'hidden') {
	    oElement.style['border'] = style;
	}
}

///////////////////////////////////////////////////////////////////
// Checkboxes
var Input = {
	initialize: function() {
		if(document.getElementsByTagName("form")) {
			var divs = document.getElementsByTagName("span");
			for(var i = 0; i < divs.length; i++) {
				if(divs[i].className.match("checkbox") || divs[i].className.match("radio")) {
					divs[i].onmousedown = Input.effect;
					divs[i].onmouseup = Input.handle;
					window.onmouseup = Input.clear;
				}
			}
		}
	},

	effect: function() {
		if(this.className == "checkbox" || this.className == "radio") {
			this.style.backgroundPosition = "0px -26px";
		} else {
			this.style.backgroundPosition = "0px -79px";
		}
	},

	handle: function() {
		selector = this.getElementsByTagName("input")[0];
		if(this.className == "checkbox") {
			selector.checked = true;
			this.className = "checkbox selected";
			this.style.backgroundPosition = "0px -52px";
		} else if(this.className == "checkbox selected") {
			selector.checked = false;
			this.className = "checkbox";
			this.style.backgroundPosition = "0px 0px";
		} else {
			selector.checked = true;
			this.className = "radio selected";
			this.style.backgroundPosition = "0px -52px";
			inputs = document.getElementsByTagName("input");
			for(i = 0; i < inputs.length; i++) {
				if(inputs[i].getAttribute("name") == selector.getAttribute("name")) {
					if(inputs[i].type != 'hidden' && inputs[i] != selector) {
						inputs[i].parentNode.className = "radio";
						inputs[i].parentNode.style.backgroundPosition = "0px 0px";
					}
				}
			}
		}
	},

	clear: function() {
		divs = document.getElementsByTagName("span");
		for(var i = 0; i < divs.length; i++) {
			if(divs[i].className == "checkbox" || divs[i].className == "radio") {
				divs[i].style.backgroundPosition = "0px 0px";
			} else if(divs[i].className == "checkbox selected" || divs[i].className == "radio selected") {
				divs[i].style.backgroundPosition = "0px -52px";
			}
		}
	}
}

addLoadEvent(unObJS);
addLoadEvent(Input.initialize);

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

//** Tab Content script- © Dynamic Drive DHTML code library (http://www.dynamicdrive.com)
//** Last updated: Nov 8th, 06

var enabletabpersistence=1 //enable tab persistence via session only cookies, so selected tab is remembered?

////NO NEED TO EDIT BELOW////////////////////////
var tabcontentIDs=new Object()

function expandcontent(linkobj){
var ulid=linkobj.parentNode.parentNode.id //id of UL element
var ullist=document.getElementById(ulid).getElementsByTagName("li") //get list of LIs corresponding to the tab contents
for (var i=0; i<ullist.length; i++){
ullist[i].className=""  //deselect all tabs
if (typeof tabcontentIDs[ulid][i]!="undefined") //if tab content within this array index exists (exception: More tabs than there are tab contents)
document.getElementById(tabcontentIDs[ulid][i]).style.display="none" //hide all tab contents
}
linkobj.parentNode.className="selected"  //highlight currently clicked on tab
document.getElementById(linkobj.getAttribute("rel")).style.display="block" //expand corresponding tab content
saveselectedtabcontentid(ulid, linkobj.getAttribute("rel"))
}

function expandtab(tabcontentid, tabnumber){ //interface for selecting a tab (plus expand corresponding content)
var thetab=document.getElementById(tabcontentid).getElementsByTagName("a")[tabnumber]
if (thetab.getAttribute("rel"))
expandcontent(thetab)
}

function savetabcontentids(ulid, relattribute){// save ids of tab content divs
if (typeof tabcontentIDs[ulid]=="undefined") //if this array doesn't exist yet
tabcontentIDs[ulid]=new Array()
tabcontentIDs[ulid][tabcontentIDs[ulid].length]=relattribute
}

function saveselectedtabcontentid(ulid, selectedtabid){ //set id of clicked on tab as selected tab id & enter into cookie
if (enabletabpersistence==1) //if persistence feature turned on
setCookie(ulid, selectedtabid)
}

function getullistlinkbyId(ulid, tabcontentid){ //returns a tab link based on the ID of the associated tab content
var ullist=document.getElementById(ulid).getElementsByTagName("li")
for (var i=0; i<ullist.length; i++){
if (ullist[i].getElementsByTagName("a")[0].getAttribute("rel")==tabcontentid){
return ullist[i].getElementsByTagName("a")[0]
break
}
}
}

function initializetabcontent(){
for (var i=0; i<arguments.length; i++){ //loop through passed UL ids
if (enabletabpersistence==0 && getCookie(arguments[i])!="") //clean up cookie if persist=off
setCookie(arguments[i], "")
var clickedontab=getCookie(arguments[i]) //retrieve ID of last clicked on tab from cookie, if any
var ulobj=document.getElementById(arguments[i])
var ulist=ulobj.getElementsByTagName("li") //array containing the LI elements within UL
for (var x=0; x<ulist.length; x++){ //loop through each LI element
var ulistlink=ulist[x].getElementsByTagName("a")[0]
if (ulistlink.getAttribute("rel")){
savetabcontentids(arguments[i], ulistlink.getAttribute("rel")) //save id of each tab content as loop runs
ulistlink.onclick=function(){
expandcontent(this)
return false
}
if (ulist[x].className=="selected" && clickedontab=="") //if a tab is set to be selected by default
expandcontent(ulistlink) //auto load currenly selected tab content
}
} //end inner for loop
if (clickedontab!=""){ //if a tab has been previously clicked on per the cookie value
var culistlink=getullistlinkbyId(arguments[i], clickedontab)
if (typeof culistlink!="undefined") //if match found between tabcontent id and rel attribute value
expandcontent(culistlink) //auto load currenly selected tab content
else //else if no match found between tabcontent id and rel attribute value (cookie mis-association)
expandcontent(ulist[0].getElementsByTagName("a")[0]) //just auto load first tab instead
}
} //end outer for loop
}


function getCookie(Name){ 
var re=new RegExp(Name+"=[^;]+", "i"); //construct RE to search for target name/value pair
if (document.cookie.match(re)) //if cookie found
return document.cookie.match(re)[0].split("=")[1] //return its value
return ""
}

function setCookie(name, value){
document.cookie = name+"="+value //cookie value is domain wide (path=/)
}

function prepend_protocol_if_none(url, node, on_save) {
	if (url.indexOf("://") == -1) {
		return 'http://' + url;
	}
	return url;
}
