var setomnilanguage = "en";
var omnilang = "en";
function getomniLang()
            {
                var everycookievalue  = document.cookie;
                var mySplit = everycookievalue.split(";");
                var patternName    = /^[^=]+/;
                var patternValue   = /[^=]+$/;
                var patternNospace = /[^\s]+/;
                var resultName;
                var resultValue;
                    for ( count=0 ; ( mySplit[count] != undefined ) ; count++ )
                    {
                        resultName = mySplit[count].match(patternName);
                        resultName = resultName[0].match(patternNospace);
                        if(resultName == 'selectedLanguage')
                        {
                            resultValue = mySplit[count].match(patternValue);
                            if ((resultValue != null) && (resultValue.length > 0))
                             {
                                     omnilang = resultValue[0].match(patternNospace);
                             }
                        }
                    }
                    return omnilang;
            }

setomnilanguage = getomniLang();



var server = 'http://community.nortel.com';
var debug = 0;
var PathArray = new Array();

var PageNameMap = new Array();
var SChannelMap = new Array();

var PageNameArray = new Array();
var SChannelArray = new Array();




/*
All directory matches should be entered with an ending "/".
If not there is the possibility you may match an incorrect path. 
(e.g if you enter:
"coporate/global/emea"
it is possible that you would match 
"coporate/global/emea.html"
which may be an incorrect match.
*/
//	     Path					pagename
PageNameMap['/go/community/nortel_customers__partners_and_guests_forums/'] = 'BAF: ' + setomnilanguage + ': Community';
PageNameMap['/go/'] = 'BAF: ' + setomnilanguage;
PageNameMap['/clearspace_community/'] =	'BAF: ' + setomnilanguage;
PageNameMap['/clearspace_community/main-threads.jspa'] =	'BAF: ' + setomnilanguage + ': Forums: Home';
PageNameMap['/go/main-threads.jspa'] =	'BAF: ' + setomnilanguage + ': Forums: Home';
PageNameMap['/go/index.jspa'] =	'BAF: ' + setomnilanguage + ': Community Home';
PageNameMap['/clearspace_community/index.jspa'] =	'BAF: ' + setomnilanguage + ': Community Home';
PageNameMap['/go/community/other/nortel_developer_program_member_forum'] =	'BAF: ' + setomnilanguage + ': Community: Other: Developer Program Forum';
PageNameMap['/clearspace_community/community/other/nortel_developer_program_member_forum'] =	'BAF: ' + setomnilanguage + ': Community: Other: Developer Program Forum';


var PageNameArray = new Array();
for (var path in PageNameMap){
	var pagename = PageNameMap[path];
	if (path.lastIndexOf('/') == path.length - 1){
		path = path.substring(0, path.length - 1);
	}
	PageNameArray[PageNameArray.length] = new Array(path, pagename);
}

var defaultPageNameValueArray			= 	new Array('/', 'BAF: ' + setomnilanguage);


/*
All directory matches should be entered with an ending "/".
If not there is the possibility you may match an incorrect path. 
(e.g if you enter:
"coporate/global/emea"
it is possible that you would match 
"coporate/global/emea.html"
which may be an incorrect match.
*/

//	     Path					s_channel
SChannelMap['/go/']		=	'Community';
SChannelMap['/community_clearspace/']		=	'Community';


var SChannelArray = new Array();
for (var path in SChannelMap){
	var channel_name = SChannelMap[path];
	if (path.lastIndexOf('/') == path.length - 1){
		path = path.substring(0, path.length - 1);
	}
	SChannelArray[SChannelArray.length] = new Array(path, channel_name);
}

var defaultSChannelValueArray 			= 	new Array('/', 'Community');

// Here's a neat trick that I realized while my thoughts simmered regarding this project...

// If the List above is reverse sorted, then going down through the list
// (in the reverse order) will find the most specific path and then it can exit.

// Thus if you're on the page '/corporate/technology/filler', and you go through this
// list, the first thing you come across that matches in a reverse ordered list is
// '/corporate/technology' (since '/corporate' would end up below '/corporate/technology' in a reverse
// sorted list. 

// Thankfully, this principle will allow you to search through the list from most specific, to least specific

PageNameArray.sort();
PageNameArray.reverse();

SChannelArray.sort();
SChannelArray.reverse();

// This function takes the principle mentioned above and applies it.
// Note that it only checks if the *beginning* of the 'target_url' matches
// the 'Path' from the list above
// If it can't find a match, it returns 'No Path', which has significance in the getMetricsArray() function
function getMostDistinctPathIndex (target_url, path_array){
	for (var i = 0; i < path_array.length; i++){
		var an_array = path_array[i];
		if (target_url.indexOf(an_array[0]) == 0){
			return i;
		}
	}
	return 'No Path';
}

// Instead of accessing the array directly (which won't work for target_url's that are not
// specified in the PathArray), we use this subroutine. If it can't find a specified PathArray,
// it returns the 'default_value_array' which is specified above
function getMetricsArray (target_url, path_array, array_name){
	var index = getMostDistinctPathIndex(target_url, path_array, array_name);
	if (index == 'No Path'){
		// 'default_value_array' Defined above
		if (array_name == 'PageNameArray'){
			return defaultPageNameValueArray;
		}
		else if (array_name == 'SChannelArray'){
			return defaultSChannelValueArray;
		}
		else {
			alert("Can't handle array_name: " + array_name);
		}
	}
	else {
		return path_array[index];
	}
}

// Pretty straight-forward, this accesses the first element the 'Path'
function getPath (target_url, path_array, array_name){
	var an_array = getMetricsArray(target_url, path_array, array_name);
	return an_array[0];
}

function getPageName (target_url){
	var an_array = getMetricsArray(target_url, PageNameArray, 'PageNameArray');
	
	var page_name = an_array[1];
	if (page_name == ''){
		return '';
	}
	
	// This is the first element (the Path) of the array that is set above 
	var matched_path = getPath(target_url, PageNameArray, 'PageNameArray');

	// This is the extra stuff after what had been previously identified...
	var extra_pathname = target_url.substring(matched_path.length, target_url.length);

	// The rest of this routine is spent massaging this 'extra_pathname

	// Remove '/index.html' from URLs that have it
	if (extra_pathname.lastIndexOf('/index.html') == extra_pathname.length - new String('/index.html').length){
		extra_pathname = 	extra_pathname.substring(0, extra_pathname.indexOf('/index.html')) + 
					extra_pathname.substring(extra_pathname.indexOf('/index.html') + new String('/index.html').length, extra_pathname.length);
	}
	
	// Remove '.html' from URLs that have it
	// if (extra_pathname.indexOf('.html') > -1){
	// alert(extra_pathname + ' ' + new String(extra_pathname.lastIndexOf('.html')) + ' ' + new String(extra_pathname.length - 5));
	if ((extra_pathname.lastIndexOf('.html') > -1) && (extra_pathname.lastIndexOf('.html') == extra_pathname.length - 5)){
		extra_pathname = 	extra_pathname.substring(0, extra_pathname.length - 5);
		// alert('hello');
		//			extra_pathname.substring(0, extra_pathname.indexOf('.html')) + 
		//			extra_pathname.substring(extra_pathname.indexOf('.html') + new String('.html').length, extra_pathname.length);
	}

	// If it starts with a slash '/', then go ahead and get rid of that first slash
	if (extra_pathname.indexOf('/') == 0){
		extra_pathname = extra_pathname.substring(1, extra_pathname.length);
	}

	// If it ends with a slash '/', then go ahead and get rid of that last slash
	// alert(extra_pathname + ' ' + extra_pathname.lastIndexOf('/') + ' ' + extra_pathname.length);
	if (extra_pathname.lastIndexOf('/') == extra_pathname.length - 1){
		// alert(extra_pathname.substring(0, extra_pathname.length - 1));
		extra_pathname = extra_pathname.substring(0, extra_pathname.length - 1);
	}
	

	// Take the pathname information between slashes, lowercase it, and uppercase the first letter of each element
	extra_pathname_array = extra_pathname.split("/");
	for (var i = 0; i < extra_pathname_array.length; i++){
		var pathname_string = new String(extra_pathname_array[i]);
		// alert(pathname_string);
		pathname_string = pathname_string.toLowerCase();
		pathname_string_array = pathname_string.split("");
		if (pathname_string_array[0]){
			pathname_string_array[0] = pathname_string_array[0].toUpperCase();
		}
		extra_pathname_array[i] = pathname_string_array.join("");
	}
	
	extra_pathname = extra_pathname_array.join(": ");
		
	// alert(extra_pathname);
	
	if (extra_pathname){
		page_name = page_name + ': ' + extra_pathname;
	}

	return page_name;
}

function getSChannel (target_url){
	var an_array = getMetricsArray(target_url, SChannelArray, 'SChannelArray');
	return an_array[1];
}

function getCurrentLocation () {
	var pathname = '';
	if (debug){	
		pathname = document.links[0].pathname;
	}
	else {		
		pathname = window.location.pathname;
	}

	if (pathname.indexOf('/') != 0){
		pathname = '/' + pathname;
	}
	
//remove jsessionid from path
	var patt=new RegExp(";jsessionid=[\-\_0-9A-Za-z]*\.tomcat[12]");

	if (patt.test(pathname)==true)
	  {
	  var patt2=new RegExp("(.*)(;jsessionid=[\-\_0-9A-Za-z]*\.tomcat[12])(.*)");
	  myArray = patt2.exec(pathname);
	  pathname = myArray[1] + myArray[3];

	  }
	return pathname; 

}

function setCurrentLocation (an_url){
	document.links[0].href = an_url;
}


