/*
* PLAN DE VILLE : CHATOU
* Version 1.0
* Creation date: Fevrier 2010
* Author: Dynamik Informatik - PELLET Alexandre - www.dynamik-informatik.fr
* Author email: pellet.alexandre@gmail.com
*/

var all_picto = {};
var all_street = {};
var all_cat = {};   
var all_place = {};  

var icon_start_street = null;
var icon_end_street = null;

var geocoder = null;

var navControl = null;

var map = null;

var overview = null;

var start_lat = null;

/**
 * Initialisation de la carte Google Maps et de ses composants.
 * @param {Object} latlng
 * @param {Object} zoom
 * @param {Object} wheel
 * @param {Object} over
 */
function initMap ( latlng, zoom )
{
	if ( GBrowserIsCompatible() )
	{
		
		
		map = new GMap2 ( document.getElementById ('map') );
		map.setCenter ( latlng, zoom );
		geocoder = new GClientGeocoder ();
		start_lat = latlng;
	
		
	
		map.enableScrollWheelZoom() ;
	
		overview =  new GOverviewMapControl ();
		map.addControl (overview );
		
		var pan_control = new PanControl ( map, "./images/carto/control/", 25, 45 );
		map.addControl ( pan_control );		
		
		//Barre de zoom
		jQuery('#mapzoomcontrol').gmap_zoom( {  
			map: map,
			b_center:false,
			size : 25,
			prefix_path:'',
			rep_img:'./styles/controls/images/'
		});
		
		//Type de plan
		jQuery('#maptypecontrol').gmap_type( {  
			map: map,  
			types: [0,1,3],
			type:1,
			f_size:10
		});
		
		
		
			//	map.setMapType ( G_PHYSICAL_MAP );				
		
		
		
		map.addControl ( new NavigationControl ( map )  );
		loadIconStreet();
		
		loadStreet ();
		loadCategory ();
		
		initTemplates ();
		/*el_tooltip = document.createElement("div");
		el_tooltip.id = "map-tooltip";
		el_tooltip.className = "tooltip";
		document.getElementById("map").appendChild(el_tooltip);
		
		jQuery ( "#map-tooltip" ).css ("width", '100px');
		jQuery ( "#map-tooltip" ).css ("height", '100px');*/
	}
}


function drawEmpriseMap2 ()
			{
				map2.clearOverlays();
				var lats = new Array ();
				var b = map.getBounds();
				var so = ( b.getSouthWest() );
				var ne = ( b.getNorthEast() );
				
				lats.push ( so );
				lats.push ( new GLatLng(so.lat(), ne.lng()));				
				lats.push ( ne );
				lats.push ( new GLatLng(ne.lat(), so.lng()));
				lats.push ( so );				
				
				
				var poly = new GPolygon(lats);
				poly.setStrokeStyle ( { weight:2 } );
				map2.addOverlay ( poly );
			}

/**
 * Fonction d'initialisation des icons utilisés sur la carte
 */
function loadIcons(){

	var icon_basic = new GIcon();
	icon_basic.iconSize = new GSize(26, 26);
	icon_basic.shadowSize = new GSize(26, 40);
	icon_basic.iconAnchor = new GPoint(13, 25);
	icon_basic.infoWindowAnchor = new GPoint(13, 0);
	
	icon_annonce = new GIcon(icon_basic, "./images/carto/annonce.png");
	
	for (var i in all_cat) {
		var c = all_cat[i];
		
		all_picto[c.id] = new GIcon(icon_basic, "./images/carto/picto_" + c.id + ".png");
		//GLog.write ( "./images/carto/picto_" + c.id + ".png");
		
	}
	
	
}
function loadIconStreet()
{
	var icon_basic_street = new GIcon ();
	icon_basic_street.iconSize = new GSize ( 32, 32 );
	icon_basic_street.shadowSize = new GSize (32,40);
	icon_basic_street.iconAnchor = new GPoint  ( 16, 31 );
	icon_basic_street.infoWindowAnchor = new GPoint  ( 16, 0 );
	
	icon_start_street = new GIcon( icon_basic_street, "./images/carto/start_street.png" );	
	icon_end_street = new GIcon( icon_basic_street, "./images/carto/end_street.png" );
	
}


 
   
function loadStreet ( )
{
	jQuery.ajax
		({
			type: "GET",
			url: "./data/rues.xml",
			dataType:'xml',
			success: function(obj)
			{
				jQuery(obj).find("rue").each ( function ()
				{
					var rue = {};
					rue.id = jQuery(this).attr('id');
					rue.libelle = jQuery(this).attr('lib');
					rue.tri = jQuery(this).attr('tri');
					rue.dep = jQuery(this).attr('dep');
					rue.fin = jQuery(this).attr('fin');
					rue.lat_d = parseFloat(jQuery(this).attr('lat_d'));
					rue.lng_d = parseFloat(jQuery(this).attr('lng_d'));
					rue.lat_f = parseFloat(jQuery(this).attr('lat_f'));
					rue.lng_f = parseFloat(jQuery(this).attr('lng_f'));
					
					rue.latlng_d = new GLatLng ( rue.lat_d, rue.lng_d );
					rue.latlng_f = new GLatLng ( rue.lat_f, rue.lng_f );
					rue.startPlace = new GMarker ( rue.latlng_d, {icon:icon_start_street} );
					rue.endPlace = new GMarker ( rue.latlng_f, {icon:icon_end_street} );
					
					all_street [rue.id] = rue;
				});
				fillSelectStreet ();
				initAutocomplete ( 0 );
			},
			error:function (XMLHttpRequest, textStatus, errorThrown)
			{
				alert(textStatus);
			}
		});	
}


function loadCategory ( )
{
	jQuery.ajax
		({
			type: "GET",
			url: "./data/cat.xml",
			dataType:'xml',
			success: function(obj)
			{
				jQuery(obj).find("cat_lieu").each ( function ()
				{
					var cat = {};
					cat.id = parseInt(jQuery(this).attr('id'));
					cat.libelle = jQuery(this).attr('lib');
					
					all_cat [cat.id] = cat;
				});
				
				
				loadIcons();
				loadPlace ();
			},
			error:function (XMLHttpRequest, textStatus, errorThrown)
			{
				alert(textStatus);
			}
		});	
}

function getCategory ( id_cat )
{
	return all_cat[id_cat].libelle;
}

function loadPlace ( )
{
	jQuery.ajax
		({
			type: "GET",
			url: "./data/lieux.xml",
			dataType:'xml',
			success: function(obj)
			{
				jQuery(obj).find("lieu").each ( function ()
				{
					var lieu = {};
					lieu.id = jQuery(this).attr('id');
					lieu.id_cat = parseInt(jQuery(this).attr('id_cat'));
					lieu.libelle = jQuery(this).attr('lib');
					lieu.adr = jQuery(this).attr('adr').replace("|","<br>");
					//alert(lieu.adr);
					lieu.tel = jQuery(this).attr('tel');
					lieu.ico = jQuery(this).attr('ico');
					lieu.img = "./images/photos/"+jQuery(this).attr('img');
					lieu.web = jQuery(this).attr('web');
					lieu.mail = jQuery(this).attr('mail');
					lieu.cp = "78400";
					lieu.ville = "Chatou";
					
					
					var img = new Image ();
					img.src = lieu.img;
					lieu.obj_img = img;
					
					lieu.lat = parseFloat(jQuery(this).attr('lat'));
					lieu.lng = parseFloat(jQuery(this).attr('lng'));
					
					lieu.latlng = new GLatLng ( lieu.lat, lieu.lng );
					lieu.marker = new GMarker ( lieu.latlng, {icon:all_picto[lieu.id_cat], title:getCategory(lieu.id_cat) + " - " + lieu.libelle} );
					
					GEvent.bind ( lieu.marker, "click", window, GEvent.callbackArgs(window, openInfoPlace, lieu.id) );
					
					all_place [lieu.id] = lieu;
				});
				
				fillSelectPlace ();
				createAccordion ();
			},
			error:function (XMLHttpRequest, textStatus, errorThrown)
			{
				alert(textStatus);
			}
		});	
}

function fillSelectStreet ()
{
	//clearSelect ('select_street');
	jQuery('#select_street').append(jQuery("<option></option>").attr("value","-1").text("Votre choix..."));

	for ( var i in all_street )
	{
		var o = all_street[i];
		jQuery('#select_street').append(jQuery("<option></option>").attr("value",o.id).text(o.libelle));
	}			
}
function fillSelectPlace ()
{
	//clearSelect ('select_place');
	jQuery('#select_place').append(jQuery("<option></option>").attr("value","-1").text("Votre choix..."));

	for ( var i in all_place )
	{
		var o = all_place[i];
		jQuery('#select_place').append(jQuery("<option></option>").attr("value",o.id).text(o.libelle));
	}			
}

function clearMap ()
{
	map.clearOverlays ();
}
function geolocStreet ( id_street )
{
	clearMap ();
	map.addOverlay ( all_street [ id_street ].startPlace );
	map.addOverlay ( all_street [ id_street ].endPlace );
	
	var bounds = new GLatLngBounds ( );
	bounds.extend ( all_street [ id_street ].latlng_d );
	bounds.extend ( all_street [ id_street ].latlng_f );
	
	map.panTo ( bounds.getCenter () );
}

function geolocPlace ( id_place )
{
	clearMap ();
	//navControl.hide();
	map.addOverlay ( all_place [ id_place ].marker );
	
	map.setCenter ( all_place [ id_place ].latlng );
	openInfoPlace ( id_place );
}



		

function openInfoPlace ( id_place )
{
	var o = all_place[id_place];
	
	
	var t = jQuery('#bubble-hidden').html();
		
	jQuery('#bubble-temp').html(jQuery('#bubble-hidden').html());		
	jQuery('#bubble-hidden').html('');
	
	
	
	
	if ( o.obj_img.width > 0 )
	{
		jQuery('#block-image').css("width", /*img.width+*/"150px");
		jQuery('#block-image').css("height", /*img.height+*/"120px");
		jQuery ('#bubble-temp #bubble-place  img').attr ('src', o.img );
	}
	else
	{
		jQuery('#block-image').css("height", "1px");
		jQuery('#block-image').css("width", "1px");
		jQuery ('#bubble-temp #bubble-place  img').attr ('src', './images/1px.png' );
	}
	
	
	jQuery ('#bubble-temp #bubble-place div[class=title-bulle-monument]').html ( o.libelle );
	jQuery ('#bubble-temp #bubble-place div[class=adresse]').html ( o.adr );
	jQuery ('#bubble-temp #bubble-place div[class=cp]').html ( o.cp + " " + o.ville );
	
	if ( o.tel != "" )
	{
		jQuery ('#bubble-temp #bubble-place div[class=tel]').html ( o.tel );
	}
	else
	{
		jQuery ('#bubble-temp #bubble-place div[class=tel]').css('display','none');
	}
	
	if ( o.mail != "" )
	{
		jQuery ('#bubble-temp #bubble-place div[class=mail] a').html ( o.mail );
		jQuery ('#bubble-temp #bubble-place div[class=mail] a').attr ( 'href', 'mailto:'+ o.mail );
	}
	else
	{
		jQuery ('#bubble-temp #bubble-place div[class=mail]').css('display','none');
	}
	
	if ( o.web != "" )
	{
		jQuery ('#bubble-temp #bubble-place div[class=web] a').html ( o.web );
		jQuery ('#bubble-temp #bubble-place div[class=web] a').attr ( 'href',  'http://'+o.web );
	}
	else
	{
		jQuery ('#bubble-temp #bubble-place div[class=web]').css('display','none');
	}
	
	
	
	
	
	jQuery('#bubble-hidden').html(t);
	
	var h = jQuery ('#bubble-temp').html ();	
	//alert (o.img);
	//var h = tmpl_info_monument.apply ( o );
	
	//console.log (o);
	
	o.marker.openInfoWindowHtml ( h );
	//map.panTo ( all_place [ id_place ].latlng );
	//navControl.show();
	setTimeout ( function (){  }, 500 );
}

function createAccordion ()
{
	for ( var i in all_cat )
	{
		var cat = all_cat [i];
		
		cat.short_lib = cat.libelle;
		if ( cat.libelle.length > 27 )
		{
			cat.short_lib = cat.libelle.substr(0,25) + "...";
		}
		
		
		jQuery('#accordion').append ('<div alt="' + cat.libelle + '" title="' + cat.libelle + '">' + cat.short_lib + '</div>');
		
		var block = '<div>';
		//jQuery('#accordion').append ('<div>');		
		for ( var j in all_place )
		{
			var place = all_place [j];
			if (place.id_cat == cat.id) 
			{
				block += '<div class="block-item"><a href="javascript:geolocPlace ('+ place.id + ')">' + place.libelle + '</a></div>';
			}	
				
		}
		block += 		'</div>';
		jQuery('#accordion').append (block);
	}
	jQuery('#accordion').dynAccordion();

}

function geolocResult ()
{
	var type = parseInt ( jQuery('input[type=radio][name=Btrecherche]:checked').attr('value') );
	var id_obj = jQuery('#recherche_'+type).attr('id_obj');
	switch (type)
	{
		case 0:
			geolocStreet ( id_obj );
		break;
		case 1 :
			geolocPlace ( id_obj );
		break;
	}
}


function initAutocomplete ( type )
{
	jQuery ('#recherche_'+type).val('');
	
	switch ( parseInt (type) )
	{
		case 0:
		jQuery ('#recherche_1').css('display', 'none');
		jQuery ('#recherche_0').css('display', 'inline');

		jQuery("#recherche_0").autocomplete(all_street,{
			minChars: 0,
			matchContains: true,
			autoFill: false,
			
			/*Champ de recherche*/
			formatItem: function(row, i, max) {
				return row.tri;
			},
			/*Affichage du résultat*/
			formatResult: function(row) 
			{
				//console.log ( "formatResult %o",row );
				return row.libelle;
			},
			formatMatch: function(row, i, max) 
			{
				//console.log ( "formatMatch %o",row );
				return row.tri;
			}
		});
		break;
		
		case 1 : 
		jQuery ('#recherche_0').css('display', 'none');
		jQuery ('#recherche_1').css('display', 'inline');

		jQuery("#recherche_1").autocomplete(all_place,{
			minChars: 0,
			matchContains: true,
			autoFill: false,
			/*width:400,*/
			/*Champ de recherche*/
			formatItem: function(row, i, max) {
				return row.libelle;
			},
			/*Affichage du résultat*/
			formatResult: function(row) 
			{
				//console.log ( "formatResult %o",row );
				return row.libelle;
			},
			formatMatch: function(row, i, max) 
			{
				//console.log ( "formatMatch %o",row );
				return row.libelle;
			}
		});
		break;
		
	}
	
}

