/**
* groupecouche.js
*/

/**
 * 
 * Construction de l'Objet Legende.
 * 
 * Cet objet represente la symbologie de la carte.
 * 
 * @constructor
 * @author Alexandre PELLET
 * @version 0.1
 */
BG.widget.Legende = function ( legendeConfig )
{
	this.init ( legendeConfig );
};

BG.widget.Legende.prototype = 
{
	lstOrdreLegende : null,
	
	placementIcon : null,
	
	styleNoVisible : null,
	
	loaded : false,
	
	init: function ( legendeConfig )
	{
		this.placementIcon = BG.widget.Legende.ICON_BOTTOM;
		this.styleNoVisible = BG.widget.Legende.NODE_DISABLE;
		
		BG.util.ApplyConfig ( legendeConfig, this );
		
		this.lstOrdreLegende = new Array();
	},
	
	AddObject : function ( object, parent )
	{
		objDebug.Push ( 'dans Legende.AddObject:'+object.name + " parent: "+parent );
		if ( parent == "root" )
		{
			//this.lstOrdreLegende [object.name] =  object ;
			this.lstOrdreLegende[this.lstOrdreLegende.length] =  object ;
			
		}
		else
		{
			this.GetGroupe ( parent ).AddObject ( object );
		}
	},
	
	GetGroupe : function ( groupename )
	{
		var i=0; 
		
		for ( i=0; i<this.lstOrdreLegende.length; i++ )
		{
			//objDebug.Push ( 'dans legende.GetGroupe:'+groupename + " " +i);
			if ( this.lstOrdreLegende[i] instanceof BG.widget.GroupeCouche && this.lstOrdreLegende[i].name == groupename )
			{
				return this.lstOrdreLegende[i];
			}
			else if ( this.lstOrdreLegende[i] instanceof BG.widget.GroupeCouche && this.lstOrdreLegende[i].name != groupename )
			{
				var objRet = this.lstOrdreLegende[i].GetGroupe ( groupename );
				if ( objRet != null )
					return objRet;
			}
				
		}
	},
	
	UpdateVisibility : function ( dVisibility )
	{
		for ( var i=0; i<this.lstOrdreLegende.length; i++ )
		{
			this.lstOrdreLegende[i].SetVisibility ( dVisibility );
		}
		
	},
	
	/**
	 * Envoi une requete de mise à jour de la carte
	 */
	UpdateLegende : function ()
	{
		var request = "";
		for ( var i=0; i<this.lstOrdreLegende.length; i++ )
		{
			request += this.lstOrdreLegende[i].GetUpdateLegende ( );
			
			if(this.lstOrdreLegende[i].typeObj==BG.widget.Legende.OBJGROUPE_TYPEANALYSE)
			{
			legende.loaded = false;
			}
		}
		request += "@";
		
		
		var i = 0;
		var chaine = szHttpServeur  +
		                "&ACTION=UpdateLegende" + 
				"!EMPRISE=" + mapleft + "@" + mapbottom + "@" + (mapright-mapleft) + "@" + (maptop - mapbottom) + 
				"!OUTPUTSIZE=" + mapClientwidth + "@" + mapClientheight +
				"!LOCATION=" + location.href + "!" + request;	
		
		SetToolSelectionCarte ();
		updateBufferMap(chaine);		
	}
	
};

BG.widget.Legende.OBJGROUPE_TYPEGROUPE 			= 0;
BG.widget.Legende.OBJGROUPE_TYPECOUCHE 			= 1;
BG.widget.Legende.OBJGROUPE_TYPEANALYSE			= 2;
BG.widget.Legende.OBJGROUPE_TYPEIMAGE			= 3;

BG.widget.Legende.GROUPE_EXPANDED				= 1;
BG.widget.Legende.GROUPE_COLLAPSED				= 2;

BG.widget.Legende.ICON_LEFT						= 0;
BG.widget.Legende.ICON_RIGHT					= 1;
BG.widget.Legende.ICON_BOTTOM					= 2;

BG.widget.Legende.NODE_DISABLE					= 0;
BG.widget.Legende.NODE_INVISIBLE				= 1;

/**
* constructeur CGroupeCouche.
* Il represente les entrées de la legende.
*
* @param	name 	nom du groupe
*/

BG.widget.GroupeCouche = function ( groupeConfig )
{
	this.init ( groupeConfig );
};
BG.widget.GroupeCouche.prototype = 
{
	/**
	 * shadeStyle contenant la symbologie du layer
	 * @type BG.collection.ShadeStyle
	 */
	shadeStyle : null,
	
	/**
	 * Representation dans le tree du Layer
	 * @type YAHOO.widget.TaskNode
	 */
	node : null,
	
	/**
	* Nom du groupe de couche
	* @type string
	*/
	name: null,
	
	/**
	* Liste de objets composant le groupe
	* @type Array
	*/
	lstObj: null,
	
	
	iVisible: 0,
	iVisibleAtScale: 0,
	iDetailVisible: 0,
	iExpanded: 0,
	ident: 0,
	iTypeObj: 0,

	
	/**
	* Initialisation du groupe de couche
	*/
	init: function ( groupeConfig )
	{
		this.lstObj = new Array ();
		this.shadeStyle = new BG.collection.ShadeStyle ('');
		
		BG.util.ApplyConfig ( groupeConfig, this );	
	},
	
	/**
	* Ajout d'un objet à la liste de objet du groupe.
	* @param {Object} object L'objet à ajouter au groupe ( couhe
	*/
	AddObject : function ( object )
	{
		this.lstObj [ this.lstObj.length ] = object;
		objDebug.Push  ( "GroupeCouche:AddObject : parent : " + this.name + " name : " + object.name );
	},
	
	GetGroupe : function( groupename )
	{
		var i=0; 
		for ( i=0; i<this.lstObj.length; i++)
		{			
			if ( this.lstObj[i] instanceof BG.widget.GroupeCouche && this.lstObj[i].name == groupename )
			{			
				return this.lstObj[i];
			}
			else if ( this.lstObj[i] instanceof  BG.widget.GroupeCouche  && this.lstObj[i].name != groupename )
			{
				var objRet = this.lstObj[i].GetGroupe( groupename );
				if ( objRet != null )
					return objRet;
			}			
		}
	},
	
	SetVisibility : function ( dVisibility )
	{	
		
		for ( var i=0; i<this.lstObj.length; i++)
		{
			this.lstObj[i].SetVisibility ( dVisibility );
			//alert ( this.lstObj[i].name );
		}
				if ( this.shadeStyle.dblZoomMin <= dVisibility && this.shadeStyle.dblZoomMax > dVisibility )
		{
			this.shadeStyle.iVisibility = 1;
			if ( legende.styleNoVisible == BG.widget.Legende.NODE_DISABLE )
			{
				this.node.enable();
			}
			else if ( legende.styleNoVisible == BG.widget.Legende.NODE_INVISIBLE )
			{
				document.getElementById ( this.node.getElId() ).style.display = "block";
			}
		}
		else
		{
			this.shadeStyle.iVisibility = 0;
			if ( legende.styleNoVisible == BG.widget.Legende.NODE_DISABLE )
			{
				this.node.disable();
			}
			else if ( legende.styleNoVisible == BG.widget.Legende.NODE_INVISIBLE )
			{
				document.getElementById ( this.node.getElId() ).style.display = "none";
			}
		}

	},
	
	/**
	 * Renvoi la chaine pour update legende
	 */
	GetUpdateLegende : function ()
	{
		var request = "";
		
		
		if ( this.shadeStyle.iDetailVisibleLegende == 0 )
		{
			var ret = "@[__groupe__]" + this.node.data.label + "=";
			if ( this.node.checked )
			{
				ret += "1";
			}
			else
			{
				ret += "0";
			}
			request = ret;
			//alert ( "il faut gérer le groupe "+ this.name +" dont les enfants ne sont pas visibles dans la legende.")
		}
		else
		{
			for ( var i=0; i<this.lstObj.length; i++)
			{
				request += this.lstObj[i].GetUpdateLegende ( );
			}
		}
		return request;
	}
	
	
	
};

/**
* constructeur CObjGroupe.
*
* @param	name 	nom de l'objet ? ajouter
* @param	type	type de l'objet
*/
function CObjGroupe ( name, type )
{
	this.szName = name;
	this.iType = type;
	this.iVisible;
	this.iVisibleAtScale;
	this.iVisibleInLegende;
	
	//Le container de la checkbox et du titre de la couche
	this.container;
	
	this.ident = -1;
	this.iTypeObj = "OBJGROUPE";
}

/**
 * On met à jour cet objet dans la legende
 */
CObjGroupe.prototype.UpdateInLegende = function ()
{
	objDebug.Push( this.iTypeObj + " " + this.szName );
}

