var messageStack = stdClass.extend({
	//constructor
	constructor: function(el, settings) {
		this.base();
		// initialize settings
		Object.extend(this.s, {
			timer: null,
			waitTime: 10000,
			effectTime: 5000,
			closeSelector: ".close"
			/* put extensions to collections here */
		});
		
		// initialize nodes
		Object.extend(this.n, {
			el: el,
			childEl: [],
			tempContainer: document.createElement('div')
			/* put extensions to nodes here */
		});
		
		// initialize collections
		Object.extend(this.c, {
			messageFunctions: {
				success: this.successMessage.bind(this),
				error: this.errorMessage.bind(this)
			}
			/* put extensions to collections here */
		});
		var eles = this.n.el.getElementsBySelector('ul');
		
		for(var x=0; x<eles.length; x++){
			var childEl = {
				el: eles[x],
				timer: null
			}
			this.startTimer(childEl);
			this._attachEvents(childEl);
		}
	},
	closeClicked: function(childEl){
		this.removeMessage(childEl);
	},
	addMessage: function(message, type){
		if(!this.n.el){
			return;
		}
		if(typeof(this.c.messageFunctions[type]) == "function"){
			var html = this.c.messageFunctions[type](message);
		}else{
			return;
		}
		
		this.n.tempContainer.innerHTML = html;
		var childEl = {
			el:this.n.tempContainer.firstChild,
			timer:null
		}
		this.n.el.appendChild(childEl.el);
		this._attachEvents(childEl);
		Element.show(this.n.el);
		if(window.Effect){
			Effect.BlindDown(childEl.el,{
				duration: (this.s.effectTime/1000),
				afterFinishInternal: function(effect) {
					effect.element.undoClipping();
					effect.element.style.height = '';
				}
			});
		}else{
			Element.show(childEl.el);
		}
		this.startTimer(childEl);
	},
	removeMessage: function(childEl) {
		this.endTimer(childEl);
		if(window.Effect){
			Effect.Fade(childEl.el, {
				duration: (this.s.effectTime/1000)
			});
			/*Effect.BlindUp(childEl.el,{
				duration: (this.s.effectTime/1000),
				afterFinishInternal: function(effect) {
					effect.element.undoClipping();
					effect.element.style.height = '';
					Element.hide(effect.element);
				}
			});*/
		}else{
			Element.hide(this.n.el);
		}
	},
	startTimer: function(childEl) {
		this.endTimer(childEl);
		this.s.timer = setTimeout(this.removeMessage.bind(this, childEl), this.s.waitTime);
	},
	endTimer: function(childEl) {
		if(childEl.timer != null){
			clearTimeout(childEl.timer);
			childEl.timer = null;
		}
	},
	successMessage: function(message){
		var html = '<ul class="success">';
		html += '<li class="title">Success</li>';
		html += '<li>' + message + '</li>';
		html += '</ul>';
		return html;
	},
	errorMessage: function(message){
		var html = '<ul class="error">';
		html += '<li class="title">Error</li>';
		html += '<li>' + message + '</li>';
		html += '<li class="close">Close me</li>';
		html += '</ul>';
		return html;
	},
	_attachEvents: function(childEl) {
		var eles = document.getElementsBySelector(this.s.closeSelector, (childEl.el||this.n.el));
		for(var x=0; x<eles.length; x++){
			this.eObserve(eles[x], 'click', this.closeClicked.bind(this, childEl));
		}
	}
});
Behaviour.register({
	'#messagestack' : function(el){
		new messageStack (el);
	}
});/**
 * Category Tree
 * @author 	chris
 * @date	September 6, 2007
 * @notes	This is difference from aTree where the php mod 
 * 			that is using this should not be generating the 
 *			entire category tree, just one layer at a time.
 *			as a request to see the next layer is needed 
 *			js will AJAJ the response and save for later use
 *			Also you can command to use any rules onCheck or on Uncheck
 */ 

var cTree = stdClass.extend({
	//constructor
	constructor: function(el, s) {
		this.base();
		// initialize settings
		Object.extend(this.s, {
			moduleName: null,
			noChildrenClass: 'noChildren',
			openClass: 'open',
			closeClass: 'closed',
			ajaj: true,
			json: {
				ver: '0.1',
				meta: {},
				data: {
					requests: []
				}
			},
			debug: false
		});
		
		// initialize nodes
		Object.extend(this.n, {
			rootNode: el,
			usedNode: document.getElementById('used'),
			checkPrimeNode: document.getElementById('ruleCheckPrime'),
			uncheckPrimeNode: document.getElementById('ruleUncheckPrime'),
			checkSecondNode: document.getElementById('ruleCheckSecond'),
			uncheckSecondNode: document.getElementById('ruleUncheckSecond'),
			checkRadioNode: document.getElementById('ruleCheckRadio'),
			attemptSelected: document.getElementById('attemptSelected'),
			expandAll: document.getElementById('treeexpandall'),
			collapseAll: document.getElementById('treecollapseall')
		});
		
		// initialize collections
		Object.extend(this.c, {
			requests: []
		});
		
		for (var key in s) {
			this.s[key] = s[key];
		}
		this.allIsLoaded = false;
		this.loadAll = Array();
		this.loadedParents = [];
		this.s.moduleName = this.findModuleName(el);
		//lets listen
		this._findToggles(this.n.rootNode);
		this._findPrimeNodes(this.n.rootNode);
		this._findSecondNodes(this.n.rootNode);
		if(this.n.expandAll != null)
		{
			this.eObserve(this.n.expandAll, 'click', this.getAll.bind(this));
		}
		if(this.n.collapseAll != null)
		{
		}
		//we need the modulename
	},
	
	/*
	 * this finds the name of the module of the form so we know where to ajaj to
	 * @param 	DOM 	*el
	 * @return 	string
	 */
	findModuleName: function(el)
	{
		if(el.tagName.toLowerCase() == 'body')
		{
			return null;
		}
		else if(el.tagName.toLowerCase() == 'form')
		{
			return Form.getInputs(el, 'hidden', '__module')[0].value;
		}
		else
		{
			return this.findModuleName(el.parentNode);
		}
	},
	
	//----------------------------------------------
	//	BEGIN: Rules methods
	
	//all of these methods require li nodes
	/*
	 * This checks the node where the class is 'available' or 'implicit'
	 * @param 	DOM 	*el
	 * @return 	void
	 */
	checkPrime: function(el)
	{
		if(el.tagName.toLowerCase() != 'li')
		{
			return;
		}
		el.getElementsByTagName('input')[0].checked = true;
	},
	
	/*
	 * This checks the all the parents
	 * @param 	DOM 	*el
	 * @return 	void
	 */
	checkPrimeParents: function(el)
	{
		if(el.tagName.toLowerCase() != 'li')
		{
			return;
		}
		if(el.parentNode && el.parentNode.parentNode && el.parentNode.parentNode.tagName.toLowerCase() == 'li')
		{
			var parentLiNode = el.parentNode.parentNode;
			if(typeof(parentLiNode.getElementsByTagName('input')[0]) != 'undefined')
			{
				parentLiNode.getElementsByTagName('input')[0].checked = true;
				this.checkPrimeParents(parentLiNode);
			}
		}
	},
	
	/*
	 * This checks the children where the class is 'available' or 'implicit'
	 * @param 	DOM 	*el
	 * @return 	void
	 */
	checkPrimeChildren: function(el)
	{
		if(el.tagName.toLowerCase() != 'li')
		{
			return;
		}
		var childNodes = el.getElementsByTagName('input');
		for(var i = 1; i < childNodes.length; i ++)
		{
			if(Element.hasClassName(childNodes[i], 'implicit') || Element.hasClassName(childNodes[i], 'available'))
			{
				childNodes[i].checked = true;
			}
		}
	},
	
	/*
	 * This checks the node where the class is 'forced' or 'explicit'
	 * @param 	DOM 	*el
	 * @return 	void
	 */
	checkSecond: function(el)
	{
		if(el.tagName.toLowerCase() != 'li')
		{
			return;
		}
		el.getElementsByTagName('input')[1].checked = true;
	},
	
	/*
	 * This checks parents where the class is 'forced' or 'explicit'
	 * @param 	DOM 	*el
	 * @return 	void
	 */
	checkSecondParents: function(el)
	{
		if(el.tagName.toLowerCase() != 'li')
		{
			return;
		}
		if(el.parentNode && el.parentNode.parentNode && el.parentNode.parentNode.tagName.toLowerCase() == 'li')
		{
			var parentLiNode = el.parentNode.parentNode;
			if(typeof(parentLiNode.getElementsByTagName('input')[1]) != 'undefined')
			{
				parentLiNode.getElementsByTagName('input')[1].checked = true;
				this.checkSecondParents(parentLiNode);
			}
		}
	},
	
	/*
	 * This checks the children node where the class is 'forced' or 'explicit'
	 * @param 	DOM 	*el
	 * @return 	void
	 */
	checkSecondChildren: function(el)
	{
		if(el.tagName.toLowerCase() != 'li')
		{
			return;
		}
		var childNodes = el.getElementsByTagName('input');
		for(var i = 3; i < childNodes.length; i ++)
		{
			if(Element.hasClassName(childNodes[i], 'explicit') || Element.hasClassName(childNodes[i], 'forced'))
			{
				childNodes[i].checked = true;
			}
		}
	},
	
	/*
	 * This unchecks the node where the class is 'available' or 'implicit'
	 * @param 	DOM 	*el
	 * @return 	void
	 */
	uncheckPrime: function(el)
	{
		if(el.tagName.toLowerCase() != 'li')
		{
			return;
		}
		el.getElementsByTagName('input')[0].checked = false;
	},
	
	/*
	 * This unchecks the parent nodes where the class is 'available' or 'implicit'
	 * @param 	DOM 	*el
	 * @return 	void
	 */
	uncheckPrimeParents: function(el)
	{
		if(el.tagName.toLowerCase() != 'li')
		{
			return;
		}
		if(el.parentNode && el.parentNode.parentNode && el.parentNode.parentNode.tagName.toLowerCase() == 'li')
		{
			var parentLiNode = el.parentNode.parentNode;
			if(typeof(parentLiNode.getElementsByTagName('input')[0]) != 'undefined')
			{
				parentLiNode.getElementsByTagName('input')[0].checked = false;
				this.uncheckPrimeParents(parentLiNode);
			}
		}
	},
	
	/*
	 * This unchecks the children nodes where the class is 'available' or 'implicit'
	 * @param 	DOM 	*el
	 * @return 	void
	 */
	uncheckPrimeChildren: function(el)
	{
		if(el.tagName.toLowerCase() != 'li')
		{
			return;
		}
		var childNodes = el.getElementsByTagName('input');
		for(var i = 1; i < childNodes.length; i ++)
		{
			if(Element.hasClassName(childNodes[i], 'implicit') || Element.hasClassName(childNodes[i], 'available'))
			{
				childNodes[i].checked = false;
			}
		}
	},
	
	/*
	 * This unchecks the node where the class is 'forced' or 'explicit'
	 * @param 	DOM 	*el
	 * @return 	void
	 */
	uncheckSecond: function(el)
	{
		if(el.tagName.toLowerCase() != 'li')
		{
			return;
		}
		el.getElementsByTagName('input')[1].checked = false;
	},
	
	/*
	 * This unchecks the parent nodes where the class is 'forced' or 'explicit'
	 * @param 	DOM 	*el
	 * @return 	void
	 */
	uncheckSecondParents: function(el)
	{
		if(el.tagName.toLowerCase() != 'li')
		{
			return;
		}
		if(el.parentNode && el.parentNode.parentNode && el.parentNode.parentNode.tagName.toLowerCase() == 'li')
		{
			var parentLiNode = el.parentNode.parentNode;
			if(typeof(parentLiNode.getElementsByTagName('input')[1]) != 'undefined')
			{
				parentLiNode.getElementsByTagName('input')[1].checked = false;
				this.uncheckSecondParents(parentLiNode);
			}
		}
	},
	
	/*
	 * This unchecks the children nodes where the class is 'forced' or 'explicit'
	 * @param 	DOM 	*el
	 * @return 	void
	 */
	uncheckSecondChildren: function(el)
	{
		if(el.tagName.toLowerCase() != 'li')
		{
			return;
		}
		var childNodes = el.getElementsByTagName('input');
		for(var i = 3; i < childNodes.length; i ++)
		{
			if(Element.hasClassName(childNodes[i], 'explicit') || Element.hasClassName(childNodes[i], 'forced'))
			{
				childNodes[i].checked = false;
			}
		}
	},
	
	/*
	 * This enables the node where the class is 'available' or 'implicit'
	 * @param 	DOM 	*el
	 * @return 	void
	 */
	enablePrime: function(el)
	{
		if(el.tagName.toLowerCase() != 'li')
		{
			return;
		}
		el.getElementsByTagName('input')[0].disabled = false;
	},
	
	/*
	 * This enables the children nodes where the class is 'available' or 'implicit'
	 * @param 	DOM 	*el
	 * @return 	void
	 */
	enablePrimeChildren: function(el)
	{
		if(el.tagName.toLowerCase() != 'li')
		{
			return;
		}
		var childNodes = el.getElementsByTagName('input');
		for(var i = 1; i < childNodes.length; i ++)
		{
			if(Element.hasClassName(childNodes[i], 'implicit') || Element.hasClassName(childNodes[i], 'available'))
			{
				childNodes[i].disabled = false;
			}
		}
	},
	
	/*
	 * This enables the sibling nodes where the class is 'available' or 'implicit'
	 * @param 	DOM 	*el
	 * @return 	void
	 */
	disablePrimeSiblings: function(el)
	{
		if(el.tagName.toLowerCase() != 'li')
		{
			return;
		}
		//lets get the id for the one we do not want to disable
		var primeId = el.getElementsByTagName('input')[0].id;
		var siblingNodes = el.parentNode.getElementsByTagName('input');
		for(var i = 0; i < siblingNodes.length; i += 2)
		{
			if(siblingNodes[i].id != primeId)
			{
				siblingNodes[i].disabled = true;
			}
		}
	},
	
	/*
	 * This uncheckes the sibling nodes where the class is 'available' or 'implicit'
	 * @param 	DOM 	*el
	 * @return 	void
	 */
	uncheckPrimeSiblings: function(el)
	{
		if(el.tagName.toLowerCase() != 'li')
		{
			return;
		}
		//lets get the id for the one we do not want to uncheck
		var primeId = el.getElementsByTagName('input')[0].id;
		var siblingNodes = el.parentNode.getElementsByTagName('input');
		for(var i = 0; i < siblingNodes.length; i += 2)
		{
			if(siblingNodes[i].id != primeId)
			{
				siblingNodes[i].checked = false;
			}
		}
	},
	
	/*
	 * This does something special for radios
	 * @param 	DOM 	*el
	 * @return 	void
	 */
	disablePrimeForRadio: function(el)
	{
		if(el.tagName.toLowerCase() != 'li')
		{
			return;
		}
		//this will disable the children if the second is a radio
		var childNodes = el.getElementsByTagName('input');
		for(i = 2; i < childNodes.length; i += 2)
		{
			if(childNodes[3].type == 'radio')
			{
				childNodes[i].disabled = true;
				childNodes[i].checked = false;
			}
		}
	},
	
	/*
	 * This expands children nodes
	 * @param 	DOM 	*el
	 * @return 	void
	 */
	expandChildren: function(el)
	{
		if(el.tagName.toLowerCase() != 'li')
		{
			return;
		}
		if( el.id != null )
		{
			if(Element.hasClassName(el, 'closed'))
			{
				Element.addClassName(el, 'open');
				Element.removeClassName(el, 'closed');
				//we first need to see if the it has been loaded
				if(this._isLoaded(el.id) || typeof(el.getElementsByTagName('ul')[0]) != 'undefined')
				{
					Element.show(el.getElementsByTagName('ul')[0]);
				}
				else
				{
					this._getNewRequest(el.id);
				}
			}
		}
	},
	
	/*
	 * This execute rules set by hidden variables
	 * @param	DOM		*ruleNode	
	 * @param 	DOM 	*el
	 * @return 	void
	 */
	executeRules: function(ruleNode, el)
	{
		if(ruleNode == null || typeof(ruleNode) == 'undefined')
		{
			return;
		}
		if(this.s.debug)
			shout('Executing rules:' + ruleNode.value);
		var rules = ruleNode.value.split(' ');
		for(var i = 0; i < rules.length; i++)
		{
			if(rules[i] == 'checkPrime')
			{
				if(this.s.debug)
					shout('checkingPrime');
				this.checkPrime(el);
			}
			else if(rules[i] == 'checkPrimeParents')
			{
				if(this.s.debug)
					shout('checkingPrimeParents');
				this.checkPrimeParents(el);
			}
			else if(rules[i] == 'checkPrimeChildren')
			{
				if(this.s.debug)
					shout('checkingPrimeChilren');
				this.checkPrimeChildren(el);
			}
			else if(rules[i] == 'checkSecond')
			{
				if(this.s.debug)
					shout('checkingSecond');
				this.checkSecond(el);
			}
			else if(rules[i] == 'checkSecondParents')
			{
				if(this.s.debug)
					shout('checkingSecondParents');
				this.checkSecondParents(el);
			}
			else if(rules[i] == 'checkSecondChildren')
			{
				if(this.s.debug)
					shout('checkingSecondChildren');
				this.checkSecondChildren(el);
			}
			else if(rules[i] == 'uncheckPrime')
			{
				if(this.s.debug)
					shout('uncheckingPrime');
				this.uncheckPrime(el);
			}
			else if(rules[i] == 'uncheckPrimeParents')
			{
				if(this.s.debug)
					shout('uncheckingPrimeParents');
				this.uncheckPrimeParents(el);
			}
			else if(rules[i] == 'uncheckPrimeChildren')
			{
				if(this.s.debug)
					shout('uncheckingPrimeChildren');
				this.uncheckPrimeChildren(el);
			}
			else if(rules[i] == 'uncheckSecond')
			{
				if(this.s.debug)
					shout('uncheckingSecond');
				this.uncheckSecond(el);
			}
			else if(rules[i] == 'uncheckSecondParents')
			{
				if(this.s.debug)
					shout('uncheckingSecondParents');
				this.uncheckSecondParents(el);
			}
			else if(rules[i] == 'uncheckSecondChildren')
			{
				if(this.s.debug)
					shout('uncheckingSecondChildren');
				this.uncheckSecondChildren(el);
			}
			else if(rules[i] == 'enablePrime')
			{
				if(this.s.debug)
					shout('enablingPrime');
				this.enablePrime(el);
			}
			else if(rules[i] == 'disablePrimeSiblings')
			{
				if(this.s.debug)
					shout('disablingPrimeSiblings');
				this.disablePrimeSiblings(el);
			}
			else if(rules[i] == 'uncheckPrimeSiblings')
			{
				if(this.s.debug)
					shout('uncheckingPrimeSiblings');
				this.uncheckPrimeSiblings(el);
			}
			else if(rules[i] == 'disablePrimeForRadio')
			{
				if(this.s.debug)
					shout('disablingPrimeForRadio');
				this.disablePrimeForRadio(el);
			}
			else if(rules[i] == 'enablePrimeChildren')
			{
				if(this.s.debug)
					shout('enablingPrimeChildren');
				this.enablePrimeChildren(el);
			}
			else if(rules[i] == 'expandChildren')
			{
				if(this.s.debug)
					shout('expandChildren');
				this.expandChildren(el);
			}
		}
	},
	
	//----------------------------------------------
	//	BEGIN: Radio methods
	/*
	 * Respond to 'explicit' or 'forced' click
	 * @param 	DOM 	*el
	 * @param	EVT		*e
	 * @return 	void
	 */
	radioSecondOnClick: function(el, e)
	{
		var parentNode = el.parentNode.parentNode;
		if(el.checked == true)
		{
			this.executeRules(this.n.checkRadioNode, parentNode);
		}
	},
	
	/*
	 * Find and Listen to 'forced' or 'explicit'
	 * @param 	DOM 	*el
	 * @return 	void
	 */
	_findRadioNodes: function(el) {
		var inputNodes = el.getElementsByTagName('input');
		for(var i=0; i < inputNodes.length; i++)
		{
			if(Element.hasClassName(inputNodes[i], 'forced') || Element.hasClassName(inputNodes[i], 'explicit'))
			{
				if(inputNodes[i].type == 'radio')
				{
					if(this.n.usedNode.value == "")
					{
						this.n.usedNode.value = inputNodes[i].value;
					}
					else if(this.n.usedNode.value.indexOf(inputNodes[i].value) == -1)
					{		
						this.n.usedNode.value +=  ' ' + inputNodes[i].value;
					}
					this.eObserve(inputNodes[i], 'click', this.radioSecondOnClick.bind(this, inputNodes[i]));
				}
			}
		} 
	},
	
	//----------------------------------------------
	//	BEGIN: Second methods
	/*
	 * Respond to 'explicit' or 'forced' click
	 * @param 	DOM 	*el
	 * @param	EVT		*e
	 * @return 	void
	 */
	checkboxSecondOnClick: function(el, e)
	{
		var parentNode = el.parentNode.parentNode;
		if(el.checked == true)
		{
			this.executeRules(this.n.checkSecondNode, parentNode);
		}
		else
		{
			this.executeRules(this.n.uncheckSecondNode, parentNode);
		}
	},
	
	/*
	 * Find and Listen to 'forced' or 'explicit'
	 * @param 	DOM 	*el
	 * @return 	void
	 */
	_findSecondNodes: function(el) {
		var inputNodes = el.getElementsByTagName('input');
		for(var i=0; i < inputNodes.length; i++)
		{
			if(Element.hasClassName(inputNodes[i], 'forced') || Element.hasClassName(inputNodes[i], 'explicit'))
			{
				if(this.n.usedNode.value == "")
				{
					this.n.usedNode.value = inputNodes[i].value;
				}
				else if(this.n.usedNode.value.indexOf(inputNodes[i].value) == -1)
				{		
					this.n.usedNode.value +=  ' ' + inputNodes[i].value;
				}
				this.eObserve(inputNodes[i], 'click', this.checkboxSecondOnClick.bind(this, inputNodes[i]));
			}
		} 
	},
	
	//----------------------------------------------
	//	BEGIN: Prime methods
	/*
	 * Respond to 'implicit' or 'available' click
	 * @param 	DOM 	*el
	 * @param	EVT		*e
	 * @return 	void
	 */
	checkboxPrimeOnClick: function(el, e)
	{
		var parentNode = el.parentNode.parentNode;
		if(el.checked == true)
		{
			this.executeRules(this.n.checkPrimeNode, parentNode);
		}
		else
		{
			this.executeRules(this.n.uncheckPrimeNode, parentNode);
		}
	},
	
	/*
	 * Find and Listen to 'available' or 'implicit'
	 * @param 	DOM 	*el
	 * @return 	void
	 */
	_findPrimeNodes: function(el) {
		var inputNodes = el.getElementsByTagName('input');
		for(var i=0; i < inputNodes.length; i++)
		{
			if(Element.hasClassName(inputNodes[i], 'available') || Element.hasClassName(inputNodes[i], 'implicit'))
			{
				if(this.n.usedNode.value == "")
				{
					this.n.usedNode.value = inputNodes[i].value;
				}
				else if(this.n.usedNode.value.indexOf(inputNodes[i].value) == -1)
				{		
					this.n.usedNode.value +=  ' ' + inputNodes[i].value;
				}
				this.eObserve(inputNodes[i], 'click', this.checkboxPrimeOnClick.bind(this, inputNodes[i]));
			}
		} 
	},
	
	//----------------------------------------------
	//	BEGIN: Toggling methods
	/**
	 * This opens/closes up a level of the tree if no level 
	 * is buffed, we will AJAJ for new information
	 * @param	dom		*el
	 * @param	event	*e
	 * @return	void
	 */ 
	toggleOnClick: function(el, e) {
		if(String(Event.element(e).tagName).toLowerCase() == 'input')
		{
			return;
		}
		if(String(Event.element(e).tagName).toLowerCase() == 'label')
		{
			return;
		}
		if((Event.element(e).className == 'registerNavigator_wrapper') || (Event.element(e).parentNode.className == 'registerNavigator_wrapper'))
		{
			return;
		}
		if(this.s.ajaj)
		{
			Event.stop(e);
			if( el.id != null )
			{
				if(Element.hasClassName(el, 'closed'))
				{
					Element.addClassName(el, 'open');
					Element.removeClassName(el, 'closed');
					//we first need to see if the it has been loaded
					if(this._isLoaded(el.id) || typeof(el.getElementsByTagName('ul')[0]) != 'undefined')
					{
						Element.show(el.getElementsByTagName('ul')[0]);
					}
					else
					{
						this._getNewRequest(el.id);
					}
				}
				else if(Element.hasClassName(el, 'open'))
				{
					Element.addClassName(el, 'closed');
					Element.removeClassName(el, 'open');
					Element.hide(el.getElementsByTagName('ul')[0]);
				}
			}
		}
		else
		{
			if(String(Event.element(e).tagName).toLowerCase() != 'li')
			{
				return;
			}
			Event.stop(e);
			if(Element.hasClassName(el, 'closed'))
			{
				Element.addClassName(el, 'open');
				Element.removeClassName(el, 'closed');
				Element.show(el.getElementsByTagName('ul')[0])
			}
			else if(Element.hasClassName(el, 'open'))
			{
				Element.addClassName(el, 'closed');
				Element.removeClassName(el, 'open');
				Element.hide(el.getElementsByTagName('ul')[0]);
			}
		}
	},
	
	/**
	 * @param	void
	 * @return	void
	 */ 
	JsonOut: function(id) {
		id = id ? document.getElementById(id) : this.n.rootNode;
		
		if(Effect.Throb){
			Effect.Throb(id);
			var position = this.getPosition(id);
			Effect.throbbers[Effect.throbbers.length-1].throbber.style.left = position[0]+'px';
			Effect.throbbers[Effect.throbbers.length-1].throbber.style.top = position[1]+'px';
		}
		//construct the post
		this.s.json.data.requests = this.c.requests;
		
		var data = '__json=' + this.s.moduleName + '&data=' + Object.toJSON(this.s.json);
		this.c.requests = [];
		
		//send out the request
		var myAjax = new Ajax.Request(window.location,
		{
			method: 'post',
			parameters: data,
			onSuccess: this.JsonIn.bind(this, id)
		});
	},
	 
	/**  
	 * We need to remember we requested for this info before 
	 * then we need to search for stuff to listen to
	 * @param	string	*t
	 * @return	void
	 */
	JsonIn: function(id, t) { 
		//if response is not how I like it
		if(t.responseText.indexOf('{') != 0)
		{	
			/*var divNode = document.createElement('div');
			divNode.style.position = 'absolute';
			divNode.style.top = '130px';
			divNode.style.left = '0px';
			divNode.style.zIndex = '9999';
			divNode.style.background = 'black';
			divNode.innerHTML = t.responseText;
			document.body.appendChild(divNode);*/
			if(Effect.CancelThrob){
				 //Effect.CancelThrob(this.n.rootNode);
			}
		}
		var result = t.responseText.evalJSON();
		
        if(Effect.CancelThrob){
	         Effect.CancelThrob(id);
         }
		(result.responses.length).times(function(i) {
			
			if(result.responses[i].type == 'getChildrenFields'){
				//lets record what id we loaded.
				this.loadedParents.push(result.responses[i].data.id);
				// Convert the HTML we get back to a DOM Node //
				var newData = result.responses[i].data.html.toDOMNodes()[0];
				//we need to refind the element
				var liNode = document.getElementById(result.responses[i].data.id);
				liNode.appendChild(newData);
				//lastly we need to find the expandables
				this._findToggles(newData);
				this._findPrimeNodes(newData);
				this._findSecondNodes(newData);
				this._findRadioNodes(newData);
				this._uncheckLoadedChildren(liNode);
			}
			if(result.responses[i].type == 'getAll'){
				var newData = result.responses[i].data.html;
				this.allIsLoaded = true;
				Element.replace(this.n.rootNode, newData);
				this._findToggles(newData);
				this._findPrimeNodes(newData);
				this._findSecondNodes(newData);
				this._findRadioNodes(newData);
				this._uncheckLoadedChildren(liNode);
			}
		}.bind(this));
	},
	
	/**
	 * if the parent is unchecked then these as well should be unclicked
	 * @param	dom		*el
	 * @return	void
	 */ 
	_uncheckLoadedChildren: function(el) {
		if(Element.hasClassName(this.n.rootNode,'nocheckJs'))
		{
			return;
		}
		var inputNodes = el.getElementsByTagName('input');
		if(inputNodes[0].checked == false)
		{
			for(var i=1; i < inputNodes.length; i++)
			{
				if(inputNodes[i].disabled == false)
				{
					inputNodes[i].checked = false;
				}
			}
		}
		/*if(inputNodes[1].checked == false)
		{
			for(var i=1; i < inputNodes.length; i+=2)
			{
				inputNodes[i].checked = false;
			}
		}*/
	},
	
	/**
	 * This finds and listens to li nodes marked with class 'closed'
	 * @param	dom		*el
	 * @return	void
	 */
	_findToggles: function(el) {
		toggles = el.getElementsByTagName('li');
		for(var i=0; i < toggles.length; i++)
		{
			if(Element.hasClassName(toggles[i]), 'closed')
			{
				this.eObserve(toggles[i], 'click', this.toggleOnClick.bind(this, toggles[i]));
			}
		}
	},
	
	getAll: function() {
		//By clicking Expand All all of your current changes will be lost! Click Cancel to keep your work.
		this._addRequest('getAll', {});
		this.JsonOut();
	},
	/**
	 * This sends it out to the CSP
	 * @param	int		*id
	 * @return	void
	 */
	_getNewRequest: function(id) {
		var params = { elId: id }
		if(this.n.attemptSelected != null)
		{
			params.attemptSelected = this.n.attemptSelected.value;
		}
		this._addRequest('getChildrenFields', params);
		this.JsonOut(id);
	},
	
	/**
	 * This forms the request
	 * @param	string		*requestName
	 * @param	obj			*dataObject
	 * @return	void
	 */
	_addRequest: function(requestName, dataObject) {
		this.c.requests.push({
			id: this.c.requests.length,
			type: requestName,
			data: dataObject
		});
	},
	
	/**
	 * checks to see if a request for a level has been requested already
	 * @param	int		*id
	 * @return	bool
	 */
	_isLoaded: function(id) {
		for(var i = 0; i < this.loadedParents.length; i ++)
		{
			//if we found it in our buff
			if(id == this.loadedParents[i])
			{
				return true;
			}
		}
		return false;
	},
	//	END: Toggling methods
	//----------------------------------------------
	/*
	 * Gets the position of the element
	 */
	getPosition: function(el) {
		if(el.style.top && el.style.left)
		{
			return [el.style.top, el.style.left];
		}
		var curleft = 0, curtop = 0;
		if (el.offsetParent) {
			do {
				curleft += el.offsetLeft;
				curtop += el.offsetTop;
			} while (el = el.offsetParent);
			return [curleft,curtop];
		}
	}
});	

EventSelectors.register({
	'ul.cTree': function(el, index) {
		new cTree(el);
	},
	'ul.cTreeFront': function(el, index) {
		new cTree(el, {ajaj: false});
	}
}, true);
/**
 * Category Tree
 * @author 	chris
 * @date	September 6, 2007
 * @notes	This is difference from aTree where the php mod 
 * 			that is using this should not be generating the 
 *			entire category tree, just one layer at a time.
 *			as a request to see the next layer is needed 
 *			js will AJAJ the response and save for later use
 *			Also you can command to use any rules onCheck or on Uncheck
 */ 

var selected = stdClass.extend({
	//constructor
	constructor: function(el, s) {
		this.base();
		// initialize settings
		Object.extend(this.s, {
			noChildrenClass: 'noChildren',
			openClass: 'open',
			closeClass: 'closed'
		});
		// initialize nodes
		Object.extend(this.n, {
			rootNode: el,
			h2Nodes: el.getElementsByTagName('h2'),
			//rootLiNodes: this.n.rootNode.select('li.root')
			//rootLiNodes: Element.select(this.n.rootNode, 'li.root')
		});
		
		this.n.rootLiNodes = this.n.rootNode.select('li.root');
		
		
		for (var key in s) {
			this.s[key] = s[key];
		}
		//this.createControls();
		this.listenToCheckbox(this.n.rootNode);
		this.listenToRootLiNodes();
		//we need the modulename
	},
	
	updateControl: function(el, e)
	{		
			var look = true;
			var parent = el;
			while(look)
			{
				parent = parent.parentNode;
				if(parent.getElementsByTagName('h2').length == 1)
				{
					look = false;
				}	
			}
			var basecount = parent.getElementsByTagName('span')[0].innerHTML.split(' ')[0];

			basecount = parseInt(basecount);
			if(el.checked == true)
			{
				basecount ++;
			}
			else
			{
				basecount --;
			}
			if(isNaN(basecount) || basecount < 0)
			{
				basecount = 0;
			}
			parent.getElementsByTagName('span')[0].innerHTML = basecount+' Selected';
	},
	
	listenToRootLiNodes: function()
	{
		for(var j = 0; j < this.n.rootLiNodes.length; j++)
		{
			this.eObserve(this.n.rootLiNodes[j], 'click', this.changeOnClick.bind(this, this.n.rootLiNodes[j]));
		}
	},
	
	changeOnClick: function(el, e)
	{
		
		return;
		for(var j = 0; j < this.n.rootLiNodes.length; j++)
		{
			if(el.id != this.n.rootLiNodes[j].id)
			{
				Element.addClassName(this.n.rootLiNodes[j], 'closed');
				Element.removeClassName(this.n.rootLiNodes[j], 'open');
				Element.hide(this.n.rootLiNodes[j].getElementsByTagName('ul')[0]);
			}	
		}
	},
	
	listenToCheckbox: function(el)
	{
		var inputs = el.getElementsByTagName('input');
		for(var j = 0; j < inputs.length; j++)
		{
			
			this.eObserve(inputs[j], 'click', this.updateControl.bind(this, inputs[j]));
		}
	}
	
});	

EventSelectors.register({
	'ul.nocheckJs': function(el, index) {
		new selected(el);
	}
}, true);
EventSelectors.register({
	'input#submitMessage': function(el, index) {
		Element.observe(Form.getInputs(el.parentNode.parentNode.parentNode.parentNode, 'image')[0], 'click', function() {
			alert(el.value);
		});
	}
}, true);var clearAll = stdClass.extend({
	//constructor
	constructor: function(el) {
		this.base();
		
		// initialize nodes
		Object.extend(this.n, {
			el: el,
			events: [],
			links: []
			/* put extensions to nodes here */
		});
	
		Event.observe(('clearAll'), 'click', function() { 
			
			//reset all checkboxes on the form treeviewform1
			var e = document.treeviewform1;
			
			var length = e.length;
			try
			{
				for (i = 0; i < length; i++) 
				{
					e[i].checked = false;
				}
			}
			catch(err_code){}
			
			//set all the counts to 0
			var e = $$('li.root');
			var length = e.length
			try
			{
				for (i = 0; i < length; i++) // loops through each of the main li's
				{
					el = e[i].select('.control');
					elem = el[0];
					elem.innerHTML = "0 selected";
				}
			}
			catch(err_code){ }
			return "cleared";
		});
		
	}
});

var expandAll = stdClass.extend({
	//constructor
	constructor: function(el) {
		this.base();
		
		// initialize nodes
		Object.extend(this.n, {
			el: el,
			events: [],
			links: []
			/* put extensions to nodes here */
		});

		Event.observe('expandAll', 'click', function() {
			var e = document.getElementsByTagName('li');
			var length = e.length;
			try
			{
				for (i = 0; i < length; i++) 
				{
					if(Element.hasClassName(e[i], 'closed'))
					{
						Element.addClassName(e[i], 'open');
						Element.removeClassName(e[i], 'closed');
						Element.show(e[i].getElementsByTagName('ul')[0])
					}
				}
			}
			catch(err_code){ }
			return "expand";
		});
		
	}
});

var collapseAll = stdClass.extend({
	//constructor
	constructor: function(el) {
		this.base();
		
		// initialize nodes
		Object.extend(this.n, {
			el: el,
			events: [],
			links: []
			/* put extensions to nodes here */
		});
	
		Event.observe('collapseAll', 'click', function() { 
			var e = document.getElementsByTagName('li');
			var length = e.length;
			try
			{
				for (i = 0; i < length; i++) 
				{
					if(Element.hasClassName(e[i], 'open'))
					{
						Element.addClassName(e[i], 'closed');
						Element.removeClassName(e[i], 'open');
						Element.hide(e[i].getElementsByTagName('ul')[0])
					}
				}
			}
			catch(err_code){ }
			
			return "collapsed";
		});
		
	}
});
	
EventSelectors.register({
	'#clearAll' : function(el) {
		new clearAll(el);
	},
	'#expandAll' : function(el) {
		new expandAll(el);
	},
	'#collapseAll' : function(el) {
		new collapseAll(el);
	}	
}, true);




