/* Common WUE scripts */

//	define main WM object
if (typeof(WM) !== "object") {
  var WM = function () {
  	//	define common variables here
  
  	return {
  		//	define 2nd-level function objects
  		main:  function () {},	//	common functions
  		form:  function () {},	//	forms
  		nav:   function () {},	//	navigation
  		share: function () {} 	//	sharing/social
  	};
  
  } ();
}

WM.main.clearTopNavBorder = function () {
  if (document.getElementById) {
	  var divextras = document.getElementById("extras");
		var extraslist = divextras.getElementsByTagName("ul");
		var extrasli = extraslist[0].getElementsByTagName("li");
		extrasli[extrasli.length-1].className = "last";
	}	
};

WM.main.clearSearch = function () {
  if (document.getElementById) {
	  var searchBox = document.getElementById("searchQ");
  		searchBox.onclick = function () {
  			if (searchBox.value == "Site Search...") {
  			  searchBox.value = "";
  			}		
		}
	}
};


//=== FORM ===

//	set the visibility, etc of companion "_Other" field
WM.form.toggleFieldOther = function (parm_elem) {

	var selected_other_id, selected_other, selected_option, selected_option_value;

  //	get companion "_Other" field and selected option
  selected_other_id = parm_elem.id + "_Other";
  selected_other = document.getElementById(selected_other_id);
  selected_option = parm_elem.options[parm_elem.selectedIndex];
	//	bail out if missing info
	if (!selected_other || !selected_option) { return false; }

  //	show field if "other" option selected
	selected_option_value = selected_option.value.length > 0 ? selected_option.value : selected_option.text;
  if (selected_option_value.substr(0,5).toLowerCase() === "other") {
  	selected_other.style.display = "inline";
		//	focus the "other" field if editing
		if (parm_elem.timing === "edit") {
			selected_other.focus();
		}
  } else {
  	selected_other.style.display = "none";
  	selected_other.value = "";
  }

};

//	set initial visibility of "_Other" fields and attach function to select
WM.form.setupFieldsOther = function () {

	var i, selects, selects_length;
	//	bail out if bad browser
	if (!document.getElementsByTagName || !document.getElementById) { return false; }

	//	get list of select elements
	selects = document.getElementsByTagName("SELECT");
	selects_length = selects.length;
	for (i = 0; i < selects_length; i++) {
		var selected, selected_other, selected_other_id;
  	selected = selects[i];
		// determine if there's a companion _Other field
		selected_other_id = selected.id + "_Other";
		selected_other = document.getElementById(selected_other_id);
		
		if (selected_other !== null) {
			//	set function on select
			selected.onchange = function () {
				this.timing = "edit";
				WM.form.toggleFieldOther(this);
			};
			WM.form.toggleFieldOther(selected);

		}
	}	//	end loop

};


//=== SHARE ===

//	add javascript to sharing icons
WM.share.enhanceShareIcons = function () {

	var share_links, i;

	// check for capable javascript
	if (document.getElementById("social") && document.getElementsByTagName) {

		//	get social links
  	share_links = document.getElementById("social").getElementsByTagName("a");
		//	process all but the first and last items
		for (i = 1; i < share_links.length - 1; i++) {
			//	attach an event handler to open a new window
			share_links[i].onclick = function() {
				if (typeof(pageTracker) !== "undefined") {
					pageTracker._trackPageview(this.href + '&amp;w=1');
				}
				//	open a new window using an extra parameter
        window.open(this.href,'share','toolbar=0,status=0,resizable,scrollbars,width=800,height=600');
        return false;
			};
		}	//	loop
	}

};


//=== INIT ===

//	start on page load
WM.init = function () {

	WM.main.clearTopNavBorder();
	WM.main.clearSearch();
	WM.share.enhanceShareIcons();
	WM.form.setupFieldsOther();
	
};

/* add page load events */
addLoadListener(WM.init);

