/*'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''\
/'			Code generated by GM Location Tool V0.1					'\
/'		A Customizable Google Maps Location for your Website		'\
/''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''\
/'				http://9tree.net/tools/GMLocation					'\
\'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''*/

//creates GMLocation
if(typeof GMLocation != 'array') var GMLocation=[];
(function(){
	//item
	var item_i=GMLocation.length>0?GMLocation.length-1:GMLocation.length;

	//main object
	GMLocation.push({
		_v:{	
			//MAP
			'map_lat':39.16144580378926,
			'map_lng':-8.786916732788086,
			'map_zoom':15,
			'map_width':520,
			'map_height':452,

			//LOCATION MARKER
			'marker_lat':39.1606721498918,
			'marker_lng':-8.788279294967651,
			'info_status':true,
			//'info_callback':{INFO_CALLBACK},

			//FROM PATH MARKER
			'f_marker_exists':false,
			'f_marker_lat':'',
			'f_marker_lng':''
		},
		init:function(){
			//inititalizes map
			GMLocation[item_i].map = new google.maps.Map2(document.getElementById('GMLocation_'+item_i), { 
				size:new google.maps.Size(GMLocation[item_i]._v.map_width, GMLocation[item_i]._v.map_height),
				mapTypes:[G_NORMAL_MAP]
			});
			
			GMLocation[item_i].map.setCenter(new google.maps.LatLng(GMLocation[item_i]._v.map_lat, GMLocation[item_i]._v.map_lng), GMLocation[item_i]._v.map_zoom);
			GMLocation[item_i].map.disableDoubleClickZoom();
			GMLocation[item_i].map.continuousZoomEnabled();
			GMLocation[item_i].map.enableScrollWheelZoom();
			GMLocation[item_i].map.addControl(new google.maps.LargeMapControl());
			
			//initializes marker
			GMLocation[item_i].marker = new google.maps.Marker(
				new google.maps.LatLng(GMLocation[item_i]._v.marker_lat, GMLocation[item_i]._v.marker_lng),
				{
					icon:new google.maps.Icon(G_DEFAULT_ICON),
					draggable:false
				});

			GMLocation[item_i].marker.bindInfoWindow(document.getElementById('GMLocation_IW_'+item_i));
			GMLocation[item_i].map.addOverlay(GMLocation[item_i].marker);
			if(GMLocation[item_i]._v.info_status) GMLocation[item_i].marker.openInfoWindow(document.getElementById('GMLocation_IW_'+item_i));

			//initializes 'from' marker
			if(GMLocation[item_i]._v.f_marker_exists){
				var m_icon=new google.maps.Icon();
				m_icon.image='http://9tree.net/tools/GMLocation/images/f_marker.png';
				m_icon.iconSize=new google.maps.Size(20,29);
				m_icon.iconAnchor=new google.maps.Point(10,29);
				GMLocation[item_i].f_marker = new google.maps.Marker(
					new google.maps.LatLng(GMLocation[item_i]._v.f_marker_lat, GMLocation[item_i]._v.f_marker_lng),
					{
					icon:m_icon,
					draggable:true
				});
				GMLocation[item_i].map.addOverlay(GMLocation[item_i].f_marker);
				GMLocation[item_i].directions = new google.maps.Directions();

				//update directions
				google.maps.Event.addListener(GMLocation[item_i].directions, "load", function() {
					GMLocation[item_i].map_line=GMLocation[item_i].directions.getPolyline();
					GMLocation[item_i].map.addOverlay(GMLocation[item_i].map_line);
				});
				google.maps.Event.addListener(GMLocation[item_i].directions, "error", function() {
					GMLocation[item_i].directions.clear();
				});

				//markers move
				google.maps.Event.addListener(GMLocation[item_i].marker, "dragend", function() {
					GMLocation[item_i].update_directions();
				});
				google.maps.Event.addListener(GMLocation[item_i].f_marker, "dragend", function() {
					GMLocation[item_i].update_directions();
				});
				
				GMLocation[item_i].update_directions();
			}
		},
		update_directions:function(){
			GMLocation[item_i].directions.clear();
			if(GMLocation[item_i].map_line!=null) GMLocation[item_i].map.removeOverlay(GMLocation[item_i].map_line);
			var f_pos=GMLocation[item_i].f_marker.getLatLng();
			var pos=GMLocation[item_i].marker.getLatLng();
			var fromAddress=f_pos.lat()+", "+f_pos.lng();
			var toAddress=pos.lat()+", "+pos.lng();
			GMLocation[item_i].directions.load(
				"from: " + fromAddress + " to: " + toAddress,
				{preserveViewport:true,
				getPolyline:true
			});
		}
	});
	
	//creates elements
	document.write('<div id="GMLocation_'+item_i+'"></div>');
	document.write('<div style="display:none;"><div id="GMLocation_IW_'+item_i+'" style="padding-right:15px;"><div id="GMLocation_IC_'+item_i+'" style="font-size:1em;"><span style="font-weight: bold;">Restaurante QG</span> <br> <br><span style="font-size: 0.9em;">Cartaxo</span></div></div></div>');
	
	//waits for google to load
	google.setOnLoadCallback(GMLocation[item_i].init());
})();
