﻿/**
 * @author apellet
 */
var adr_start = "";
var cur_item = 0;
var cur_nom = "";

function showItemThickbox ( id )
{
	//<input alt="#TB_inline?height=300&width=400&inlineId=myOnPageContent" title="add a caption to title attribute / or leave blank" class="thickbox" type="button" value="Show" />  
	//<a href="#TB_inline?height=155&width=300&inlineId=map&modal=true" class="thickbox">Show hidden modal content.</a>
	cur_item = id;
	$("#iti").css("display", "none" );
	$("#container_map").css("display", "block" );
	map.checkResize();
	
	$.ajax({
		    type: "POST",
			data : {id_item:id},
		    url: "./map/php/loadItem1.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('idt'));
					opt['lat'] = parseFloat(jQuery(this).attr('lat'));
					opt['lng'] = (jQuery(this).attr('longi'));
					opt['marker'] = (jQuery(this).attr('marker'));
					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'));

					
					//adr_start = opt['adresse'] + " " + opt['cp'] + " " + opt['ville'];
					adr_start = opt['lat'] + ", " + opt['lng']
					cur_nom = opt['nom'];
					
					
					var img = new Image();
					img.src = './photos/' + opt.image;
					
					if ( img.width > img.height )
					{
						par = "width:160px;";
					}
					else
					{
						par = "height:120px;";
					}
					
					
					var info = "<div style='margin-top:15px;margin-left:15px;float:left;'>" + "<span style='font-weight:bold;color:#2586fb;'>" + opt.nom + "</span><br/>" + opt.adresse + "<br/>" + opt.cp + " " + opt.ville + "<br/>" + opt.tel + "<br/>" + opt.email + "<br/>" + opt.site + "</div>";
					
					var h = '<div style="margin:10px;">';
					h += '<div id="small_map" style="width:250px;height:250px;float:left;border:4px solid #959190 ;"></div>';
					h += '<div id="infos" style="margin-left:10px;width:350px;">' + info + '</div>'; 

					h += '<br/><span style="font-weight:bold;color:#2586fb;margin-left:15px">Indiquez votre adresse de départ</span><br> <span style="margin-left:15px">(ex: 20 rue de metz 75010 paris)</span> <br/><br/>';
					h += '<span style="margin-left:15px">Votre adresse <input type="text" id="adr" width="150"/>'; 
					h += '<input type="button" value="Calculer" onclick="calculItineraire( ' + opt.lat + ', '+opt.lng + ' );"/></span>';

					
					 
					
					
					h += '</div><div style="clear:both;"></div>';
					
					Shadowbox.init({
				        // let's skip the automatic setup because we don't have any
				        // properly configured link elements on the page
				        skipSetup: true
				    });
				
					


					Shadowbox.open({
				        player:     'html',
				        content:    h,
				        height:     280,
				        width:      650,
						title: 'Fiche d&eacute;taill&eacute;e',
				        options:    {
							enableKeys:false,
				            onFinish: function(item)
							{
				                if(GBrowserIsCompatible())
								{
									var icon = new GIcon ();
									icon.iconSize = new GSize ( 34, 29 );
									icon.shadowSize = new GSize (40,34);
									icon.iconAnchor = new GPoint  ( 12, 33 );
									icon.infoWindowAnchor = new GPoint  ( 12, 0 );
									icon.image = "./map/markers/" + opt['marker'];
									icon.shadow = "./map/markers/shadow.png";	
		
		
				                    var small_map = new GMap2(document.getElementById('small_map'));
				                    small_map.setCenter(new GLatLng(46, 2), 5);
				
									var latlng = new GLatLng ( parseFloat (opt['lat']), parseFloat (opt['lng'] ));
									var m = new GMarker ( latlng, {icon:icon, title:opt['ville']} );
									small_map.addOverlay ( m );
									small_map.setCenter ( latlng, 12 );
				                   
				                 
				
				                    // add some simple controls
				                    small_map.addControl(new GSmallZoomControl3D() );
				                    //small_map.addControl(new GMapTypeControl());
				                }
				            }
				        }
				    });
					
				});
				
			
				
			},
			
			error:function (XMLHttpRequest, textStatus, errorThrown)
			{
				alert(textStatus + " loadItem"); // the options for this ajax request
				
			}
		});
	

}

var map_iti;
var direction = null;
function calculItineraire (lat, lng)
{
	$("#iti").css("display", "block" );
	$("#container_map").css("display", "none" );
	
	var adr_end = $("#adr").val(); 
	//alert ( lat + " " + lng + " " + adr_iti );
	
	
	
	Shadowbox.close();
	
	if (GBrowserIsCompatible()) 
  	{
		map_iti = new GMap2(document.getElementById("map_iti"));
		if ( direction == null )
		{
			direction = new GDirections( map_iti, document.getElementById("text_iti"));
			GEvent.bind(direction, "load", this, GEvent.callback(this, this.callBackRouting));
			GEvent.bind(direction, "error", this, GEvent.callback(this, this.callBackRoutingError));
		}
		
		direction.load("from: " + adr_end + " to: "+ adr_start,  { "locale": "fr" });
	
		$("#pt_vente").html (cur_nom);
		
	}
}

function callBackRouting ()
{
	
}

function callBackRoutingError ()
{
	if (direction.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
	{
		alert("L'adresse n'a pu être trouvée. Verifiez votre saisie.");
		showItemThickbox(cur_item);
	}
		
}

