/**
 * @author apellet
 */

/**
 * Classe qui gere des collections d'objet afin de permettre un affichage/désaffichage simultané
 * @param {Object} opt
 */
Layer = function ( opt )
{
	this.init ( opt );
};
Layer.prototype = 
{
	_obj : {},
	_zoom : 8,
	_map : null,
	/*Défini si le zoom passé en parametre est le zoom mini ou maxi*/
	_min_max : 'min',
	_statut:'F',
	_name : '',
	
	init : function ( opt )
	{
		jQuery.extend(this, opt);
		this._obj = new Object();
		//this._map = opt._map;
		//this._min_max = opt._min_max;
		GEvent.bind ( this._map, "zoomend", this, GEvent.callback ( this, this.checkZoom));
	},
	/**
	 * Ajout d'un objet à la collection et sur la carte
	 * @param {Object} m le marker
	 * @param {Object} id l'identifiant du marker
	 */
	add : function (m, id)
	{
		this._obj[id] = m;
		m.hidden = 0;
		this._map.addOverlay ( m );
	},
	
	get : function ( id )
	{
		return ( this._obj[id] );
	},
	
	remove : function ( id )
	{
		if ( this._name=="departements")
		{
			this._obj[id].hide();
			this._obj[id].ishidden == 1;
		}
		else
		{
			this._map.removeOverlay ( this._obj[id] );
			this._obj[i].isRemove = 1;
			//delete this._obj[id];
		}
		
	},
	
	/**
	 * Permet de supprimer tous les marqueurs de la collection afin qu'ils ne soient plus visibles sur la carte.
	 */
	remove_all : function ()
	{
		for ( var i in this._obj )
		{
			
			if ( this._name=="departements")
			{
				this._obj[i].hide();
				this._obj[i].ishidden == 1;
			}
			else
			{
				this._map.removeOverlay ( this._obj[i]);
				this._obj[i].isRemove = 1;
			}
			
		}
	},
	/**
	 * On verifie le zoom courant et on affiche/masque la couche
	 * On tiendra également compte de la case à cocher correspondante dans la légende.
	 * @param {Object} old_z ancien zoom
	 * @param {Object} new_z nouveau zoom
	 */
	//Pour l'instant, checkZoom ne prends pas en compte le niveau de zoom
	checkZoom : function ( old_z, new_z )
	{
		if ( this._name == "departements" )
		{
			if ( this._min_max == 'max' )
			{
				if ( new_z >= this._zoom )
				{
					this.hideAll ();
				}
				else
				{
					if ( jQuery("#legende_body input[code='F']").attr('checked') )
					{
						this.showAll ();
					}				
				}
			}
		}
		/*if ( this._min_max == 'max' )
		{
			if ( new_z >= this._zoom )
			{
				this.hideAll ();
			}
			else
			{
				if ( jQuery("#legende_body input[code='F']").attr('checked') )
				{
					this.showAll ();
				}				
			}
		}
		else
		{
			if ( new_z < this._zoom )
			{
				this.hideAll ();
			}
			else
			{
				if ( jQuery("#legende_body input[code='F']").attr('checked') )
				{
					this.showAll ();
				}				
			}
		}*/
	},
	/**
	 * Affiche tous les objets de la couche
	 */
	showAll : function ()
	{
		
		for (var key in this._obj)
		{
			if ( this._name=="departements")
			{
				if ( this._obj[key].ishidden == 0 )
				{
					this._obj[key].show ();
				}
			}
			else
			{
				if ( this._obj[key].isRemove == 0 )
				{
					this._obj[key].show ();
				}
				else
				{
					this._obj[key].isRemove = 0;
					this._map.addOverlay ( this._obj[key] );
					this._obj[key].show ();
				}
			}
			
		}
	},
	/**
	 * Masque tous les objets de la couche
	 */
	hideAll : function ()
	{
		for (var key in this._obj)
		{
			this._obj[key].hide ();
		}
	}
};


/**
 * Classe qui represente une association.
 * Elle permet de sdavoir si toutes les infos sont chargées
 * Elle gère le marker
 * Elle gère la bulle d'information
 * @param {Object} opt
 */

Association = function ( opt )
{
	this.init ( opt );
};
Association.prototype = 
{
	statut : -1,
	a_code : '',
	a_nom : '',
	full_load : false,
	lat : 0,
	lng : 0,
	arr_acti : [],
	lib_activite : {},

	init: function(opt)
	{
		jQuery.extend(this, opt);
		this.arr_acti = new Array ();
		this.lib_activite = new Object ();
		
		this.lib_activite['1'] = "Culturel, expression";
		this.lib_activite['2'] = "Sportives et de plein-air";
		this.lib_activite['3'] = "R&eacute;cr&eacute;atives et festives";
		this.lib_activite['4'] = "&Eacute;ducatives, sociales";
		this.lib_activite['5'] = "Services";
		this.lib_activite['6'] = "Divers";
		
		if ( this.a_code.substring(0,1) == "F" )
		{
			this.statut = "F";
		}
		if ( this.a_code.substring(0,1) == "D" )
		{
			this.statut = "D";
		}
		if ( this.a_code.substring(0,2) == "CD" )
		{
			this.statut = "CD";
		}
		if ( this.a_code.substring(0,2) == "CR" )
		{
			this.statut = "CR";
		}
		if ( this.a_code.substring(0,1) == "R" )
		{
			this.statut = "R";
		}
		if ( this.a_code.substring(0,1) == "N" )
		{
			this.statut = "N";
		}
		
		this.createMarker ();
	},
	/**
	 * Creation du marker represantant l'association
	 */
	createMarker : function ()
	{
		var ic = icon_asso[this.statut];
		var m = new GMarker ( new GLatLng ( parseFloat(this.lat), parseFloat (this.lng) ), {icon:ic, title:this.a_nom} );
		
		map_layer_asso[this.statut].add ( m, this.a_code );
		
		
		GEvent.bind ( m, "click", this, GEvent.callback (this, this.openInfoWindow) );
		
		
	
	},
	
	/**
	 * Méthode qui créée le contenu de la bulle d'inforamtion évoluée : lightbox
	 * On n'affiche que les informations choisies dans Gestanet.
	 */
	getMoreInfoContent : function ()
	{
		if ( !this.full_load )
		{
			var that = this;
			this.full_load = true;
			var data = {};
			data['a_code'] = this.a_code;
			data['georef_asso'] = this.georef_asso;
			data['georef_president'] = this.georef_president;
			data['georef_contact'] = this.georef_contact;
			data['georef_activite'] = this.georef_activite;
			
			jQuery.ajax({
	            type: "POST",
	            url: prefix_php + "php/loadMoreInfoAsso.php",
	            data: data,
				dataType:'xml',
	            success: function(xml)
				{
					jQuery(xml).find("asso").each ( function()
					{
						if ( that.georef_president == 1 )
						{
							that.a_preztitre = jQuery(this).attr ('a_preztitre');
							that.a_preznom = parsefromXML(jQuery(this).attr ('a_preznom'));
							that.a_prezprenom = parsefromXML(jQuery(this).attr ('a_prezprenom'));
							that.a_prezadr = parsefromXML(jQuery(this).attr ('a_prezadr'));
							that.a_prezcp = jQuery(this).attr ('a_prezcp');
							that.a_prezville = parsefromXML(jQuery(this).attr ('a_prezville'));
							that.a_preztel = jQuery(this).attr ('a_preztel');
							that.a_preztel2 = jQuery(this).attr ('a_preztel2');
							that.a_prezmel = jQuery(this).attr ('a_prezmel');
						}
						
						if ( that.georef_contact == 1 )
						{
							that.a_resptitre = jQuery(this).attr ('a_resptitre');
							that.a_respnom = parsefromXML(jQuery(this).attr ('a_respnom'));
							that.a_respprenom = parsefromXML(jQuery(this).attr ('a_respprenom'));
							that.a_respfonc = parsefromXML(jQuery(this).attr ('a_respfonc'));
							that.a_respadr = parsefromXML(jQuery(this).attr ('a_respadr'));
							that.a_respcp = jQuery(this).attr ('a_respcp');
							that.a_respville = parsefromXML(jQuery(this).attr ('a_respville'));
							that.a_resptel = jQuery(this).attr ('a_resptel');
							that.a_resptel2 = jQuery(this).attr ('a_resptel2');
							that.a_respmel = jQuery(this).attr ('a_respmel');
						}
						
						if ( that.georef_activite == 1 )
						{
							jQuery(this).find("acti").each ( function ()
							{
								that.arr_acti.push({
									lib: parsefromXML(jQuery(this).attr('pr_nom')),
									parent: jQuery(this).attr('parent')
								});
							});
						}
						
						
					});

						
					
					that.getMoreInfoContent();
	            },
				error:function (XMLHttpRequest, textStatus, errorThrown)
				{
					alert(textStatus); 
				}
	        });
		}
		/**
		 * Les informations sont en mémoire, il convient de les formater.
		 */
		else
		{
			//console.log ( this );
			this.fillDialogInfo ();
		}
		
	},
	
	
	/**
	 * Méthode chargée de remplir la fiche d'info détaillée.
	 */
	fillDialogInfo : function ()
	{
		//console.log ( this );
		jQuery("#asso_title").html ( this.a_nom );		
		
		jQuery("#asso_chez").html ( this.a_chez );
		jQuery("#asso_adresse").html ( this.a_adresse );
		jQuery("#asso_cod_ville").html ( this.a_codpos +" "+ this.a_commune );
		if ( this.a_tel == "" )
		{
			jQuery("#asso_tel").css ('display', 'none');
		}
		else
		{
			jQuery("#asso_tel").css ('display', 'block');
			jQuery("#asso_tel_val").html ( this.a_tel );
		}
		if ( this.a_fax == "" )
		{
			jQuery("#asso_fax").css ('display', 'none');
		}
		else
		{
			jQuery("#asso_fax").css ('display', 'block');
			jQuery("#asso_fax_val").html ( this.a_fax );
		}
		if ( this.a_mel == "" )
		{
			jQuery("#asso_email").css ('display', 'none');
		}
		else
		{
			jQuery("#asso_email").css ('display', 'block');
			jQuery("#asso_email a").html ( this.a_mel );
			jQuery("#asso_email a").attr("href", "mailto:"+ this.a_mel);
		}
		
		
		jQuery("#asso_web a").html ( this.a_web );
		jQuery("#asso_web a").attr("href", "http://"+ this.a_web);
							
		if ( this.georef_president == 1 )
		{
			jQuery("#block_president").css ('display', 'block');
			jQuery("#president_adresse1").html ( this.a_preznom + " " + this.a_prezprenom );
			jQuery("#president_adresse2").html ( this.a_prezadr );
			jQuery("#president_cod_ville").html ( this.a_prezcp + " " + this.a_prezville );
			if ( this.a_preztel == "" )
			{
				jQuery("#president_tel").css ('display', 'none');
			}
			else
			{
				jQuery("#president_tel").css ('display', 'block');
				jQuery("#president_tel_val").html ( this.a_preztel );
			}
			if ( this.a_preztel2 == "" )
			{
				jQuery("#president_portable").css ('display', 'none');
			}
			else
			{
				jQuery("#president_portable").css ('display', 'block');
				jQuery("#president_portable_val").html ( this.a_preztel2 );
			}
			if ( this.a_prezmel == "" )
			{
				jQuery("#president_email").css ('display', 'none');
			}
			else
			{
				jQuery("#president_email").css ('display', 'block');
				jQuery("#president_email a").html ( this.a_prezmel );
				jQuery("#president_email a").attr("href", "mailto:"+ this.a_prezmel);
			}
			
			
		}
		else
		{
			jQuery("#block_president").css ('display', 'none');
		}
		
		if ( this.georef_contact == 1 )
		{
			jQuery("#block_contact").css ('display', 'block');
			jQuery("#contact_adresse1").html ( this.a_respnom + " " + this.a_respprenom );
			jQuery("#contact_adresse2").html ( this.a_respadr );
			jQuery("#contact_cod_ville").html ( this.a_respcp + " " + this.a_respville );
			if ( this.a_resptel == "" )
			{
				jQuery("#contact_tel").css ('display', 'none');
			}
			else
			{
				jQuery("#contact_tel").css ('display', 'block');
				jQuery("#contact_tel_val").html ( this.a_resptel );
			}
			if ( this.a_resptel2 == "" )
			{
				jQuery("#contact_portable").css ('display', 'none');
			}
			else
			{
				jQuery("#contact_portable").css ('display', 'block');
				jQuery("#contact_portable_val").html ( this.a_resptel2 );
			}
			if ( this.a_respmel == "" )
			{
				jQuery("#contact_email").css ('display', 'none');
			}
			else
			{
				jQuery("#contact_email").css ('display', 'block');
				jQuery("#contact_email a").html ( this.a_respmel );
				jQuery("#contact_email a").attr("href", "mailto:"+ this.a_respmel);
			}
			
		}	
		else
		{
			jQuery("#block_contact").css ('display', 'none');
		}	
		
		if ( this.georef_activite == 1 && this.arr_acti.length > 0)
		{
			jQuery("#no_activites").css ('display', 'none');
			jQuery("#type_acti_1").css ('display', 'none');
			jQuery("#block_acti_1").css ('display', 'none');
			jQuery("#block_acti_1").html ('');
			
			jQuery("#type_acti_2").css ('display', 'none');
			jQuery("#block_acti_2").css ('display', 'none');
			jQuery("#block_acti_2").html ('');
			
			jQuery("#type_acti_3").css ('display', 'none');
			jQuery("#block_acti_3").css ('display', 'none');
			jQuery("#block_acti_3").html ('');
			
			jQuery("#type_acti_4").css ('display', 'none');
			jQuery("#block_acti_4").css ('display', 'none');
			jQuery("#block_acti_4").html ('');
			
			jQuery("#type_acti_5").css ('display', 'none');
			jQuery("#block_acti_5").css ('display', 'none');
			jQuery("#block_acti_5").html ('');
			
			jQuery("#type_acti_6").css ('display', 'none');
			jQuery("#block_acti_6").css ('display', 'none');
			jQuery("#block_acti_6").html ('');
			
			jQuery("#block_activite").css ('display', 'block');
			
			var cpt = 0;
			for ( var i =0; i<this.arr_acti.length; i++)
			{
				var o = this.arr_acti[i];
				if ( o.parent == 0 )
				{
					cpt++;
				}
				jQuery("#type_acti_"+o.parent).css ('display', 'block');
				jQuery("#block_acti_"+o.parent).css ('display', 'block');
				
				jQuery("#block_acti_"+o.parent).append (o.lib + "<br/>");
			}
			if ( cpt == this.arr_acti.length )
			{
				jQuery("#no_activites").css ('display', 'block');
			}
		}
		else
		{
			jQuery("#type_acti_1").css ('display', 'none');
			jQuery("#block_acti_1").css ('display', 'none');
			jQuery("#block_acti_1").html ('');
			
			jQuery("#type_acti_2").css ('display', 'none');
			jQuery("#block_acti_2").css ('display', 'none');
			jQuery("#block_acti_2").html ('');
			
			jQuery("#type_acti_3").css ('display', 'none');
			jQuery("#block_acti_3").css ('display', 'none');
			jQuery("#block_acti_3").html ('');
			
			jQuery("#type_acti_4").css ('display', 'none');
			jQuery("#block_acti_4").css ('display', 'none');
			jQuery("#block_acti_4").html ('');
			
			jQuery("#type_acti_5").css ('display', 'none');
			jQuery("#block_acti_5").css ('display', 'none');
			jQuery("#block_acti_5").html ('');
			
			jQuery("#type_acti_6").css ('display', 'none');
			jQuery("#block_acti_6").css ('display', 'none');
			jQuery("#block_acti_6").html ('');
			jQuery("#block_activite").css ('display', 'block');
			jQuery("#no_activites").css ('display', 'block');
		}
		
		openDlgInfo();
	},
	/**
	 * Méthode qui créée le contenu de la bulle d'information simple.
	 */
	getInfoContent : function ()
	{
		var h = '<div class="bubble">';
		h += '<h3>'+ this.a_nom +'</h3>';
		
		if ( this.a_chez != "" )
		{
			h += '<div>'+this.a_chez + '</div>';
		}
		
		h += '<div>'+this.a_adresse+'</div>';
		h += '<div>'+this.a_codpos+'&nbsp;' + this.a_commune + '</div>';
		
		if ( this.a_tel != "" )
		{
			h += '<div><span class="">Tel :</span>&nbsp;'+this.a_tel + '</div>';
		}
		if ( this.a_fax != "" )
		{
			h += '<div><span class="">Fax :</span>&nbsp;'+this.a_fax + '</div>';
		}
		if ( this.a_mel != "" )
		{
			h += '<div><span class="">Mail :</span>&nbsp;<a href="mailto:'+this.a_mel + '">'+this.a_mel + '</a></div>';
		}
		if ( this.a_web != "" )
		{
			h += '<div><span class="">Web :</span>&nbsp;<a target="_blank" href="http://'+this.a_web + '">'+this.a_web + '</a></div>';
		}

		h += '<div id="more_info" class=""><a href="javascript:map_asso[\''+ this.a_code +'\'].getMoreInfoContent();">Plus d\'information</a></div>';
		
		h += '<div id="routing" class=""><a href="javascript:showRouting(map_asso[\''+this.a_code+'\']);">Comment y aller</a></div>';
		
		
		h += '</div>';
		return h;
	},
	
	/**
	 * Méthode qui permet l'ouverture de la bulle d'information de l'association.
	 */
	openInfoWindow : function ()
	{
		var m = map_layer_asso[this.statut].get ( this.a_code );
		
		
		//Sans tabs
		m.openInfoWindowHtml ( this.getInfoContent () );
		
		//Avec des tabs
		/*var h = '';
		h += '<div>Saisissez votre adresse : </div>';
		h += '<div><input type="text" id="bubble_adress_routing" /><input type="button" value="OK"  onclick="showRouting(map_asso[\''+this.a_code+'\'])"/></div>';
		
		var infoTabs = [
	  		new GInfoWindowTab ( "Info", this.getInfoContent () ),
			new GInfoWindowTab ( "Comment y aller", h )
		];
		m.openInfoWindowTabsHtml(infoTabs, {maxWidth:400});*/
		
		
		jQuery("#list-result li a").each ( function () 
		{
			jQuery(this).removeClass('selected');
			jQuery(this).addClass('unselected');
		} );
		
		jQuery("li[id='element_" + this.a_code + "'] a").removeClass('unselected');
		jQuery("li[id='element_" + this.a_code + "'] a").addClass('selected');
		
		
		
		
		
	}
};
