// JavaScript Document

/* To do: Now that we have JQuery this file should be optimised to make use of it */

var TF_HeightsFixed = 0;
function TF_FixHeights() {
	var nodes = document.getElementsByTagName('DIV');
	var n = nodes.length;
	var LastLeft = null;
	for (i=0;i<n;i++) {
		if (/(^| )Left( |$)/.test(nodes[i].className&&nodes[i].className)) {
			LastLeft = nodes[i];
		}
		if (/(^| )Right( |$)/.test(nodes[i].className&&nodes[i].className)) {
			if (LastLeft != null && (LastLeft.offsetHeight - 15) > nodes[i].offsetHeight) {
				nodes[i].style.height = (LastLeft.offsetHeight - 15) + 'px';	
			}
		}
		// Vertical image box
		/*if (/(^| )VerticalImgBox( |$)/.test(nodes[i].className)) {
			//alert('found');
			var cn = nodes[i].childNodes;
			var c= 0;
			var h = nodes[i].parentNode.parentNode.offsetHeight;
			for (var j=0; j<cn.length; j++) {
				if (cn[j].tagName) {
					
					if (cn[j].tagName == 'A') {
						c++;
						h -= cn[j].firstChild.offsetHeight;
					//} else {
						//h -= cn[j].offsetHeight;
					}
				}
			}
			//alert(h);
			if (document.getElementById('RightContact')) {
				var ns = document.getElementById('RightContact')
				h -= ns.offsetHeight
				alert(ns.offsetHeight)
			}
			
			h -= 15;
			
			//alert(h);
			
			if (h>0){
				var m = ((h)/(c))+1;
				if (c>4) {
					m += 1;
				}
				var cc = 1
				for (var j=0; j<cn.length; j++) {
					if (cn[j].tagName && cc<c){
						//
						var m2;
						if (TF_IsEven(m2)) {
							m2 = Math.ceil(m);
						} else {
							m2 = Math.floor(m);
						}
						if (cn[j].tagName == 'A') {
							cn[j].firstChild.style.marginBottom = m2+'px';
						}// else {
						//	cn[j].style.marginBottom = m2+'px';
						//}
						cc++;
					}
				}
			}
		}*/
	}
	TF_HeightsFixed ++;
}

function TF_IsEven(someNumber){
	return (someNumber%2 == 0) ? true : false;
}

$(window).load(TF_FixHeights);

$(window).scroll(function () {
	if (navigator.appName != 'Microsoft Internet Explorer' || !/MSIE [1-7]\./.test(navigator.appVersion)) {
		var t = document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop;
		var n = document.getElementById('Menu');
		if (t > 140) {
			n.style.position = 'fixed';
			n.style.top = '-1px';
		} else {
			n.style.position = '';
			n.style.top = ''
		}
	}
});

function TF_InitServices() {
	var d = document.getElementById('SGrid');
	var nodes = d.getElementsByTagName('DIV');
	var n = nodes.length;
	var time = 100;
	var int = 30;
	var opacity = 1;
	for (i=0;i<n;i++) {
		if (nodes[i].className == 'Active') {
			nodes[i].getElementsByTagName("IMG").item(0).title = nodes[i].getElementsByTagName("IMG").item(0).alt;
			nodes[i].onmouseover = TF_SMOver;
			nodes[i].onmouseout = function() {
				document.getElementById('SText').innerHTML = '&nbsp;';
			}
		}
		if (nodes[i].className != '') {
			setTimeout("TF_FadeIn(document.getElementById('SGrid').getElementsByTagName('DIV').item("+i+"), "+opacity+")",time+int);
			time+=int;
		}
	}
}

function TF_SMOver () {
	document.getElementById('SText').innerHTML = this.getElementsByTagName("IMG").item(0).alt;
}

function TF_InitImgBar (id) {
	var d = document.getElementById(id);
	var nodes = d.getElementsByTagName('IMG');
	var n = nodes.length;
	var time = 100;
	var int = 60;
	var opacity = 1;
	for (i=0;i<n;i++) {
		setTimeout("TF_FadeIn(document.getElementById('"+id+"').getElementsByTagName('IMG').item("+i+"), "+opacity+")",time+int);
		time+=int;
	}
}

function TF_DoFadeIns() {
	var nodes = document.getElementsByTagName('IMG');
	var n = nodes.length;
	var time = 100;
	var int = 60;
	var opacity = 1;
	for (i=0;i<n;i++) {
		if (/(^| )FadeIn( |$)/.test(nodes[i].className)) {
			setTimeout("TF_FadeIn(document.getElementsByTagName('IMG').item("+i+"), "+opacity+")",time+int);
			time+=int;
		}
	}
}

function TF_FadeIn(node,opacity) {
	//var myFx = new Fx.Tween(node);
	//myFx.set('opacity', 0);
	//node.style.visibility = 'visible';
	//myFx.start('opacity', 0, opacity);
	$(node).fadeTo('', opacity).show();
}

// Adapted from Bolt_Highlight for The Fount menu scheme
// Auto menu highlight and image rollover class
// (c) Copyright 2008 ChristopherBolt.Com

function TF_SetClassName (n,c) {
	var s = String(n.className).replace(/\s*(actual|current|over)\s*/gi, '');
	if (c) s+=' '+c;
	n.className = s;
}
function TF_TestClassName(n,c) {
	var r = new RegExp("(^| )"+c+"($| )","gi");
	return r.test(String(n.className));
}
function TF_HighlightMenu(id) {
	var highlight = new TF_Highlight();
	highlight.Highlight(id);
}

function TF_Highlight () {
	
	this.QuoteMeta = function (str) {
		str = str.replace( /([^A-Za-z0-9])/g , "\\$1" );
		return str;
	}
	
	this._location = String(document.location);
	this._location2 = this._location.replace(/^([\s\S]*\/)[^\/]*$/i, "$1");
	this._domain = this._location.replace(/(^(http|https):\/\/[^\/]*)[\s\S]*/i, '$1');
	
	this._locationRegex = new RegExp('^'+this.QuoteMeta(this._location).replace(/[\\]&/gi,'(\\&|\\&amp\\;)')+'#', 'gi');
	
	this.UrlFormatting = function (url) {
		url = String(url);
		if (url.match(/^javascript:/gi)) {
			return url;
		}
		if ( /^www\./i.test(url)) {
			url = 'http://'+url;	
		}
		
		url = url.replace(this._locationRegex, '#');
	
		var location = this._location2;
		
		url = url.replace(/^\//gi, this._domain+'/');
		url = url.replace(/^([^#][^:"]*)$/gi, location+'$1');
		while(url.match(/([^:][^\/])\/[^\/]*\/\.\.\//i)) {
			url = url.replace(/([^:][^\/])\/[^\/]*\/\.\.\//i, '$1/');
		}
		url = url.replace(/\/\.\.\//gi, '/');
		
		// delete everything after #
		//url = url.replace(/(^[^#]*#)[\s\S]+$/g, "$1");
		//url = url.replace(/^[^#]*#[\s\S]+$/g, function (x){s=x.split('#');return s[0]+'#'+unescape(s[1]);});
		
		url = unescape(url);
		
		return url;
	}
	
	this.Highlight = function (id) {
		//var id = 'mainMenu';
		var as = document.getElementById(id).getElementsByTagName('A');
		var location = this.UrlFormatting(this._location);
			
		for (var i=0;i<as.length;i++) {
			var href = String(as[i].href);
			
			href = this.UrlFormatting(href);
			
			// hack for team page
			if (/\/team\.php/i.test(location)) {
				location = location.replace(/\/team\.php/i, '/about-team.php');
			}
			// hack for design-process page
			if (/\/design-/i.test(location)) {
				location = location.replace(/\/design-/i, '/about-');
			}
			// hack for portfolio_ and services_ being the same menu
			if (/\/portfolio-/i.test(location)) {
				location = location.replace(/\/portfolio-/i, '/services-2');
			}
			if (/\/portfolio-/i.test(href)) {
				href = href.replace(/\/portfolio-/i, '/services-2');
			}
			// hack for info_ and services_ being the same menu
			if (/\/info-/i.test(location)) {
				location = location.replace(/\/info-/i, '/services-3');
			}
			if (/\/info-/i.test(href)) {
				href = href.replace(/\/info-/i, '/services-3');
			}
			if (/\/article-/i.test(location)) {
				location = location.replace(/\/article-/i, '/articles-');
			}
			
			var href_noindex = href.replace(/index\.[a-z]{2,4}($|\?|#)/gi,'');
			var href_noextension = href.replace(/\.[a-z]{2,4}($|\?|#)/gi,'');
						
			if (href == location.substr(0,href.length) 
				|| href_noindex == location.substr(0,href_noindex.length)
				|| href_noextension == location.substr(0,href_noextension.length)
				|| (/services-logo-design\.php/.test(location) && /services-logo-brand\.php/.test(href))
				|| (/about-team\.php/.test(location) && /team\.php/.test(href))
				|| (/about-process\.php/.test(location) && /design-process\.php/.test(href))
				)
				
				 {
						
				
				//alert(href);
				//alert(/\/design_[a-z\-_]+\.html($|\?|#)/.test(href));
				doactive = true;
				if (as[i].id == 'HomeLink' && ((href != location && href_noindex != location) && !/\/design-[a-z\-_]+\.php($|\?|#)/.test(location))	) {
					doactive = false;	
				}
				
				//alert(location);
				if (/#/.test(href) && /#/.test(location)) {
					doactive = false;	
				}
				
				if (doactive) {
					
					var className = 'current';
					location = location.replace(/#[\S\s]*/, '');
					if ((href == location || href_noindex == location)) {
						className = 'actual'; 
					}
					
					var ul = as[i].nextSibling;
					while (ul && ul.nextSibling && !ul.tagName) {
						ul = ul.nextSibling;
					}
					if (ul && ul.tagName && ul.tagName == 'UL') {
						TF_SetClassName(ul, 'current');
					}
					
					TF_SetClassName(as[i], className);
				} 
			} 
			as[i].onmouseover = this.OnMouseOver;
			as[i].onmouseout = this.OnMouseOut;
		}		
	}
	
	this.OnMouseOver = function (e) {
		if (!TF_TestClassName(this, '(current|actual|over)')) {
			TF_SetClassName(this, 'over');	
		}
	}
	
	this.OnMouseOut = function (e) {
		if (TF_TestClassName(this,'over')) {
			TF_SetClassName(this, '');
		}
	}
	
}
