// ============================================================================================================
// IDS Webhelp scripts used in the content topics
// KdG Eluxis.com
// ============================================================================================================

/* *************************************** */
/* Initialize                              */
/* *************************************** */
function initialize() {
	try {
		//If not in iframe, show print button
		if (window.self == window.top) {
			var el = document.getElementById("cbuttonPrint");
			if (el) el.style.display = "block";
		}
	} catch (e) {
		console.log ("Initialize failure: "+e)
	}
//console.log(document.body.scrollHeight+"\n"+document.body.clientHeight+"\n"+document.body.offsetHeight +"\n"+window.innerHeight+"\n"+(document.body.offsetHeight > 1.3*window.innerHeight))

	//Highlight search terms 
	var searchString = localStorage.getItem("searchstring");//Note: only works when topic and wh_framework are in same domain (server configuration)
	if (searchString) {
		var stxt = searchString.toLowerCase();
		stxt= stxt.replace(/\s+or\s+|\s+and\s+/g,' ');//Remove all 'and' and 'or'
		var searchterms = stxt.split(' ');
		//console.log(searchterms.length+' searchterms: '+searchterms)
		MarkTerms("",searchterms,"","searchterm")
	}
}

// ============================================================================================================
// Content TOC
// ============================================================================================================
// Show the TOC popup from the Content-Buttton <span id="cbuttonToc"..> at the top of topics and maps
function toggleContentTOC(show) {
	var el = document.getElementById('contentTOC');	
	if (el.style.display=="block" || show!=true) {
		el.style.display="none";
	}else {
		el.style.display="block";
	}
}

// ============================================================================================================
// popup tooltip
// ============================================================================================================
// Show the tooltip with conditional-audience, topic-metainfo or defenitionlist-text.
function showtip(e,content) {
	if (hasClass(document.getElementById("tooltip"),"tooltip-show")) {
		hidetip()
		return;
	}
	else {
		var x = e.clientX + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
		var y = e.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop) + 5;
		var wx= (document.body.clientWidth);
		var left = true;
		if (x >wx/2) {//place tooltip left of cursor (and therefore limit the width)
			x=wx-x;
			left = false;
		}
		if (navigator.userAgent.indexOf("MSIE") != -1) {
			x -= 20; //For IE subtract margin, as specified in the css for the body element
		}
		var frame= '<div class="ttip" style=""><span>'+content+'</span></div>';
		elm=document.getElementById("tooltip");
		elm.innerHTML=frame;
		if (left) {elm.style.left=x+"px";elm.style.right="";}
		else {elm.style.left="";elm.style.right=x+"px";}
		elm.style.top=y+"px";
		toggleClass(elm,"tooltip-hidden", "tooltip-show");
	}
} 
				
function hidetip(){
	var elm=document.getElementById("tooltip");
	toggleClass(elm,"tooltip-show", "tooltip-hidden");
}

//Function to toggle figure size
function toggleImgSize(figid) {
	var figE=document.getElementById(figid);
	if ( hasClass(figE,'thumbnail') ) {
		removeClass(figE,'thumbnail');
	} else {
		addClass(figE,'thumbnail');
	}
}

// ============================================================================================================
// Class Helper functions
// ============================================================================================================
function hasClass(ele,cls) {
	if ( !ele ) return;
	return ele.className.match(new RegExp('(\\s|^)'+cls+'(\\s|$)'));
}
function toggleClass(ele,fromClas, toClas) {
	if ( !ele ) return;
	removeClass(ele,fromClas);
	addClass(ele,toClas);
}
function addClass(ele,cls) {
	if ( !ele ) return;
	if (!this.hasClass(ele,cls)) ele.className += " "+cls;
}
 
function removeClass(ele,cls) {
	if (hasClass(ele,cls)) {
		var reg = new RegExp('(\\s|^)'+cls);
		var c = ele.className;
		c = c.replace(reg,'');
		c=c.replace(/(^\s+)|\s+$/g,"");//strip spaces
		ele.className=c;
	}
}

function getElementsByClassName(classname, node) {
	if(!node) node = document.getElementsByTagName("body")[0];
	var a = [];
	var re = new RegExp('\\b' + classname + '\\b');
	var els = node.getElementsByTagName("*");
	for(var i=0,j=els.length; i<j; i++){
		if(re.test(els[i].className))a.push(els[i]);
	}
	return a;
}


