/* page prep functions for common items */
function prep_page_common()
{
	// set searchbox
	var sb = $('sp-q');
	if(sb)
	{
		sb.onfocus = searchbox_value;
		sb.onblur = searchbox_value;

		// set search onsubmit
		$('atomz').onsubmit = is_searchbox_empty;
	}
	// maniupulate links for print
	printable_links();
}

Event.observe(window,'load',prep_page_common,false);


// find the next element of a certain type
function get_next(src,objType)
{
	obj = $(src);
	while(obj.nodeName != objType)
	{
		if(obj.nextSibling) { obj = obj.nextSibling; }
		else { return false; }
	}

	return obj;
}

// find out if an object has a value or not
function is_searchbox_empty()
{
	if($('sp-q').value.empty() || $('sp-q').value == ' Search' || $('sp-q').value == '&nbsp;Search')
	{
		alert('Please enter a search term');
		return false; 
	}
	else { return true; }
}

// clear the starting value of the search box
function searchbox_value()
{
	var sb = $('sp-q');
	if(sb.value.empty()) { sb.value = ' Search'; }
	else if (sb.value == ' Search') { sb.value = ''; }
	else if (sb.value = '&nbsp;Search') { sb.value = ''; }
}

// read cookies
// via quirksmode.org
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;
}

// add item id to cookie
function addCookie(catid) {
	document.cookie = catid + '=1; path=/';
}

// remove item id from cookie
function removeCookie(catid) {
	var now = new Date();
	document.cookie = catid + '=; path=/; expires=' + now.toGMTString();
}


// insert span of full each link's full url at end of document
// use CSS to hide on screen and show for print
function printable_links()
{
	if($('cBody'))
	{
		// append footnote tag to links
		var cAnchors = $('cBody').getElementsByTagName('a');
		var cUrls = new Array();

		for(var i = 0; i < cAnchors.length; i++)
		{
			// save href for later
			cUrls[i] = cAnchors[i].href;

			// add text to new node
			var aText = document.createTextNode(" [" + (i+1) + "] ");
			var aElm = document.createElement("SPAN");
			aElm.className = 'footnotes';
			aElm.appendChild(aText);

			// append the child
			cAnchors[i].appendChild(aElm);
		}

		// add footnotes
		var links = '';
		var p = document.createElement('P');

		for(var a = 0; a < cUrls.length; a++)
		{
			p.appendChild(document.createTextNode('[' + (a+1) +'] ' + cUrls[a]));
			p.appendChild(document.createElement('BR'));
		}

		var footnotes = document.createElement('DIV');
		footnotes.className = 'footnotes';
		h2 = document.createElement('H2');
		h2.appendChild(document.createTextNode('Links in Page'));
		footnotes.appendChild(h2);
		footnotes.appendChild(p);
		document.body.appendChild(footnotes);
	}
}

// copy to clipboard
// from http://ajaxian.com/archives/auto-copy-to-clipboard
function copy(inElement)
{
	if (inElement.createTextRange)
	{
		var range = inElement.createTextRange();
		//if (range && BodyLoaded==1)
		if (range)
		 range.execCommand('Copy');
	}
	else
	{
		var flashcopier = 'flashcopier';
		if(!document.getElementById(flashcopier))
		{
			var divholder = document.createElement('div');
			divholder.id = flashcopier;
			document.body.appendChild(divholder);
		}
		document.getElementById(flashcopier).innerHTML = '';
		var divinfo = '<embed src="/js/copy_clipboard.swf" FlashVars="clipboard='+escape(inElement.value)+
					  '" width="0" height="0" type="application/x-shockwave-flash"></embed>';
		document.getElementById(flashcopier).innerHTML = divinfo;
	}
}

// get the page scroll position
function get_page_scroll()
{
	var x, y;
	if(window.pageYOffset && window.pageYOffset)
	{
		// Safari & FireFox
		x = window.pageXOffset;
		y = window.pageYOffset;
	}
	else if(window.scrollY && window.scrollX)
	{
		// Safari & FireFox
		x = window.scrollX;
		y = window.scrollY;
	}
	else
	{
		// FireFox, IE6 & IE7
		x = document.documentElement.scrollLeft;
		y = document.documentElement.scrollTop;
	}
	
	return {xScroll: x, yScroll: y};
}

// get the navigator window height and width
function get_window_size()
{
	var x, y;
	if(window.innerHeight && window.innerWidth)
	{
		// most newer browsers
		x = window.innerWidth;
		y = window.innerHeight;
	}
	else if(document.documentElement.clientHeight && document.documentElement.clientWidth)
	{
		// most browsers, but most importantly to get the window size in IE6 Strict
		// most other browsers will return document height which can be smaller or larger than the window
		x = document.documentElement.clientWidth;
		y = document.documentElement.clientHeight;
	}
	else
	{
		// IE 4, 5 & 6 Quirks
		x = document.body.clientWidth;
		y = document.body.clientHeight;
	}
	
	return {width: x, height: y};
}

// Cookies
// from: http://gorondowtl.sourceforge.net/wiki/Cookie
var Cookie = {
	set: function(name, value, daysToExpire) {
		var expire = '';
		if (daysToExpire != undefined) {
			var d = new Date();
			d.setTime(d.getTime() + (86400000 * parseFloat(daysToExpire)));
			expire = '; expires=' + d.toGMTString();
		}
		return (document.cookie = escape(name) + '=' + escape(value || '') + expire);
	},
	get: function(name) {
		var cookie = document.cookie.match(new RegExp('(^|;)\\s*' + escape(name) + '=([^;\\s]*)'));
		return (cookie ? unescape(cookie[2]) : null);
	},
	erase: function(name) {
		var cookie = Cookie.get(name) || true;
		Cookie.set(name, '', -1);
		return cookie;
	},
	accept: function() {
		if (typeof navigator.cookieEnabled == 'boolean') {
			return navigator.cookieEnabled;
		}
		Cookie.set('_test', '1');
		return (Cookie.erase('_test') === '1');
	}
};
