/*
hide.js
(c) 1992-2005 Cisco Systems, Inc. All rights reserved. 
Terms and Conditions: http://cisco.com/en/US/swassets/sw293/sitewide_important_notices.html
*/	

document.writeln('<style>');
document.writeln('.hide-target-hidden{display:none;}');
document.writeln('.hide-noscript{display:none;}');
document.writeln('</style>');

function HidePair(tri, tar) {
	this.trigger = tri;
	this.target = tar;

	this.getTrigger = function() { return this.trigger; }
	this.setTriggerClass = function(newclass) { this.trigger.className = newclass; }
	this.getTarget = function() { return this.target; }
	this.setTargetClass = function(newclass) { this.target.className = newclass; }
}

function HideGroup(name) {
	this.GroupName = name;
	this.Pairs = new Array();
	this.Active = new String();
	this.getName = function() { return this.GroupName; }
	this.setName = function(str) { this.GroupName = str; }
	this.getActive = function() { return this.Active; }
	this.setActive = function(str) { this.Active = str; }
	this.addPair = function(trigger, target) { 
		newp = new HidePair(trigger, target)
		this.Pairs.push( newp ); 
	}
	this.hideAll = function() {
		for (var i=0; i < this.Pairs.length; i++) {
			this.Pairs[i].setTriggerClass(cssTriggerRoot + '-' + this.GroupName);
			
			if (this.Pairs[i].getTarget() && (this.Pairs[i].getTarget() != '') ) {
				this.Pairs[i].setTargetClass( cssTargetHidden );
			} else {
				hide_debug("ERROR: Can't HIDE target for trigger: '" + this.Pairs[i].getTrigger().id + "' it doesn't exist.");
			}
		}
	}
}

function HideOptPair(token, value) {
	this.Token = token;
	this.Val = value;
	
	this.setValue = function(value) {
		this.Val = value;
	}
	this.getPair = function() {
		return this;	
	}
	this.getToken = function() { return this.Token }
	this.getValue = function() { return this.Val }
	this.toString = function() { 
		var s = new String( this.Token + ':' + this.Val );
		hide_debug("HideOptPair.toString(): " + s);
		return s;	
	}
}

function HideGroupOptions(name) {
	// for each display group we have an array of options
	this.GroupName = name;
	this.GroupOpts = new Array();
	
	this.getName = function() { return this.GroupName; }
	this.getOptions = function() { return this.GroupOpts; }
	
	this.setOption = function(token, value) {
		found = -1;
		for (i = 0; i < this.GroupOpts.length; i++) {
			if ( this.GroupOpts[i].getToken() == token ) {
				found = i;
				this.GroupOpts[i].setValue(value);
				break;
			}
		}
		if ( found < 0 ) {
			this.GroupOpts.push( new HideOptPair(token, value) );
		}
	}
	this.getOption = function(token) {
		value = null;
		for (i = 0; i < this.GroupOpts.length; i++) {
			if ( this.GroupOpts[i].getToken() == token ) {
				value = this.GroupOpts[i].getValue();
				break;
			}
		}
		return value;
	}
	this.toString = function() {
		var s = new String();
		for (i = 0; i < this.GroupOpts.length; i++) {
			s = s.concat(this.GroupName + "=>" + this.GroupOpts[i].toString() + "\n");
		}
		hide_debug("HideGroupOptions.toString(): " + s);
		return s;
	}
}
function hide_GetOptionsGroup(group) {
	// lookup the options group if it exists.
	// if it doesn't exist, create a new one, and add it to the array.
	var index = hide_LookupOptionsGroup(group);
	
	if (index < 0) {
		hide_Options.push( new HideGroupOptions(group) );
		outGroup = hide_Options[hide_Options.length-1];
	} else {
		outGroup = hide_Options[index];
	}
	return outGroup;
}

function hide_LookupOptionsGroup(group) {
	// find the correct group index, if it exists.
	// returns -1 on failure.
	var found = -1;
	for (var i = 0; i < hide_Options.length; i++) {
		if ( hide_Options[i].getName() == group) {
			found = i;
			break;
		}
	}
	return found;
}

function hide_GetOption(group, token) {
	// get the value for a given group, and token.
	var found = null;
	optindex = hide_LookupOptionsGroup(group);
	if (optindex >= 0) {
		found = hide_Options[optindex].getOption(token);
	}
	return found;
}

function hide_SetOption(group, token, value) {
	// set a token value pair, for a given hide group.
	hide_debug('HideSetOption(): group: ' + group + ' token: ' + token + ' value: ' + value);
	
	OptGroup = hide_GetOptionsGroup(group);
	OptGroup.setOption(token, value);	
}
function hide_ShowOptions() {
	// alert dump of set options
	var s = new String("hide.js options\n[Group Name]=>[token]:[value]\n");
	s = s.concat("--------------------\n");
	for (i = 0; i < hide_Options.length; i++) {
		hide_debug("hide_ShowOptions(): " + i );
		s = s.concat( hide_Options[i].toString() );
	}	
	alert (s);
}

// Trigger IDs
var triggerKey = new String ('hide-id-trigger-');
var targetKey = new String ('hide-id-target-');

// CSS Strings
var cssTriggerRoot = new String ('hide-trigger');
var cssTargetShown = new String ('hide-target-shown');
var cssTargetHidden = new String ('hide-target-hidden');

// Script Variables
var hide_Groups = new Array();
var hide_Options = new Array();

function hide_debug(s) {
	// lets us leave debugging in.
	try {
		debug(s);
	} catch (error) {
		// do nothing;
	}
}

function hide_ShowClassInfo() {
	var s = new String("hide.js css trigger classes\n[Group Name]\n[inactive]:[active]\n");

	s = s.concat("--------------------\n");

	for (var i = 0; i < hide_Groups.length; i++) {
		g = hide_Groups[i].getName();
		s = s.concat("Group: '" + g + "'\n" );
		s = s.concat(cssTriggerRoot + "-" + g + ":" + cssTriggerRoot + "-" + g + "-" + "active\n\n");
	}
	alert (s);
		
}


function hide_DoClick(argTrigger, history, hide_active) {
	var trigger = argTrigger;
	hide_debug("hide_DoClick(): trigger = '" + trigger.id + "', " + history + "," + hide_active);
	
	// remove selection border from browser.
	trigger.blur();

	// determine the trigger's group and identifier (not html id.)
	var temp_sGroup = hide_ParseGroup(trigger.id, triggerKey);
	var temp_sPairID = hide_ParsePairID(trigger.id, triggerKey);
	
	// retrieve the group this trigger is in.
	thisGroup = hide_GetGroup(temp_sGroup);
	hide_debug("  -> Corresponding target = '" + targetKey + temp_sGroup + "-" + temp_sPairID);
	
	// hide all target's in the triggers's group (including its own target)
	thisGroup.hideAll();
	
	// show the clicked trigger's target.
	// if the trigger is active already, and toggle is true we 
	// do nothing, so it will revert to closed state.
	if ( (thisGroup.getActive() != trigger.id) || !hide_active ) {
		target = document.getElementById(targetKey + temp_sGroup + "-" + temp_sPairID);
		if (target) {
			target.className = cssTargetShown;
		} else {
			hide_debug("  -> ERROR: Can't SHOW target for trigger: '" + trigger.id + "'");
		}
		trigger.className = cssTriggerRoot + '-' + thisGroup.getName() + '-active';
		thisGroup.setActive(trigger.id);
	} else if ( (thisGroup.getActive() == trigger.id) && hide_active) { 
		// leave the panel closed, but update the active, (and thus the history/init state.)
		thisGroup.setActive('');
	}
	
	if (history) {
		hide_debug("  -> update history");
		hide_setHash();
	}
	return false;
}

function hide_GetGroup(group) {
	var index = hide_LookupGroup(group);
	if (index < 0) {
		hide_Groups.push( new HideGroup(group) );
		outGroup = hide_Groups[hide_Groups.length-1];
	} else {
		outGroup = hide_Groups[index];
	}
	return outGroup;
}

function hide_LookupGroup(group) {
	var found;
	found = -1;
	for (var i = 0; i < hide_Groups.length; i++) {
		if ( hide_Groups[i].getName() == group) {
			found = i;
		}
	}
	return found;
}

function hide_InitActive() {
	hide_debug("hide_InitActive():");

	var htriggers = cdc.util.getFromHash()['hide_v3'];
	htriggers = new String(htriggers);
	hide_debug(" --> cdc.util.getFromHash(): '" + htriggers + "'");

	htriggers = htriggers.split("+");
	for (var i = 0; i < htriggers.length; i++) {
		hide_debug(" --> hash triggers[" + i + "] : '" + htriggers[i] + "'");
		if ( htriggers[i] != '' ) {
			if ( trigger = document.getElementById(htriggers[i]) ) {
				hide_DoClick(trigger, false, false);
			}	
		}
	}
}

function hide_setHash() {
	var newhash = new String();
	var active;
	for (var i = 0; i < hide_Groups.length; i++) {
		newhash = newhash.concat( "+" + hide_Groups[i].getActive() ); 
	}
	cdc.util.setToHash('hide_v3',newhash);
	hide_debug("  -> Set Active Trigger cdc.util.setToHash(): '" + newhash + "'");	
}

function hide_ParseGroup(id, key) {
	var info = id.substr(key.length, id.length);
	var out = info.split("-")[0];
	return out;
}

function hide_ParsePairID(id, key) {
	var info = id.substr(key.length, id.length);
	var out = info.split("-")[1];
	return out;
}

function hide_Init() {
	var nTriggers = 0;
	var nTriggerErrors = 0;
	var group;
	var index;
	
	hide_debug("hide_Init():");
	
	hide_debug("  -> Finding Triggers");
	// run through the anchors looking for id's like our keys
	var pageLinks = document.getElementsByTagName('a');
	var numlinks = pageLinks.length;
	hide_debug("  -> Links in page: " + numlinks + "<br />");
	for (var i = 0; i < numlinks; i++) {
		var trigger = pageLinks[i];
		var triggerID = trigger.id;
		
		if (triggerID.indexOf(triggerKey) != -1) {
			// found a trigger
			hide_debug("Trigger found, id: '" + triggerID + "'");
			nTriggers++;
			
			// parse out it's group
			gname = hide_ParseGroup(triggerID, triggerKey);
			
			// get the options for the group
			var ghist = hide_GetOption(gname, 'history');
			var ghideall = hide_GetOption(gname, 'allow_hideall');
			if (ghist == null) {
				ghist = true;
			}
			if (ghideall == null) {
				ghideall = true;
			}
			
			// set its onclick
			trigger.onclick = new Function("{ return hide_DoClick(this, " + ghist + ", " + ghideall + "); }");
			hide_debug("  -> onclick set: hide_DoClick(this, " + ghist + ", " + ghideall + ");");
			
			// parse out the index/identifer of the trigger
			// hide-id-trigger-[group]-[index]
			// index can be a string.
			index = hide_ParsePairID(triggerID, triggerKey);
			
			// retrieve the group from the list of groups.
			Group = hide_GetGroup(gname);
			hide_debug("  -> GetGroup returned: " + Group.getName());

			// build the corresponding target id string.
			targetID = targetKey + gname + '-' + index;
			
			// get the target out of the DOM
			target = document.getElementById( targetID );
			if (!target) {
				// oops
				hide_debug("  -> ERROR: Hideable Trigger without Target: id: '" + triggerID + "'");
				nTriggerErrors++;
				Group.addPair(trigger, null);
			} else {
				// add the trigger/target pair to the group list.
				csstriggeractive = cssTriggerRoot + '-' + gname + '-active'
				hide_debug("  -> target found: " + target.id);
				hide_debug("  -> trigger class: '" + trigger.className + "': looking for " + csstriggeractive );
				if (trigger.className == csstriggeractive) {
					target.className = cssTargetShown;
				} else {
					trigger.className = cssTriggerRoot + '-' + gname;
					target.className = cssTargetHidden;
				}
				hide_debug("  -> target classnmae set: classname=" + target.className);
				Group.addPair(trigger, target);
			}
			hide_debug("");
		}
	}
	
	
	
	hide_debug("Summary:");
	// build list of groups.
	s = '';
	for (i = 0; i < hide_Groups.length; i++) {
		s += "'" + hide_Groups[i].getName() + "', ";
	}
	hide_debug("Total Hideable Triggers: " + nTriggers + " / " + nTriggerErrors + " Error(s)");
	hide_debug("Total Hideable Groups: " + hide_Groups.length + ": " + s + "<br />");
	
	hide_InitActive();
		
	hide_debug("hide_Init(): Complete ---------------------------");
}
$(document).ready(hide_Init);




