/* Usage: getURLParam('nameOfParamToGet');
   Example: If the current URL is "...?opendocument&id=testid" then calling getURLParam("id") will return "testid"
*/
function getURLParam(strParamName){
	var strReturn = "";  
	var strHref = window.location.href;  
	if ( strHref.indexOf("?") > -1 ){
		var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
		var aQueryString = strQueryString.split("&");
		for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
			if (aQueryString[iParam].indexOf(strParamName + "=") > -1 ){ 
				var aParam = aQueryString[iParam].split("=");
				strReturn = aParam[1];
				break; 
			}
		}
	}  
	return strReturn;
}

function capitalizeFirst(str) {
    newVal = '';
    str = str.replace('_',' ');
    str = str.split(' ');
    for(var c=0; c < str.length; c++) {
		newVal += str[c].substring(0,1).toUpperCase() + str[c].substring(1,str[c].length) + ' ';
    }
    return newVal;
}

//new breadcrumb creator.
//variables
var crumbsep = " > ";
var precrumb = "<span class=\"crumb\">";
var postcrumb = "</span>";
var currentUrl = "";
var allbread = "";
//look at pageUrl to find out where we are
var pageUrl = (new String(document.location.href));	
//increment the url for links
currentUrl = pageUrl.substring(0, pageUrl.indexOf("//")+2);
//remove protocol
pageUrl = pageUrl.replace(pageUrl.substring(0, pageUrl.indexOf("//")+2), ""); 
//increment the url for links
currentUrl += pageUrl.substring(0, pageUrl.indexOf("/")+1);
//remove in.gov or localhost
pageUrl = pageUrl.replace(pageUrl.substring(0, pageUrl.indexOf("/")+1),"")	
//remove indianatourism or visitindiana
currentUrl += pageUrl.substring(0, pageUrl.indexOf("/")+1);
//set the home link in the breadcrumbs
pageUrl = pageUrl.replace(pageUrl.substring(0, pageUrl.indexOf("/")+1),"");
//create crumb for home.
allbread += precrumb + "<a href=\"" + currentUrl + "index.aspx\">Home</a>" + postcrumb;

if((pageUrl.indexOf("/")>=0)&&pageUrl.indexOf("?")<0){
	//has subfolders
	while(pageUrl.indexOf("/")>0){
		//write other in between crumbs 
		currentUrl += pageUrl.substring(0, pageUrl.indexOf("/")+1);
		allbread += crumbsep + precrumb + "<a href=\"" + currentUrl + "index.aspx\">" + capitalizeFirst(pageUrl.substring(0,pageUrl.indexOf("/"))) + "</a>" + postcrumb;
		pageUrl = pageUrl.replace(pageUrl.substring(0, pageUrl.indexOf("/")+1),"");
	}
	//write final crumb based on location
	allbread += crumbsep + document.title;
} else {
	//no sub folders
	if (pageUrl.indexOf("trip")>=0){	//tripplanner
		//check for search terms in the querystring
		allbread += crumbsep + precrumb + "<a href=\"" + currentUrl + "tripplanner.aspx\">Trip Planner</a>" + postcrumb;
		//write final breadcrumb based on search parameter
		var tripCrumb = "Search";
		var tripCrumb2 = "";
		if (getURLParam('city').length>0){tripCrumb += ": " + capitalizeFirst(getURLParam("city").replace("_"," "))}
		else if (getURLParam('region').length>0){tripCrumb += ": " + capitalizeFirst(getURLParam("region").replace("_"," "))}
		else if (getURLParam('search').length>0){tripCrumb += ": " + capitalizeFirst(getURLParam("search").replace("_"," "))}
		else if (getURLParam('activity').length>0){tripCrumb += ": " + capitalizeFirst(getURLParam("activity").replace("_"," "))}
		else if (getURLParam('lodging').length>0){tripCrumb += ": " + capitalizeFirst(getURLParam("lodging").replace("_"," "))}
		else {tripCrumb = "Trip Planner";}
		allbread += crumbsep + precrumb + tripCrumb + postcrumb;
	} else if (pageUrl.indexOf("contest")>=0){ //contest
		allbread += crumbsep + precrumb + "Contests" + postcrumb;
	} else {
		allbread += crumbsep + precrumb + capitalizeFirst(document.title) + postcrumb;
	}
}

//write to document
document.write(allbread);
