// ----------------------------------------------------------------------
// Copyright 2006 by Prophet. All rights reserved.
// Revision 1.0 (March 2006)
// ----------------------------------------------------------------------

var Global = {};


// ----------------------------------------------------------------------
// Helper to add multiple onload events to the document.
// See <http://www.tek-tips.com/faqs.cfm?fid=4862>
// ----------------------------------------------------------------------

function add_onload_event (fnc) {
	if (typeof window.addEventListener != "undefined") {
		window.addEventListener("load", fnc, false);
	} else if (typeof window.attachEvent != "undefined") {
		window.attachEvent("onload", fnc);
	} else {
		if (window.onload != null) {
			var oldOnload = window.onload;
			window.onload = function (e) { oldOnload(e); window[fnc](); }
		} else {
			window.onload = fnc;
		}
	}
}


// ----------------------------------------------------------------------
// Handler for "standard" forms to check for required field entries.
// ----------------------------------------------------------------------

function check_standard_form (form) {
	var message = "";
	var labels = form.getElementsByTagName("LABEL");
	for (var i=0; i<labels.length; i++) {
		var label = labels.item(i);
		if (Element.hasClassName(label, "required")) {
			var field_ok = true;
			var field_name = label.getAttribute("for") ? label.getAttribute("for") : label.getAttribute("htmlFor");
			if (! field_name) { continue; }
			var field = $(field_name);
			if (! field) { continue; }
			var field_tag = field.nodeName.toUpperCase();
			if (field_tag == "INPUT" && field.value == "") {
				field_ok = false;
			} else if (field_tag == "SELECT" && field.options[field.selectedIndex].value == "") {
				field_ok = false;
			}
			if (! field_ok) {
				field_desc = label.innerHTML.stripTags().replace(/:$/, "");
				message += "Please provide a value for " + field_desc + ".\n";
				Element.addClassName(field, "missing");
			}
		}
	}
	if (message != "") {
		alert(message);
		return false;
	} else {
		return true;
	}
}


// ----------------------------------------------------------------------
// Function to get a particular cookie value.
// ----------------------------------------------------------------------

function get_cookie (name) {
	var cookies = document.cookie.split(';');
	for (var i=0; i < cookies.length; i++) {
		var cookie = cookies[i];
		while (cookie.charAt(0) == ' ') {
			cookie = cookie.substring(1,cookie.length);
		}
		if (cookie.indexOf(name+'=') == 0) {
			return cookie.substring(name.length+1,cookie.length);
		}
	}
	return '';
}


// ----------------------------------------------------------------------
// Helper for the navigation menu. For browsers that don't support :hover
// on arbitrary elements (i.e., MSIE) this adds mouseover/mouseout events
// to the <LI> elements to simulate :hover with a CSS class. Note that
// this function uses Prototype to do the class addition/removal.
// ----------------------------------------------------------------------

function init_nav_hover () {
	var nav = $("nav");
	if (! nav) { return; }
	var elems = nav.getElementsByTagName("LI");
	for (var i=0; i<elems.length; i++) {
		elems[i].onmouseover=function() {
			Element.addClassName(this,"sfhover");
		}
		elems[i].onmouseout=function() {
			Element.removeClassName(this,"sfhover");
		}
	}
}

// Only create an onload event for MSIE.
if (window.attachEvent) window.attachEvent("onload", init_nav_hover);


// ----------------------------------------------------------------------
// Side-navigation styler. After the page loads, check for a <body> ID
// and a corresponding <a> tag in the side navigation column. If found,
// change its class so it will appear to be the "active" page/link.
// ----------------------------------------------------------------------

function init_sidenav_link () {
	var page_id = document.body.getAttribute("id");
	if (! page_id) return;
	var the_link = $("link_"+page_id);
	if (! the_link) return;
	Element.addClassName(the_link, "currpage");
}

add_onload_event(init_sidenav_link);


// ----------------------------------------------------------------------
// Convenience function for opening a basic popup window.
// ----------------------------------------------------------------------

function popup (url, width, height, extra) {
	var spec = 'width='+width+',height='+height+',status,resizable';
	if (extra) { spec += ',' + extra; }
	var w = window.open(url, 'thepopup', spec);
	if (w) { w.focus(); }
	return w;
}


// ----------------------------------------------------------------------
// Helpers for pseudo-pulldown menus based on CSS and JavaScript.
// ----------------------------------------------------------------------

function pulldown_toggle (name) {
	if (Global.pulldown && Global.pulldown != name) {
		pulldown_toggle(Global.pulldown);
	}
	var div = $(name);
	var new_visibility = div.style.visibility == "visible" ? "hidden" : "visible";
	div.style.visibility = new_visibility;
	if (new_visibility == "visible") {
		Global.pulldown = name;
		Event.observe(document.body, "click", pulldown_click_event, true);
	} else {
		Global.pulldown = "";
		Event.stopObserving(document.body, "click", pulldown_click_event, true);
	}
}

function pulldown_click_event (e) {
	// only toggle the pulldown if user clicked outside the pulldown AND its picker
	if (Global.pulldown && ! Position.within($(Global.pulldown), Event.pointerX(e), Event.pointerY(e)) && ! Position.within($(Global.pulldown+"_picker"), Event.pointerX(e), Event.pointerY(e))) {
		pulldown_toggle(Global.pulldown);
	}
}


// ----------------------------------------------------------------------
// Handler for the onfocus event of the search text field.
// ----------------------------------------------------------------------

function search_focus (obj) {
	if (obj.value == "Search") { obj.value = ""; }
	obj.select();
	return true;	
}


// ----------------------------------------------------------------------
// Handler for the onsubmit event of the search form.
// ----------------------------------------------------------------------

function search_submit (form) {
	if (form.words.value == "Search") { form.words.value = ""; }
	if (form.words.value == "") {
		alert("Please enter a word or phrase to search for.");
		return false;
	}
	return true;	
}


// ----------------------------------------------------------------------
// Utility function to set the selected option in a select list. Right
// now it only works with single-select lists.
// ----------------------------------------------------------------------

function set_selected_option (select_list_id, option_value) {
	var s = $(select_list_id);
	if (s) {
		for (var i=0; i<s.length; i++) {
			if (s.options[i].value == option_value) {
				s.selectedIndex = i;
				break;
			}
		}
	}
}


// ----------------------------------------------------------------------
// Function to display the subscription popup.
// ----------------------------------------------------------------------

function subscribepop () {
	var tlpopup = get_cookie('tlpopup');
	if (tlpopup.length) {
		// already seen and dismissed the popup; don't show again
	} else if (Math.floor(Math.random() * 100) < 50) {
		// visitors get a 1-in-2 chance of seeing the popup
		var top = 100; var left = 100;
		var offtop = 250; var offleft = 180;
		if (window.screenTop) {
			top = window.screenTop + offtop;
			left = window.screenLeft + offleft;
		} else if (window.screenY) {
			top = window.screenY + offtop;
			left = window.screenX + offleft;
		}
		var w = window.open("/promo/subscribepop/", "subscribepop", "width=490,height=140,top="+top+",left="+left+",menubar=no,scrollbars=no,toolbar=no");
		if (w) { w.focus(); }
	}
}


// ----------------------------------------------------------------------
// Track our downloads.
// ----------------------------------------------------------------------

add_onload_event(function(){
	if (! window.urchinTracker) { return; }
	var my_host = window.location.host.replace(/:.+$/,"");
	$A(document.getElementsByTagName("A")).each(function(a){
		if (a.hostname != my_host) {
			Event.observe(a, 'click', function(e){
				var url = this.hostname + this.pathname + this.search.replace(/^\?/,"/");
				urchinTracker('/linkto/' + url);
			});
		} else if (a.pathname.match(/[.](avi|doc|flv|mov|mp3|mp4|pdf|ppt|swf|vbs|wma|wmv)$/i)) {
			Event.observe(a, 'click', function(e){
				urchinTracker(this.pathname);
			});
		}
	});
});
