/**
 * @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=="regions")
		{
			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=="regions")
			{
				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 == "regions" )
		{
			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=="regions")
			{
				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
 */

Acteur = function ( opt )
{
	this.init ( opt );
};
Acteur.prototype = 
{
	statut : -1,
	id : '',
	struct_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 ()
	{
		//console.log ( this );
		//var ic = icon_asso[this.statut];
		var ic=null;
		
		/*if ( this.service_etat == 0 )
			ic = icon_acteur;
		else
			ic = icon_etat;*/
		ic = icon_acteur;
			
		var m = new GMarker ( new GLatLng ( parseFloat(this.lat), parseFloat (this.lng) ), {/*icon:ic,*/ title:this.struct_nom} );//icon_acteur
		
		
		//map.addOverlay ( m );
		map_layer_acteur[this.statut].add ( m, this.id );
		
		
		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 = {id:this.id};
			//console.log ( this );
			
			jQuery.ajax({
	            type: "POST",
	            url: prefix_php + "php/__load_more_info_acteur.php",
	            data: data,
				dataType:'xml',
	            success: function(xml)
				{
					jQuery(xml).find("annuaire_acteur").each ( function()
					{
						that.region_depart = jQuery(this).attr('struct_region') + " / " + jQuery(this).attr('struct_departement'); 
						that.struct_logo = jQuery(this).attr('struct_logo');
						that.struct_date_creation = jQuery(this).attr('struct_date_creation');
						that.struct_label = parsefromXML(jQuery(this).attr('struct_label'));
						
						that.struct_all_forme_juridique = jQuery(this).attr('struct_all_forme_juridique');
						if ( that.struct_all_forme_juridique.lastIndexOf('|') ==  that.struct_all_forme_juridique.length -1 )
						{
							that.struct_all_forme_juridique = that.struct_all_forme_juridique.substr ( 0, that.struct_all_forme_juridique.length -1  );
						}						
						that.struct_all_forme_juridique = that.struct_all_forme_juridique.replace("|"," / ", "g");
						
						
						that.struct_all_mission = jQuery(this).attr('struct_all_mission');
						
						if ( that.struct_all_mission.lastIndexOf('|') ==  that.struct_all_mission.length -1 )
						{
							that.struct_all_mission = that.struct_all_mission.substr ( 0, that.struct_all_mission.length -1  );
						}						
						that.arr_struct_all_mission = that.struct_all_mission.split("|");
						that.struct_mission_lib = parsefromXML(jQuery(this).attr('struct_mission_lib'));
						if ( that.struct_mission_lib != "" )
						{
							that.arr_struct_all_mission.push ( that.struct_mission_lib );
						}
						
						
						
						that.resp_photo = jQuery(this).attr('resp_photo');
						that.resp_prenom_nom = jQuery(this).attr('resp_prenom') + " " + jQuery(this).attr('resp_nom');
						that.resp_telephone = jQuery(this).attr('resp_telephone');
						that.resp_mail = jQuery(this).attr('resp_mail');

						var resp_id_statut = parseInt(jQuery(this).attr('resp_id_statut'));
						if ( resp_id_statut == -1 )
							that.resp_statut = jQuery(this).attr('resp_lib_statut');
						else
							that.resp_statut = jQuery(this).attr('resp_statut_choix');
						
						
						that.struct_web = jQuery(this).attr('struct_web');
						that.struct_telecopie = jQuery(this).attr('struct_telecopie');
						
						
						that.struct_description = parsefromXML(jQuery(this).attr('struct_description'));
						that.struct_objectifs = parsefromXML(jQuery(this).attr('struct_objectifs'));
						that.struct_equipe = parsefromXML(jQuery(this).attr('struct_equipe'));
						that.struct_actions = parsefromXML(jQuery(this).attr('struct_actions'));
						that.struct_partenaires = parsefromXML(jQuery(this).attr('struct_partenaires'));

						that.reseau_soc_1 = jQuery(this).attr('reseau_soc_1');
						that.reseau_soc_2 = jQuery(this).attr('reseau_soc_2');
						that.reseau_soc_3 = jQuery(this).attr('reseau_soc_3');
						that.reseau_soc_4 = jQuery(this).attr('reseau_soc_4');

						
						
						
					});

						
					
					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 ()
	{
		////STRUCTURE
		//Titre de la structure
		jQuery('h1#struct_nom').html ( this.struct_nom );
		if ( this.struct_logo == "" )
			this.struct_logo = "./images/1px.gif";
		jQuery('img#struct_logo').attr('src', this.struct_logo );
		
		//Region/departement
		jQuery('p#region_depart').html ( this.region_depart );
		
		//Region/departement
		jQuery('p#region_depart').html ( this.region_depart );
		
		//Statut juridique
		jQuery('p#struct_juridique').html ( this.struct_all_forme_juridique );
		
		//Date de creation
		jQuery('p#struct_date_creation').html ( this.struct_date_creation );
		
		//Label
		//jQuery('p#struct_label').html ( this.struct_label );
		fillBlockInfo ( this.struct_label, "h2_label", 'p#struct_label' );		
					
					
		////RESPONSABLE
		jQuery('span#resp_prenom_nom').html ( this.resp_prenom_nom );
		jQuery('span#resp_statut').html ( this.resp_statut );
		jQuery('span#resp_telephone').html ( this.resp_telephone );
		jQuery('a#resp_mail').attr('href', 'mailto:' + this.resp_mail).html ( this.resp_mail );
		//photo
		if ( this.resp_photo == "" )
			this.resp_photo = "./images/1px.gif";
		jQuery('img#resp_photo').attr('src', this.resp_photo );
		
		
		
		
		jQuery('p#p_struct_nom').html ( this.struct_nom );
		if ( this.struct_web != "" )
		{
			jQuery('#td_snapshot').css('display', 'block');
	        jQuery('a#struct_web').attr('href', 'http://'+this.struct_web);
			jQuery('img#struct_snapshot').attr('src', 'http://www.apercite.fr/apercite/320x240/oui/oui/http://'+this.struct_web);	
			
			jQuery('a#struct_web_text').attr('href', 'http://'+this.struct_web);
			jQuery('a#struct_web_text').html ( 'http://'+this.struct_web );
					   
		}
		else
		{
			jQuery('#td_snapshot').css('display', 'none');
		}
				
		//RESEAUX SOCIAUX
		if ( this.reseau_soc_1 != "" )
		{
			jQuery ('#a_reseau_soc_1').attr ('href', this.reseau_soc_1);
			jQuery ('#a_reseau_soc_1').html ( this.reseau_soc_1);
			jQuery ('#d_reseau_soc_1').css ('display', 'block');
		}
		else
		{
			jQuery ('#d_reseau_soc_1').css ('display', 'none');
		}
		//
		if ( this.reseau_soc_2 != "" )
		{
			jQuery ('#a_reseau_soc_2').attr ('href', this.reseau_soc_2);
			jQuery ('#a_reseau_soc_2').html ( this.reseau_soc_2);
			jQuery ('#d_reseau_soc_2').css ('display', 'block');
		}
		else
		{
			jQuery ('#d_reseau_soc_2').css ('display', 'none');
		}
		//
		if ( this.reseau_soc_3 != "" )
		{
			jQuery ('#a_reseau_soc_3').attr ('href', this.reseau_soc_3);
			jQuery ('#a_reseau_soc_3').html ( this.reseau_soc_3);
			jQuery ('#d_reseau_soc_3').css ('display', 'block');
		}
		else
		{
			jQuery ('#d_reseau_soc_3').css ('display', 'none');
		}
		//
		if ( this.reseau_soc_4 != "" )
		{
			jQuery ('#a_reseau_soc_4').attr ('href', this.reseau_soc_4);
			jQuery ('#a_reseau_soc_4').html ( this.reseau_soc_4);
			jQuery ('#d_reseau_soc_4').css ('display', 'block');
		}
		else
		{
			jQuery ('#d_reseau_soc_4').css ('display', 'none');
		}
		
		/////DETAIL ACTEUR
		//Adresse
		jQuery('span#struct_adresse_1').html ( this.struct_adresse_1 );
		
		if (this.struct_adresse_2 != "")
		{
			jQuery('tr#tr_struct_adresse_2').css('display', 'block');
			jQuery('span#struct_adresse_2').html ( this.struct_adresse_2 );
		}	
		else
		{
			jQuery('tr#tr_struct_adresse_2').css('display', 'none');
		}
			
			
		jQuery('span#struct_code_postal').html ( this.struct_code_postal );
		jQuery('span#struct_ville').html ( this.struct_ville );
		jQuery('span#struct_telephone').html ( this.struct_telephone );
		if ( this.struct_telecopie != "" )
		{
			jQuery('tr#tr_struct_telecopie').css('display', 'block');
			jQuery('span#struct_telecopie').html ( this.struct_telecopie );
		}	
		else
		{
			jQuery('tr#tr_struct_telecopie').css('display', 'none');
		}
			
			
		jQuery('a#struct_mail').attr ( 'href', 'mailto:'+this.struct_mail ).html(this.struct_mail );
		
	
		
	
		//Description
		//jQuery('p#struct_description').html(this.struct_description);
		fillBlockInfo ( this.struct_description, "h2_description", 'p#struct_description' );
		
		
		//Mission
		//console.log ( that.arr_struct_all_mission );
		//
		jQuery('#struct_missions').empty();
		for ( var i=0; i<this.arr_struct_all_mission.length; i++ )
		{
			jQuery('#struct_missions').append(jQuery("<li></li>").html(this.arr_struct_all_mission[i]));
			
			
		}
		
		
		//Equipe
		//jQuery('p#struct_equipe').html ( this.struct_equipe );
		fillBlockInfo ( this.struct_equipe, "h2_equipe", 'p#struct_equipe' );

		//Objectifs
		//jQuery('p#struct_objectifs').html ( this.struct_objectifs );
		fillBlockInfo ( this.struct_objectifs, "h2_objectif", 'p#struct_objectifs' );

		//Actions
		//jQuery('p#struct_actions').html ( this.struct_actions );
		fillBlockInfo ( this.struct_actions, "h2_action", 'p#struct_actions' );

		//Partenaires
		//jQuery('p#struct_partenaires').html ( this.struct_partenaires );
		fillBlockInfo ( this.struct_partenaires, "h2_partenaire", 'p#struct_partenaires' );
		
		
		
		//Mise à jour de l'identifiant de l'acteur pour le formulaire de demande de modification
		jQuery("#frmModif>#acteur_id").val ( this.id );
		
		//console.log ( this);
		
		
		openDlgInfo();
	},
	/**
	 * Méthode qui créée le contenu de la bulle d'information simple.
	 */
	getInfoContent : function ()
	{
		var h = '<div style="margin-top:10px;" class="bubble">';
		h += '<h3>'+ this.struct_nom +'</h3>';
		
		
		
		h += '<div>'+this.struct_adresse_1 + " " + this.struct_adresse_2 + " " + this.struct_code_postal + " " + this.struct_ville + '</div>';
		
		h += "<p></p>";
		
		if ( this.struct_telephone != "" )
		{
			h += '<div><span class="">T&eacute;l :</span>&nbsp;'+this.struct_telephone + '</div>';
		}
		/*if ( this.a_fax != "" )
		{
			h += '<div><span class="">Fax :</span>&nbsp;'+this.a_fax + '</div>';
		}*/
		if ( this.struct_mail != "" )
		{
			h += '<div><span class="">@ :</span>&nbsp;<a href="mailto:'+this.struct_mail + '">'+this.struct_mail + '</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_acteur[\''+ this.id +'\'].getMoreInfoContent();">Plus d\'information</a></div>';
		
		h += '<div style="margin-bottom:10px;" id="routing" class=""><a href="javascript:showRouting(map_acteur[\''+this.id+'\']);">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 ()
	{
		//Temporaire, il faut le récupérer d'une collection
		//var m = this.m;
		var m = map_layer_acteur[this.statut].get ( this.id );
		
		
		//Sans tabs
		m.openInfoWindowHtml ( this.getInfoContent () );
		
		
		
		/*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');*/
		
		
		
		
		
	}
};

function fillBlockInfo ( val, titre_id, block_id )
{
	if ( val != "")
	{
		jQuery("#" + titre_id).css('display','block');
		jQuery(block_id).css('display','block');
		jQuery(block_id).html( val );
	}
	else
	{
		jQuery("#" + titre_id).css('display','none');	
		jQuery(block_id).css('display','none');
	}		
}

