if(typeof String.prototype.trim !== 'function') { 
  String.prototype.trim = function() { 
    return this.replace(/^\s+|\s+$/g, '');  
  } 
} 
var favourites_globals = function() {
	this.navPaneReplace = 'favorites/webhelp_favorites.htm';
	this.navPaneOriginal = 'whskin_pdhtml.htm';
	this.navPaneTOC = 1;
	this.navPaneIDX = 2;
	this.navPaneFTS = 3;
	this.navPaneGLO = 4;
	this.showTOC = 'favourites_showTOC';
	this.showIDX = 'favourites_showIDX';
	this.showFTS = 'favourites_showFTS';
	this.showGLO = 'favourites_showGLO';
	this.isfavourite = false;
	this.favourites = '';
	this.separator = "|?|";
	this.titleseparator = "|#|";
	this.favcookiename = 'wvw_favourites'+wvw_favbutton_helpers.getRoot();
	this.addButton = document.getElementById('wvw-button-fav-yes');
	this.deleteButton = document.getElementById('wvw-button-fav-no');
	this.noFavouritesFound = "You have not yet marked any topics as favorite.";
	this.setFavourite = "Remove topic from Favorites";
	this.unsetFavourite = "Add topic to Favorites";
}
var favourites_helper = function() {
	this.getTopicPane = function () {//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;
	}
	this.getNavigationPane = function () {		
		var topicPane = this.getTopicPane();
		return topicPane.parent.frames[0];
	}
	this.getNavigationPaneHolder = function() {
		var navpane = this.getNavigationPane();
		return navpane.frames[1];
	}
	this.getNavigationPaneWidth = function() {
		var topicPane = this.getTopicPane();
		var navPane = this.getNavigationPane();
		if(gbIE) {
			/*
			This method is for IE. IE doesn't correctly support clientWidth: it will allways return the body width even if a frame has a width of 0.
			Now we get the width of the frameset in the properties of the frameset.

			Note that this method only works for LTR help. If you use RTL help, you need to amend the colsetting compare to "*,0".
			*/

			if(topicPane.parent.document.getElementsByTagName("FRAMESET")[0].getAttribute) {
				var colsetting = topicPane.parent.document.getElementsByTagName("FRAMESET")[0].getAttribute("cols");
			} else {
				var colsetting = topicPane.parent.document.getElementsByTagName("FRAMESET")[0].cols;
			}
			
			var navpanewidth = colsetting.substring(0, colsetting.langth-2);

		} else {
			var navpanewidth = navPane.document.body.clientWidth;
		}
		return navpanewidth;
	}
	this.isNavigationPaneVisible = function() {//Is navigation pane visible?	
		var width = this.getNavigationPaneWidth();
		if(width == 0) {
			return false;
		} else {
			return true;
		}
	}
	this.getToolbarPane = function() {
		if (top.frames[0].name == "ContentFrame") {
			return top.frames[0].frames[0];
		} else {
			return top.frames[0];
		}
	}
	this.getRoot = function() {
		var myTop = top.location.toString();
		return myTop.substring(0, myTop.lastIndexOf("/")+1);
	}	
	this.getRelLink = function() {
		var file = document.location.toString().substring(this.getRoot().length);
	
		if(file.match(/\?/g)) {
			file = file.substring(0, file.lastIndexOf("?"));
		}
		if(file.match("#")) {
			file = file.substring(0, file.lastIndexOf("#"));
		}
		
		return file;
	}
}
var favbuttons_settings = function() {
	this.hasLocalStorage = function () {
		return typeof window.localStorage != 'undefined';
	}
	this.saveSetting = function (name,value) {	
		if(this.hasLocalStorage()) {//Save in local storage
			localStorage.setItem(name, value);
		} else {//Save in cookie - fallback method
			var date = new Date();
			var days = 365;
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
			document.cookie = name+"="+value+expires+"; path=/";
		}
	}
	this.getSetting = function (name) {
		if(this.hasLocalStorage()) {
			return localStorage.getItem(name);
		} else {
			var nameEQ = name + "=";
			var ca = document.cookie.split(';');
			for(var i=0;i < ca.length;i++) {
				var c = ca[i];
				while (c.charAt(0)==' ') c = c.substring(1,c.length);
				if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
			}
			return null;
		}
	}
	this.removeSetting = function (name) {
		if(this.hasLocalStorage()) {
			localStorage.removeItem(name);
		} else {
			this.saveSetting(name,"");
		}
	}
}

var wvw_favbutton_helpers = new favourites_helper();
var wvw_favbutton_variables = new favourites_globals();
var wvw_favbutton_settings = new favbuttons_settings();

/* Listeners */
if(window.addEventListener) {
	window.addEventListener("message", favourites_listener, false);
	document.addEventListener("DOMContentLoaded", setFavButtons, false);
} else if(window.attachEvent) {/* For IE */
	window.attachEvent("onmessage", function(e) {
		favourites_listener(e);
	});
	document.attachEvent("onreadystatechange", setFavButtons);
} else {
	//Only load on modern browsers.
	var bAdd = document.getElementById('wvw-button-fav-yes');
	var bDelete = document.getElementById('wvw-button-fav-no');
	if(bAdd !== null) {
		bAdd.style.display = 'none';
		bDelete.style.display = 'none';
	}
}
function favourites_listener(oMsg) {
	
	var g = new favourites_globals();
	if(oMsg.data == g.showTOC) {/* Show the TOC */
		resetPane(g.navPaneTOC);
	} else if(oMsg.data == g.showIDX) {/* Show the Index */
		resetPane(g.navPaneIDX);
	} else if(oMsg.data == g.showFTS) {/* Show the Search */
		resetPane(g.navPaneFTS);
	} else if(oMsg.data == g.showGLO) {/* Show the Glossary */
		resetPane(g.navPaneGLO);
	} else if(oMsg.data == "syncfavourites") {/* Sync the favourites list in the sidebar */
		document.location.reload();
	} else if(oMsg.data == "updateFavButtons") {/* Sync the favourites button in the topic */
		setFavButtons();
	}
}
function resetPane(paneToReset) {
	
	/* 	Set the pane to reset.
		This variable sets the CSH call variable to manipulate the loading of the sidebar.
		When loading the original file, the system checks this variable to determine the pane to load.
	*/
	top.gnCmd = paneToReset;
	
	var navPaneHolder = wvw_favbutton_helpers.getNavigationPaneHolder();/* Set the navigation pane back to the original file. */
	navPaneHolder.location = wvw_favbutton_variables.navPaneOriginal;
	
	/* Highlight the correct button */
	resetButtons(paneToReset);
}

function favShowNavPane() {/* Show pane */
	if(typeof(notify) == "function") {
		var pMsg = new whMessage(WH_MSG_SHOWPANE, null, null);
		notify(pMsg);
	} else {
		var pMsg = new whMessage(WH_MSG_SHOWPANE, this, 1, null);
		SendMessage(pMsg);
	}
}


function showFavoritesList() {
	var navPane = wvw_favbutton_helpers.getNavigationPane();
	var navPaneHolder = wvw_favbutton_helpers.getNavigationPaneHolder();
	
	resetButtons();
	favShowNavPane();
	
	/* Show the replaced loader */
	navPaneHolder.location = wvw_favbutton_variables.navPaneReplace;
	
}
function resetButtons(toolbar) {
/* Reset buttons in toolbar */
	try {
		/* Use default flip button function. */
		switch (toolbar) {
			case 1:
				var pane = 'toc';
				break;
			case 2:
				var pane = 'idx';
				break;
			case 2:
				var pane = 'fts';
				break;
			case 4:
				var pane = 'glo';
				break;
			default:
				var pane = 'favorites';
		}
		
		flipPaneButton(pane);
		
	} catch(e) {
		//Silent exception
	}
}
function setFavButtons() {
	
	/* Apply correct language string */
	var bAdd = document.getElementById('wvw-button-fav-yes');
	var bDelete = document.getElementById('wvw-button-fav-no');
	if(bAdd != null) {
		bAdd.setAttribute("alt", wvw_favbutton_variables.setFavourite);
		bAdd.setAttribute("title", wvw_favbutton_variables.setFavourite);
	}
	
	if(bDelete != null) {
		bDelete.setAttribute("alt", wvw_favbutton_variables.unsetFavourite);
		bDelete.setAttribute("title", wvw_favbutton_variables.unsetFavourite);
	}
	
	/* Check whether the page is a favourite */
	if(isFavourite(wvw_favbutton_helpers.getRelLink())) {
		wvw_favbutton_variables.isfavourite = true;
		checkSavedTitle();
	} else {
		wvw_favbutton_variables.isfavourite = false;
	}
	
	/* Set the buttons */
	setbuttons(wvw_favbutton_variables.isfavourite);
}
function checkSavedTitle() {
	var title = document.title;
	var savedtitle;
	
	var favs = getFavourites();
	for(var i = 0; i< favs.length; i++) {
		var favurl = favs[i].split(wvw_favbutton_variables.titleseparator)[0];
		if(favurl.toLowerCase() == document.location.toString().toLowerCase()) {
			savedtitle = favs[i].split(wvw_favbutton_variables.titleseparator)[1];
			break;
		}
	}
	
	if(title != savedtitle) {
		//Topic title has changed. Update topic title in cookie.
		removeFavourite(getFavString());
		setFavourite(getFavString());
	}
	
}
function setbuttons(fav) {
	
	if(wvw_favbutton_variables.addButton !== null) {
		if(!fav) {//Not a favourite
			wvw_favbutton_variables.addButton.style.display = 'none';
			wvw_favbutton_variables.deleteButton.style.display = 'inline';
		} else {//A favourite
			wvw_favbutton_variables.addButton.style.display = 'inline';
			wvw_favbutton_variables.deleteButton.style.display = 'none';
		}
	}
}
function setFavourites(favourites) {
	
	var favouritesstring = "";
	for(var i = 0; i<favourites.length;i++) {
		if(favourites[i].trim() != "") {
			favouritesstring+=separator+favourites[i];
		}
	}
	
	if(favouritesstring == "") {
		wvw_favbutton_settings.removeSetting(favcookiename);/* No favourites left. Remove cookie */
	} else {
		wvw_favbutton_settings.saveSetting(favcookiename, favouritesstring);
	}
}
function getFavString() {
	var title = document.title;
	return  decodeURIComponent((wvw_favbutton_helpers.getRelLink()+wvw_favbutton_variables.titleseparator+title + '').replace(/\+/g, '%20'));/* Decode because spaces are sometimes handled differently */
}
function toggleFavourite(elem) {
	
	if(wvw_favbutton_variables.isfavourite){
		wvw_favbutton_variables.isfavourite = false;
		removeFavourite(getFavString());
	} else {
		wvw_favbutton_variables.isfavourite = true;
		setFavourite(getFavString());
	}
	syncSidebar();/* Sync the sidebar */
	setbuttons(wvw_favbutton_variables.isfavourite);/* Show the correct button. */
}
function setFavourite(url) {
	var favs = getFavourites();
	favs.push(url);
	setFavourites(favs);
}
function removeFavourite(url) {
	var favs = getFavourites();
	for(var i = 0; i<favs.length; i++) {
		var favurl = favs[i].split(wvw_favbutton_variables.titleseparator)[0];
		var cururl = url.split(wvw_favbutton_variables.titleseparator)[0]
		if(favurl.toLowerCase() == cururl.toLowerCase()) {
			favs.splice(i, 1);
			break;
		}
	}
	setFavourites(favs);
}
function isFavourite(url) {
	var isFav = false;
	var favs = getFavourites();
	for(var i = 0; i< favs.length; i++) {
		
		var favurl = favs[i].split(wvw_favbutton_variables.titleseparator)[0];
		
		if(favurl.toLowerCase() == url.toLowerCase()) {
			isFav = true;
			break;
		}
	}
	return isFav;
}
function getFavourites() {
	var arraystring = wvw_favbutton_settings.getSetting(wvw_favbutton_variables.favcookiename);
	if(arraystring == null) {
		var empty = new Array();
		return empty;
	} else {
		return arraystring.split(wvw_favbutton_variables.separator);
	}
}
function setFavourites(favourites) {
	var favouritesstring = "";
	for(var i = 0; i<favourites.length;i++) {
		if(favourites[i].trim() != "") {
			favouritesstring+=wvw_favbutton_variables.separator+favourites[i];
		}
	}
	
	if(favouritesstring == "") {
		wvw_favbutton_settings.removeSetting(wvw_favbutton_variables.favcookiename);/* No favourites left. Remove cookie */
	} else {
		wvw_favbutton_settings.saveSetting(wvw_favbutton_variables.favcookiename, favouritesstring)
	}
}

/* Favourites list function */
function writeFavoritesList(elemid) {
	var favourites = wvw_favbutton_settings.getSetting(wvw_favbutton_variables.favcookiename);
	
	var sep = wvw_favbutton_variables.separator;
	var septitle = wvw_favbutton_variables.titleseparator;
	
	var newHTML = '';
	
	if(favourites == "" || favourites == null) {
		newHTML+='<p>'+wvw_favbutton_variables.noFavouritesFound+'</p>';
		
		/* newHTML+='<p>'+wvw_favbutton_variables.noFavouritesFound+' <a href="../what_s_new_in_this_release.html">What \'s this?</a></p>'; */
		
	} else {
		var favouritesArray = favourites.split(sep);
		var favouritesArrayTmp = new Array();
		
		//Sort the favourites alphabetically by title
		for(var i = 0; i<favouritesArray.length; i++) {
			if(favouritesArray[i] == "") {
				continue;
			}
			var url = favouritesArray[i].split(septitle)[0];
			var title = favouritesArray[i].split(septitle)[1];
			favouritesArrayTmp.push(title+septitle+url);
			
		}
		
		favouritesArrayTmp.sort();
		if(favouritesArrayTmp.lenght !== 0) {
			
			newHTML+='<table class="favoritesholder"><tbody>';
			for(var i = 0; i<favouritesArrayTmp.length; i++) {
				var url = favouritesArrayTmp[i].split(septitle)[1];
				var title = favouritesArrayTmp[i].split(septitle)[0];
				
				newHTML+='<tr class="favoritelistitem"><td class="favorite"><a href="javascript:OpenTopic(\''+wvw_favbutton_helpers.getRoot()+url+'\',\''+url+'\');" class="favorite">'+title+'</a></td><td class="removelink"><a href="javascript:removeFavouriteSidebar(\''+url+'\');" class="removelink">[X]</a></td></tr>';
			}
			
			newHTML+='</tbody></table>';
			
		} else {
			newHTML+='<p>'+wvw_favbutton_variables.noFavouritesFound+'</p>';
			
			/* newHTML+='<p>'+wvw_favbutton_variables.noFavouritesFound+' <a href="../what_s_new_in_this_release.html">What \'s this?</a></p>'; */
		}
		
	}
	
	var elem = document.getElementById(elemid);
	elem.innerHTML = newHTML;
	
}
function syncSidebar() {
	var sidebar = wvw_favbutton_helpers.getNavigationPaneHolder();
	if(sidebar.postMessage) {
		sidebar.postMessage('syncfavourites', '*');
	} else if(typeof(sidebar.favourites_listener) == "function") {
		sidebar.favourites_listener('syncfavourites');
	}
}
function removeFavouriteSidebar(url) {
	removeFavourite(url);/* Remove the favourite */
	syncSidebar();/* Sync the sidebar */
	
	/* Show the correct favicon in the topic */
	var topic = wvw_favbutton_helpers.getTopicPane();
	if(topic.postMessage) {
		topic.postMessage('updateFavButtons', '*');
	} else if(typeof(topic.setFavButtons) == "function") {
		topic.setFavButtons();
	}
	
}
function OpenTopic(fullurl, favurl) {
	var topic = wvw_favbutton_helpers.getTopicPane();
	topic.location = fullurl;
}