﻿
// Show/hide a block of content
function ToggleShowHideContent(strContentID, strToggleID) {
	// Try and get the contents
	objContent = document.getElementById(strContentID);
	if ( objContent.className == 'hidden-content' ) {
		objContent.className = 'visible-content'
		document.getElementById(strToggleID).innerHTML = "&ndash;";
		SaveShowHideViewState(strContentID, strToggleID, '', 'visible-content');
	} else {
		objContent.className = 'hidden-content';
		document.getElementById(strToggleID).innerHTML = "+";
		SaveShowHideViewState(strContentID, strToggleID, '', 'hidden-content');
	}
}

// Show/hide a block of content in a bar
function ToggleShowHideBarContent(strContentID, strToggleID, strBarID) {
	// Try and get the contents
	objContent = document.getElementById(strContentID);
	if ( objContent.className == 'hidden-content' ) {
		objContent.className = 'visible-content'
		document.getElementById(strToggleID).innerHTML = "&ndash;";
		document.getElementById(strBarID).className = "EDC-step-by-step-bar-on";
		SaveShowHideViewState(strContentID, strToggleID, strBarID, 'visible-content');
	} else {
		objContent.className = 'hidden-content';
		document.getElementById(strToggleID).innerHTML = "+";
		document.getElementById(strBarID).className = "EDC-step-by-step-bar-off";
		SaveShowHideViewState(strContentID, strToggleID, strBarID, 'hidden-content');
	}
}

// Saves the status of an item into the hash
function SaveShowHideViewState(strContentID, strToggleID, strBarID, strDisplay) {
	// document.location.hash = xxx;
	
	// Get the current hash
	var strHash = GetShowHideHash();
	// Break the hash down
	var aPairs = ArrayFromShowHideHash(strHash);
	
	// See if this content id is already in the hash
	var bFound = false;
	strHash = "";
	for( var iPair=0;iPair<aPairs.length;iPair++) {
		// Try and split the pair
		var aPair = aPairs[iPair].split("=");
		if ( aPair.length == 2 ) {
			if ( aPair[0] == strContentID ) {
				aPair[1] = strToggleID + "~" + strBarID + "~" + strDisplay;
				bFound = true;
			}
			// Update the hash
			if ( strHash != "" ) {
				strHash += "&";
			}
			strHash += aPair[0] + "=" + aPair[1]; 
		}
	}
	// If we didn't find this id, then add it
	if ( !bFound ) {
		if ( strHash != "" ) {
			strHash += "&";
		}
		strHash += strContentID + "=" + strToggleID + "~" + strBarID + "~" + strDisplay;
	}	
	// Set the hash
	document.location.hash = strHash;
}

// Restore the status of an item from the hash
function RestoreShowHideViewState() {
	// See if the identifier is found in the hash
	var strHash = GetShowHideHash();
	
	// Try and split it by pairs
	var aPairs = strHash.split("&");
	for( var iPair=0;iPair<aPairs.length;iPair++) {
		// Try and split the pair
		var aPair = aPairs[iPair].split("=");
		if ( aPair.length == 2 ) {
			var aParam = aPair[1].split("~");
			if ( aParam.length == 3 ) {
				var strContentID = aPair[0];
				var strToggleID = aParam[0];
				var strBarID = aParam[1];
				var strDisplay = aParam[2];
				if ( strDisplay == "visible-content" ) {
					if ( strBarID == "" ) {
						ToggleShowHideContent(strContentID, strToggleID);
					} else {
						ToggleShowHideBarContent(strContentID, strToggleID, strBarID);
					}
				}
			}
		}
	}
}

// Convert the hash into an array of pairs
function ArrayFromShowHideHash(strHash) {
	// Try and split it by pairs
	return strHash.split("&");
}

// Get the current hash, if any
function GetShowHideHash() {
	// See if the identifier is found in the hash
	var strHash = String(document.location.hash);
	if ( strHash.charAt(0) == '#' ) {
		strHash = strHash.substring(1);
	}
	return strHash;
}