﻿/**
 * @author apellet
 */

var map = null;
var map_cat = null;
var map_item = null;

//Function qui charge la carte au démarrage de la page
function loadMap ()
{
	if (GBrowserIsCompatible()) 
  	{
		
        map = new GMap2(document.getElementById("map"));
		
		map.addControl(new GLargeMapControl3D());
		map.addControl(new GMapTypeControl());
		map.addControl(new GOverviewMapControl()); 
		
		map.enableContinuousZoom() ;
		map.enableScrollWheelZoom();
		map.setCenter(new GLatLng(46,2),6);
	
	}
	map_cat = new Object ();
	map_item = new Object ();
	loadAllCatList (  );
}


function parseFromXML(xmlStr) 
{
	xmlStr = xmlStr.replace(/&lt;/g,'<'); // 60 3C
	xmlStr = xmlStr.replace(/&gt;/g,'>'); // 62 3E
	xmlStr = xmlStr.replace(/&quot;/g,'"'); // 62 3E
	xmlStr = xmlStr.replace(/&#39;/g,"'"); // 62 3E
	xmlStr = xmlStr.replace(/&amp;/g,'#'); // 62 3E

	xmlStr = xmlStr.replace(/\\\\'/g, "'");
	xmlStr = xmlStr.replace(/\\'/g, "'");

	return xmlStr; 
} 

//Function qui charge toutes les catégories de la base de données et qui les affiche.
function loadAllCatList (  )
{
	jQuery.ajax({
        type: "POST",
        url: "map1/php/loadListCat6.php",
        data: {id:-1},
		dataType:'xml',
        success: function(xml)
		{
			jQuery(xml).find("cat").each ( function()
			{
				var opt = {};
				opt['marker'] = (jQuery(this).attr('marker'));
				opt['id'] = (jQuery(this).attr('idt'));
				opt['type'] = parseFromXML(jQuery(this).attr('type'));
				
				var cat = new Categorie ( opt );
				
				map_cat[opt['id']] = cat;
				
				$("#allCat").append ('<div style="margin-left:5px;"><span class="lienagenda"><table width="205" border="0" cellspacing="0" cellpadding="0"><tr><td width="45"><a href="javascript:map_cat['+opt["id"]+'].loadItem()"><img style="border:0px;" src="./markers/'+opt['marker']+'" /></a></td><td><a href="javascript:map_cat['+opt["id"]+'].loadItem()"> '+opt['type']+'</a></td></tr></table></span></div><br>');
				
				
			});
				
			
			
        },
		error:function (XMLHttpRequest, textStatus, errorThrown)
		{
			alert(textStatus + " loadAllCatList \n"+ errorThrown); 
		}
    });
}


function loadAllItems ()
{
	map.clearOverlays ();
	
	var bounds = new GLatLngBounds ();
	
	$.ajax({
			    type: "POST",
				data : {id_cat:-1},
			    url: "./map1/php/loadItemCat1.php",
				dataType: "xml",
			    success: function(xml)
				{			
					
					jQuery(xml).find("item").each ( function()
					{
						var opt = {};
						opt['id'] = (jQuery(this).attr('ids'));
						opt['cat'] = (jQuery(this).attr('cat'));
						opt['lat'] = parseFloat(jQuery(this).attr('lat'));
						opt['lng'] = (jQuery(this).attr('longi'));
						opt['nom'] = parseFromXML(jQuery(this).attr('nom'));

						opt['adresse'] = parseFromXML(jQuery(this).attr('adresse'));
						opt['cp'] = parseFromXML(jQuery(this).attr('cp'));
						opt['ville'] = parseFromXML(jQuery(this).attr('ville'));
						opt['tel'] = parseFromXML(jQuery(this).attr('tel'));
						opt['email'] = parseFromXML(jQuery(this).attr('email'));
						opt['site'] = parseFromXML(jQuery(this).attr('site'));
						opt['presentation'] = parseFromXML(jQuery(this).attr('presentation'));
						opt['description'] = parseFromXML(jQuery(this).attr('description'));
						opt['image'] = parseFromXML(jQuery(this).attr('image'));

						
						
						
						var latlng = new GLatLng ( parseFloat (opt['lat']), parseFloat (opt['lng'] ));
						var m = new GMarker ( latlng, {icon:map_cat[opt['cat']].icon, title:opt['nom']} );
						
						GEvent.addListener ( m, "mouseover", function (){ map_item[opt['id']].openInfo (); } )
						
						bounds.extend ( latlng );
						map.addOverlay ( m );
						
						map_cat[opt['cat']].hashmap[opt['id']] = m;
						map_cat[opt['cat']].loaded = true;
						
						
						
						opt['m'] = m;
						var item = new Item ( opt );
						map_item[opt['id']] = item;
					});
					
					map.setCenter ( bounds.getCenter (), map.getBoundsZoomLevel ( bounds )-1 );
					
				},
				
				error:function (XMLHttpRequest, textStatus, errorThrown)
				{
					alert(textStatus + " loadItem"); // the options for this ajax request
					
				}
			});
}


