// Change Log
/********************************************************************************
	WHEN:			WHO:	WHAT:	
	03/01/08	mlr		Created.
	04/29/08	mlr		Added code for Expand/Collapse.
	
********************************************************************************/

	//******************************
	//	START Helper Functions
	
	function openWindow(linkUrl, winName, winWidth, winHeight, winScroll, winResize, mbar, tbar){
	
		var adjTop = (screen.height - winHeight) / 2;
		var adjLeft = (screen.width - winWidth) / 2;
		
		if (winScroll == "y") {var scroll_choice = "yes";}
		else {var scroll_choice = "no";	}
		
		if (winResize == "y") {var resize_choice = "yes";}
		else {var resize_choice = "no";}

		if (mbar == null) {mbar = 'no'};  //if menubar not specified in call, set to 'no'
		if (tbar == null) {tbar = 'no'};	//if toolbar not specified in call, set to 'no'

		var var_options = "width=" + winWidth + ",height=" + winHeight +            
		",scrollbars=" + scroll_choice + ",resizable=" + resize_choice + ",left=" + adjLeft + ",top=" + adjTop + ",menubar="+mbar+",status=no,toolbar="+tbar+",location=no,directories=no,history=no";
	
		var newWin=window.open(linkUrl, winName, var_options); 
		newWin.focus();
	}
	
	function showNAMsg()  {
		alert('Not available at this time.');
	}
	
	//************************************************************
	//This function grabs an element and stores it in an array
	//Can input one element or multiple separated by a ","
	//Shortcut to document.getElementById('element');
	//************************************************************
	function $() {
		var elements = new Array();
		for (var i = 0; i < arguments.length; i++) {
			var element = arguments[i];
			if (typeof element == 'string')
				element = document.getElementById(element);
			if (arguments.length == 1)
				return element;
			elements.push(element);
		}
		return elements;
	}

	//********************************************************************************
	//Simon Wilson's addLoadEvent
	//	Allows an event to be added to the window object onload using:
	//		addLoadEvent(function() {
	//			MM_preloadImages('../../images/ico_print_over.gif'); 
	//			setRowHighlights(); 
	//			etc.
	//		});
	//********************************************************************************
	function addLoadEvent(func) {
		var oldonload = window.onload;
		if (typeof window.onload != 'function') {
			window.onload = func;
		} 
		else {
			window.onload = function() {
				if (oldonload) {
				oldonload();
				}
				func();
			}
		}
	}
	//	END Helper Functions
	//******************************
	
	
	//******************************
	//	START Class Handling

	//From prototype.js   Needed for Class.create statements
	var Class = {
		create: function() {
			return function() {
				this.initialize.apply(this, arguments);
			}
		}
	}

	//***************************************************************************************************
	//Like getElementsByTagName - but className
	//Warning, will apply to partial matches due to the className.search
	//Uses search so that it will work for multiple class declarations, i.e. class="name name2 name3" 
	//The original used a RegExp which didn't seem to work
	//Node and tag arguments are optional, just pass null to them if they're not needed.
	//***************************************************************************************************
	function getElementsByClass(searchClass,node,tag) {
		var classElements = new Array();
		if ( node == null ){
			node = document;
		}
		if ( tag == null ){
			tag = '*';
		}
		var els = node.getElementsByTagName(tag);
		var elsLen = els.length;
		//var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
		for (i = 0, j = 0; i < elsLen; i++) {
			if (els[i].className.search(searchClass) != -1) {
				classElements[j] = els[i];
				j++;
			}
		}
		return classElements;
	}
	
	//Adds any class to an element - without overwriting existing ones, except the newly added class
	function add_class (el, StrClassToAdd ) {
		var el = $(el);
		if(el.nodeType == 1){
			remove_class(el, StrClassToAdd);  //don't want to add duplicates
			var StrClassToAdd = ' '+StrClassToAdd;
			el.className += StrClassToAdd;
		}
	}
	//Removes any class from an element
	function remove_class (el, StrClassToRemove) {
		var el = $(el);
		if(el.nodeType == 1){
			var RE = new RegExp(StrClassToRemove);
			el.className = el.className.replace(RE,'');
		}
	}
	//Returns true if the element has the str_class in it's class attribute.
	function has_class(el, str_class){
		var el = $(el);
		if(el.nodeType == 1){
			if(el.className.search(str_class) != -1) return true;
			else return false;
		}
		return false;
	}
	//	END Class Handling
	//******************************
	
	
	//******************************
	//	START Expand/Collapse


	/*
	Each collapsable section on a page is called/defined with:
	var params = {
		allButton : 'tab_2ToggleAll', 
		startId : 'tab_2', 
		iconPath : '../../../../site_files_versioned/ILP_R5/site_files/images/ILP/'  <<< probably don't need with buttons
	};
	tab2Links = new MULE.expanderGroup(params);
	*/
	
	//Ensure MULE object exists for naming below.
	if (typeof(MULE)=="undefined") {
		MULE = {}	
	}

	//Use to make a Expand/Collapse All link with dependant sections
	MULE.expanderGroup = Class.create();
	MULE.expanderGroup.prototype = {
		initialize : function(params){
//alert('_init');
//console.log(params.startId);  //only good in FireFox


			this._init(params);
		},
		toggleAll : function(e){
//alert('toggleAll');

			var Len = this.pairs.length;
//alert('allButton.id = '+this.allButton.id );
// NOTE:  For whatever reason, the prototype.js version (commented out) of the class handling functions does not work in IE. 
//			if (this.allButton.hasClassName('expanded')){
			if (has_class('expand_all_button','expanded')){
//alert('has expanded');
				for(var i=0; i<Len; i++){
					this.pairs[i].collapse.call(this.pairs[i]);
				}
//				this.allButton.removeClassName('expanded');
				remove_class('expand_all_button','expanded');
				this.allButton.value = 'Expand All';
//				this.allButton.innerHTML = this.expandTpl;
			} else {
//alert('no expanded');
				for(var i=0; i<Len; i++){
					this.pairs[i].expand.call(this.pairs[i]);
				}
//				this.allButton.addClassName('expanded');
				add_class('expand_all_button','expanded');
				this.allButton.value = 'Collapse All';
//				this.allButton.innerHTML = this.collapseTpl;

			}

//alert('Exiting toggleAll');

			Event.stop(e);
		},
		buttons : null,	//after init this will be template

//		collapseTpl : null,	//after init these two will be templates
//		expandTpl : null,	
		_init : function(params){
			this.allButton = $(params.allButton);
			this.pairs = [];
//alert($(params.startId).id);
			//NOTE: None of the following 3 statements worked in IE but did in FireFox.  Had to go with $$() from prototype.js 
			//this.buttons = $(params.startId).getElementsByClass('toggle');
			//this.buttons = $(params.startId).getElementsByClassName('toggle');
			//this.buttons = getElementsByClass('toggle');

			this.buttons = $$('td.toggle');
//alert(params.startId + ' after');
//			this.links = $(params.startId).getElementsByClassName('toggle');
//			this.iconPath = params.iconPath;
//			this.collapseTpl = '<img src="' + this.iconPath + 'btn_minus.gif" alt="" width="13" height="13" /><span>Collapse All</span>';
//			this.expandTpl = '<img src="' + this.iconPath + 'btn_plus.gif" alt="" width="13" height="13" /><span>Expand All</span>';
//			this.collapseTpl = '<input type="button" value="Collapse" class="exp_col_button" >ccc ALL';
//			this.expandTpl = '<input type="button" value="Expand" class="exp_col_button" >eee ALL';
			var Len = this.buttons.length;
//alert('Len ' + Len);
//			var Len = this.links.length;
			for(var i=0; i<Len; i++){
				var abutton = this.buttons[i];

//alert('i = ' + i + '  abutton ' + abutton);
				var ttar = abutton.up('tr.expander_heading').next('tr');
//				var ttar = abutton.up('tr.heading_2_history').next('tr');
				this.pairs.push(new MULE.expander({el : abutton, target : ttar}));
//				var alink = this.links[i];
//				var ttar = alink.up('div.header_line').next('div.box_content');
//				this.pairs.push(new MULE.expander({el : alink, target : ttar, iconPath : this.iconPath}));
			}
			Event.observe(this.allButton, 'click', this.toggleAll.bindAsEventListener(this));
		}
	}
	
	//use to make a single open close link
	MULE.expander = Class.create();
	MULE.expander.prototype = {
		initialize : function(params){
			//params = {el : abutton, target : ttar}
			// params = {el : alink, target : ttar, iconPath : this.iconPath}
			this._init(params);	
		},
		_init : function(params){
			this.el = $(params.el);
			this.target = $(params.target);
//			this.collapseTpl = '<input type="button" value="Collapse" class="exp_col_button" >';
//			this.expandTpl = '<input type="button" value="Expand" class="exp_col_button" >';
//			this.collapseTpl = '<img src="' + params.iconPath + 'btn_minus.gif" alt="" width="13" height="13" /><span>Collapse</span>';
//			this.expandTpl = '<img src="' + params.iconPath + 'btn_plus.gif" alt="" width="13" height="13" /><span>Expand</span>';
			Event.observe(this.el, 'click', this.toggle.bindAsEventListener(this)); 
		},
		toggle : function(e){
//alert('toggle, this.el.value = ' + this.el.value);
			if(this.el.hasClassName('expanded')){
				this.collapse();
			} else {
				this.expand();
			}
			Event.stop(e);
		},
		expand : function(){
//			this.el.value = 'Collapse';
//alert('exp, this.el.value = ' + this.el.value + ' id = ' + this.el.id);
			//Since this is at the 'td' level, we can use .down to reference something below that level in the DOM (i.e. the input element).
			this.el.down('input').value = 'Collapse';
//			this.el.innerHTML = this.collapseTpl;
			this.el.addClassName('expanded');
			this.target.show();
		},
		collapse : function(el, target){
//alert('col, this.el.value = ' + this.el.value + ' id = ' + this.el.id);
//			this.el.value = 'Expand';
			this.el.down('input').value = 'Expand';
//			this.el.innerHTML = this.expandTpl;
			this.el.removeClassName('expanded');
			this.target.hide();
		}
	}
	//	END Expand/Collapse
	//******************************
