///////////////////////////////////////////////////////////////////////
// Title: Tigra Tree
// Description: See the demo at url
// URL: http://www.softcomplex.com/products/tigra_menu_tree/
// Version: 1.1
// Date: 11-12-2002 (mm-dd-yyyy)
// Notes: This script is free. Visit official site for further details.
///////////////////////////////////////////////////////////////////////
/*
	Modifications to include the following 
	(Marc Richard: Feb 2006)
	- a target element in the array as some pages needed to be opened in the same window.
	- Expanding and Selecting appropriate menu items for the current page displayed.
	- removal of a querystring
	- removal of the Default.asp filename
	(Marc Richard: May 2007)
	- removal of the images before the menu item as per requested.
	- Divs instead of Tables

*/
function cleanURL(passedValue) {

	// remove the NAME element for the page
	if(passedValue.indexOf('#',passedValue) > 0) {
		passedValue = passedValue.slice(0,  passedValue.indexOf('#',passedValue))	// Remove the # reference
	} else {
		passedValue = passedValue
	}
	
	// remove the querystring as this will change in the same page.
	if(passedValue.indexOf('?',passedValue) > 0) {
		passedValue = passedValue.slice(0,  passedValue.indexOf('?',passedValue))	// Remove the ? reference
	} else {
		passedValue = passedValue
	}
	
	// Check for the Default.asp file and remove it from the string.
	if (passedValue.indexOf('default.aspx', passedValue) > 0) {
		passedValue = passedValue.slice(0, passedValue.indexOf('default.aspx', passedValue))	// Remove default.aspx filename
	}
	else if(passedValue.indexOf('default.asp',passedValue) > 0) {
		// Check for the Default.asp file and remove it from the string.
		passedValue = passedValue.slice(0, passedValue.indexOf('default.asp', passedValue))	// Remove default.asp filename
	}


	return passedValue;
}

function tree(a_items, ApplicationPath) {
	myString = new String(window.location) 			// Get Current Location
	myString = myString.toLowerCase();				// To Lower Case
	FindThis = myString.slice(0,myString.indexOf("/",8));// Holds the Domain Name
	newString = new String ("")            			// Repalce with this
	curURL = myString.replace(FindThis, newString) 	// Do the Replace
	curURL = cleanURL(curURL);

	// Single Source with many pages underneath.
	if(curURL.indexOf('/bulletins/',curURL) > 1) { curURL = '/cityhall/services/bulletins/'; }
	if(curURL.indexOf('/directory/',curURL) > 1) { curURL = '/business/directory/'; }
	if(curURL.indexOf('/cityproperties/',curURL) > 1) { curURL = '/cityhall/services/cityproperties/'; }
	if(curURL.indexOf('/otm/news_releases_details.asp',curURL) > 1) { curURL = '/cityhall/otm/news_releases.asp'; }
	if(curURL.indexOf('/department/planning/uplan_',curURL) > 1) { curURL = '/cityhall/department/planning/uplan.asp'; }
	if(curURL.indexOf('/economic_sectors/sendfiles.asp',curURL) > 1) { curURL = '/business/economic_sectors/document.asp'; }
	if(curURL.indexOf('/services/emailsubscription/',curURL) > 1) { curURL = '/cityhall/services/emailsubscription/'; }

	this.a_config   = a_items; 						// Menu Array
	this.o_root     = this;							// Internal Loop
	this.a_index    = [];							// empty's the array
	this.o_selected = null;							// clears the content of this string
	this.n_depth = -1; 						// clears the content of this integer

	if (ApplicationPath == undefined) {
		this.ApplicationPath = ''
	} else if (ApplicationPath == '/') {
		this.ApplicationPath = ''
	} else {
		this.ApplicationPath = ApplicationPath
	}

	// functions to show / hide / highlight and the mouse events
	this.toggle = function (n_id) {	var o_item = this.a_index[n_id]; o_item.open(o_item.b_opened) };
	this.select = function (n_id) { return this.a_index[n_id].select(); };
	this.mout   = function (n_id) { this.a_index[n_id].upstatus(true) };
	this.mover  = function (n_id) { this.a_index[n_id].upstatus() };

	this.a_children = [];
	for (var i = 0; i < a_items.length; i++) {
		new tree_item(this, i, this.ApplicationPath);
	}

	this.n_id = trees.length;
	trees[this.n_id] = this;

	for (var i = 0; i < this.a_children.length; i++) {
		document.write(this.a_children[i].init());
		//this.a_children[i].open();
	}

	for(x in this.a_index) { 
		for(y in this.a_index[x]) {
			for(z in this.a_index[x][y]) {
				if((y=='o_parent') && (z=='n_id')) {
					tmpURL = '';
					if (this.a_index[x].a_config[1] != null) { 
						tmpURL = cleanURL(this.a_index[x].a_config[1]);
					}
					if(curURL == tmpURL) {
						// Setup the String Variable to store all the n_id's
						var strExpand = "";
						
						// To expand the last node if there are any children
						if(this.a_index[x].a_children) { if (x > 0) { strExpand = x; } }

						// Fill array to be able to sort in reverse to expand the menu
						tmpExpand = this.a_index[x].o_parent.n_id;
						do {
							if(tmpExpand > 0) { strExpand = tmpExpand + ',' + strExpand; }
							tmpExpand = this.a_index[tmpExpand].o_parent.n_id;
						} while(tmpExpand > 0)

						// Split the String into an Array
						arrExpand = strExpand.split(",")

						// To show the selection of the current page
						for(i=0; i<arrExpand.length; i++) {
							if(arrExpand[i] != '') {
								this.toggle(arrExpand[i]);
								this.select(arrExpand[i]);
							} else {
								this.select(0);
							}
						}
					}
				}
			}
		}
	}
}

function tree_item(o_parent, n_order, ApplicationPath) {
	// Iterates through the entire tree to show the links to the pages.
	this.ApplicationPath = ApplicationPath;
	this.n_depth  = o_parent.n_depth + 1;
	this.a_config = o_parent.a_config[n_order + (this.n_depth ? 3 : 0)];
	if (!this.a_config) return;

	this.o_root    = o_parent.o_root; 	// is an object!
	this.o_parent  = o_parent;			// is an object!
	this.n_order   = n_order;			// display order
	// this.n_depth equals how deep the link is..... 0 = Root, 1 = First Child, 2 = Second Child... etc...
	//this.b_opened  = !this.n_depth;		// if this.n_depth = 0 then it expands it.

	this.n_id = this.o_root.a_index.length; // is the n_id of the item in the list.
	this.o_root.a_index[this.n_id] = this;
	o_parent.a_children[n_order] = this;

	this.a_children = [];
	for (var i = 0; i < this.a_config.length - 2; i++)
		new tree_item(this, i, ApplicationPath);

	//this.get_icon = item_get_icon;
	this.open     = item_open;
	this.select   = item_select;
	this.init     = item_init;
	this.upstatus = item_upstatus;
	this.is_last  = function () { return this.n_order == this.o_parent.a_children.length - 1 };
}

function item_open (b_close) {
	var o_idiv = get_element('i_div' + this.o_root.n_id + '_' + this.n_id);
	if (!o_idiv) return;

	if (!o_idiv.innerHTML) {
		var a_children = [];
		for (var i = 0; i < this.a_children.length; i++)
			a_children[i]= this.a_children[i].init();
		o_idiv.innerHTML = a_children.join('');
	}
	o_idiv.style.display = (b_close ? 'none' : 'block');
	
	if (b_close) { 
		this.b_opened;
	} else {
		!this.b_opened;
	}
	this.b_opened = b_close;
	this.upstatus();
}

function item_select (b_deselect) {
	if (!b_deselect) {
		var o_olditem = this.o_root.o_selected;
		this.o_root.o_selected = this;
		if (o_olditem) o_olditem.select(true);
	}
	get_element('i_txt' + this.o_root.n_id + '_' + this.n_id).style.fontWeight = b_deselect ? 'normal' : 'bold';
	//get_element('i_txt' + this.o_root.n_id + '_' + this.n_id).style.bgcolor = b_deselect ? '#000000' : '#ffffff';
	//get_element('i_txt' + this.o_root.n_id + '_' + this.n_id).style.color = b_deselect ? '#ffffff' : '#000000';
	
	this.upstatus();
	return Boolean(this.a_config[1]);
}

function item_upstatus (b_clear) {
	/* do display the link in the status bar.
		window.setTimeout('window.status="' + (b_clear ? '' : this.a_config[0] + (this.a_config[1] ? ' ('+ this.a_config[1] + ')' : '')) + '"', 10);   */
	// DO NOT display the link in the status bar.
	window.setTimeout('window.status="' + (b_clear ? '' : this.a_config[0]) + '"', 10);
}

function item_init () {
	var spacer = 14; // unit in px
	var step = 10; // unit in px
	var a_offset = [],
		o_current_item = this.o_parent;
	for (var i = this.n_depth; i > 1; i--) {
		a_offset[i] = spacer + step;
		o_current_item = o_current_item.o_parent;
	}
	
	switch (this.n_depth) { 
		case 0 : 
			var frColor="#004747"; 
			var bgColor="#BADDDD";
			break; 
		case 1 : 
			var frColor="#004747"; 
			var bgColor="#C7E4E4";
			break; 
		case 2 : 
			var frColor="#004747"; 
			var bgColor="#D5EAEA";
			break; 
		case 3 : 
			var frColor="#004747"; 
			var bgColor="#E4F1F1";
			break; 
		case 4 : 
			var frColor="#004747"; 
			var bgColor="#F1F8F8";
			break; 
		case 5 : 
			var frColor="#004747"; 
			var bgColor="#ffffff";
			break; 
	} 

	spacer = spacer + (this.n_depth * step);
	
	return '<div id="i_txt' + this.o_root.n_id + '_' + this.n_id + '" '
		+ 'style="color: ' + frColor + '; background-color:' + bgColor + '; border-bottom: 1px solid #FFF; width: ' + (200 - spacer) + 'px; padding: 4px 4px 3px ' + spacer + 'px; text-decoration: none;">'
		+ '<a href="' + this.ApplicationPath + this.a_config[1] + '" '
			+'target="' + this.a_config[2] + '" '
			+'onclick="trees[' + this.o_root.n_id + '].toggle(' + this.n_id + '); '
			+'return trees[' + this.o_root.n_id + '].select(' + this.n_id + ')" '
			+'ondblclick="trees[' + this.o_root.n_id + '].toggle(' + this.n_id + ')" '
			+'onmouseover="trees[' + this.o_root.n_id + '].mover(' + this.n_id + ')" '
			+'onmouseout="trees[' + this.o_root.n_id + '].mout(' + this.n_id + ')"'
			+'style="color: ' + frColor + '; background-color:' + bgColor + '; text-decoration: none;">'
			+ this.a_config[0] + '</a>   '  // Text Link  ' - ' + this.o_parent.n_id + ':' + this.n_id + 
			+'</div>'
			+ (this.a_children.length ? '<span id="i_div' + this.o_root.n_id + '_' + this.n_id + '"></span>' : ''); // with children : without children
}

var trees = [];
get_element = document.all ? function (s_id) { /* for IE browsers */return document.all[s_id] } : function (s_id) { return document.getElementById(s_id); };