/**
 * @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_dep = {};
	
	var data = "year=" + new Date().getFullYear ();
    
	
	//Chargement du nombre d'associations par département via AJAX	
	jQuery.ajax({
        type: "POST",
        url: prefix_php + "php/loadDep.php",
        data: data,
		dataType:'xml',
        success: function(xml)
		{
			layer_dep = new Layer ({_map:map, _zoom:zoom_dep, _min_max:'max', _name:'departements'});
			//Affichage de chacun des départements sous forme de cercle
			jQuery(xml).find("dep").each ( function()
			{
				jQuery(this).attr('nb_asso');
				jQuery(this).attr('lng');	
				jQuery(this).attr('lng');	
				
				var latlng = new GLatLng ( parseFloat ( jQuery(this).attr('lat') ), parseFloat ( jQuery(this).attr('lng') ) );
				var m_dep = new GMarker ( latlng , {icon:class_circle.getCircle ( jQuery(this).attr('nb_asso') ), title:jQuery(this).attr('lib')} );
				
				
				//map.addOverlay ( m_dep );
				layer_dep.add ( m_dep, jQuery(this).attr('dep') );
				
				GEvent.addListener ( m_dep, "click", GEvent.callbackArgs ( window, window.openInfoDepartement, m_dep, {lib:jQuery(this).attr('lib'), nb_asso:jQuery(this).attr('nb_asso'), dep:jQuery(this).attr('dep')} ) );
				GEvent.addListener ( m_dep, "dblclick", GEvent.callbackArgs ( window, window.loadAssoFromDep, jQuery(this).attr('dep'), false ) );
				
				
				
				obj_dep[jQuery(this).attr('dep')] = {lib:jQuery(this).attr('lib'),
												num:jQuery(this).attr('dep'),
												nb_asso:jQuery(this).attr('nb_asso'),
												lat:jQuery(this).attr('lat'),	
												lng:jQuery(this).attr('lng')};
				
				
				
				
			});
			loading.stop ();
			
			/*Remplissage de la liste des départements*/
			fillSelectDep();
        },
		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 openInfoDepartement ( m, opt )
{
	
	var h = "<div class='bubble'><h3>" + opt.lib + "</h3>" + "Nombre d'associations : " + opt.nb_asso;
	h += '<br/>';
	h += '<br/>';
	
	h += '<a href="javascript:loadAssoFromDep(\'' + opt.dep +'\', false);">Cliquez pour visualiser toutes les assocations</a>';
	h += "</div>";
	
	m.openInfoWindowHtml ( h );
}




