/**
 * @author apellet
 */
/**
 * Objet destiné à afficher la légende active sur la carte.
 * @param {Object} map
 */
LegendControl = function ( map, json_asso )
{
	GControl.call ( this );
	this.json_types_asso = json_asso
};

LegendControl.prototype = new GControl();	
LegendControl.prototype.img_path = "";
LegendControl.prototype.json_types_asso = null;


LegendControl.prototype.createLine = function ( table, o )
{
	var tbody = document.createElement ("TBODY");
	var tr_1 = document.createElement ("TR");
	
	var td_0 = document.createElement ("TD");
	var td_1 = document.createElement ("TD");
	var td_2 = document.createElement ("TD");
	
	var img = document.createElement ("IMG");
	img.src = o.image;
	img.setAttribute ('width', '18px');
	
	
	var chk = document.createElement ("input");
	chk.setAttribute ("type", "checkbox");
	chk.setAttribute ("title", "Cliquez pour masquer les icones correspondant");
	chk.setAttribute ("code", o.code );
	
		
	var cocher = this.s_cocher;
	var decocher = this.s_decocher;
	var les = this.s_les;
	
	chk.onclick = function ( )
	{
		var code = this.getAttribute ( "code" );
		var lay = map_layer_asso[code];
		//lorsque l'on coche, il convient de tester si le layer à le droit de s'afficher a ce niveau de zoom
		if ( this.checked == true )
		{
			//if ( map.getZoom() >= lay._zoom )
			{
				map_layer_asso[code].showAll ();			
				chk.setAttribute("title", "Cliquez pour masquer les icones correspondant");
			}
		}
		//Lorsque l'on décoche, quelque soit le niveau de zoom on hide.
		else		
		{
			map_layer_asso[code].hideAll();
			chk.setAttribute("title", "Cliquez pour afficher les icones correspondant");
		}
		
		
	};
	
	td_0.appendChild ( chk );
	td_1.appendChild ( img );
	td_2.appendChild ( document.createTextNode ( '' ) );
	
	tr_1.appendChild ( td_0 );
	tr_1.appendChild ( td_1 );
	tr_1.appendChild ( td_2 );
	
	tbody.appendChild ( tr_1 );
	chk.setAttribute ("checked", true);	
	table.appendChild ( tbody );
	
	td_2.innerHTML = o.title;
};	

LegendControl.prototype.onLegendeClick = function (c)
{
	if ( c.checked )
	{
		document.getElementById ( "legende_body" ).style.display = "block";
		document.getElementById ( "chk_legende" ).setAttribute ("title", "Cliquez pour masquer la légende" );
	}
	else 
	{
		document.getElementById ( "legende_body" ).style.display = "none";
		document.getElementById ( "chk_legende" ).setAttribute ("title", "Cliquez pour afficher la légende" );
	}
};




/**
 * Constructeur
 * @param {Object} map	L'objet GMap
 */
LegendControl.prototype.initialize = function ( map )
{
	var container = document.createElement("div");
	container.className = "control_legend";
	
	var titre = document.createElement ("DIV");
	titre.className = "titre";
		
	var check = document.createElement ("input");
	check.type = "checkbox";
	check.id = "chk_legende";
	
	check.title = "Cliquez pour masquer la légende";

	check.onclick = GEvent.callbackArgs ( this, this.onLegendeClick, check );
	
	var label = document.createElement ("LABEL");
	label.innerHTML = "&nbsp;&nbsp;&nbsp;L&eacute;gende&nbsp;";
	label.setAttribute ("for", "chk_legende");
	
	titre.appendChild ( check );
	titre.appendChild ( label );
	
	var block = document.createElement ("DIV");
	block.className = "body";
	block.id = "legende_body"
	
	var table = document.createElement ("TABLE");
	
	for ( var j=0; j<this.json_types_asso.length; j++ )
	{
		var o = this.json_types_asso[j];
		this.createLine(table, o);
	}
	
		
	
	block.appendChild ( table );
		
	container.appendChild ( titre );
	
	container.appendChild ( block );
	
	check.checked = true;
	
	map.getContainer().appendChild(container);
    return container;
}
LegendControl.prototype.getDefaultPosition=function()
{
	return new GControlPosition(G_ANCHOR_TOP_LEFT,new GSize( 2,2 ));
}


