

function arrQueryStr(arr, str)
{
	for ( a in arr )
	{
		if ( arr[a] == str )
		{
			return parseInt(a);
			break;
		}
	}
	return -1;
}

String.prototype.trim = function() 
{
	return this.replace(/^\s+|\s+$/g,"");
}

function navGroup()
{
	this.regObjs = new Array();
	this.regChildren = new Array();
	this.arrParents = new Array();
	this.noGrowers = new Array();
	this.noGrowersState = new Array();

	this.register = function(obj)
	{
		obj.isArray = function()
		{
			if (this.constructor.toString().indexOf("Array") == -1)
			  return false;
		   else
			  return true;
		};
		
		if (typeof obj == "object" && obj.tagName == undefined && obj.isArray() == false )
		{
			obj.activate.subscribe(this.fireEvent, this, true);
			obj.activate.subscribe(this.swapSelectedNoGrow, this, true);
			this.regObjs.push(obj);
			this.regChildren.push(obj.id);
			this.arrParents.push(obj);
		}
		else if ( typeof obj == "object" & obj.tagName != undefined && obj.isArray() == false || typeof obj == "string" )
		{
			var el = new YAHOO.util.Element(obj);
			el.setStyle('cursor','pointer');
			el.on("click",this.swapSelectedNoGrow,el,this);
			this.noGrowers.push(el);
		}
		else if ( typeof obj == "object" & obj.isArray())
		{
			for (o = 0; o < obj.length; o++ )
			{
				var el = new YAHOO.util.Element(obj[o]);
				el.setStyle('cursor','pointer');
				el.on("click",this.swapSelectedNoGrow,el,this);
				this.noGrowers.push(el);
				this.noGrowersState.push(el.get("element").getAttribute("mState"));
				
			}
		}
	}

	this.ngNoSelect = function()
	{
		for ( o in this.noGrowers )
		{
			target = this.noGrowers[o].getElementsByClassName('lnavItemLabel')[0];
			//this.noGrowers[o].style.cursor = "pointer";
			if ( target != undefined )
			{	
				if (typeof target.onselectstart!="undefined") //IE route
					target.onselectstart=function(){return false}
				else if (typeof target.style.MozUserSelect!="undefined") //Firefox route
					target.style.MozUserSelect="none"
				else //All other route (ie: Opera)
					target.onmousedown=function(){return false}
					target.style.cursor = "select"
			}
			else
			{
				target2 = this.noGrowers[o].getElementsByTagName('IMG');
				//alert(target2[0].onclick);
				YAHOO.util.Event.addListener(target2[0], "mousedown", this.preventDrag, target2[0]);
				YAHOO.util.Event.addListener(target2[0], "click", this.ngAddLink, this.noGrowers[o].get("element").getAttribute('mState'));
				target2[0].onclick = "";
				
			}
		}
	}

	this.preventDrag = function(e, args)
	{
	   try
	   {
		   e.preventDefault();
	   }
	   catch(err)
	   {
		args.ondrag = function(){return false;}
	   }
	}

	this.ngAddLink = function(e, args)
	{
		YAHOO.util.Event.preventDefault(e);
		loadPage(args);
		return false;
	}

	this.ngMakeActive = function(strEl)
	{
		if ( typeof strEl == "string" )
		{
			for ( n = 0; n < this.noGrowers.length; n++ )
			{
				this.noGrowers[n].get('id') == strEl ? this.swapSelectedNoGrow("n",this.noGrowers[n]):null;
			}
		}
	}
	
	this.swapSelectedNoGrow = function(e, el)
	{
		for ( n in this.noGrowers )
		{
			if (this.noGrowers[n] != el )
			{
				this.noGrowers[n].hasClass("expanded") ? this.noGrowers[n].removeClass("expanded") : null;
			}
			else
			{
				this.noGrowers[n].hasClass("expanded") ? null : this.noGrowers[n].addClass("expanded");
			}
		}
	}
	
	this.registerChildren = function(arrChild, navObj)
	{
		for ( c in arrChild )
		{
			this.regChildren.push(arrChild[c]);
			this.arrParents.push(navObj);
		}
	}
	
	this.fireEvent = function(e, args)
	{
		for (o in this.regObjs )
		{
			args[0] != this.regObjs[o] ? this.regObjs[o].collapse() : null;
		}
	}

	this.stateChange = function(state)
	{
		if (arrQueryStr(this.noGrowersState, state) == -1 )
		{
			
			for (o in this.regObjs )
			{
				regQuery = arrQueryStr(this.regChildren, state);
				if (typeof regQuery == 'number' && this.regObjs[o] == this.arrParents[regQuery]) 
				{
					this.regObjs[o].expand();
				}
				else
				{
					this.regObjs[o].collapse();
				}
			}
		}
		else

		{
			ngQuery = arrQueryStr(this.noGrowersState, state);
			this.ngMakeActive(this.noGrowers[ngQuery].get("id"));
			for (o in this.regObjs )
			{
				this.regObjs[o].collapse();
			}
		}
	}
}


function lNavGrower(id, oDiv, args, group)
{
	this.id = id
	this.duration = 0.35;
	this.multiselector = false;
	for ( a in args )
	{
		arg = args[a].split(":");
		switch(arg[0].trim())
		{
			case "multiselect":
				arg[1].trim() == "true" ? this.multiselector = true : this.multiselector = false;
				break;
			case "duration":
				this.duration = arg[1].trim();
				break;
		}
	}
	this.expanded == undefined ? this.expanded = false : null;
	this.el = new YAHOO.util.Element(oDiv);
	this.navItemEntries = this.el.getElementsByClassName('lnavItemEntry');
	this.navItemLabel = this.el.getElementsByClassName('lnavItemLabel');
	this.navItem = this.el.getElementsByClassName('lnavItem');
	var pos = YAHOO.util.Dom.getRegion(this.navItem[0]);
	this.maxHeight = parseInt(pos.bottom) - parseInt(pos.top);
	var pos2 = YAHOO.util.Dom.getRegion(this.navItemLabel[0]);
	this.minHeight = parseInt(pos2.bottom) - parseInt(pos2.top);
	var navItemLabel = new YAHOO.util.Element(this.navItemLabel); 
	this.activate = new YAHOO.util.CustomEvent("select");
	this.onStartExpand = new YAHOO.util.CustomEvent("startExpand");
	this.onCompleteExpand = new YAHOO.util.CustomEvent("completeExpand");
	this.onStartCollapse = new YAHOO.util.CustomEvent("startCollapse");
	this.onCompleteCollapse = new YAHOO.util.CustomEvent("completeCollapse");
	
	//alert(this.id + "," + this.maxHeight + "," + this.minHeight);
	
	for ( l in this.navItemEntries )
	{
		var tState = this.navItemEntries[l].getAttribute("mState").toString();
		YAHOO.util.Event.addListener(this.navItemEntries[l], "click", function(){loadPage(this.getAttribute('mState'))}, this.navItemEntries[l], true);
	}
	
	for ( i in this.navItemEntries )
	{
		target= this.navItemEntries[i];
		if (typeof target.onselectstart!="undefined") //IE route
			target.onselectstart=function(){return false}
		else if (typeof target.style.MozUserSelect!="undefined") //Firefox route
			target.style.MozUserSelect="none"
		else //All other route (ie: Opera)
			target.onmousedown=function(){return false}
			target.style.cursor = "select"
	}
	
	if ( group != undefined )
	{
		group.register(this)
		for ( o in this.navItemEntries )
		{
			group.registerChildren([this.navItemEntries[o].getAttribute("mState")], this);
		}
	}	
	
	this.callback = function(e)
	{
		var animate = false;
		switch(this.expanded)
		{
			case false:
				var myAnim = new YAHOO.util.Anim(this.el.get('id'));
				myAnim.onStart.subscribe(function(){this.onStartExpand.fire(this)}, this, true);
				myAnim.onComplete.subscribe(function(){this.onCompleteExpand.fire(this)}, this, true); 
				myAnim.attributes.height = { to: this.maxHeight };
				myAnim.duration = this.duration;
				myAnim.method = YAHOO.util.Easing.easeOut;
				navItemLabel.addClass('expanded');
				this.expanded = true;
				this.activate.fire(this);
				animate = true;
				break;
			case true:
				if ( this.multiselector == true )
				{
					this.collapse();
				}
				else
				{
					this.onCompleteExpand.fire(this);
				}
				this.activate.fire(this);
				break;
		}
		animate == true? myAnim.animate() : null;
	};
	
	this.expand = function()
	{
		var myAnim = new YAHOO.util.Anim(this.el.get('id'));
		myAnim.attributes.height = { to: this.maxHeight };
		myAnim.duration = this.duration;
		myAnim.method = YAHOO.util.Easing.easeOut;
		navItemLabel.addClass('expanded');
		this.expanded = true;
		myAnim.animate();
		this.activate.fire(this);
	}
	
	this.collapse = function()
	{
		var myAnimX = new YAHOO.util.Anim(this.el.get('id'));
		myAnimX.attributes.height = { to: this.minHeight };
		myAnimX.duration = this.duration;
		myAnimX.method = YAHOO.util.Easing.easeOut;
		navItemLabel.removeClass('expanded');
		this.expanded = false;
		myAnimX.animate();
	}
	
	this.disableSelection = function(target)
	{
		if (typeof target.onselectstart!="undefined") //IE route
			target.onselectstart=function(){return false}
		else if (typeof target.style.MozUserSelect!="undefined") //Firefox route
			target.style.MozUserSelect="none"
		else //All other route (ie: Opera)
			target.onmousedown=function(){return false}
			target.style.cursor = "select"
	}
	
	YAHOO.util.Event.addListener(this.navItemLabel[0].id, "click", this.callback, this, true);
	this.disableSelection(this.navItemLabel[0]);

	
}


