/*
	This script lets you add Print and show all / hide all buttons to your webhelp toolbar.
	
	Many thanks to Adobe and Peter Grainge for creating the original print script and topic pane method.

	Author: Willam van Weelden - http://www.wvanweelden.eu/robohelp/scripts/toolbarscripts
	Version 20100111
	
	----- Setting up the script -----
	1. Add the script as a baggage file to your project.
	2. If you use twisties, other than the standard RoboHelp twisties, Set the following variable to 'false':*/
	
	var RHTwisty = true;//Set to false if you use an alternative twisty method

/*	3. In the skin editor, create a new button.
	4. On the Action tab, choose 'JavaScript' in the Action section.
	5. In the JavaScript section, choose 'External JavaScript' and link this script.
	6. In the OnClick field, add the function for the button you want. You can add the following functions:
	   - BookmarkTopic() -> Bookmark the current topic.
	   - PrintTopic() -> Expand dropdowns and expandspots and print the topic.
	   - PrintTopicNotExpand() -> Print the topic.
	   - Show() -> Expand all dropdowns and expandspots
	   - Hide() -> Hide all dropdowns and expandspots
	7. Save your skin.
	8. In the SSL dialog, choose the buttons you want to include in your output.
	9. Generate.
*/
function BookmarkTopic() {//Add bookmark for topic
	
	//Check for local webhelp
	var MasterUrl = GetMaster().location.toString();
	if(MasterUrl.match(/file:\/\/\//g)) {
		alert('Bookmarking does not work on locally installed webhelp.');
		return false;
	}
	
	var topicPane = GetTopicPane();
	var title = topicPane.document.title;
	var topicUrl = topicPane.location.toString();
	
	//Get the relative path from the project root to the topic
	var RelPath = _getRelativePath(MasterUrl, topicUrl).substring(_getRelativePath(topicUrl, MasterUrl).length);
	var TopicName = topicUrl.substring(topicUrl.lastIndexOf('/') + 1);
	var url = MasterUrl+'#'+RelPath+TopicName;//Complete path to the topic, with the skin.
	
	if (window.sidebar) // Firefox
		window.sidebar.addPanel(title, url, "");
	else if(window.opera && window.print){ // Opera
		var elem = document.createElement('a');
		elem.setAttribute('href',url);
		elem.setAttribute('title',title);
		elem.setAttribute('rel','sidebar');
		elem.click();
	}
	else if(document.all)// Ie
		window.external.AddFavorite(url, title);
	else//No Chrome support.
		alert('Your browser does not support the bookmark function. Please set your bookmark manually.');
}
function PrintTopic() {//Expand and print
	var topicPane = GetTopicPane();
	ShowHide('show');
	topicPane.print();
}
function PrintTopicNotExpand() {//Print only
	var topicPane = GetTopicPane();
	topicPane.print();
}
function Show() {//Show all dropdowns and expandspots
	ShowHide('show');
}
function Hide() {//Hide all dropdowns and expandspots
	ShowHide('hide');
}
function GetTopicPane() {//Get the topic pane
	var topicPane;
	if (top.frames[0].name == "ContentFrame")
		topicPane = top.frames[0].frames[1].frames[1];
	else
		topicPane = top.frames[1].frames[1];
	topicPane.focus();
	return topicPane;
}
function GetMaster() {
	if (top.frames[0].name == "ContentFrame")
		master = top.frames[0];
	else
		master = top;
	return master;
}
function ShowHide(action) {
	var topicPane = GetTopicPane();
	//Get divs, spans and links
	var elemDivs = topicPane.document.getElementsByTagName('div');
	var elemSpans = topicPane.document.getElementsByTagName('span');
	var elemLinks = topicPane.document.getElementsByTagName('a');
	
	//Show/hide all dropdowns
	for (i = 0; i < elemDivs.length; i++) {
		if (elemDivs[i].className == "droptext") {
			if(action == "show" && elemDivs[i].style.display == "none") {
				elemDivs[i].style.display = '';
			} else if (action == "hide" && elemDivs[i].style.display != 'none') {
				elemDivs[i].style.display = 'none';
			}
		}
	}
	
	//Show/hide all expandspots
	for (i = 0; i < elemSpans.length; i++) {
		if (elemSpans[i].className == "expandtext") {
			var inner = elemSpans[i].innerHTML;
			if(inner.indexOf("&nbsp;") != 0 && inner.indexOf(" ") != 0) {
				inner = "&nbsp;" + inner;
				elemSpans[i].innerHTML = inner;
			}
			
			if(action == "show") {
				elemSpans[i].style.display = "";
			} else if (action == "hide") {
				elemSpans[i].style.display = "none";
			}
		}
	}
	
	//Twisties
	if(RHTwisty == false)
		return false;
		
	for(i=0;i<elemLinks.length;i++) {
		if(elemLinks[i].childNodes[0]) {//Only if both RH twisties are available
			if(elemLinks[i].childNodes[0].className == "TwistyImage") {//This is a twisty
				if(action == "show") {
					elemLinks[i].childNodes[0].style.display = '';
					elemLinks[i].childNodes[1].style.display = 'none';
				} else if (action == "hide") {
					elemLinks[i].childNodes[0].style.display = 'none';
					elemLinks[i].childNodes[1].style.display = '';
				}
			}
		}
	}	
}
function replaceSubstring(inputString, fromString, toString) {
	// Goes through the inputString and replaces every occurrence of fromString with toString
	var temp = inputString;
 
	if (fromString == "")
		return inputString;
 
	if (toString.indexOf(fromString) == -1) { 
		while (temp.indexOf(fromString) != -1) {
			 var toTheLeft = temp.substring(0, temp.indexOf(fromString));
			 var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
			 temp = toTheLeft + toString + toTheRight;
		}
	}
	// Send the updated string back to the user
	return temp; 
}