var idUrl = window.location.toString();
var tier_limit = new Number(2);
var parentContentID = "";
var arrayDimensions = new Array();
var dimension = 0;
var position = "";
var n_id_count = 0;
var select_n_id = 0;
var myTree = null;

function parseUrl(url,param){
	if ((start = url.indexOf(param))!=-1){
		start = start + (param.length + 1);
		end = url.indexOf('&',start);
		if (end == -1){
			end = url.length;
		}
		param = url.substring(start,end);
	}
	else {
		param = null;
	}
	return param;
}

var searchContentID = parseUrl(idUrl,'contentID');
//alert("searchContentID "+searchContentID);
var o_root_n_id = parseUrl(idUrl,'o_root_n_id');

function searchArray(passedArray,searchString,position){
	for (curArrayPos=0;curArrayPos<passedArray.length;curArrayPos++){
		var t = typeof passedArray[curArrayPos];
		if (t=='object'){
			arrayDimensions[dimension] = curArrayPos;
			dimension++;
			new searchArray(passedArray[curArrayPos],searchString,position);
			dimension--;
			curArrayPos = arrayDimensions[dimension];

		}
		else {
			if (passedArray[curArrayPos].indexOf(searchString)!=-1){
				select_n_id = n_id_count;
				//alert("select_n_id "+select_n_id);
				position = '[' + curArrayPos + ']';
				for(a=dimension;a>0;a--){
					position = '[' + arrayDimensions[a-1] + ']' + position;
				}
				//quit if possible
			}
			if (curArrayPos==1){
				n_id_count++;
			}
		}
	}
}

function setURLParameter(url,paramNme,paramValue){

	if ((endCID = url.indexOf("/pdf_files"))!=-1){
		//endCID = startCID + (paramNme.length + 1);
		//endCID = url.indexOf('&',startCID);
		//if (endCID == -1){
		//	endCID = url.length;
		//}
		//alert(url.substring(0,endCID));
		url = url.substring(0,endCID+1) + paramValue;
		//alert(url);
	}
	else {

		url = url + paramValue;
		//alert(url);
	}

	return url;
}

function tree (a_items, a_template) {

//	alert ("o_root_n_id is "+o_root_n_id);
	if (a_items.length!=0){
		parentContentID = a_items[0][1];
		this.a_tpl      = a_template;
		this.a_config   = a_items;
		this.o_root     = this;
		this.a_index    = [];
		this.o_selected = null;
		this.n_depth    = -1;

		//variable added by ndd9246 for SR#D3412.011
		this.cookie = document.cookie;
		
		var o_icone = new Image(),
		o_iconl = new Image();
		o_icone.src = a_template['icon_e'];
		o_iconl.src = a_template['icon_l'];
		a_template['im_e'] = o_icone;
		a_template['im_l'] = o_iconl;
		for (var i = 0; i < 64; i++){
			if (a_template['icon_' + i]) {
				var o_icon = new Image();
				a_template['im_' + i] = o_icon;
				o_icon.src = a_template['icon_' + i];
			}
		}

		// code added by ndd9246 for SR#D3412.011
		/*
		 * Below code saves the tree state in a cookie and uses this cookie to main tree state whenever
		 * it loads tree again.
		 */
		this.setCurrState = function (label,persistence) {    
                          //this.cookie = document.cookie = "currState=" + persistence;
                          this.cookie = document.cookie = label + "=" + persistence  + '; path=/';
                       };

		this.getCurrState = function (label) {               
	                       //var label = "currState=";
	                       label = label + "=";
	                       var labelLen = label.length;
	                       var cLen = this.cookie.length;
	                       var i = 0;
                          while (i < cLen) {
                             var j = i + labelLen;
                             if (this.cookie.substring(i,j) == label) {
                                var cEnd = this.cookie.indexOf(";",j);
                                if (cEnd == -1) {
                                   cEnd = this.cookie.length;
                                }
                                return unescape(this.cookie.substring(j,cEnd));
                              }
                              i++
                           }
                           return ""
                        };
		// code end here
		
		this.toggle = function (origin,root_n_id,n_id,dest) {
			//alert("Entered in Toggle :: ");
			var o_item = this.a_index[n_id];
			//set contentID
			//idUrl = setURLParameter(idUrl,'',dest);
			//set parentContentID
			//idUrl = setURLParameter(idUrl,'',dest);
			//set reNav boolean
			//if (o_item.n_depth < tier_limit){
				//alert("if (o_item.n_depth < tier_limit)");
			idUrl = setURLParameter(idUrl,'',dest);

				/*
				if (n_id==0){
					idUrl = setURLParameter(idUrl,'',dest);
				}
				else {
					//idUrl = setURLParameter(idUrl,'reNav','false');
				}
				*/
			//}
			//else {
				//alert("else (o_item.n_depth < tier_limit)");
				//idUrl = setURLParameter(idUrl,'',dest);
			//}
			//set o_root_n_id
			//idUrl = setURLParameter(idUrl,'o_root_n_id',root_n_id);
			//alert("root_n_id "+root_n_id);

			if (origin!=1){//from item
				//window.location = idUrl;
				winPreview= window.open(idUrl,"Preview","status=1,scrollbars=1,resizable=1");
				winPreview.focus();
			}
			else {//from +/-
				//alert("o_item.n_depth == "+o_item.n_depth);
				//alert("tier_limit =="+tier_limit);
				//if (o_item.n_depth < tier_limit){
					o_item.open(o_item.b_opened);


				// code added by ndd9246 for SR#D3412.011
		/*
		 * Below code retrives the tree state from a cookie and uses it to retain the previous saved tree state whenever
		 * it loads tree again.
		 */

		var temp_cookie = this.getCurrState("currState");

                    // If open insert a flag into the cookie so it remebers
                    if (o_item.b_opened) {
                       temp_cookie = temp_cookie + ':' + o_item.n_id + ':';
                       this.setCurrState("currState",temp_cookie);
                    }
                    // If closed remove the flag in the cookie
                    else {
                       var removeId = new RegExp (":"+n_id+":","g");
                       temp_cookie = temp_cookie.replace(removeId,'');
                       this.setCurrState("currState",temp_cookie);
                    }
		// code ends here

				//}
				//else {
				//	window.location = idUrl;
				//}
			}
			//alert("IDURL :: "+idUrl.toString());

		

		};
		
	
		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() };

	

		// ABYSS: added a function for opening a specific node
		
		this.openCurLeaf = function() {
			var o_item = null;
			var n_id = null;
			//var locArray = location.href.split('/');
			var curFilename = searchContentID;
			//alert("curFilename "+curFilename);
			for(var i=1; i< this.a_index.length;i++){
				o_item = this.a_index[i]; 
				if(o_item.a_config[1] == curFilename){
					n_id = i;
					break;
				}
			}
	
			if(o_item.o_parent!=this){

			//alert("Setting cookie with n_id "+n_id);

				o_item.recursiveopen(o_item.b_opened)

				
				var temp_cookie = this.getCurrState("currState");

                // If open insert a flag into the cookie so it remebers
                    
                temp_cookie = temp_cookie + ':' + n_id + ':';
                this.setCurrState("currState",temp_cookie);
					


			}
			else{
				//alert("Setting cookie3");
				//this.toggle(n_id);
				this.toggle(1,0,n_id,null)

			}};
			
		//code ended here




		this.a_children = [];
		for (var i = 0; i < a_items.length; i++){
			new tree_item(this, i);
		}
	
		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();
		}

		
		/*
		if (idUrl.indexOf('reNav=false')!=-1){
			trees[o_root_n_id].select(select_n_id);
			trees[o_root_n_id].toggle(1,o_root_n_id,select_n_id,null);
		}
		
		else {
			//select the top item in the nav
			trees[0].select(0);
		}
		*/

	   // Here we are opening the folders which have a open flag in the cookie
	   var temp_cookie = this.o_root.getCurrState("currState");    
	   if (temp_cookie.length > 0) {
		  var re= new RegExp (":","g");
		  var cookie_arr = temp_cookie.split(re);

		  for (var x = 0; x < cookie_arr.length; x++) {
			 // var o_item = this.a_index[cookie_arr[cookie_arr.length-1]];
			 //modified by Nagsen
			 /*
			 var o_item = this.a_index[cookie_arr[x]];
			 o_item.open(o_item.b_opened);
			 */
			 if(cookie_arr[x]) {
				var o_item = this.a_index[cookie_arr[x]];
				o_item.open(o_item.b_opened);
				}	
			 //modification end here
		  }
	   }
   
   // Make the current url bold in the menu tree
	   var active_id = this.o_root.getCurrState("active_id");
	   if (active_id.length > 0) {
		  get_element('i_txt' + this.o_root.n_id + '_' + active_id).style.fontWeight = 'bold';
	   }

	}
}

function tree_item (o_parent, n_order) {
	this.n_depth  = o_parent.n_depth + 1;
	this.a_config = o_parent.a_config[n_order + (this.n_depth ? 2 : 0)];

	if (!this.a_config) return;

	this.o_root    = o_parent.o_root;
	this.o_parent  = o_parent;
	this.n_order   = n_order;
	this.b_opened  = !this.n_depth;

	this.n_id = this.o_root.a_index.length;
	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);
	}

	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 };

	// ABYSS
	this.recursiveopen = item_recopen;	
}

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');
	
	this.b_opened = !b_close;
	var o_jicon = document.images['j_img' + this.o_root.n_id + '_' + this.n_id],
		o_iicon = document.images['i_img' + this.o_root.n_id + '_' + this.n_id];
	if (o_jicon) o_jicon.src = this.get_icon(true);
	if (o_iicon) o_iicon.src = this.get_icon();
	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);
	}
	var o_iicon = document.images['i_img' + this.o_root.n_id + '_' + this.n_id];
	if (o_iicon) o_iicon.src = this.get_icon();
	//**
	this.o_root.setCurrState("active_id",this.n_id);

	get_element('i_txt' + this.o_root.n_id + '_' + this.n_id).style.fontWeight = b_deselect ? 'normal' : 'bold';
	
	this.upstatus();
	return Boolean(this.a_config[1]);
}

function item_upstatus (b_clear) {
	//window.setTimeout('window.status="' + (b_clear ? '' : this.a_config[0] + (this.a_config[1] ? ' ('+ this.a_config[1] + ')' : '')) + '"', 10);
}

function item_init () {
	var a_offset = [],
		o_current_item = this.o_parent;

	for (var i = this.n_depth; i > 1; i--) {
		a_offset[i] = '<img src="' + this.o_root.a_tpl[o_current_item.is_last() ? 'icon_e' : 'icon_l'] + '" border="0" align="absbottom">';
		o_current_item = o_current_item.o_parent;
	}
	//alert(this.a_config[1] + "   Length is =" + this.a_config[1].length);
	if(this.a_config[1].length > 0){
	return '<table cellpadding="0" cellspacing="0" border="0"><tr><td nowrap>' + (this.n_depth ? a_offset.join('') + (this.a_children.length
		? '<a href="javascript: trees[' + this.o_root.n_id + '].toggle(1,' + this.o_root.n_id + ',' + this.n_id + ',\'' + this.a_config[1] + '\')" onmouseover="trees[' + this.o_root.n_id + '].mover(' + this.n_id + ')" onmouseout="trees[' + this.o_root.n_id + '].mout(' + this.n_id + ')"><img src="' + this.get_icon(true) + '" border="0" align="absbottom" name="j_img' + this.o_root.n_id + '_' + this.n_id + '"></a>'
		: '<img src="' + this.get_icon(true) + '" border="0" align="absbottom">') : '') 
		+ '<a href="javascript: trees[' + this.o_root.n_id + '].toggle(2,' + this.o_root.n_id + ',' + this.n_id + ',\'' + this.a_config[1] + '\')" onmouseover="trees[' + this.o_root.n_id + '].mover(' + this.n_id + ')" onmouseout="trees[' + this.o_root.n_id + '].mout(' + this.n_id + ')" class="t' + this.o_root.n_id + 'i" id="i_txt' + this.o_root.n_id + '_' + this.n_id + '"><img src="' + this.get_icon() + '" border="0" align="absbottom" name="i_img' + this.o_root.n_id + '_' + this.n_id + '" class="t' + this.o_root.n_id + 'im">' + this.a_config[0] + '</a></td></tr></table>' + (this.a_children.length ? '<div id="i_div' + this.o_root.n_id + '_' + this.n_id + '" style="display:none"></div>' : '');
	} else {
	return '<table cellpadding="0" cellspacing="0" border="0"><tr><td nowrap>' + (this.n_depth ? a_offset.join('') + (this.a_children.length
		? '<a href="javascript: trees[' + this.o_root.n_id + '].toggle(1,' + this.o_root.n_id + ',' + this.n_id + ',\'' + this.a_config[1] + '\')" onmouseover="trees[' + this.o_root.n_id + '].mover(' + this.n_id + ')" onmouseout="trees[' + this.o_root.n_id + '].mout(' + this.n_id + ')"><img src="' + this.get_icon(true) + '" border="0" align="absbottom" name="j_img' + this.o_root.n_id + '_' + this.n_id + '"></a>'
		: '<img src="' + this.get_icon(true) + '" border="0" align="absbottom">') : '') 
		+ this.a_config[0] + '</td></tr></table>' + (this.a_children.length ? '<div id="i_div' + this.o_root.n_id + '_' + this.n_id + '" style="display:none"></div>' : '');
	}
}

function item_get_icon (b_junction) {
	return this.o_root.a_tpl['icon_' + ((this.n_depth ? 0 : 32) + (this.a_children.length ? 16 : 0) + (this.a_children.length && this.b_opened ? 8 : 0) + (!b_junction && this.o_root.o_selected == this ? 4 : 0) + (b_junction ? 2 : 0) + (b_junction && this.is_last() ? 1 : 0))];
}

var trees = [];
get_element = document.all ?
	function (s_id) { return document.all[s_id] } :
	function (s_id) { return document.getElementById(s_id) };

//searchArray(IDNAV,searchContentID,'');


function item_recopen(b_close){
	if(this.o_parent!=this.o_root){
		this.o_parent.recursiveopen(b_close);
	}
	this.open(b_close);
} 


function openItemByCaption (s_caption, o_tree) {
	// set to true when debugging the application
	var B_DEBUG = true;

	// exit if required parameter isn't specified
	if (!s_caption)
		return (B_DEBUG
			? alert("Required parameter to function openItemByCaption is missing")
			: false
		);

	// use first tree on the page if tree object isn't explicitly defined
	if (!o_tree)
		o_tree = (TREES[0]);
	if (!o_tree)
		return (B_DEBUG
			? alert("No Tigra Tree Menu PRO instances can be found on this page")
			: false
		);

    // find item with specified caption
    var a_item = o_tree.find_item(s_caption);
    for(var n=0; n < a_item.length; n++) {
	o_item=a_item[n];
	// collect info about all item's parents
	var n_id = o_item.n_id,
		n_depth = o_item.n_depth,
		a_index = o_item.o_root.a_index,
		a_parents = [o_item];

	while (n_depth) {
		if (a_index[n_id].n_depth < n_depth) {
			a_parents[a_parents.length] = a_index[n_id];
			n_depth--;
		}
		n_id--;
	}
		
	// open all parents starting from root
	for (var i = a_parents.length-1; i >= 0; i--)
	   // check if node or root
	   if (a_parents[i].n_state & 48)
	      a_parents[i].open();
	   else
	      if (B_DEBUG) 
	         alert("Item with caption '" + a_parents[i].a_config[0]+ 
	            "' is a leaf.\nHierarchy will be opened to its parent node only.")
    }
}

