/* -----------------------------------------------------------------------------
	Template Name:	scag.core.js
	Description:	common/site-wide scripts
|* ------------------------------------------------------------------------------ */

function trim(i){i=i.replace(/([ \t\r\n\f])*$/gi,"");	return i.replace(/^([ \t\r\n\f])*/gi,"");}
function setMenu(id){
	var h=document.getElementById(id);
	if (!h) return false;
	h.className="current";
	return true;
}
function setMnuHere(id){
	if (!setMenu(id))
		setMenu("topnav"+id);
}


// -------------------------------------------------------------------------------
//	function: validateHeadSearch
// -------------------------------------------------------------------------------
function validateSearch(){
	var h=document.getElementById("topSearch");
	if (h.isValid()) return true;
	alert("Please enter one or more search words (minimum length 3 characters)");
	h.focus();
	return false;
}

// -------------------------------------------------------------------------------
//	function: wireHeadSearch
//	description: Attaches behaviors to header's search box, showing & hiding some default text.
// -------------------------------------------------------------------------------
function wireSearch(){
	var h= document.getElementById("topSearch");
	h.defaultText= "Search SCAG";
	if (h.value == "")
		h.value=h.defaultText;
	// doing this because if user's use browser back button may need to handle initial state IS default text
	if (h.value == h.defaultText)
		h.style.color="#999";

	h.onfocus = function (){
		this.value=trim(this.value);
		if (this.value == this.defaultText){
			this.value="";
			this.style.color="#000";
		}
	}
	
	h.onblur = function (){
		this.value=trim(this.value);
		if (this.value.length<1){
			this.value=this.defaultText;
			this.style.color="#999";
		}
	}
	
	h.isValid= function(){
		this.value=trim(this.value);
		return ((this.value.length>2) && (this.value!=this.defaultText));
	}
}

// -------------------------------------------------------------------------------
//	function: enhanceLinks
//	description: runs through the DOM's anchors, looking for hrefs pointing to:
//		file ending in ".mp3"
//		or containing "/listen.m3u?" or "/listen.ram?"
//
//	the nifty for loop retrieved from "Delicious.Mp3"
// -------------------------------------------------------------------------------
function enhanceLinks(){
	var 
		pdfRegEx = /\.pdf$/i,
		audioRegEx = /(\.mp3$|\/listen\.m3u\?|\/listen\.ram\?)/i,
		mp3RegEx = /\.mp3$/i;
	var links= document.getElementById("content").getElementsByTagName('a');
	var isMP3;
	var isPDF;
	for (var i = 0, o; o = links[i]; i++){
		if(o.href.match(pdfRegEx)){
			o.className+= ' pdf';
			if (o.title == "")
				o.title = 'download PDF document';
		}
		/*
		if(o.href.match(audioRegEx)){
			isMP3 = (o.href.match(mp3RegEx));
			o.className+= isMP3?' mp3': ' showAudio';
			if (o.title == "")
				o.title = isMP3?'listen to MP3': 'listen to show archive segment';
		}
		*/
	}
}

// -------------------------------------------------------------------------------
//	function: loadDone
// -------------------------------------------------------------------------------
function loadDone(){
	wireSearch();
	enhanceLinks();
}
