///////////////////////////////////////
//
//  Generic onload by Brothercake
//  http://www.brothercake.com/
//
///////////////////////////////////////

//setup onload function
if(typeof window.addEventListener != 'undefined'){
	//.. gecko, safari, konqueror and standard
	window.addEventListener('load', initJavaScript, false);
}else if(typeof document.addEventListener != 'undefined'){
	//.. opera 7
	document.addEventListener('load', initJavaScript, false);
}else if(typeof window.attachEvent != 'undefined'){
	//.. win/ie
	window.attachEvent('onload', initJavaScript);
}

function initJavaScript() {

	if (document.getElementById('header')) { 
		//initiate the header hover links
		headHoverlinks();

    //intitate the new target links
    getPopuplinks();

		//initiate the color changing code
		footColorlinks();
	}

}


//gets the header links
function headHoverlinks(){
	var headerDiv = document.getElementById("header");
	var lnks = headerDiv.getElementsByTagName("a");

	for(var i=0;i<lnks.length;i++){
		lnks[i].onmouseover=function(){
			applyOver(this);
		}
		lnks[i].onmouseout=function(){
			applyOut(this);
		}
	}
}

//changes mouseover class
function applyOver(obj){
	arr=new Array()
	arr["home"]="home_hover"
	arr["contact"]="contact_hover"
	arr["faqs"]="faqs_hover"

	if(arr[obj.id]){
		var hiddenImages = document.getElementById('hidden_images');
		hiddenImages.className = arr[obj.id];
	}
}

//changes mouseout class
function applyOut(obj){
	arr=new Array()
	arr["home"]="blank"
	arr["contact"]="blank"
	arr["faqs"]="blank"

	if(arr[obj.id]){
		var hiddenImages = document.getElementById('hidden_images');
		hiddenImages.className = arr[obj.id];
	}
}


//finds all links to open in a new window
function getPopuplinks(){
	lnks=document.getElementsByTagName("a")
	for(var i=0;i<lnks.length;i++){
		if (lnks[i].className.match(/\btarget_new\b/i)){
			lnks[i].onclick=function(){
				applyTargetnew(this);
				return false;
			}
		}
	}
}

//opens links a new window
function applyTargetnew(obj){
	window.open(obj);
}


//Start of style changer code
function footColorlinks(){
	var footerDiv = document.getElementById("footer");
	var lnks = footerDiv.getElementsByTagName("a");

	for(var i=0;i<lnks.length;i++){
		lnks[i].onclick=function(){
			applyClick(this);
			return false;
		}
	}
}

function applyClick(obj){
	arr=new Array()
	arr["yellow"]="yellow"
	arr["black"]="black"
	arr["purple"]="purple"
	arr["green"]="green"
	arr["white"]="white"
	arr["orange"]="orange"

	if(arr[obj.id]){
		changeColor(arr[obj.id]);
	}
}


//Changes layout color
function changeColor(title) {
	var i, a, main;
	for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
		if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
			a.disabled = true;
			if(a.getAttribute("title") == title) a.disabled = false;
		}
	}
	backToTop();
	var title = getActiveStyleSheet();
	createCookie("color", title, 365);
}

//Gets the current stylesheet in use
function getActiveStyleSheet() {
	var i, a;
	for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
		if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
	}
	return 'orange';
}

//Gets the preferred stylesheet
function getPreferredStyleSheet() {
	var i, a;
	for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
		if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("rel").indexOf("alt") == -1 && a.getAttribute("title")) return a.getAttribute("title");
	}
	return 'orange';
}

//Scrolls page to the top
function backToTop() {
    var x1 = x2 = x3 = 0;
    var y1 = y2 = y3 = 0;

    if (document.documentElement) {
        x1 = document.documentElement.scrollLeft || 0;
        y1 = document.documentElement.scrollTop || 0;
    }

    if (document.body) {
        x2 = document.body.scrollLeft || 0;
        y2 = document.body.scrollTop || 0;
    }

    x3 = window.scrollX || 0;
    y3 = window.scrollY || 0;

    var x = Math.max(x1, Math.max(x2, x3));
    var y = Math.max(y1, Math.max(y2, y3));

    window.scrollTo(Math.floor(x / 2), Math.floor(y / 2));

    if (x > 0 || y > 0) {
        window.setTimeout("backToTop()", 25);
    }
}

//cookie create function
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

//cookie read function
function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return 'orange';
}
//Ends style changer code


//Open websites in a new window
function pop(id) {
	window.open(id);
}

//Creates auto sized popup window
function openPopup (imageURL, caption) {

  var windowTop = 100;                // Top position of popup
  var windowLeft = 100                // Left position of popup
  var defaultWidth = 730;             // Default width (for browsers that cannot resize)
  var defaultHeight = 532;            // Default height (for browsers that cannot resize)
  var onLoseFocusExit = true;         // Set if window to exit when it loses focus
  var undefined;

  var Options = "width=" + defaultWidth + ",height=" + defaultHeight + ",top=" + windowTop + ",left=" + windowLeft + ",resizable"
  
  var myScript = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n" +
    "<html>\n" + 
    "<head>\n" + 
    "<title>" + caption + "\</title>\n" +
    "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\">\n" +
    "<meta http-equiv=\"Content-Language\" content=\"en-gb\">\n" +
    "<script language=\"JavaScript\" type=\"text/javascript\">\n" +
    "function resizewindow () {\n" +
    "  var width = document.myimage.width;\n" + 
    "  var height = document.myimage.height;\n";
  
  // Netscape  
  if (navigator.appName.indexOf("Netscape") != -1) {  
    myScript = myScript +  "  window.innerHeight = height;\n  window.innerWidth = width;\n"
  }
   
  // Opera 
  else if (navigator.appName.indexOf("Opera") != -1) {
    myScript = myScript +  "  window.resizeTo (width+12, height+31);\n"
  }
  
  // Microsoft 
  else if (navigator.appName.indexOf("Microsoft") != -1) { 
    myScript = myScript + "  window.resizeTo (width+12, height+31);\n" 
  }
  
  // Assume a frig factor for any other browsers
  else {
    myScript = myScript + "  window.resizeTo (width+14, height+34);\n"
  }
      
  myScript = myScript + "}\n" + "window.onload = resizewindow;\n" +
    "</script>\n</head>\n" + "<body ";
    
  if (onLoseFocusExit) {myScript = myScript + "onblur=\"self.close()\" ";}
    
  myScript = myScript + "style=\"margin: 0px; padding: 0px;\">\n" +
    "<img src=\"" + imageURL + "\" alt=\"" + caption + "\" title=\"" + caption + "\" name=\"myimage\">\n" + 
    "</body>\n" +  "</html>\n";
        
  
  // Create the popup window
  var imageWindow = window.open ("","imageWin",Options);
  imageWindow.document.write (myScript)
  imageWindow.document.close ();
  if (window.focus) imageWindow.focus();
  return false;
}