/*	-------------------------------------------------------------
	MCC Computers Ltd
	Making IT simple
	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	Description:	Generic Utilties / Functions
	Filename:		utilities.js
	Author:			Dave Harrison
	Version:		1.0
	Date:			14/02/2006
	-------------------------------------------------------------	*/


// <![CDATA[
favicon = new Image();
favicon.src="../favicon.ico"; 
// ]]>


function addEvent(obj, evType, fn, useCapture){
  if (obj.addEventListener){
    obj.addEventListener(evType, fn, useCapture);
    return true;
  } else if (obj.attachEvent){
    var r = obj.attachEvent("on"+evType, fn);
    return r;
  } else {
    alert("Handler could not be attached");
  }
}

function removeEvent(obj, evType, fn, useCapture){
  if (obj.removeEventListener){
    obj.removeEventListener(evType, fn, useCapture);
    return true;
  } else if (obj.detachEvent){
    var r = obj.detachEvent("on"+evType, fn);
    return r;
  } else {
    alert("Handler could not be removed");
  }
}


/*	-------------------------------------------------------------
	function toggleAccreditions()
	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	Description:	We use this to toggle the accreditions area for nosey people?.
	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	Credits:		none
	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -	*/

function toggleAccreditions(){
	// if browser doesn't support this method, return false
	if(!document.getElementById) return false;
	
	// create the anchor content and element
	var linkText = document.createTextNode('Show all');
	var toggleLink = document.createElement('a');
	toggleLink.href = 'javascript:void(null)';
	toggleLink.appendChild(linkText);
	
	//create a paragraph element for the anchor to live in
	var linkPara = document.createElement('p');
	linkPara.setAttribute('id','toggle');
	linkPara.appendChild(toggleLink);
	
	// Get a reference to the accreditions area and it's parent div
	var accreditionsArea = document.getElementById('accreditions');
	var accreditionsTitle = document.getElementById('accreditionstitle');
	
	//place the paragraph element at the end of the comment fieldset
	accreditionsArea.insertBefore(linkPara,accreditionsTitle);
	// set the callback function
	toggleLink.onclick = function(){
		var theText = this.firstChild;
		var newText;
		var accreditionsArea = document.getElementById('accreditions');
		
		if(theText.nodeValue == 'Show all'){
			// make the height of the accredition area bigger
			//accreditionsArea.setAttribute('class','accreditionsshowall');
			accreditionsArea.style.height = '650px';
			
			// I was origionally setting the nodevalue of the anchor text.
			// however the size of the node wasn't changing, so I figured I'd have to
			// use the replace child method instead
			newText = document.createTextNode('Show just two');
			this.replaceChild(newText,theText);
			
		} else {
			// return the accredition area to it's default html size
			//accreditionsArea.setAttribute('class','');
			accreditionsArea.style.height = '150px';
			newText = document.createTextNode('Show all');
			this.replaceChild(newText,theText);
			
		}

	}
		
}

	/*	-------------------------------------------------------------
	function doOnResize()
	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	Description:	We use this to trigger the determinestyle.
	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	Credits:		Script found on uxmag who got it from 
					particle Tree who got it from.....
					http://www.uxmag.com
					http://www.particletree.com
	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -	*/
	
	function doOnResize () {
			//alert("resizing");			
			determineStyle();		
	}
	
		
/*	-------------------------------------------------------------
	function getBrowserWidth()
	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	Description:	We use this to determine the width of the
					browser. Strange that eh?.
	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	Credits:		Script found on uxmag who got it from 
					particle Tree who got it from.....
					http://www.uxmag.com
					http://www.particletree.com
	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -	*/
	
		<!--
		function getBrowserWidth(){
			//alert("checking browser width");
			
			if (window.innerWidth){
				//alert("1=" + window.innerWidth);
				return window.innerWidth;
				
				}
			else if (document.documentElement && document.documentElement.clientWidth != 0){
				//alert("2=" + document.documentElement.clientWidth);
				return document.documentElement.clientWidth;
				
				}
			else if (document.body){return document.body.clientWidth;
				//alert("3=" + document.body.clientWidth);
				}		
				return 0;
		}
		//-->

/*	-------------------------------------------------------------
	function createCookie(),readCookie(name)
	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	Description:	This fellow creates and reads cookies
					A baker if you will!
	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	Credits:		Mash up of style switcher found on A List Apart
					http://www.alistapart.com
	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -	*/
		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=/";
		}
		
		//wee function to read cookie
		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 null;
		}

/*	-------------------------------------------------------------
	function determineStyle()
	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	Description:	With this here function we can swap styles.
	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	Credits:		Script found on A List Apart
					http://www.alistapart.com
	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -	*/
	
		<!--
		function determineStyle(){
			//alert("trying to determine style");
			//some browser specific stuff
			var moze, cookie, expl;
			if(document.getElementById) moze=true;
			if(navigator.appName.indexOf("Explorer")>-1) expl=true;
			
			
			var browserWidth = getBrowserWidth();
			var cookie = readCookie("style");
				
			var i, a, main;
			
			if (browserWidth <= 1025){
				setActiveStyleSheet('narrow')
			}
			
			if (browserWidth > 1025){
				setActiveStyleSheet('wide')
			}
			
			if (cookie == 'textversion'){
				setActiveStyleSheet('textversion')
			}
		}
		//-->
		
	/*	-------------------------------------------------------------
	function getPreferredStyleSheet()
	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	Description:	With this here function we determine which to show?.
	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	Credits:		Script found on A List Apart
					http://www.alistapart.com
	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -	*/
	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 null;
	}
/*	-------------------------------------------------------------
	function terminateSwitcher()
	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	Description:	With this here function we store cookie.
	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	Credits:		Script found on A List Apart
					http://www.alistapart.com
	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -	*/

	//store how to display page in cookie
	function terminateSwitcher() {
	  var title = getActiveStyleSheet();
	  createCookie("style", title, 365);
	}
	
	/*	-------------------------------------------------------------
	function setActiveStyleSheet()
	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	Description:	With this here function we store cookie.
	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	Credits:		Script found on A List Apart
					http://www.alistapart.com
	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -	*/
	function setActiveStyleSheet(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;
		}
	  }
	}

	/*	-------------------------------------------------------------
	function getActiveStyleSheet()
	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	Description:	With this here function we store cookie.
	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	Credits:		Script found on A List Apart
					http://www.alistapart.com
	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -	*/
	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 null;
	}


/*	-------------------------------------------------------------
	function prepareSwitcher()
	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	Description:	Use unobtrusive method to attach DOM behaviours
					to the style switcher links
	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	Credits:		Toll and hard effort!
	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -	*/

	function prepareSwitcher() {
	  //alert("im here prepareswitcher");
	  if (!document.getElementById || !document.getElementsByTagName || !document.getElementById("styleswitcher")) return false;
	  var tv = document.getElementById("sstextversion")
	  var gv = document.getElementById("ssmaster")
	  tv.onclick = function(){
			setActiveStyleSheet('textversion');return false;
	  }  
	  gv.onclick = function(){
			setActiveStyleSheet('master');return false;
	  }  
	}
	

/*	-------------------------------------------------------------
	function fade_element()
	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	Description:	The Fade Anything Technique
	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	Credits:		
	// @namespace http://www.axentric.com/aside/fat/
	// @version   1.0-RC1
	// @author    Adam Michela
	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -	*/
var Fat = {
	make_hex : function (r,g,b) 
	{
		r = r.toString(16); if (r.length == 1) r = '0' + r;
		g = g.toString(16); if (g.length == 1) g = '0' + g;
		b = b.toString(16); if (b.length == 1) b = '0' + b;
		return "#" + r + g + b;
	},
	fade_all : function ()
	{
		var a = document.getElementsByTagName("*");
		for (var i = 0; i < a.length; i++) 
		{
			var o = a[i];
			var r = /fade-?(\w{3,6})?/.exec(o.className);
			if (r)
			{
				if (!r[1]) r[1] = "";
				if (o.id) Fat.fade_element(o.id,null,null,"#"+r[1]);
			}
		}
	},
	fade_element : function (id, fps, duration, from, to) 
	{
		if (!fps) fps = 30;
		if (!duration) duration = 3000;
		if (!from || from=="#") from = "#a7d4bf";
		if (!to) to = this.get_bgcolor(id);
		
		var frames = Math.round(fps * (duration / 1000));
		var interval = duration / frames;
		var delay = interval;
		var frame = 0;
		
		if (from.length < 7) from += from.substr(1,3);
		if (to.length < 7) to += to.substr(1,3);
		
		var rf = parseInt(from.substr(1,2),16);
		var gf = parseInt(from.substr(3,2),16);
		var bf = parseInt(from.substr(5,2),16);
		var rt = parseInt(to.substr(1,2),16);
		var gt = parseInt(to.substr(3,2),16);
		var bt = parseInt(to.substr(5,2),16);
		
		var r,g,b,h;
		while (frame < frames)
		{
			r = Math.floor(rf * ((frames-frame)/frames) + rt * (frame/frames));
			g = Math.floor(gf * ((frames-frame)/frames) + gt * (frame/frames));
			b = Math.floor(bf * ((frames-frame)/frames) + bt * (frame/frames));
			h = this.make_hex(r,g,b);
		
			setTimeout("Fat.set_bgcolor('"+id+"','"+h+"')", delay);

			frame++;
			delay = interval * frame; 
		}
		setTimeout("Fat.set_bgcolor('"+id+"','"+to+"')", delay);
	},
	set_bgcolor : function (id, c)
	{
		var o = document.getElementById(id);
		o.style.backgroundColor = c;
	},
	get_bgcolor : function (id)
	{
		var o = document.getElementById(id);
		while(o)
		{
			var c;
			if (window.getComputedStyle) c = window.getComputedStyle(o,null).getPropertyValue("background-color");
			if (o.currentStyle) c = o.currentStyle.backgroundColor;
			if ((c != "" && c != "transparent") || o.tagName == "BODY") { break; }
			o = o.parentNode;
		}
		if (c == undefined || c == "" || c == "transparent") c = "#FFFFFF";
		var rgb = c.match(/rgb\s*\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*\)/);
		if (rgb) c = this.make_hex(parseInt(rgb[1]),parseInt(rgb[2]),parseInt(rgb[3]));
		return c;
	}
}

function HandleSearch()
	{
if (document.getElementById('sitesearch')){
	
		//clear search field when user puts focus on field
		document.getElementById('q').onfocus = function()
			{
				if(this.value == "Search this site"){this.value = "";}
			};
		
		//return value to default text if field loses focus and is still empty	
		document.getElementById('q').onblur = function()
			{
				if(this.value == ""){this.value = "Search this site";}
			};
			
			
	}}


