// cahs_activenav.js

/* 	version: 	1.2
	release: 	january 27, 2008
	author:  	gbenjamin
	desc:		activenavstyle is used to dynamically apply styles
				to navigation list items based on where the current
				page resides in the folder structure of the site
	
	usage:		- stringtoreplace:
					removes http://siteroot from url
				  	important to strip url down to main section in order
				  	for folderCount var to be initialized correctly
				- folderCount:
					starting with main section, returns count of folder heirarchy
					if count > 0 then we are at least at the main section folder level
				- el:
					uses jquery xpath to get the first listitem in the first unorderedlist
					in the lefthand navigation div 'contentLeft'
				- if(folderCount > 1):
					if folderCount is > 1, then we are at least in a child folder of the main section
					applies the 'sectionTitle' class to the first listitem and removes the link
					then gets the child folder of the main section 'loc[1]'
					applies the 'activeSection' class to listitem with href 
					containing foldername found in loc[1]
				- if(folderCount < 1):
					applies class 'sectionTitleRoot' to the first listitem in the first unordered list
					in 'contentLeft'
				  
	notes:		requires jquery version 1.3.1+
	 
*/

$(function() {
	var sPath = window.location.pathname;
	var sPage = sPath.substring(sPath.lastIndexOf('\\') + 1);
		//sPage = sPath.substring(sPath.lastIndexOf('/cahsweb/') + 1).replace(sPage.split("/")[0],'');
	var loc = sPage.substring(sPage.indexOf('/')+1).split("/");
	var folderCount = loc.length-1;
	if(folderCount>0){
		var el = "#contentLeft ul:first li:first";		
		if(folderCount>1){
			$(el).html($(el + " a")).addClass('sectionTitle');
			var p = 'index.html';
			if(folderCount>2){
				for(i=3;i<=folderCount;i++){
					p = "../" + p;
				}
			}
			$("a[href = " + p + "]").parent().addClass('activeSection');
			// highlight active page in right menu
			var el2 = "#contentRightMoreInfo ul:first li";
			var ap = sPath.substring(sPath.lastIndexOf('/') + 1);
			$("a[href = " + ap + "]").parent().addClass('active');
		}
		else
		{
			$(el).html($(el + " a").html()).addClass('sectionTitleRoot');
		}	
	}
	vid = getUrlParams("vid");
	var vd = getUrlParams("vd").replace(/%20/g," ");
	var vt = getUrlParams("vt").replace(/%20/g," ");
	
	if(vid !=0){
		// set video page variables
		$('#videoTitle').html(vt);
		$('#videoDescription').html(vd);
	}
});

function getUrlParams(par)
{
	par = par.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var rgxs = "[\\?&]"+par+"=([^&#]*)";
	var rgx = new RegExp(rgxs);
	var results = rgx.exec( window.location.href );
	if( results == null ){
		return "0";
	} else {
		return results[1];
	}
}