/****************************************************
	 * GOOGLE MAP RELATED SCRIPT BELOW
	 ****************************************************/
	
	function showMap(latitude,longitude,name){
		//showPointOnMap('point-map-canvas',latitude,longitude);
		if ($('show-on-map-title')){
			$('show-on-map-title').innerHTML = name + ' on map';
		}
		$('show-on-map').show();
		window.scrollTo(0,0);
		showPointOnMap('point-map-canvas',latitude,longitude);
	}
	
	function showPointOnMap(id,latitude,longitude) {
	  if (GBrowserIsCompatible()) {
			var map = new GMap2(document.getElementById(id));
			map.setMapType(G_NORMAL_MAP);
			var point = new GLatLng(latitude,longitude);
			map.setCenter(point, 11);			
			map.addOverlay(new GMarker(point));
			map.setUIToDefault();
	  }
	}
	
	var map;
	var marker;
	var mapClicked = false;
	var dog = true;
	var markerImage = "images/gsmarker.png";

	/* following function uses global marker variable */
	function followCursor(map){ 
	  var noMore = false;
	  var mouseMove = GEvent.addListener(map, 'mousemove', function(cursorPoint){
		if(!noMore){
		  marker = new GMarker(cursorPoint,{draggable:true, autoPan:false});
		  map.addOverlay(marker);
		  marker.setImage(markerImage);
		  noMore = true;
		  // This function deletes the marker when dragged outside map
		  GEvent.addListener(marker, 'drag', function(markerPoint){
			if(!map.getBounds().containsLatLng(markerPoint)){
			}
		  });	
		  GEvent.addListener(marker, 'dragend', function(markerPoint){
			mapClicked = true;
			map.setCenter(markerPoint);
		  });	
		}
		if(dog){
		  marker.setLatLng(cursorPoint);
		}
	  });
	  if (!mapClicked){
		  var mapClick = GEvent.addListener(map, 'click', function(overlay,latlng,overlaylatlng){
			dog = false;
			// 'mousemove' event listener is deleted to save resources
			GEvent.removeListener(mouseMove);
			GEvent.removeListener(mapClick);
			mapClicked = true;
			map.setCenter(latlng);
		  });
	  }
	}

	function doMarkersSetCallback(info){
		for (var i=0; i<info.length; i++) {
          var result = info[i].result;
		  if(i == 0){
			  var html = '<b>Search Result:</b> '+ result.titleNoFormatting +', '+ result.country +'<span class="middle" style="vertical-align:top;"> - <a href="javascript:void(0);" onclick="setMarker('+result.lat+','+result.lng+');">select this place</a> - <a href="javascript:void(0);" onclick="hideDiv(\'map-custom-results\');">clear</a></span>';
			  $('map-custom-results').innerHTML = html;
			  $('map-custom-results').show();
		  }
          info[i].marker.hide();
        }
	}

	function hideDiv(id){
		$(id).hide();
	}

	/* following function uses global map and global marker variable */
	function setMarker(lat,lng){
		var p = new GLatLng(lat,lng);
		dog = "false";
		GEvent.clearListeners(map,"click");
		GEvent.clearListeners(map,"mousemove");
		marker.setLatLng(p);
		mapClicked = true;
		map.setCenter(p);
	}

	/* this function uses global map variable */
	function showAddDestMap(mapid, mapSearchResultsId) {
	  if (GBrowserIsCompatible()) {
			var mapOptions = {googleBarOptions : {
				style : "new", 
				resultList: document.getElementById(mapSearchResultsId), 
				listingTypes: G_GOOGLEBAR_TYPE_BLENDED_RESULTS, 
				suppressInitialResultSelection : true,
				onMarkersSetCallback: doMarkersSetCallback}};
			map = new GMap2(document.getElementById(mapid), mapOptions);
			map.setMapType(G_NORMAL_MAP);
			var point = new GLatLng(22.593684,78.96288);//india's center
			map.setCenter(point, 3);
			map.setUIToDefault();
			map.enableGoogleBar();
			followCursor(map);
	  }
	}	
	
	var startMarkerImage = "images/gs-start-marker.png";
	var onrouteMarkerImage = "images/gs-onroute-marker.png";
	var endMarkerImage = "images/gs-end-marker.png";
	
	var startIcon = new GIcon();
	startIcon.image = startMarkerImage;
	startIcon.iconSize = new GSize(29, 35);
	startIcon.iconAnchor = new GPoint(10, 35);//relative to the top left corner
	startIcon.infoWindowAnchor = new GPoint(14, 10);

	var onrouteIcon = new GIcon();
	onrouteIcon.image = onrouteMarkerImage;
	onrouteIcon.iconSize = new GSize(29, 35);
	onrouteIcon.iconAnchor = new GPoint(10, 35);
	onrouteIcon.infoWindowAnchor = new GPoint(14, 10);

	var endIcon = new GIcon();
	endIcon.image = endMarkerImage;
	endIcon.iconSize = new GSize(29, 35);
	endIcon.iconAnchor = new GPoint(10, 35);//relative to the top left corner
	endIcon.infoWindowAnchor = new GPoint(14, 10);

	function createMarker(latLng, position, titleString){
		var marker = null;
		var gicon = onrouteIcon;
		if (position == 's')
		{
			gicon = startIcon;
		}else if (position == 'e'){
			gicon = endIcon;
		}
		if (typeof titleString != 'undefined' && titleString != '')
		{
			marker = new GMarker(latLng,{draggable: false, icon: gicon, title: titleString});
		}else{
			marker = new GMarker(latLng,{draggable: false, icon: gicon});
		}
		
		return marker;
	}

	function directionsError(obj){	//obj -> GSGDirections
		var ind = obj.getIndex();
		var list = new Array();
		list[0] = new GLatLng(latLngArr[ind][0],latLngArr[ind][1]);
		list[1] = new GLatLng(latLngArr[ind+1][0],latLngArr[ind+1][1]);
		var polyline = new GPolyline(list,"#4545f5",5,0.6);
		var dist = polyline.getLength()/1000;
		latLngArr[ind][3] = dist;
		map.addOverlay(polyline);
	}

	function directionsLoad(obj)//obj -> GSGDirections
	{
		var ind = obj.getIndex();
		var directionsPolyline = obj.getPolyline();
		var dist = directionsPolyline.getLength()/1000;
		latLngArr[ind][3] = dist;
		map.addOverlay(directionsPolyline);
		//	alert(directions.getRoute(0).getDistance().html+' in '+directions.getRoute(0).getDuration().html);
		//	alert(directions.getRoute(1).getDistance().html+' in '+directions.getRoute(1).getDuration().html);
		//	alert(directions.getRoute(2).getDistance().html+' in '+directions.getRoute(2).getDuration().html);
	}

	GSGDirections.prototype = new GDirections();
	GSGDirections.prototype.constructor=GSGDirections;
	function GSGDirections(index){ 
		GDirections.call(this);
		this.index=index;
	} 
	GSGDirections.prototype.setIndex=function(index){ 
		this.index = index;
	} 
	GSGDirections.prototype.getIndex=function(){ 
		return this.index;
	} 

	
	var latLngArr = new Array();
	//latLngArr[index][0] = lat
	//latLngArr[index][1] = lng
	//latLngArr[index][2] = title
	//latLngArr[index][3] = distance
	
	function drawroute(mapid)
	{
		if (GBrowserIsCompatible())
		{
			var point = new GLatLng(22.593684,78.96288);
			map = new GMap2(document.getElementById(mapid));
			map.setCenter(point, 2);
			map.setUIToDefault();
			map.removeMapType(G_HYBRID_MAP);
			map.removeMapType(G_SATELLITE_MAP);
			map.removeMapType(G_PHYSICAL_MAP);
			
			if (latLngArr.length > 1)
			{
				//setting map bounds
				var bounds = new GLatLngBounds();
				for (var k = 0; latLngArr.length > k ; k++) {
					var p = new GLatLng(latLngArr[k][0],latLngArr[k][1]);
					bounds.extend(p);
				}
				var zoom = map.getBoundsZoomLevel(bounds);
				map.setCenter(bounds.getCenter(),zoom);
			}		

			for (var i =0;(latLngArr.length-1) > i;i++ )
			{
				var directions = new GSGDirections(i);
				GEvent.addListener(directions,'error',directionsError);
				GEvent.addListener(directions,'load',directionsLoad);

				if (i == 0)
				{
					directions.load('from: '+latLngArr[i][0]+','+latLngArr[i][1]+' to: '+latLngArr[i+1][0]+','+latLngArr[i+1][1],{ getSteps: true, getPolyline:true, preserveViewport: true});
				}else{
					directions.load('from: '+latLngArr[i][0]+','+latLngArr[i][1]+' to: '+latLngArr[i+1][0]+','+latLngArr[i+1][1],{ getSteps: true, getPolyline:true, preserveViewport: true});
				}			
								
			}
			//creating markers
			if (latLngArr.length > 1)
			{
				for (var k = 0; latLngArr.length > k ; k++) {
					var p = new GLatLng(latLngArr[k][0],latLngArr[k][1]);
					var title = "";
					var posString = "i"; //intermediate
					if (k==0){
						posString = "s"; //start point
					}else if(k== latLngArr.length-1){
						posString = "e"; //end point
					}
					var marker = createMarker(p,posString,latLngArr[k][2]);
					map.addOverlay(marker);
				}
			}
		}		
	}
