// script specific to IE and support it's DOM properties and methods. Will not run on Firefox or other cotemporary browsers.
//get language of browser

var sDoc = document;
var isIE = (window.ActiveXObject)?true:false;
var IEVer = getInternetExplorerVersion();

var blang = "";
		if (isIE) {
			blang = navigator.browserLanguage;
		} else{
			blang = navigator.language;
		}
		
		sysLang = getSysLang(blang);

		

//load glossary entries into memory
 if (isIE) {
	var xmlDoc = new ActiveXObject('Microsoft.XMLDOM');
	xmlDoc.async = 'false';
	//var glosspath = "../" + sysLang + "/glossary.html";
	xmlDoc.load('glossary.xml');
	var topChk = xmlDoc.hasChildNodes();
	//topChk = true;
}



else {
	
	try { //for all other browsers except Firefox
		var req = new XMLHttpRequest();
		var glosspath = "../" + sysLang + "/glossary.html";
		req.open('GET', glosspath, false); 
		req.setRequestHeader('Content-Type', 'text/xml')
		req.send("");
		var xmlDoc = req.responseXML;
		var topChk = xmlDoc.hasChildNodes();
		
	}																																					

	catch(e) { // Firefox doesn't support reading XML doc above current HTML folder location.
		xmlDoc = null;
		parmDoc = null;
		var parmChk = false;
		var topChk = false;
		//probably need function to read values for building a hubURL to product support site.
	}
}



if (topChk != false) { //process glossary.html and get terms/definitions
	var glossNodes = xmlDoc.getElementsByTagName('dl');
	
}


//jquery container to fire functions upon document loading
$(document).ready(function() {
	setTopicHeading();
	setCollapsibleDivs();
	/*function position() {
			$( ".topicglossopen" ).position({
				of: $(" #topicheading"),
				my: $( "right" ) + " " + $( "top" ),
				at: $( "right" ) + " " + $( "bottom" ),
				offset: $( "0 0" ),
				collision: $( "fit" ) + " " + $( "fit" )
			});
		}*/
	
	
	
	//check topic for glossary entries
	if (topChk != false) {
	var topicGTerms = getTopicGlossTerms();
	if (topicGTerms.length > 0) {
			bldGlossary(topicGTerms);
		}
	}
	
	
	
});




function setTopicHeading() {
		
		//create div for non-scroll region and move heading to this region
		var bodyTag = sDoc.getElementsByTagName("body")[0];
		var topDiv = sDoc.getElementsByTagName("div")[0];
		var headTag = sDoc.getElementsByTagName("h2")[0];
		if (headTag == null) {
			headTag = sDoc.getElementsByTagName("h1")[0]; //look for H1 tag in topic, if h2 doesn't exist. For glossary.
		}
		var nsrDiv = sDoc.createElement("div");
		var headParent = headTag.parentNode;
		var cloneHDiv = headTag.cloneNode(true);
		nsrDiv.setAttribute("id","topicheading");
		nsrDiv.appendChild(cloneHDiv);
		headParent.removeChild(headTag);
		bodyTag.insertBefore(nsrDiv,topDiv);
		
	}

function setCollapsibleDivs() {
	var pElems = document.getElementsByTagName('span');
	var i = 0;
	while (pElems.item(i) != null) {
		var currP = pElems[i];
		if(IEVer == "6" || IEVer == "7") {
			var classAtt = currP.className;
		}
		else {
			var classAtt = currP.getAttribute("class");
		}
		if (classAtt == "escapesequence") { //div trigger found
			var divTrig = currP.parentNode;
			if(IEVer == "6" || IEVer == "7") {
				divTrig.onclick = function(){toggleDiv(event)};
				divTrig.className = "divtrigclosed";
			}
			else {
				divTrig.setAttribute("onclick","toggleDiv(this)");
				divTrig.setAttribute("class","divtrigclosed");
			}
			
		}
		i++;
	}
}


function getTopicGlossTerms() {
	//check HTML for elements with Glossary terms
	var aElems = document.getElementsByTagName('a');
	var gTerms = new Array();
	var i = 0;
	while (aElems.item(i) != null) {
		var currA = aElems[i];
		//alert(currA);
		if(IEVer == "6" || IEVer == "7") {
			var classAtt = currA.className;
			
		}
		else {
			var classAtt = currA.getAttribute("class");
			//alert(classAtt);
		}
		if (classAtt == "glossterm") { //if html topic has gloss terms, return list
			
			var glossID = currA.getAttribute("href");
			var hrefSegs = glossID.split("#");
			var gtermSegs = hrefSegs[1].split("_");
			if (gtermSegs.length > 2) { // if author used multiple underscores in term link, need to parse segments
				var hrefsegmt = "";
				for(x=1;x<gtermSegs.length;x++){
				 hrefsegmt = hrefsegmt + "_" + gtermSegs[x];
				 
				}
				hrefSegs[1] = "gloss" + hrefsegmt;
				gtermSegs[1] = hrefsegmt.substr(1);
			}
			
			gTerms.push(hrefSegs[1]);
			
			currA.removeAttribute("href"); //Remove href att.
			currA.setAttribute("onclick","toggleGlossary(this);");
			currA.setAttribute("id","glink_" + gtermSegs[1]);
			if(IEVer == "6") { //For IE 6, build link differently.
				currA.setAttribute("href",glossID);
			}
			if(IEVer == "7") {
				//currA.setAttribute("href","#");
				currA.onclick = function(){toggleGlossary(event)};
			}
		}
		i++;
	}
	return gTerms;
}

function bldGlossary(topicGTerms) {
	
	//build glossary div, populate with glosary term and definition.	
	var colDivElems = sDoc.getElementsByTagName('div');	
	var bannerDiv = colDivElems[1]; //grab banner div. use this as for container to create glossary.
	var glossDiv = sDoc.createElement("div");
	var glossLink = sDoc.createElement("p");
	var glossLinkA = sDoc.createElement("a");
	//var glossLinkTxt = sDoc.createTextNode("Close"); //Need to write function for passing in variable for text with localized term.
	var glossLinkTxt = sDoc.createTextNode("__"); //Need to write function for passing in variable for text with localized term.
	
	//Add div with text link for trigger
	glossLinkA.appendChild(glossLinkTxt);
	glossLink.appendChild(glossLinkA);
	glossDiv.appendChild(glossLink);
	bannerDiv.appendChild(glossDiv);
	//set attributes
	glossDiv.setAttribute("id","topicgloss");
	glossLinkA.setAttribute("href","#close");
	if (IEVer == "6" || IEVer == "7") { // IE 6 workaround
		glossDiv.className = "topicglossclosed";
		glossLink.className = "glosslink";
		//glossLinkA.onclick = function(){toggleGlossary("close")};
		glossDiv.onclick = function(){toggleGlossary("close")};
	}
	else {
		glossDiv.setAttribute("class","topicglossclosed");
		glossLink.setAttribute("class","glosslink");
		//glossLinkA.setAttribute("onclick","toggleGlossary('close');");
		glossDiv.setAttribute("onclick","toggleGlossary('close');");
	}
	glossLink.setAttribute("id","glosspanelink");
	//set build definition term list
	var i = 0;
	while (topicGTerms[i] != null) {
		var glossSet = buildGlossEntry(topicGTerms[i]);
		glossDiv.appendChild(glossSet);
		i++;
	}
}

function buildGlossEntry(gTerm) {

	var glossDl = sDoc.createElement("dl");
	glossDl.setAttribute("id",gTerm);
	if (IEVer == "6" || IEVer =="7") {
			glossDl.className = "glosstermclosed";
		}
	else {
		glossDl.setAttribute("class","glosstermclosed");
	}
	var i = 0;
	var termTxt = gTerm + " not found";
	var defTxt = "definition not found";
	while (glossNodes.item(i) != null)	//loop through glossary entries to get matched term/definition.	 
   {
   	var currNode = glossNodes[i];
   	var glossID = currNode.getAttribute("id");
   	if (gTerm == glossID) {
   		termTxt = currNode.firstChild.childNodes[0].nodeValue;
   		defTxt = currNode.firstChild.nextSibling.firstChild.childNodes[0].nodeValue;
   	}
   	i++;
 	}
 	var dtTag = sDoc.createElement("dt");
	  var ddTag = sDoc.createElement("dd");
	  var tTxt = sDoc.createTextNode(termTxt);
	  var dTxt = sDoc.createTextNode(defTxt);
	  dtTag.appendChild(tTxt);
	  ddTag.appendChild(dTxt);
	  glossDl.appendChild(dtTag);
	  glossDl.appendChild(ddTag);
	return glossDl;
}

function toggleDiv(divTriggerElem) {
	
	//get class name on trigger, open div if trigger is collapsed. CLose div it's open.
	if (IEVer == "6" || IEVer == "7") {
	 var elemClicked = divTriggerElem.srcElement;
	 var bodyDiv = elemClicked.parentNode.nextSibling;
	 if (elemClicked.parentNode.className == "divtrigclosed") {
		//open div and change class on trigger
		bodyDiv.className = "bodydivopen";
		elemClicked.parentNode.className = "divtrigopen";
	 }
	 
	//add additional conditional blocks to support DOM model on older IE versions 
	 else if (elemClicked.className == "divtrigclosed") {
		var bodyDiv = elemClicked.nextSibling;
		bodyDiv.className = "bodydivopen";
		elemClicked.className = "divtrigopen";
	 }
	 
	 else if (elemClicked.className == "divtrigopen") {
		var bodyDiv = elemClicked.nextSibling;
		bodyDiv.className = "bodydiv";
		elemClicked.className = "divtrigclosed";
	 }
	 
	 else { 
		
		//close div and change class on trigger
		bodyDiv.className = "bodydiv";
		elemClicked.parentNode.className = "divtrigclosed";
	 }
	}
	else {
	 var elemClicked = divTriggerElem;
	 var bodyDiv = elemClicked.nextSibling;
	 
	 
	 if (IEVer == "9" || IEVer == "10") { //for IE 9, which has slightly different DOM structure.
	 		bodyDiv = bodyDiv.nextSibling;
	 	}
		
	 if (elemClicked.getAttribute("class") == "divtrigclosed") {
	 	
		bodyDiv.setAttribute("class","bodydivopen");
		elemClicked.setAttribute("class","divtrigopen");
	 }
	 else { 
		bodyDiv.setAttribute("class","bodydiv");
		elemClicked.setAttribute("class","divtrigclosed");
	 }
	}
	
	
	
	
}
function toggleGlossary(linkID) {
	
	if (linkID != "close") {	
		if (IEVer == "6" || IEVer == "7") {
		 var elementClicked = linkID.srcElement;	 
		 gLinkAtt = elementClicked.className;
		}
		else {
		 var gLinkAtt = linkID.getAttribute("class");
		}
	}
	
	if (gLinkAtt != null) {
		
		var elemID = "";
		if (IEVer == "6" || IEVer == "7") {
			elemID = elementClicked.getAttribute("id");
		}
		else {
			elemID = linkID.getAttribute("id");
		}
		
		var linkSegs = elemID.split("_");
		elemID = "gloss_" + linkSegs[1];
		if (linkSegs.length > 2) { // if author uses multiple underscores in term ID, need to parse segments
				
				var linksegmt = "";
				for(x=1;x<linkSegs.length;x++){
				 linksegmt = linksegmt + "_" + linkSegs[x];
				}
				elemID = "gloss" + linksegmt;
			}
		
	}
	var glossDiv = sDoc.getElementById("topicgloss");
	if (IEVer == "6" || IEVer == "7") {
			var glossstate = glossDiv.className;
		}
	else {
		var glossstate = glossDiv.getAttribute("class");
	}
	var glossDLs = sDoc.getElementsByTagName("dl");
	
	for(x=0;x<glossDLs.length;x++) {
		var glossDLclass = "";
		if (IEVer == "6" || IEVer =="7") {
			glossDLclass = glossDLs[x].className;
		}
		else {
			glossDLclass = glossDLs[x].getAttribute("class");
		}

	
		if (glossDLclass == "glosstermopen") {
			var openGlossID = glossDLs[x]; //find open gloss term
		}
	}
	if (linkID == "close") { //called from Close link. close glossary and terms
		if (IEVer == "6" || IEVer == "7") {
			glossDiv.className = "topicglossclosed";
			openGlossID.className = "glosstermclosed";
		}
		else {
			glossDiv.setAttribute("class","topicglossclosed");
			openGlossID.setAttribute("class","glosstermclosed");
		}
	}
	else { //called from topic glossary link
		var glossDl = sDoc.getElementById(elemID);
	
		if (glossstate == "topicglossclosed") { //glossary is closed, so open it.
			if (IEVer == "6" || IEVer == "7") {
				glossDl.className = "glosstermopen";
				glossDiv.className = "topicglossopen";
			}
			else {
				glossDl.setAttribute("class","glosstermopen");
				glossDiv.setAttribute("class","topicglossopen");
			}
		}
		
		else { //glossary is open. If ID on link is different from open term, then load new term; otherwise close glossary and term.
			if (openGlossID.getAttribute("id") == elemID) { //id on clicked element matches open glossary term, so close glossary.
				if (IEVer == "6" || IEVer == "7") {
					glossDiv.className = "topicglossclosed";
					glossDl.className = "glosstermclosed";
				}
				else {
					glossDiv.setAttribute("class","topicglossclosed"); 
					glossDl.setAttribute("class","glosstermclosed");
				}
			}
			else { //id is different, so change glossary term.
				if (IEVer == "6" || IEVer == "7") { 
					openGlossID.className = "glosstermclosed";
					glossDl.className = "glosstermopen";
				}
				else {
					openGlossID.setAttribute("class","glosstermclosed");
					glossDl.setAttribute("class","glosstermopen");
				}
			}
		}
		
	}
}




function getSysLang(lng) {
	
	lnTwo = lng.substring(2,0);
	switch(lnTwo) {
				case "ar": 
					lng = "1025";
					break;
				case "bg": 
					lng = "1026";
					break;
				case "zh":
					if (lng == "zh-tw" || lng == "zh-hk") {
						lng = "1028";
					}
					else {
						lng = "2052";
					}
					break;
				case "hr":
					lng = "1050";
					break;
				case "cs":
					lng = "1029";
					break;
				case "da":
					lng = "1030";
					break;
				case "nl":
					lng = "1043";
					break;
				case "en":
					lng = "en_ww"; //"1033";
					break;
				case "fi":
					lng = "1035";
					break;
				case "fr":
					lng = "1036";
					break;
				case "de":
					lng = "1031";
					break;
				case "el":
					lng = "1032";
					break;
				case "he":
					lng = "1037";
					break;
				case "hu":
					lng = "1038";
					break;
				case "id":
					lng = "1057";
					break;
				case "it":
					lng = "1040";
					break;
				case "ja":
					lng = "1041";
					break;
				case "ko":
					lng = "1042";
					break;
				case "lv":
					lng = "1062";
					break;
				case "lt":
					lng = "1063";
					break;
				case "no":
					lng = "1044";
					break;
				case "pl":
					lng = "1045";
					break;
				case "pt":
					lng = "1046";
					break;
				case "ro":
					lng = "1048";
					break;
				case "ru":
					lng = "1049";
					break;
				case "sk":
					lng = "1051";
					break;
 				case "sl":
					lng = "1060";
					break;
				case "es":
					lng = "1034";
					break;
				case "sv":
					lng = "1053";
					break;
				case "th":
					lng = "1054";
					break;
				case "tr":
					lng = "1055";
					break;
				case "kk":
					lng = "1087";
					break;
				case "sr":
					lng = "2074";
					break;
				case "uk":
					lng = "1058";
					break;
				default:
					lng = "1033";
		}
		
		
		// now that we have the language load the css for either RTL or LTR languages
		//loadCorrectCss(lng);
		
		return lng;

}

function loadCorrectCss(lng) {
	
	// now that we have the language load the css for either RTL or LTR languages
		
		var cssFileName = "css/slang.css";
		
		// if RTL load RTl Css
		if( lng == "1025" || lng == "1037" ){
			cssFileName = "css/slang_rtl.css";
		}
		
		
		var fileref=document.createElement("link");
		fileref.setAttribute("rel", "stylesheet");
		fileref.setAttribute("type", "text/css");
		fileref.setAttribute("href", cssFileName);
		
		// now add the div tags to the html page after we have the css loaded.
		
		document.getElementsByTagName("head")[0].appendChild(fileref)
		
	
}



function getInternetExplorerVersion()
// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
{
  var rv = 9; // Return value assumes failure.CR 53780
  if (navigator.appName == 'Microsoft Internet Explorer')
  {
    var ua = navigator.userAgent;
    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null)
      rv = parseFloat( RegExp.$1 );
  }
  return rv;
}

