/**
 * @author apellet
 */

/**
 * Creation des icons en forme de cercle.
 */
function createIconCircle ()
{
	icon_circle = new Array ();

	//1er icon	
	var icon_1 = new GIcon();
	icon_1.iconSize = new GSize ( 20, 20 );
	icon_1.iconAnchor = new GPoint  ( 10,10 );
	icon_1.infoWindowAnchor = new GPoint  ( 10, 0 );
	icon_1.image = prefix_php + "images/carto/circle_20.png";
	
	icon_circle.push ( icon_1);
	
	//2eme icon
	var icon_2 = new GIcon();
	icon_2.iconSize = new GSize ( 25, 25 );
	icon_2.iconAnchor = new GPoint  ( 12,12 );
	icon_2.infoWindowAnchor = new GPoint  ( 12, 0 );
	icon_2.image = prefix_php + "images/carto/circle_25.png";
	
	icon_circle.push ( icon_2);

	//2eme icon
	var icon_3 = new GIcon();
	icon_3.iconSize = new GSize ( 30, 30 );
	icon_3.iconAnchor = new GPoint  ( 15,15 );
	icon_3.infoWindowAnchor = new GPoint  ( 15, 0 );
	icon_3.image = prefix_php + "images/carto/circle_30.png";
	
	icon_circle.push ( icon_3);

}

/**
 * Objet qui gère le dessin des cercles.
 */							
ClassCircle = function ( opt )
{
	this.init ( opt );
	//Init des icons en forme de cercle
	//createIconCircle ();
};
ClassCircle.prototype =
{
	_arr : [],
	
	/**
	 * Constructeur
	 */
	init : function ( opt )
	{
		jQuery.extend(this, opt);
	},
	/**
	 * Renvoi un marker cercle en fonction du nombre d'association
	 * @param {Object} val le nombre d'associations du département
	 */
	getCircle : function ( val )
	{
		for ( var i=0; i<this._arr.length; i++ )
		{
			if ( val >= this._arr[i].min && val < this._arr[i].max )
			{
				return this._arr[i].ico;
			}
		}
	}
} 	


/**
 * Dessin des cercles en fonction du nombre d'associations par département
 */
function drawCircles ()
{
	obj_reg = {};
	
	var data = "";
    
	
	//Chargement du nombre d'acteur par département via AJAX	
	jQuery.ajax({
        type: "POST",
        url: prefix_php + "php/__load_acteur_region.php",
        data: data,
		dataType:'xml',
        success: function(xml)
		{
			layer_reg = new Layer ({_map:map, _zoom:zoom_dep, _min_max:'max', _name:'regions'});
			//Affichage de chacun des départements sous forme de cercle
			jQuery(xml).find("region").each ( function()
			{
				
				var sw = jQuery(this).attr('south_west').split(',');	
				var ne = jQuery(this).attr('north_east').split(',');
				
				
		
				var b = new GLatLngBounds( 
					new GLatLng ( sw[0], sw[1] ),
					new GLatLng ( ne[0], ne[1] )
		 		);
		 		
				//var latlng = new GLatLng ( parseFloat ( jQuery(this).attr('lat') ), parseFloat ( jQuery(this).attr('lng') ) );
				var latlng = b.getCenter() ;
				/*var m_reg = new GMarker ( latlng , {icon:icon_region, 
													title:parsefromXML(jQuery(this).attr('lib_reg')) + " : " + jQuery(this).attr('nb_acteur') + " structures"} );
				
				*/
				var m_reg = new GMarker ( latlng , {icon:class_circle.getCircle ( jQuery(this).attr('nb_acteur') ), 
				title:parsefromXML(jQuery(this).attr('lib_reg')) + " : " + jQuery(this).attr('nb_acteur') + " structures"} );
				
				
				layer_reg.add ( m_reg, jQuery(this).attr('id_reg') );
				
				GEvent.addListener ( m_reg, "click", GEvent.callbackArgs ( window, window.openInfoRegion, m_reg, {
									lib_reg:parsefromXML(jQuery(this).attr('lib_reg')), 
									nb_acteur:jQuery(this).attr('nb_acteur'), 
									id_reg:jQuery(this).attr('id_reg'),
									liste_dep : jQuery(this).attr('liste_dep')
									} ) );
									
				//GEvent.addListener ( m_reg, "dblclick", GEvent.callbackArgs ( window, window.loadAssoFromDep, jQuery(this).attr('dep'), false ) );
				
				
				
				obj_reg[jQuery(this).attr('id_reg')] = {lib:parsefromXML(jQuery(this).attr('lib_reg')),
												num:jQuery(this).attr('id_reg'),
												nb_asso:jQuery(this).attr('nb_acteur'),
												lat:latlng.lat(),	
												lng:latlng.lng(),
												departement_loaded : false,
												liste_dep : jQuery(this).attr('liste_dep')};
				
				
				
				
			});
			loading.stop ();
			
			/*Remplissage de la liste des regions*/
			fillSelectRegion();
        },
		error:function (XMLHttpRequest, textStatus, errorThrown)
		{
			alert(textStatus + " drawCircle \n"+ errorThrown); 
		}
    });
}



/**
 * Gestion du contenu de la bulle d'info des départements
 * @param {Object} m le marker du département
 * @param {Object} opt les valeurs à afficher
 */
function openInfoRegion ( m, opt )
{
	//console.log ( opt.liste_dep );
	
	if ( !obj_reg[opt.id_reg].departement_loaded )
	{
		jQuery.ajax({
	        type: "POST",
	        url: prefix_php + "./php/__load_departement_region.php",
	        data: {id_reg:opt.id_reg},
			dataType:'xml',
	        success: function(obj)
			{
				var h = "<p class=\"bulle_region_titre\">R&eacute;gion "+opt.lib_reg + "</p>";
				h += "<p>Nombre de structures : " + opt.nb_acteur + "</p>";
				h+= "<p><a href='javascript:loadActeurFromDep(\""+ opt.liste_dep +"\", false, "+opt.id_reg+");'>Cliquez pour voir la carte des acteurs en r&eacute;gion</a></p>"//
				
				h += "<hr/>";
				h += "<p></p>";
				 

				//Affichage de chacun des départements sous forme de cercle
				jQuery(obj).find("departement").each ( function()
				{
					h += "<a href='javascript:loadActeurFromDep(" + jQuery(this).attr('id_dep') + ", false, "+opt.id_reg+");'>"+jQuery(this).attr("lib_dep") + "</a>"+ " (" + jQuery(this).attr ('nb_acteur') + ")<br>";//
					
				});
				
				
				h += "<p>" + "Cliquez sur un d&eacute;partement" + "</p>";
				
				obj_reg[opt.id_reg].departement_loaded = true;
				obj_reg[opt.id_reg].departement_bubble = h;
				
				
				m.openInfoWindowHtml ( h );
			}
		});
	}
	
	else
	{
		m.openInfoWindowHtml ( obj_reg[opt.id_reg].departement_bubble );
	}
	
	
	
	
}

