/* DOMScriptsLibrary.js contains highly reusable DOM scripts for use amongs all sites. Updated by JB 6th Nov 2007 */

/* Simplifies onload, you will no longer have to add an onload event call just call addLoadEvent */
function addLoadEvent(func,arg){

if (!arg){
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
}
}
  else{/*if the onload event has an argument/parameter cater for that*/
  if (arg){
  oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func(arg); 
	}
     window.onload = function() {
      oldonload();
      func(arg); 
    }
  }
}
}


/* prepareJumpMenu to handle destination drop down/Jump menu onsubmit event. The function will replace the form action with the value of the selects option. If JS is disabled the default action will occur. You simply need to add the id destination_select */
function prepareJumpMenu(){
 if (!document.getElementById) return false;
 if (!document.getElementById("jumpMenu")) return false;
 var jumpMenuForm = document.getElementById("jumpMenu");
  jumpMenuForm.onsubmit = function(){
   jumpMenuForm.action = document.getElementById("destination_select").value;
  }
}

/* makes all of div clickable, linking to the first href found within the row. You simply need to add the class 'clickBox' */
function clickableDivs(){
if (!document.getElementsByTagName) return false; //added to test the browsers DOM compatibility
if (!document.getElementsByTagName('div')) return false; //added to test for any divs
var divs = document.getElementsByTagName('div');
 for(var i=0,j=divs.length;i<j;i++){
  if (divs[i].className.match('clickBox')){ // .match to deal with multiple class names
  if (!divs[i].getElementsByTagName('a')[0]) continue; // to ensure no errors if href is not present
  divs[i].style.cursor = "pointer"; // change the cursor to a pointer if onclick is applied
   divs[i].onclick = function (){
   
     window.location = this.getElementsByTagName('a')[0].href; //regular link
   }
  }
 }
}


/* function - remove content from an input on focus */
function clearInputContent(){
	if (!document.getElementById) return false;
	if (!document.observe) return false;
	if (!$$('input.resetText').length) return false;
		var elements = $$('input.resetText');
		 	for (var i=0; i<elements.length; i++){
				var originalValue = elements[i].readAttribute('value');
			 	elements[i].onfocus = function(){
					 if (this.value == originalValue){
						 this.clear();
							 this.onblur = function(){
								if (this.value==""){
								this.value = originalValue;	 
							}
						}
					}
				}
		 	}
}

function loadGAScript(){
	/*Check browser for Dom compatibility*/
if (!document.getElementsByTagName) return false;
/*Determines whether the page is using a secure or unsecure protocol*/
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
/*Writes in the script to the document head*/
var gaScript = document.createElement("script");
gaScript.setAttribute("src",gaJsHost +"google-analytics.com/ga.js");
gaScript.setAttribute("type","text/javascript");
var domHead = document.getElementsByTagName("head")[0]
domHead.appendChild(gaScript);
	}
	loadGAScript();

/*Calls the function*/
function callGA(){	
var pageTracker = _gat._getTracker("UA-924458-1");
pageTracker._initData();
pageTracker._trackPageview();
}


/* track links with the class of rss */
function trackRss(){
if (!document.getElementsByTagName) return false; //added to test the browsers DOM compatibility
if (!document.getElementsByTagName('a')) return false; //added to test for any divs
var divs = document.getElementsByTagName('a');
 for(var i=0,j=divs.length;i<j;i++){
  if (divs[i].className.match('rss')){ // .match to deal with multiple class names
   divs[i].onclick = function (){
		var pageTracker = _gat._getTracker("UA-924458-1");
		pageTracker._initData();
		pageTracker._trackPageview("/rss/subscribe/"); 
   }
  }
 }
}
/*Show blog posts widget*/
function blogWidget(){
 if (!document.getElementById) return false;
 if (!document.getElementById("blogWidget")) return false;
 var blogWidget = document.getElementById("blogWidget");
  blogWidget.onclick = function(){
   document.getElementById("homepageContent").style.display="block";
   document.getElementById("homepageContentFirst").style.display="none";
   return false;
  }
}


addLoadEvent(blogWidget);
addLoadEvent(callGA);
addLoadEvent(clickableDivs);
addLoadEvent(trackRss);