// JavaScript Document

// Google Analytics
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-20193527-1']);
_gaq.push(['_trackPageview']);

(function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();


// This searches the site via the header search
function searchSite(){
	alert("Search needs work.");	
	window.location="/search/";
}


// This highlights the active link in the Left Navigation
function leftNavHighlight(theNav){
		
	var currentUrl = window.location.href;
			
	if(theNav){
		
		
		var navLinks = theNav.getElementsByTagName('a');
		var activeLink = null;
		
		for(j=0;j<navLinks.length;j++){
			
			//alert(navLinks[j].href + " : " + location.href);
			//alert(currentUrl.substr(0, currentUrl.length-1));
			
			//remember active link and set it after the loop. this causes only the last matching item to be highlighted.
			if(navLinks[j].href == currentUrl || navLinks[j].href == currentUrl.substr(0, currentUrl.length-1) || navLinks[j]+"default.aspx" == currentUrl || navLinks[j] == currentUrl+"default.aspx"){
					activeLink = navLinks[j];
			}
			
		}
		
		if(activeLink != null)
		{
			activeLink.className="mmhide_active";
		}
	}
}




// This sets the mmhide_active class on the appropriate gloval nav link
function navHighlight(){
		
	theNav = document.getElementById('siteHeader');
	currentPage = window.location.href;
	
	if(theNav){
		navLinks = theNav.getElementsByTagName('a');
			
		for(h=0;h<navLinks.length;h++){
		
			//get the first folder in nav link
			tmpSplitLink = navLinks[h].href.split("/");	
			while(tmpSplitLink[3].lastIndexOf('.aspx') > 0 || tmpSplitLink[tmpSplitLink.length-1]==""){
				tmpSplitLink.pop();
			}
			
			//get the last folder in the current location
			currentSplitLink = currentPage.split("/");
			while(currentSplitLink[3].lastIndexOf('.aspx') > 0 || tmpSplitLink[currentSplitLink.length-1]==""){
				currentSplitLink.pop();
			}
			
			//even the arrays	
			while(currentSplitLink.length > tmpSplitLink.length){
				currentSplitLink.pop();	
			}
			while(tmpSplitLink.length > currentSplitLink.length){
				tmpSplitLink.pop();	
			}
			
			
			//alert(tmpSplitLink + " : " + currentSplitLink);
			
			//see if they're a match
			matchFound=true;
			for(m=0;m<tmpSplitLink.length;m++){
				
				if(tmpSplitLink[m]!=currentSplitLink[m]){
					
					matchFound=false;
				}
			}
			if(matchFound){
				
				
				if(navLinks[h].className==""){
					navLinks[h].className="mmhide_active";
				}else{
					navLinks[h].className+=" mmhide_active";
				}
			}
			//alert(tmpSplitLink.length + " : " + currentSplitLink.length);
		}
	}
}

function setHomeBanner(){
	try{
		
		var banner = $("#homeBanner");
		var bannerSource;
		
		var reflection = $(".mmhide_home");
		var reflectionSource;
		
		var homeBannerCookie = parseInt(getCookie("homeBanner"));
		var maxBanners = 4;
		
		banner.css("background-image","");
		banner.css("display","none");
		
		if(homeBannerCookie > 0 && homeBannerCookie!=maxBanners){
			bannerSource = "/home/images/home-banner"+ (homeBannerCookie+1) + ".jpg";
			reflectionSource = "/home/images/home-reflection"+ (homeBannerCookie+1) + ".jpg";
			setCookie("homeBanner", (homeBannerCookie+1));
		}else{
			bannerSource = "/home/images/home-banner1.jpg";
			reflectionSource = "/home/images/home-reflection1.jpg";
			setCookie("homeBanner", 1);
			
		}
		
		banner.attr("src",bannerSource);		
		banner.animate({opacity:'show'}, 'slow');
		
		reflection.css("background-image","url("+reflectionSource+")");
		

		//localTrace(bannerSource);
	}catch(err){
		localTrace("Could not set random banner.\n"+err);	
	}
}


//
// Replaces the link to show the users email address, which when click, will log the user out
//
function setLogoutLink(emailAddr){

	var aloginLink = $("#aLoginLink");
	var liLoginLink = $("#liLoginLink");

	aloginLink.html('LOGOFF: ' + emailAddr);
	aloginLink.attr( {
		href: "/members/logoff.aspx",
		title: "Click to logoff",
		onClick: "return confirm('Are you sure you want to logoff?');"
		});

	liLoginLink.removeClass('mmhide_last');
	liLoginLink.addClass('mmhide_login');
}


function inputStyles(){
	pageInputs = document.getElementsByTagName('input');
	
	
	//style buttons
	for(pi=0;pi<pageInputs.length;pi++){
		if(pageInputs[pi].className.length<=0){
			if(pageInputs[pi].type=='submit' || pageInputs[pi].type=='reset' || pageInputs[pi].type=='button'){
				pageInputs[pi].className="input-button";
			}else if(pageInputs[pi].type=="checkbox" || pageInputs[pi].type=="radio"){
				pageInputs[pi].className="input-checkbox";	
			}
		}
	}
	
}




function setCookie( Name, Value )
{
	document.cookie = Name + "=" + Value;
}



function getCookie( Name ) 
{
	var search = Name + "="
	var returnvalue = "";
	
	if (document.cookie.length > 0) 
	{
		offset = document.cookie.indexOf(search)
		// if cookie exists
		if (offset != -1) 
		{ 
			offset += search.length
			// set index of beginning of value
			end = document.cookie.indexOf(";", offset);
			// set index of end of cookie value
			if (end == -1) end = document.cookie.length;
			returnvalue=unescape(document.cookie.substring(offset, end))
		}
	}
	
	return returnvalue;
} 



function localTrace(msg){
	if(debug){
		try{
			console.log(msg);
		}catch(ex){
			//alert(ex.message);	
			alert(msg);
		}
	}
}

