/**
 *  ------------------------------------------------------------------------------
 *                       		 group.js
 *  ------------------------------------------------------------------------------
 *  Copyright (C) 2003  Frederic Lecointre <frederic.lecointre@burnweb.net>
 *  ------------------------------------------------------------------------------
 * 
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 * 
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 * 
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *  ------------------------------------------------------------------------------
 */
/** 
 *
 * @package group
 * @version 1.1.0 
 * @author Frédéric LECOINTRE<frederic.lecointre@burnweb.net>
 * 
 */

var __groups = Array();
var __groupElements = Array();
var __groupRetvalEnum = 0;

var GROUP_UNKNOWN_ELEMENT = __groupRetvalEnum++;
var GROUP_REGISTRATION_EXCEPTION = __groupRetvalEnum++;
var GROUP_DISABLE_ELEMENT_EXCEPTION = __groupRetvalEnum++;
var GROUP_ENABLE_ELEMENT_EXCEPTION = __groupRetvalEnum++;

/**
 *
 * @param string groupName
 * @param string suffix
 * @param function fnEnable
 * @param function fnDisable
 * @return integer
 */
function registerGroup(groupName, suffix, fnEnable, fnDisable, method){

	if(__groups[groupName] == undefined){
	
		if(method == null){
			method = 1;
		}//end if
		
		__groups[groupName] = Array();
		__groups[groupName]['suffix'] = suffix;
		__groups[groupName]['fnEnable'] = fnEnable;
		__groups[groupName]['fnDisable'] = fnDisable;
		__groups[groupName]['active'] = null;
		__groups[groupName]['method'] = method;
	}//end if
	
}//end function	

/**
 *
 * @param string groupName
 * @param string entryId
 * @return integer
 */
function registerEntry(groupName, entryId){

	if(__groupElements[entryId] == undefined && __groups[groupName] != undefined){
	
		try{
			__groupElements[entryId] = Array();
			__groupElements[entryId]['group'] = groupName;
			
			if(__groups[groupName]['method'] == 0){
				document.getElementById(entryId).onmouseover = function(e){ showGroup(this.id); }//-----
			}//end if
			else{
				document.getElementById(entryId).onmousedown = function(e){ showGroup(this.id); }//-----
			}//end else
			
		}//end try
		catch(e){
			return GROUP_REGISTRATION_EXCEPTION;
		}//end catch
			
	}//end if
	else{
		return GROUP_UNKNOWN_ELEMENT;
	}//end catch
}//end function	

/**
 *
 * @param string groupName
 * @return string
 */
function getGroupActiveElement(groupName){

	if(__groups[groupName] != undefined){
		return __groups[groupName]['active'];
	}//end if
	else return null;
	
}//end function

/**
 *
 * @param string entryId
 * @return integer
 */
function showGroup(entryId){

	if(entryId == null || entryId == '' || __groupElements[entryId] == undefined){
		return GROUP_UNKNOWN_ELEMENT;
	}//end if

	var retval = true;
	var groupName = __groupElements[entryId]['group'];
	var active = __groups[groupName]['active'];
	var fnEnable = __groups[groupName]['fnEnable'];
	var fnDisable = __groups[groupName]['fnDisable'];
	var suffix = __groupElements[entryId]['suffix'];
	var target =  document.getElementById(entryId + __groups[groupName]['suffix']);	
	var activeTarget = document.getElementById(active + __groups[groupName]['suffix']);	
	
	if(target == activeTarget && __groups[groupName]['method'] != 0){
		try{
			fnDisable(target);
		}//end try
		catch(e){
			retval = GROUP_DISABLE_ELEMENT_EXCEPTION
		}//end catch
		
		__groups[groupName]['active'] = null;

	}//end if
	else{
	
		if(activeTarget != null ){
		
			try{
				fnDisable(activeTarget);
			}//end try
			catch(e){
				retval = GROUP_DISABLE_ELEMENT_EXCEPTION
			}//end catch
			
		}//end if

		if(target != null ){
		
			__groups[groupName]['active'] = entryId;
			
			try{
				fnEnable(target);
			}//end try
			catch(e){
				__groups[groupName]['active'] = null;
				retval = GROUP_DISABLE_ELEMENT_EXCEPTION
			}//end catch
			
		}//end if
		
	}//end else

	return retval;
	
}//end function		