
var ceMap = {
	mapCanvas:		null,
	dirContainer:	null,
	mapInUse:		false,
	companyPos:		null,
	lat:			40,
	lng:			-100,
	zoom:			8,
	travelMode:		'DRIVING',
	
	
	companyMarker:	null,
	markerImage:	null,
	markerShadow:	null,
	companyImage:	null,
	companyShadow:	null,
	infowindow: 	null,
	
	mapTypeControl:		true,
	mapTypeId:			google.maps.MapTypeId.ROADMAP,
	navigationControl:	true,

	// API Objects
	dirService:		null,
	dirRenderer:	null,
	map:			null,
	geocoder:		new google.maps.Geocoder(),
	input: 			{
						highways:	null,
						tolls:		null,
						address:	null,
						//unitInput:		null,
						lat:		null,
						lng:		null,
						zoom:		null
					},
	lang: 			{
						directionsFailed:	'Directions failed',
						geocodeError:		'Geocode was not successful for the following reason',
						showIPBasedLocation:'Show IP-Based Location',
						address:			'Address'
					},
	
	getMarkerImage: function(markerIcon, markerShadow) {
		if (markerIcon) {
		  ceMap.companyImage	= new google.maps.MarkerImage(markerIcon);
		}
		if (markerShadow) {
		  ceMap.companyMarker	= new google.maps.MarkerImage(markerShadow);
		}
	},
	
	
	showDirections: function(dirResult, dirStatus) {
		if (dirStatus != google.maps.DirectionsStatus.OK) {
		  alert( ceMap.lang.directionsFailed + ': ' + dirStatus);
		  return;
		}
		// Show directions
		ceMap.dirRenderer.setMap(ceMap.map);
		ceMap.dirRenderer.setPanel(ceMap.dirContainer);
		ceMap.dirRenderer.setDirections(dirResult);
	},

	getSelectedTravelMode: function() {
		return ceMap.travelMode;
	},

	getDirections: function() {
		if(ceMap.mapInUse){
			ceMap.reset();
		}
		ceMap.mapInUse	= true;
		// API Objects
		ceMap.dirService	=	new google.maps.DirectionsService();
		ceMap.dirRenderer	=	new google.maps.DirectionsRenderer();
		ceMap.dirContainer	=	$('ce-directionsPanel');

		var dirRequest = {
				origin: 		$(ceMap.input.address).getProperty('value'),
				destination:	ceMap.companyPos,
				travelMode:		ceMap.getSelectedTravelMode(),
				provideRouteAlternatives: true,
				avoidHighways:	$(ceMap.input.highways).checked,
				avoidTolls:		$(ceMap.input.tolls).checked
		  
		};
		ceMap.dirService.route(dirRequest, ceMap.showDirections);
	},
	reset: function() {
		if(!ceMap.mapInUse){
			return false;
		}
		ceMap.init();
		ceMap.dirRenderer.setMap(null);
		ceMap.dirRenderer.setPanel(null);
		ceMap.dirRenderer = new google.maps.DirectionsRenderer();
		ceMap.dirRenderer.setMap(ceMap.map);
		ceMap.dirRenderer.setPanel(ceMap.dirContainer);
	},
	
	init: function() {
		if(ceMap.lat == 40 && ceMap.lng == -100 && google.loader.ClientLocation){
			ceMap.lat	= google.loader.ClientLocation.latitude;
			ceMap.lng	= google.loader.ClientLocation.longitude;
			if(!ceMap.infoWindowContent){
				ceMap.infoWindowContent 	= ceMap.lang.showIPBasedLocation+': <br />'
													+'<b>'+ ceMap.getFormattedLocation() + '</b>';
			}
			if(ceMap.editMode){
				$(ceMap.input.lat).setProperty('value', ceMap.lat );
				$(ceMap.input.lng).setProperty('value', ceMap.lng );
			}
		}
		ceMap.companyPos	= new google.maps.LatLng(ceMap.lat,ceMap.lng);
		origin 				= ceMap.companyPos;
		var companyOptions = {
			zoom: ceMap.zoom,
			center: origin,
			mapTypeControl:		ceMap.typeControl,
			mapTypeId:			ceMap.typeId,
			navigationControl:	ceMap.navigationControl
		};
		ceMap.map = new google.maps.Map($(ceMap.mapCanvas), companyOptions);
		
		ceMap.companyMarker= new google.maps.Marker({
			position: ceMap.companyPos,
			map: ceMap.map,
			icon: ceMap.companyImage,
			shadow: ceMap.companyShadow,
			title: ceMap.mapTitle,
			draggable: ceMap.companyMarkerDraggable,
			zIndex: 4
		});
		
		ceMap.infowindow = new google.maps.InfoWindow({
			content:  ceMap.infoWindowContent
		});
		ceMap.infowindow.open(ceMap.map,ceMap.companyMarker);
		if(ceMap.companyMarkerDraggable){
			google.maps.event.addListener(ceMap.companyMarker, 'drag', function() {
				ceMap.infowindow.close();
			});
			google.maps.event.addListener(ceMap.companyMarker, 'dragend', function() {
				if(ceMap.editMode){
					$(ceMap.input.lat).setProperty('value', ceMap.companyMarker.getPosition().lat() );
					$(ceMap.input.lng).setProperty('value', ceMap.companyMarker.getPosition().lng() );
				}
				ceMap.infowindow.open(ceMap.map,ceMap.companyMarker);
			});
		}
		if(ceMap.editMode){
			google.maps.event.addListener(ceMap.map, 'zoom_changed', function() {
			    zoomLevel = ceMap.map.getZoom();
			    if (zoomLevel == 0) {
			      ceMap.map.setZoom(10);
			    }
			    $(ceMap.input.zoom).setProperty('value', ceMap.map.getZoom() );
			});
		}
	},
	
	getFormattedLocation: function() {
	    if (google.loader.ClientLocation.address.region) {
	      return google.loader.ClientLocation.address.city + ', ' 
	          + google.loader.ClientLocation.address.region.toUpperCase() + ', '
	          + google.loader.ClientLocation.address.country_code;
	    } else {
	      return  google.loader.ClientLocation.address.city + ', '
	          + google.loader.ClientLocation.address.country_code;
	    }
	},
	
	codeAddress: function() {
		var address	= $(ceMap.input.address).getText().clean();
		ceMap.geocoder.geocode( { 'address': address}, function(results, status) {
			if (status == google.maps.GeocoderStatus.OK) {
				ceMap.map.setCenter(results[0].geometry.location);
				ceMap.companyMarker.setPosition(results[0].geometry.location);
				if(ceMap.map.getZoom() <10){
					ceMap.map.setZoom(15);
				}
				ceMap.infowindow.setContent($(ceMap.input.address).getText());
				
				$(ceMap.input.lat).setProperty('value', results[0].geometry.location.lat() );
				$(ceMap.input.lng).setProperty('value', results[0].geometry.location.lng() );
			} else {
				alert(ceMap.lang.geocodeError+": " + status
						+"\n\n"+ceMap.lang.address +": "+ address);
			}
		});
	}
};