// JavaScript Document

// window.addEvent('domready',function() { new SmoothScroll({ duration: 1500 }); });

	function showmenu(){
		if(document.getElementById('submenu').style.visibility == "hidden"){
			document.getElementById('submenu').style.visibility = "visible";
		}
		else{
			document.getElementById('submenu').style.visibility = "hidden";
		}
	}

	function showmenuinfo(){
		if(document.getElementById('submenu').style.visibility == "hidden"){
			document.getElementById('submenu').style.visibility = "visible";
		}
		else{
			document.getElementById('submenu').style.visibility = "hidden";
		}
	}

//	Javascript added by Patrick Hegarty for tracking Phone numbers
//	Added on 07/06/10
//	Modified by Patrick Hegarty on 07/06/10

//	Allows multiple functions to be added to the onload event
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

//Setup for picking the number

var numberTag = '';
var numberHTML = new Array();
var phoneNo = new Array();
var currentURL = document.URL;
var currentDomain ='';
var cookieName ='phoneTracking';
var googlePaid ='Paid';
var googleSearch = 'Search';
var woContact = null;

//	This sets up the various information for altering the site.
//	This needs altering to fit the site it's working on
function setup()
{
	 numberTag = document.getElementById('header');
	 // This is to see if you are on the wocontact page
//	 alert('Element:' + document.getElementById('phoneNumber'));
	 if(document.getElementById('phoneNumber') !=null)
	 {
		 woContact=document.getElementById('phoneNumber');
	 }
	 numberHTML['direct']	= '/images/bg-header.gif';
	 numberHTML['organic']	= '/images/HP_nonpaidsearch_banner.gif';
	 numberHTML['paid']		= '/images/HP_ppc_banner.gif';
	 phoneNo['organic']		= '0844 245 1021';
	 phoneNo['paid']		= '0844 245 1022';
}

// get the Domain the URL is on
function getDomain()
{
	
	// replace Blog Entries
	currentURL = currentURL.replace('blog.','www.');
	
	if(currentURL != '')
	{
		return currentURL.replace(/^http\:\/\/(www\.|)([^\/]+)(.*)$/,'$2');
	}
	else
	{
		return '';
	}
}

//	Pick the Number to display depended upon Referrer
function setNumber()
{
	if(!checkCookie(cookieName))
	{
	//	alert('Referrer:' + document.referrer + '\n');
		var Referrer = document.referrer;
		if(Referrer != null && Referrer != '')
		{
	//		alert('Referrer is Set:' + Referrer + '\nDomain is :' + getDomain());
			currentDomain = getDomain();
			if(Referrer.match(new RegExp('(.*)' + currentDomain + '(.*)$')))
			{
//				alert('Same Domain, internal Referrer\n' + currentURL + ' - ' + Referrer);
			}
			else if(Referrer.match(/^(.*)google(.*)$/))
			{
//				alert('From Google\n' + currentURL + ' - ' + Referrer);
				if( currentURL.match(/^(.*)?gclid\=(.*)$/) || currentURL.match(/^(.*)(\?|\&)utm\_term\=ppc(.*)$/) || document.cookie.match(/^(.*)utmcmd\=\(not\%20set\)(.*)$/) || document.cookie.match(/^(.*)utmcmd\=ppc(.*)$/))
				{
//					alert('From Paid Google Link\n' + currentURL + ' - ' + Referrer);
					numberTag.style.backgroundImage = "url(" + numberHTML['paid'] + ")";
					if(woContact != null) woContact.innerHTML='0845 245 1022';
					setCookie(cookieName,googlePaid,null);
				}
				else
				{
//					alert('From Non-Paid Google link\n' + currentURL + ' - ' + Referrer);
					numberTag.style.backgroundImage = "url(" + numberHTML['organic'] + ")";
					if(woContact != null) woContact.innerHTML='0845 245 1021';
					setCookie(cookieName,googleSearch,null);
				}
			}
			else
			{
				// Other search engines and referrers or sponsor links
//				alert('Another Referrer\n' + currentURL + ' - ' + Referrer);
			}
		}
		else
		{
//			alert('No Referrer, so Direct link\n' + currentURL + ' - ' + Referrer);
	//		numberTag.style.backgroundImage = "url(" + numberHTML['direct'] + ")";
		}
	}
	else
	{
//		alert('Cookie Found :' + getCookie(cookieName));
		var currentCookie = getCookie(cookieName);
		switch(currentCookie)
		{
			case googleSearch : 
				numberTag.style.backgroundImage = "url(" + numberHTML['organic'] + ")";
				woContact.innerHTML = phoneNo['organic'];
				break;
			
			case googlePaid : 
				numberTag.style.backgroundImage = "url(" + numberHTML['paid'] + ")";
				woContact.innerHTML = phoneNo['paid'];
				break;
		}
	}
}

// Cookie Functions

function setCookie(c_name,value,expiredays)
{
//	alert('Setting Cookie');
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+ ";host=" + getDomain() + ";path=/" +
	((expiredays==null) ? "" : ";expires="+exdate.toUTCString());
}

function getCookie(c_name)
{
	if (document.cookie.length>0)
	  {
	  c_start=document.cookie.indexOf(c_name + "=");
	  if (c_start!=-1)
		{
		c_start=c_start + c_name.length+1;
		c_end=document.cookie.indexOf(";",c_start);
		if (c_end==-1) c_end=document.cookie.length;
		return unescape(document.cookie.substring(c_start,c_end));
		}
	  }
	return "";
}

function checkCookie(cookieName)
{
	var currentCookie=getCookie(cookieName);
	if (currentCookie!=null && currentCookie!="")
	  {
		return true;
	  }
	else
	  {
	  	return false;
	  }
}

addLoadEvent(setup);
addLoadEvent(setNumber);
addLoadEvent(init);

// Link Building Pro ver 1.0

function init(){

// Options:
var useMetaKeyword = false; // Otherwise, page title
var minLength = 40; // Min selection chars
var useMetaAuthor = false; // Otherwise use domain
var addLinks = true; // Otherwise, just cite at end
var skip = new Array("home","link","click here"); // Don't link these (lowercase!)
	
function D(b,a,c){if(b.addEventListener)b.addEventListener(a,c,false);else b.attachEvent&&b.attachEvent("on"+a,c)}function o(b,a){if(typeof b=="undefined"||b==null||!RegExp)return false;a=new RegExp("(^|\\s)"+a+"(\\s|$)");if(typeof b=="string")return a.test(b);else if(typeof b=="object"&&b.className)return a.test(b.className);return false}function E(b,a){var c=false,j;for(j=b.parentNode;j!=undefined;){if(b.parentNode==e.body)break;else if(b.parentNode==a){c=true;break}j=j.parentNode}return c}
function F(b){return b.replace(/^\s*/,"")}function G(b){return b.replace(/\s*$/,"")}function H(b){return G(F(b))}var I=new Array("home","link","click here"),e=document,x=window,t=e.getElementsByTagName("body")[0],p=e.getElementsByName("author"),i=e.getElementsByName("keywords"),q=x.location.toString(),u=e.title.toString(),d;if(!Array.indexOf)Array.prototype.indexOf=function(b,a){var c=-1;for(a=a|0;a<this.length||a==-1;a++)if(this[a]==b)c=a;return c};if(i.length>0&&useMetaKeyword){i=e.getElementsByName("keywords")[0].getAttribute("content").split(",");
u=Math.floor(Math.random()*i.length);i=i[u].replace(/^\s*|\s*$/,"")}else i=u;p=(p.length>0&&useMetaAuthor)?p[0].getAttribute("content"):e.domain;var y="<p id='credit'>Read more about <a href='"+q+"'>"+i+"</a> at <a href='http://"+e.domain+"' />"+p+"</a></p>";if(/MSIE/g.test(navigator.userAgent))var v="msie";else if(/Safari/g.test(navigator.userAgent))v="safChrome";q=e.createElement("span");q.setAttribute("id","sasText");t.appendChild(q);d=e.getElementById("sasText");posType=document.all&&!window.opera&&!window.XMLHttpRequest?
"absolute":"fixed";d.style.position=posType;d.style.top="0px";d.style.left="-9999px";D(t,"copy",function(){d.innerHTML=y;if(v=="msie"){for(var b=e.selection.createRange(),a=b.parentElement();a.nodeName!="BODY"&&!o(a,"lbExclude");)a=a.parentNode;if(o(a,"lbExclude"))return true;a=e.body.createTextRange();a.moveToElementText(d);var c=b.duplicate();c=c.htmlText;if(c.length>minLength){d.id="tempSasText";d.innerHTML=c+y;(c=e.getElementById("sasText"))&&c.parentNode.removeChild(c);d.id="sasText";a.select()}}else{b=
x.getSelection();for(a=b.anchorNode;a.nodeName!="BODY"&&!o(a,"lbExclude");)a=a.parentNode;if(o(a,"lbExclude"))return false;if(b==""&&v=="safChrome"){d.innerHTML=t.innerHTML;a=document.createRange();b.removeAllRanges();a.selectNodeContents(d);b.addRange(a)}else if(b.toString().length>minLength){var j=e.getElementById("credit");a=b.getRangeAt(0);c=a.cloneContents();d.id="tempSasText";d.insertBefore(c,j);(c=e.getElementById("sasText"))&&c.parentNode.removeChild(c);d.id="sasText";b.removeAllRanges();a.selectNode(d);
b.addRange(a)}}var w=[];a=d.getElementsByTagName("a");for(b=0;b<a.length;b++)w.push(a[b].href);if(addLinks){a=e.getElementsByTagName("a");for(b=0;b<a.length;b++){var r=a[b].href;if(w.indexOf(r)==-1)if(E(a[b],d)==false){var f=H(a[b].innerHTML).toLowerCase();if(skip.indexOf(f)==-1)if((new RegExp(e.domain,"g")).test(r)){var z=[];function n(g,k,l){for(var A=g.childNodes.length;A-- >0;){var h=g.childNodes[A];if(h.nodeType===1)h.tagName.toLowerCase()!=="a"&&n(h,k,l);else if(h.nodeType===3)for(var m=h.data.length;1;){m=
h.data.lastIndexOf(k,m);if(m===-1||z.indexOf(k.toLowerCase())!==-1)break;var B=/\w/;if(h.nodeValue.charAt(m-1).match(B)||h.nodeValue.charAt(m+f.length).match(B))break;l.call(window,h,m)}}}function s(g,k){g.splitText(k+f.length);var l=e.createElement("a");l.href=r;l.appendChild(g.splitText(k));g.parentNode.insertBefore(l,g.nextSibling);z.push(f.toLowerCase());w.push(r)}n(d,f,s);f=f.charAt(0).toUpperCase()+f.slice(1);n(d,f,s);f=f.toUpperCase();n(d,f,s);f=f.replace(/\w\S*/g,function(g){return g.charAt(0).toUpperCase()+
g.substr(1).toLowerCase()});n(d,f,s)}}}}})}

// Tim's fade image fandango for homepage.
	$(function() {
		$("ul#homemenu span").css("opacity","0");
		$("ul#homemenu span").hover(function () {$(this).stop().animate({opacity: 1}, 250);},
		function () {$(this).stop().animate({opacity: 0}, 750);
		});});