var auth = 'lixlpixel';
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");
	}
}
var DEF_VAL = 'search...';
var isSafari = ((parseInt(navigator.productSub)>=20020000) && (navigator.vendor.indexOf('Apple Computer')!=-1));
function replaceSearchField()
{
	if (!document.getElementById)
		return;
	var searchField = document.getElementById('query');
	var searchLabel = document.getElementById('searchlabel');
	var searchButton = document.getElementById('searchbutton');
	if (isSafari)
	{
		searchField.setAttribute('type', 'search');
		searchField.setAttribute('autosave', 'com.fundisom.search');
		searchField.setAttribute('results', '5');
		searchField.setAttribute('placeholder', DEF_VAL);
		searchLabel.style.display = 'none';
		searchButton.style.display = 'none';
	}else{
		addEvent(searchField, 'focus', focusSearch, false);
		addEvent(searchField, 'blur',	blurSearch, false);
		if (searchField.value=='') searchField.value = DEF_VAL;
	}
	
}
function focusSearch()
{
	if (this.value==DEF_VAL)
	{
		this.value = '';
		this.setAttribute('class', 'focus');
	}
}
function blurSearch()
{
	if (this.value=='')
	{
		this.value = DEF_VAL;
		this.removeAttribute('class', 'focus');
	}
}
function externalLinks()
{
/*
	var sb = 'track';
	var track = new Image();
	track.src = 'http://' + sb + '.' + auth + '.com/?pg=' + document.URL
	+ '&w=' + window.innerWidth + '&h=' + window.innerHeight
	+ '&ref=' + document.referrer ;
*/
	if (!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName('a');
	for (var i=0; i<anchors.length; i++)
	{
		var anchor = anchors[i];
		var therel = anchor.getAttribute('rel');
		if (anchor.getAttribute('href') && therel == 'nofollow') anchor.target = '_blank';
		if (anchor.getAttribute('title') 
			&& therel != 'start' 
			&& therel != 'first' 
			&& therel != 'prev' 
			&& therel != 'next' 
			&& therel != 'last')
		{
			anchor.innerHTML = anchor.innerHTML + '<span class="title">' + anchor.getAttribute('title') + '</span>';
			anchor.setAttribute('title','');
		}
	}
}
function windowwidth()
{
	if (window.innerWidth)
	{
		return window.innerWidth;
	}else if(document.body && document.body.offsetWidth)
	{
		return document.body.offsetWidth;
	}else{
		return 0;
	}
}
function redraw()
{
	newwidth = windowwidth();
	if(newwidth < 900)
	{
		document.getElementById('c1').className = 'full';
		document.getElementById('c2').className = 'full';
		document.getElementById('d1').className = 'full';
		document.getElementById('d2').className = 'full';
	}else{
		document.getElementById('c1').className = 'half';
		document.getElementById('c2').className = 'half';
		document.getElementById('d1').className = 'half';
		document.getElementById('d2').className = 'half';
	}
}
function toggle(what)
{
	if(document.getElementById(what).style.display=='none')
	{
		document.getElementById(what).style.display='block';
	}else{
		document.getElementById(what).style.display='none';
	}
}
function show(what)
{
	document.getElementById(what).style.display='block';
}
function hide(what)
{
	document.getElementById(what).style.display='none';
}
var google_text_color = '#000000';
var google_link_colors = new Array('#ffff66','#a0ffff','#99ff99','#ff9999','#ff66ff');

function init_google(){
	var pattern = /google\./i;
	if (pattern.exec(document.referrer) != null){
		var url_parts = document.referrer.split('?');
		if (url_parts[1]){ 
			var url_args = url_parts[1].split('&');
			for(var i=0; i<url_args.length; i++){
				var keyval = url_args[i].split('=');
				if (keyval[0] == 'q'){
					go_google(decode_url(keyval[1]));
					show('google');
					return;
				}
			}
		}
	}
}
function decode_url(url){
	return unescape(url.replace(/\+/g,' '));
}
function go_google(terms){
	terms = terms.replace(/\"/g,"");
	terms = terms.replace('  ',' ');
	var terms_split = terms.split(' ');
	var c = 0;
	for(var i=0; i<terms_split.length; i++){
		highlight_goolge(terms_split[i], document.body,google_link_colors[c]);
		c = (c == google_link_colors.length-1)?0:c+1;
	}
}
function highlight_goolge(term, container, color){
	var term_low = term.toLowerCase();
	for(var i=0; i<container.childNodes.length; i++){
		var node = container.childNodes[i];
		if (node.nodeType == 3){
			var data = node.data;
			var data_low = data.toLowerCase();
			if (data_low.indexOf(term_low) != -1){
				//term found!
				var new_node = document.createElement('SPAN');
				node.parentNode.replaceChild(new_node,node);
				var result;
				while((result = data_low.indexOf(term_low)) != -1){
					new_node.appendChild(document.createTextNode(data.substr(0,result)));
					new_node.appendChild(create_node_google(document.createTextNode(data.substr(result,term.length)),color));
					data = data.substr(result + term.length);
					data_low = data_low.substr(result + term.length);
				}
				new_node.appendChild(document.createTextNode(data));
			}
		}else{
			highlight_goolge(term, node, color);
		}
	}
}
function create_node_google(child, color){
	var node = document.createElement('SPAN');
	node.style.backgroundColor = color;
	node.style.color = google_text_color;
	node.appendChild(child);
	return node;
}
function do_search()
{
	promptText = 'please enter the terms you want highlighted:';
	searchText = prompt(promptText,'');
	if(searchText) go_google(searchText);
}

addEvent(window, 'resize', redraw, false);
addEvent(window, 'load', redraw, false);
addEvent(window, 'load', externalLinks, false);
addEvent(window, 'load', replaceSearchField, false);
