// get element by id shorthand
function g(strElementID) {
	return document.getElementById(strElementID);
}

// create a cookie
function createCookie(name,value,days) {
	if(days){
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}else{
		var expires = "";
	}
	document.cookie = name+"="+value+expires+"; path=/";
}

// read a cookie
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;
}

// erase a cookie
function eraseCookie(name) {
	createCookie(name,"",-1);
}

// bookmark a page
function bookmarkWithSocialNetwork(strSocialNetwork){
	var strOpenThisHREF = "";
	var strBookmarkHREF = encodeURIComponent(document.location.href);
	var strBookmarkTitle = encodeURIComponent(document.title);
	// ifs or cases that is the question...
	if(strSocialNetwork == "delicious"){
		strOpenThisHREF = "http://del.icio.us/post?v=2&url=" + strBookmarkHREF + "&title=" + strBookmarkTitle + " ";
	} else if(strSocialNetwork == "digg"){
		strOpenThisHREF = "http://digg.com/submit?phase=2&url=" + strBookmarkHREF + " ";
	} else if(strSocialNetwork == "facebook"){
		strOpenThisHREF = "http://www.facebook.com/sharer.php?u=" + strBookmarkHREF + "&t=" + strBookmarkTitle + " ";
	} else if(strSocialNetwork == "reddit"){
		strOpenThisHREF = "http://www.reddit.com/submit?t=1&url=" + strBookmarkHREF + "&title=" + strBookmarkTitle + " ";
	} else if(strSocialNetwork == "stumbleupon"){
		strOpenThisHREF = "http://www.stumbleupon.com/submit?url=" + strBookmarkHREF + "&title=" + strBookmarkTitle + " ";
	}
	// providing we were passed a valid socialnetwork
	if(strOpenThisHREF != ""){
		window.open(strOpenThisHREF,"share","toolbar=0,status=0,resizable=1,width=800,height=600");
	}
}

// URL encode a string
function URLEncode(strToEncode){
	var strEncoded = "";
	var numURLCount = 0;
	strToEncode = strToEncode.toString();
	var strURLregex = /(^[a-zA-Z0-9_.]*)/;
	while (numURLCount < strToEncode.length) {
		var match = strURLregex.exec(strToEncode.substr(numURLCount));
		if(match != null && match.length > 1 && match[1] != ""){
			strEncoded += match[1];
			numURLCount += match[1].length;
		} else {
			if(strToEncode[numURLCount] == " "){
				strEncoded += "+";
			} else {
				var strCHR = strToEncode.charCodeAt(numURLCount);
				var strHex = strCHR.toString(16);
				strEncoded += "%" + (strHex.length < 2 ? "0" : "") + strHex.toUpperCase();
			}
			numURLCount++;
		}
	}
	return strEncoded;
}

// display submenu
function displaySubMenu(numMenuID,numSubMenuID,blnStopTimer){
	strSubmenuElementID = "submenu_"+numMenuID+"_"+numSubMenuID;
	if(document.getElementById(strSubmenuElementID)){
		objSubMenu = document.getElementById(strSubmenuElementID);
		objSubMenu.style.display = "block";
		// change the 'parent' menuitem to be highlighted
		document.getElementById('main_nav_href_'+numMenuID).className = 'main_nav_items_highlighted';
		if(blnStopTimer == 1) { } else { startTimer(); }
	}
}

// hide all submenus
function hideSubMenus(blnChangeBackgroundImage) {
	liMenuIDsPullDownIDs = document.getElementById("liMenuIDsAndPullDownIDs").value;
	if(liMenuIDsPullDownIDs != ""){
		arrMenuIDsPullDownIDs = liMenuIDsPullDownIDs.split(",");
		for(var sm=0; sm < arrMenuIDsPullDownIDs.length; sm++) {
			numMenuID = arrMenuIDsPullDownIDs[sm].split("|")[0];
			numSubMenuID = arrMenuIDsPullDownIDs[sm].split("|")[1];
			strSubmenuElementID = "submenu_"+numMenuID+"_"+numSubMenuID;
			if(document.getElementById(strSubmenuElementID)){
				// change the 'parent' menuitem to be normal
				document.getElementById('main_nav_href_'+numMenuID).className = 'main_nav_items';
				document.getElementById(strSubmenuElementID).style.display = "none";
				// switch the background back
				if(blnChangeBackgroundImage){
					strMenuElement = "main_nav_"+numMenuID;
					if(document.getElementById(strMenuElement)){
						changeMenuItemImage(strMenuElement,0);
					}
				}
			}
		}
	}
 	// displaySubMenu(arrDefaultSubmenu[0],arrDefaultSubmenu[1]);
		resetTimer();
}

// reset timer
function resetTimer(numMenuItemID){
	numTimerID = document.getElementById("numTimerID").value;
	if(numTimerID != -1){
		clearTimeout(numTimerID);
	}
	if(numMenuItemID){
		strMenuElement = "main_nav_" + numMenuItemID;
		changeMenuItemImage(strMenuElement,1);
	}
}

// start timer
function startTimer(strElementID){
	document.getElementById("numTimerID").value = setTimeout("timeOut()",numIdleLimit);
	if((strElementID) && document.getElementById(strElementID)){
		changeMenuItemImage(strElementID,1);
	}
}

// times up!
function timeOut(){
	if(document.getElementById("numTimerID").value != -1){
		document.getElementById("numTimerID").value = -1;
		hideSubMenus(1);
	}
}

// change menuitem image
function changeMenuItemImage(strMenuElement,blnHighlight){
	if(document.getElementById(strMenuElement)){
		document.getElementById(strMenuElement).className = document.getElementById(strMenuElement).className + "_highlight";
		strBackgroundURL = document.getElementById(strMenuElement).style.backgroundImage;
		if(blnHighlight == 1){
			if(strBackgroundURL.indexOf("_CLICK.gif") == -1){
				strNewBackgroundURL = strBackgroundURL.replace(" HEADER"," HEADER CLICKS");
				strNewBackgroundURL = strNewBackgroundURL.replace(".gif","_CLICK.gif");
				document.getElementById(strMenuElement).style.backgroundImage = strNewBackgroundURL;
			}
		} else {
			strNewBackgroundURL = strBackgroundURL.replace(" HEADER CLICKS"," HEADER");
			strNewBackgroundURL = strNewBackgroundURL.replace("_CLICK.gif",".gif");
			document.getElementById(strMenuElement).style.backgroundImage = strNewBackgroundURL;
		}
		// alert(strBackgroundURL);
		// alert(strNewBackgroundURL);
	}
}

// reset menuitems
function resetMenuItems(strElementToExclude){
	// alert(strElementToExclude);
	liMenuIDsPullDownIDs = document.getElementById("liMenuIDsAndPullDownIDs").value;
	if(liMenuIDsPullDownIDs != ""){
		arrMenuIDsPullDownIDs = liMenuIDsPullDownIDs.split(",");
		for(var sm=0; sm < arrMenuIDsPullDownIDs.length; sm++) {
			numMenuID = arrMenuIDsPullDownIDs[sm].split("|")[0];
			strMenuElement = "main_nav_"+numMenuID;
			if(document.getElementById(strMenuElement) && strMenuElement != strElementToExclude){
				changeMenuItemImage(strMenuElement,0);
			}
		}
	}
}

// returns a 'titled' case string in js
function titleCase(title) {
	var small = "(an|and|as|at|but|by|en|for|if|in|of|on|or|the|to|v[.]?|via|vs[.]?)";
	var punct = "([!\"##$%&'()*+,./:;<=>?@[\\\\\\]^_`{|}~-]*)";
  	var parts = [], split = /[:.;?!] |(?: |^)["Ò]/g, index = 0;
	title = lower(title);
	while (true) {
		var m = split.exec(title);
		parts.push( title.substring(index, m ? m.index : title.length)
			.replace(/\b([A-Za-z][a-z.'Õ]*)\b/g, function(all){
				return /[A-Za-z]\.[A-Za-z]/.test(all) ? all : upper(all);
			})
			.replace(RegExp("\\b" + small + "\\b", "ig"), lower)
			.replace(RegExp("^" + punct + small + "\\b", "ig"), function(all, punct, word){
				return punct + upper(word);
			})
			.replace(RegExp("\\b" + small + punct + "$", "ig"), upper));

		index = split.lastIndex;

		if ( m ) parts.push( m[0] );
		else break;
	}

	return parts.join("").replace(/ V(s?)\. /ig, " v$1. ")
		.replace(/(['Õ])S\b/ig, "$1s")
		.replace(/\b(AT&T|Q&A)\b/ig, function(all){
			return all.toUpperCase();
		});
}

// converts a word to lowercase
function lower(word){
	return word.toLowerCase();
}

// converts a word to uppercase
function upper(word){
  return word.substr(0,1).toUpperCase() + word.substr(1);
}

// parent function to fade the object
function changeOpacity(strElementId, numOpacStart, numOpacEnd, numTransitionTime) {
    // set variables
    var numSpeed = Math.round(numTransitionTime / 100);
    var numTimer = 0;
    // determine the direction for the blending, if start and end are the same nothing happens
    if(numOpacStart > numOpacEnd) {
        for(i = numOpacStart; i >= numOpacEnd; i--) {
            setTimeout("setOpacity(" + i + ",'" + strElementId + "')",(numTimer * numSpeed)); numTimer++;
        }
    } else if(numOpacStart < numOpacEnd) {
        for(i = numOpacStart; i <= numOpacEnd; i++) {
            setTimeout("setOpacity(" + i + ",'" + strElementId + "')",(numTimer * numSpeed)); numTimer++;
        }
    }
}

// set the opacity
function setOpacity(numOpacity, strElementId) {
    var object = g(strElementId).style;
	object.opacity = (numOpacity / 100);
    object.MozOpacity = (numOpacity / 100);
    object.KhtmlOpacity = (numOpacity / 100);
    object.filter = "alpha(opacity=" + numOpacity + ")";
	// push to the back
	if(numOpacity == 0){ object.zIndex = 1; }
}

// function to load images allowing us to control the order
function loadImage(strImageID,strImageFileName){
	if(!document.images){ return; }
	document.images[strImageID].src = strImageFileName;
}

// function to find the X position of an element
function findPosX(obj){
	var curleft = 0;
	if(obj.offsetParent)
	    while(1) 
	    {
	      curleft += obj.offsetLeft;
	      if(!obj.offsetParent)
	        break;
	      obj = obj.offsetParent;
	    }
	else if(obj.x)
	    curleft += obj.x;
	return curleft;
}

// function to find the Y position of an element
function findPosY(obj){
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
}

// function to open mini flash radio player
function openRadioPlayer(strPlayerURL) {
	window.open(strPlayerURL,"Player","toolbar=0,status=0,resizable=1,width=180,height=70");
}

// Browser detection!
var blnSafari = false;
var blnFireFox = false;
var blnOpera = false;
var blnIE = false;
var blnChrome = false;
var blnMac = false;
if((navigator.vendor) && (navigator.vendor.indexOf("Apple") > -1)){ blnSafari = true; }
else if((navigator.userAgent) && (navigator.userAgent.indexOf("Firefox") > -1)) { blnFireFox = true; }
else if((window.opera) && (window.opera)) { blnOpera = true; }
else if((navigator.userAgent) && (navigator.userAgent.indexOf("MSIE") > -1)) { blnIE = true; }
else if((navigator.userAgent) && (navigator.userAgent.indexOf("Chrome") > -1)) { blnChrome = true; }
if((navigator.platform) && (navigator.platform.indexOf("Mac") > -1)){ blnMac = true; }

