/**
 * @author apellet
 */

Item = function ( opt )
{
	this.init ( opt );
};
Item.prototype = {
	opt: null,
	id: -1,	
	m : null,
	
	init: function(opt)
	{
		this.opt = opt;		
		jQuery.extend(this, opt);		
	},
	openInfo : function ()
	{
		var par = "";
		var img = new Image();
		img.src = './photos/' + this.image;
		
		if ( img.width > img.height )
		{
			par = "width:70px;";
		}
		else
		{
			par = "height:50px;";
		}
		
		
		
		var h = "<div style='width:300px;height:200px'><div>" + "<span style='font-weight:bold;color:#2586fb;font-size:12px;'>" + this.nom + "</span><br/><span class='Style5'>" + this.adresse + "<br/>" + this.cp + " " + this.ville + "<br/>" + this.tel + "<br/>" + this.email + "<br/>" + this.site + "</span></div>";
		h += "<div style='margin-top:5px;'><table><tr><td><span style='text-decoration:underline;font-size:12px;'>Presentation</span> :<br/>" + "<span class='Style5'>" + this.description + "</span>";
		h +="</td><td><img style='"+par+"' src='./photos/" + this.image + "'/></td></tr></table></div>";
		h += '<a style="font-weight:bold;font-size:12px;" href="javascript:showItemThickbox('+ this.id +')">Plus de d&eacute;tail</a></div>';
		//alert(this.image);
		
		this.m.openInfoWindowHtml ( h );
	}
}

Categorie = function ( opt )
{	
	this.init ( opt );
};
Categorie.prototype = {
	opt: null,
	id: -1,
	icon : '',
	marker: '',
	type: '',
	hashmap : null,
	loaded : false,
	bounds : null,
	
	init: function(opt)
	{
		this.opt = opt;
		this.hashmap = new Object ();
		this.bounds = new GLatLngBounds();
		jQuery.extend(this, opt);
		this.createIcon();
	},
	createIcon : function()
	{		
		this.icon = new GIcon ();
		this.icon.iconSize = new GSize ( 40, 40 );
		this.icon.shadowSize = new GSize (40,40);
		this.icon.iconAnchor = new GPoint  ( 12, 33 );
		this.icon.infoWindowAnchor = new GPoint  ( 12, 0 );
		this.icon.image = "./map/markers/" + this.marker;
		this.icon.shadow = "./map/markers/shadow.png";	
	},
	loadItem : function ()
	{
		var that = this;
		var id = this.id;
		
		map.clearOverlays() ;
		
		if ( this.loaded )
		{
			for ( var i in that.hashmap)
			{
				var o  = that.hashmap[i];
				map.addOverlay ( o );
				that.bounds.extend ( o.getLatLng() );
			}
			map.setCenter ( that.bounds.getCenter (), map.getBoundsZoomLevel ( that.bounds )-1 );
		}
		else
		{
			$.ajax({
			    type: "POST",
				data : {id_cat:id},
			    url: "./map/php/loadItemCat.php",
				dataType: "xml",
			    success: function(xml)
				{			
					
					jQuery(xml).find("item").each ( function()
					{
						var opt = {};
						opt['id'] = (jQuery(this).attr('ids'));
						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:that.icon, title:opt['nom']} );
						
						GEvent.addListener ( m, "mouseover", function (){ map_item[opt['id']].openInfo (); } )
						
						that.bounds.extend ( latlng );
						map.addOverlay ( m );
						
						that.hashmap[opt['id']] = m;
						map.setCenter ( that.bounds.getCenter (), map.getBoundsZoomLevel ( that.bounds )-1 );
						
						opt['m'] = m;
						var item = new Item ( opt );
						map_item[opt['id']] = item;
					});
					
					
					that.loaded = true;
				},
				
				error:function (XMLHttpRequest, textStatus, errorThrown)
				{
					alert(textStatus + " loadItem"); // the options for this ajax request
					
				}
			});
		}
		
		
	}
	
	
};



