/*
		Author	:	Christopher Robinson
		Email		:	christopher@edg3.co.uk
		Website	:	http://www.edg3.co.uk/
		
		Search from OrderedList, credit where credit is due, this guy rocks:
		http://orderedlist.com/articles/howto-animated-live-search/
*/

// Preload Images
var imgArray = ["/wp-content/themes/edg3/image/background_footer_rss.jpg","/wp-content/themes/edg3/image/background_footer_rss_hover.jpg","/wp-content/themes/edg3/image/background_footer_valid.jpg","/wp-content/themes/edg3/image/background_footer_valid_hover.jpg","/wp-content/themes/edg3/image/background_header.jpg","/wp-content/themes/edg3/image/background_header_logo.jpg","/wp-content/themes/edg3/image/background_header_navigation.jpg","/wp-content/themes/edg3/image/background_navigation.jpg","/wp-content/themes/edg3/image/background_search_close.jpg","/wp-content/themes/edg3/image/background_sidebar.jpg","/wp-content/themes/edg3/image/background_sidebar_bullfrog.jpg","/wp-content/themes/edg3/image/background_sidebar_eight.jpg","/wp-content/themes/edg3/image/background_sidebar_links.jpg","/wp-content/themes/edg3/image/background_sidebar_one.jpg","/wp-content/themes/edg3/image/background_sidebar_paces.jpg","/wp-content/themes/edg3/image/background_sidebar_spring.jpg","/wp-content/themes/edg3/image/background_sidebar_two.jpg","/wp-content/themes/edg3/image/blank.gif","/wp-content/themes/edg3/image/close.gif","/wp-content/themes/edg3/image/closelabel.gif","/wp-content/themes/edg3/image/loading.gif","/wp-content/themes/edg3/image/next.gif","/wp-content/themes/edg3/image/nextlabel.gif","/wp-content/themes/edg3/image/prev.gif","/wp-content/themes/edg3/image/prevlabel.gif","/wp-content/themes/edg3/image/spinner.gif","/wp-content/themes/edg3/image/trackback_pingback.gif"];
var imgtoNow = [];
function loadImages(imgArray) {
	for (var i=0; i<imgArray.length; i+=1) {
		imgtoNow[i] = new Image();
		imgtoNow[i].src = imgArray[i];
	}
}

// Search
function activateSearch() {
	if ($('searchform')) {
		$('s').value = 'Input Some Keywords Then Hit Enter...';
		var o = document.createElement('div');
		var n = document.createElement('div');
		var a = document.createElement('a');
		var c = document.createElement('img');
		$('searchform').onsubmit = function() { doSearch();return false; };
		$('s').onfocus = focusS;
		a.href = 'http://www.edg3.co.uk/';
		a.id = 'cancel-search';
		a.onclick = function() { cancelSearch(); return false; };
		c.src = '/wordpress/wp-content/themes/edg3/image/background_search_close.jpg';
		c.style.border = 'none';
		var s = $('search-results');
		var f = $('searchform');
		o.id = 'old-search-results';
		n.id = 'current-search-results';
		a.appendChild(c);
		s.appendChild(n);
		s.appendChild(o);
		f.appendChild(a);
		Element.toggle(a);
		has_searched = false;
		o.style.display = 'none';
		n.style.display = 'none';
		show_search = false;
		is_searching = false;
	}
}

function doSearch() {
	if (is_searching) return false;
	s = $F('s');
	if (s == '' || s == 'Input Some Keywords Then Hit Enter...') return false;
	is_searching = true;
	c = $('current-search-results');
	o = $('old-search-results');
	if (!show_search) a = new Effect.Scale('s',88,{scaleY:false,scaleContent:false,duration:1});
	o.innerHTML = c.innerHTML;
	c.style.display = 'none';
	o.style.display = 'block';
	pars = 's=' + escape(s) + '&ajax';
	var myAjax = new Ajax.Request('http://www.edg3.co.uk/', {method: 'get', parameters: pars, onComplete:doSearchResponse});
}

function doSearchResponse(response) {
	$('current-search-results').innerHTML = response.responseText;
	x = new Effect.BlindUp('latest',{duration:1});
	y = new Effect.BlindUp('old-search-results',{duration:1});
	z = new Effect.BlindDown('current-search-results',{duration:1, afterFinish:resetForm});
}

function resetForm() {
	if (!show_search) Element.toggle($('cancel-search'));
	show_search = true;
	is_searching = false;
}

function focusS() {
	if ($F('s') == 'Input Some Keywords Then Hit Enter...') $('s').value = '';
}

function cancelSearch() {
	show_search = false;
	$('s').value = 'Input Some Keywords Then Hit Enter...';
	Element.toggle($('cancel-search'));
	a = new Effect.Scale('s',106,{scaleY:false,scaleContent:false,duration:1});
	x = new Effect.BlindDown('latest',{duration:1});
	$('old-search-results').innerHTML = '';
	z = new Effect.BlindUp('current-search-results',{duration:1,afterFinish:function() { $('current-search-results').innerHTML = ''; }});
}

// Add Event
Array.prototype.inArray = function (value) {
	var i;
	for (i=0; i < this.length; i++) {
		if (this[i] === value) {
			return true;
		}
	}
	return false;
};
function addEvent( obj, type, fn ) {
	if (obj.addEventListener) {
		obj.addEventListener( type, fn, false );
		EventCache.add(obj, type, fn);
	}
	else if (obj.attachEvent) {
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
		obj.attachEvent( "on"+type, obj[type+fn] );
		EventCache.add(obj, type, fn);
	}
	else {
		obj["on"+type] = obj["e"+type+fn];
	}
}
	
var EventCache = function(){
	var listEvents = [];
	return {
		listEvents : listEvents,
		add : function(node, sEventName, fHandler){
			listEvents.push(arguments);
		},
		flush : function(){
			var i, item;
			for(i = listEvents.length - 1; i >= 0; i = i - 1){
				item = listEvents[i];
				if(item[0].removeEventListener){
					item[0].removeEventListener(item[1], item[2], item[3]);
				};
				if(item[1].substring(0, 2) != "on"){
					item[1] = "on" + item[1];
				};
				if(item[0].detachEvent){
					item[0].detachEvent(item[1], item[2]);
				};
				item[0][item[1]] = null;
			};
		}
	};
}();

// Sweet Titles
var sweetTitles = { 
	xCord : 0,								// @Number: x pixel value of current cursor position
	yCord : 0,								// @Number: y pixel value of current cursor position
	tipElements : ['a','abbr','acronym'],	// @Array: Allowable elements that can have the toolTip
	obj : Object,							// @Element: That of which you're hovering over
	tip : Object,							// @Element: The actual toolTip itself
	active : 0,								// @Number: 0: Not Active || 1: Active
	init : function() {
		if ( !document.getElementById ||
			!document.createElement ||
			!document.getElementsByTagName ) {
			return;
		}
		var i,j;
		this.tip = document.createElement('div');
		this.tip.id = 'toolTip';
		document.getElementsByTagName('body')[0].appendChild(this.tip);
		this.tip.style.top = '0';
		this.tip.style.visibility = 'hidden';
		var tipLen = this.tipElements.length;
		for ( i=0; i<tipLen; i++ ) {
			var current = document.getElementsByTagName(this.tipElements[i]);
			var curLen = current.length;
			for ( j=0; j<curLen; j++ ) {
				if(current[j].getAttribute('title') != null && current[j].getAttribute('title') != ""){
					addEvent(current[j],'mouseover',this.tipOver);
					addEvent(current[j],'mouseout',this.tipOut);
					current[j].setAttribute('tip',current[j].title);
					current[j].removeAttribute('title');
				}
			}			
		}
	},
	updateXY : function(e) {
		if ( document.captureEvents ) {
			sweetTitles.xCord = e.pageX;
			sweetTitles.yCord = e.pageY;
		} else if ( window.event.clientX ) {
			sweetTitles.xCord = window.event.clientX+document.documentElement.scrollLeft;
			sweetTitles.yCord = window.event.clientY+document.documentElement.scrollTop;
		}
	},
	tipOut: function() {
		if ( window.tID ) {
			clearTimeout(tID);
		}
		if ( window.opacityID ) {
			clearTimeout(opacityID);
		}
		sweetTitles.tip.style.visibility = 'hidden';
	},
	checkNode : function() {
		var trueObj = this.obj;
		if ( this.tipElements.inArray(trueObj.nodeName.toLowerCase()) ) {
			return trueObj;
		} else {
			return trueObj.parentNode;
		}
	},
	tipOver : function(e) {
		sweetTitles.obj = this;
		tID = window.setTimeout("sweetTitles.tipShow()",500);
		sweetTitles.updateXY(e);
	},
	tipShow : function() {		
		var scrX = Number(this.xCord);
		var scrY = Number(this.yCord);
		var tp = parseInt(scrY+15);
		var lt = parseInt(scrX+10);
		var anch = this.checkNode();
		var addy = '';
		var access = '';
		if ( anch.nodeName.toLowerCase() == 'a' ) {
			addy = (anch.href.length > 25 ? anch.href.toString().substring(0,25)+"..." : anch.href);
			var access = ( anch.accessKey ? ' <span>['+anch.accessKey+']</span> ' : '' );
		} else {
			addy = anch.firstChild.nodeValue;
		}
		this.tip.innerHTML = anch.getAttribute('tip');
		if ( parseInt(document.documentElement.clientWidth+document.documentElement.scrollLeft) < parseInt(this.tip.offsetWidth+lt) ) {
			this.tip.style.left = parseInt(lt-(this.tip.offsetWidth+10))+'px';
		} else {
			this.tip.style.left = lt+'px';
		}
		if ( parseInt(document.documentElement.clientHeight+document.documentElement.scrollTop) < parseInt(this.tip.offsetHeight+tp) ) {
			this.tip.style.top = parseInt(tp-(this.tip.offsetHeight+10))+'px';
		} else {
			this.tip.style.top = tp+'px';
		}
		this.tip.style.visibility = 'visible';
		this.tip.style.opacity = '.1';
		this.tipFade(10);
	},
	tipFade: function(opac) {
		var passed = parseInt(opac);
		var newOpac = parseInt(passed+10);
		if ( newOpac < 80 ) {
			this.tip.style.opacity = '.'+newOpac;
			this.tip.style.filter = "alpha(opacity:"+newOpac+")";
			opacityID = window.setTimeout("sweetTitles.tipFade('"+newOpac+"')",20);
		}
		else { 
			this.tip.style.opacity = '.80';
			this.tip.style.filter = "alpha(opacity:80)";
		}
	}
};

// Register Events
addEvent(window,'unload',EventCache.flush);
Behaviour.addLoadEvent(activateSearch);
Behaviour.addLoadEvent(function(){loadImages(imgArray);});
Behaviour.addLoadEvent(function(){sweetTitles.init();});