
function groupsButtonMgrInit(container)
{

	if(container=='' || container==undefined || container==null) container = 'body';
	
	//attach our behaviors
	$$(container+' .buttonMgr').each( function(el) { addEvent(el, 'click', groupsButtonManager) } );
	
	//for our paging functions
	$$(container+' .paging').each( function(el) { addEvent(el, 'click', pagingFunctions) } );
	
	//generic behaviors
	//defined with attribute behavior='function name()'
	$$(container+' .behavior').each( function(el)
												{
													var behavior = eval(el.getAttribute('behavior'));
													if(behavior != null)
														addEvent(el, 'click', behavior)
												} 
									);
}

function groupsButtonTrigger(currElement)
{
	currElement.onclick = groupsButtonManager;
		currElement.onclick();	
}

//determine button behaviors
function groupsButtonManager(el)
{
	var contentLoaded = 0;

	//setup our id to load our content into
	if(this.getAttribute('loadform') !=null)
	{
		var templateFile = this.getAttribute('loadform');
		
		//split our load content name up
		var loadNameArray = templateFile.split('_');
	
		var loadFormID = loadNameArray[0]+'_form';
		
		//alert(loadFormID+":"+templateFile);
		//load the template file
		loadContent(loadFormID, this, templateFile);
		//return 0;
	}

	
	//load child mod
	if(this.getAttribute('loadChild') !=null)
	{
		var childMod = this.getAttribute('loadChild');
		var parentMod = this.getAttribute('modName');
		
		loadChild(parentMod, childMod)
	}
	else if(this.getAttribute('showParent') !=null) //show parent
	{
		var parentMod = this.getAttribute('showParent');
		showParent(parentMod)
	}
	else if(this.getAttribute('reloadParent') !=null) //reload parent
	{
		var parentMod = this.getAttribute('reloadParent');
		reloadParent(parentMod)
	}

	//reload the module
	if(this.getAttribute('reloadcontent') !=null)
	{
		modName = this.getAttribute('reloadcontent');
		//moduleID = module+contentDBID;
		//alert(moduleID);
		reloadContent(modName, this);
	}
	
	//alert(contentID);
	//alert($(objID).getHeight()); 

	//check if we need to hide or show elements without a load attribute
	if( this.getAttribute('loadform') == null && (this.getAttribute('hide') !=null || this.getAttribute('show') !=null) )
		toggleContent(this);
	
}

function showParent(parentMod)
{
	Element.hide(parentMod+'_child');
	Element.show(parentMod+'_parent');
	$(parentMod+'_child').innerHTML = '';
}

function reloadParent(modName)
{
	var ajaxConfig = new Object();
	ajaxConfig.parameters = new Object();
	
	ajaxConfig.method = 'get';

	ajaxConfig.onSuccess = function(t) {
		$(modName+'_container').innerHTML = t.responseText; 
		formsInit(modName+'_container');
		
		groupsButtonMgrInit('#'+modName+'_container');
	};
	
	ajaxConfig.parameters.modName = modName;
	
	//send user id and group id
	ajaxConfig.parameters.id = id;
	ajaxConfig.parameters.groupID = groupID;
	if($(modName+'_parent').getAttribute('showParent') != null) {
		var parentMod=$(modName+'_parent').getAttribute('showParent');
		ajaxConfig.parameters.parentMod = parentMod;
	}
	ajaxConfig.parameters.reloadContent = "true";

	new Ajax.Request(interactDir+'modServer.php',ajaxConfig);
}

function loadChild(parentMod, childMod, postSuccessFunc)
{
	//setup ajax object
	var ajaxConfig = new Object();
	ajaxConfig.parameters = new Object();

	ajaxConfig.method = 'get';

	//parentMod='groupFiles';	
	//setup our success function
	ajaxConfig.onSuccess = function(t) {
		//update our content
		//alert(t.responseText);
		$(parentMod+'_child').innerHTML = t.responseText; 
		
		//Hide parent mod and show child mod
		Element.hide(parentMod+'_parent');
		Element.show(parentMod+'_child');
		
		/*
		//function call after everything
		if( el.getAttribute('behavior') !=null )
			eval(el.getAttribute('behavior'));
		*/

		//init form functions
		formsInit(parentMod+'_child');

		//init button manager on the new content
		groupsButtonMgrInit('#'+parentMod+'_child');
	};
	
	//setup ajax vars
	ajaxConfig.parameters.modName = childMod;
	ajaxConfig.parameters.parentMod = parentMod;

	//send user id
	ajaxConfig.parameters.id = id;
	ajaxConfig.parameters.groupID = groupID;

	new Ajax.Request(interactDir+'modServer.php',ajaxConfig);
}


function loadContent(objID, el, templateFile, postSuccessFunc)
{

	//get our module name
	if( el.getAttribute('modName') !=null )
		var modName = el.getAttribute('modName');
	else
		var modName = null;

	//alert('modname: '+modName);

	//setup ajax object
	var ajaxConfig = new Object();
	ajaxConfig.parameters = new Object();

	ajaxConfig.method = 'get';
	
	//setup our success function
	ajaxConfig.onSuccess = function(t) {
		//update our content
		$(objID).innerHTML = t.responseText; 

		//check to see if we need to hide/show elements
		if( el.getAttribute('hide') !=null || el.getAttribute('show') !=null )
			toggleContent(el);
			
		//fadeContent(objID);
		
		//function call after everything
		if( el.getAttribute('behavior') !=null )
			eval(el.getAttribute('behavior'));
		

		//init form functions
		formsInit(objID);

		//init button manager on the new content
		groupsButtonMgrInit('#'+objID);
	};
	
	//setup ajax vars
	ajaxConfig.parameters.template = templateFile;
	ajaxConfig.parameters.modName = modName;
	//send user id
	ajaxConfig.parameters.id = id;
	ajaxConfig.parameters.groupID = groupID;

	new Ajax.Request(interactDir+'modServer.php',ajaxConfig);
}



function reloadContent(modName, el, postSuccessFunc)
{
	var ajaxConfig = new Object();
	ajaxConfig.parameters = new Object();
	
	ajaxConfig.method = 'get';

	ajaxConfig.onSuccess = function(t) {
		$(modName).innerHTML = t.responseText; 
		
		//function call after everything
		eval(postSuccessFunc);
		
		groupsButtonMgrInit('#'+modName);
	};
	
	ajaxConfig.parameters.modName = modName;
	
	//send user id and group id
	ajaxConfig.parameters.id = id;
	ajaxConfig.parameters.groupID = groupID;
	
	new Ajax.Request(interactDir+'modServer.php',ajaxConfig);
}

//fades content/form ids in and out
function fadeContent(obj)
{

	var toObjId = obj.id;
	var toObjIDArray = obj.id.split('_');
	$(toObjId).style.height='';

	//fade to default content
	if(toObjIDArray[toObjIDArray.length-1]=="content")
	{
		var fromObjId = obj.id;
		var fromObjIdArray = fromObjId.split('_');
		fromObjIdArray[fromObjIdArray.length-1] = "form";
		fromObjId = fromObjIdArray.join('_');
	}
	else
	//fade to form content
	{
		var fromObjId = obj.id;
		var fromObjIdArray = fromObjId.split('_');
		fromObjIdArray[fromObjIdArray.length-1] = "content";
		fromObjId = fromObjIdArray.join('_');
	}

	//get our default content height so we can resize back to original
	$(fromObjId).style.overflow = 'hidden';
	var contentHeight = $(fromObjId).getHeight();
	$(fromObjId).style.overflow = '';
		
	//create obj instances for fade/resize
	opacityTweenHide = new OpacityTween($(fromObjId),Tween.regularEaseOut, 100, 0, .5);
	opacityTweenShow = new OpacityTween(obj,Tween.regularEaseOut, 0, 100, .5);
	
	//stretch our content height to match our form height
	contentResize = new Tween($(fromObjId).style,'height',Tween.regularEaseIn,$(fromObjId).getHeight(),obj.getHeight(),1,'px');
	
	//when finished fading out, start resize
	opacityTweenHide.onMotionFinished= function(){ contentResize.start() };
	
	//when finished resizing hide/show elements and fade in
	contentResize.onMotionFinished= function(){ 

		Element.hide($(fromObjId));
		Element.show(obj);
		opacityTweenShow.start();
		
		//reset our content height
		$(fromObjId).style.height = contentHeight+'px';
		};

	//start pimped out profile
	opacityTweenHide.start();


}

// hide/show elements based on element attributes
function toggleContent(obj)
{
	//alert(obj.getAttribute('hide')+' '+obj.getAttribute('show'));
	//get our elements to hide
	if(hidenames = obj.getAttribute('hide'))
	{
		try
		{
			//go through our elements and hide them
			Hnames = hidenames.split(' ');
			//alert(Hnames);
			for(var loop=0;loop<Hnames.length;loop++)
			{
				if(Hnames[loop] == 'this')
					Element.hide(obj);
				else
				{
					//alert($(Hnames[loop]).id)
					//if($(Hnames[loop].id)!=null)
						Element.hide(Hnames[loop]);
						//alert($(Hnames[loop]));
				}
			}
		}
		catch(err) {}
	}
	
	//get our elements to show
	if(shownames = obj.getAttribute('show'))
	{
		try
		{
			//go through our elements and show them
			Snames = shownames.split(' ');
			for(var loop=0;loop<Snames.length;loop++)
			{
				if(Snames[loop] == 'this')
					Element.show(obj);
				else
				{
					objID = Snames[loop].split('_');
					if(objID[objID.length-1]=="content" || objID[objID.length-1]=="form" )
						fadeContent($(Snames[loop]));
					else
					{
						//alert($(Snames[loop]).id)
						//if($(Snames[loop].id)!=null);
						//alert($(Snames[loop])!=null);
							Element.show(Snames[loop]);
					}
				}
			}
		}
		catch(err) {}
	}
}
