// tlhunter 2010-01-21 - Initial Build
//          2010-04-20 - i18n check
// Automatically highlights current page using simplediv structure
//Modified for Polyurethanes Revamp 08/2011
$(document).ready(function() {
	var currentPath = window.location.href;
	var languageLink = 0;//added
	var pagePath = 0;
	if (currentPath.match(/\/([a-z]{2})\/([a-z]{3})\/([a-z]{2})\//)) { // ~ na/usa/en
		firstImportantSegment = 3;
	}
	else if (currentPath.match(/\/([a-z]{2})\/([a-z]{2})\//)) { // ~ na/en
		firstImportantSegment = 2;
	}
	else {
		firstImportantSegment = 0;
	}
	var domain = document.domain;
	if (domain == "usmdlsdoww200" || domain == "usmdlsdoww840") {
		var metaPath = "http://" + $("meta[name=dow-site-base-internal]").attr("content") + "/";
	}
	else if (domain.indexOf("-st.") >= 0 || domain.indexOf("preview-") >= 0) {
		var metaPath = "http://preview-" + $("meta[name=dow-site-base]").attr("content") + "/";
	}
	else {
		var metaPath = "http://" + $("meta[name=dow-site-base]").attr("content") + "/";
	}
	if (metaPath) {
		errorLog("currentPath: " + currentPath, 'info');
		errorLog("metaPath: " + metaPath, 'info');

		if (currentPath.indexOf(metaPath) == 0) {
			subPath = removeTrailingSlash(currentPath.substring(metaPath.length, currentPath.length));
			//errorLog("subPath: " + subPath, 'info');
				
			chopPath = removeEmptyArrayElements(currentPath.substr(metaPath.length).split("/"));
			pagePath = chopPath;
			$(".primary-nav ul li").each(function() {
				var link = removeEmptyArrayElements($(this).find("a").attr("href").split("/"));

				if (link[firstImportantSegment + 1] == chopPath[firstImportantSegment]) {
					$(this).addClass("primary-nav-selected");
					$(this).removeClass("primary-nav-unselected");
					//errorLog("link: " + link + " chopPath: " + chopPath, 'log');
					if (link[firstImportantSegment + 2] == chopPath[firstImportantSegment + 1]){
						$(this).addClass("primary-nav-currentpage");
					}
					else{
						$(this).removeClass("primary-nav-currentpage");
					}
				}
				else{
					$(this).removeClass("primary-nav-selected");
					$(this).addClass("primary-nav-unselected");
				}
				
			});
				
		}
		else {
			errorLog("dow-site-base meta tag is lying to us.", 'error');
		}
	}
	else {
		errorLog("Site doesn't use dow-site-base meta tag.", 'info');
	}
	
	if (chopPath[firstImportantSegment + 1] != 'index.htm'){  //If not on an index page, expand current link's ul
		$(".primary-nav-currentpage ul").removeClass("navigation-collapsed"); //Displays secondary subpage ul
	}
	
	//Automatically sets language links
	var languagePath = "/polyurethane";
	//English
	for (var languageIndex = firstImportantSegment; chopPath[languageIndex] != null; languageIndex++){
		languagePath = languagePath + "/" + chopPath[languageIndex];
	}
	$(".language-select .english a").attr("href", languagePath); //sets language href path
	//Chinese
	languagePath = "/polyurethane/ap/zh";
	for (var languageIndex = firstImportantSegment; chopPath[languageIndex] != null; languageIndex++){
		languagePath = languagePath + "/" + chopPath[languageIndex];
	}
	$(".language-select .chinese a").attr("href", languagePath); //sets language href path
	//errorLog("languagePath: " + languagePath, 'log');
	//End set language----------------
	
});

	function removeEmptyArrayElements(myArray) {
		for (var i in myArray) {
			if (myArray[i] == '') {
				myArray.splice(i, 1);
			}
		}
		return myArray;
	}

	function removeTrailingSlash(string) {
		if (string.substring(string.length - 1) == '/') {
			string = string.substring(0, string.length - 1);
		}
		return string;
	}
