var directions = {
	defaults: {
		map_width: 800,
		map_height: 600,
		location_lat: 37.0625,
		location_long: -95.677068,
		location_zoom: 16
	},
	
	input_start: '#directions-start',
	input_end: '#directions-end',
	map_container_id: 'direction-map',
	opts: {},
	map: {},
	map_dir: {},
	map_dir_service: {},
	
	setup: function(opts) {
		if (!opts.map_width) opts.max_width = this.defaults.map_width;
		if (!opts.map_height) opts.map_height = this.defaults.map_height;
		if (!opts.location_lat) opts.location_lat = this.defaults.location_lat;
		if (!opts.location_long) opts.location_long = this.defaults.location_long;
		if (!opts.location_zoom) opts.location_zoom = this.defaults.location_zoom;
		
		this.opts = opts;
		this.setup_html();
	},
	
	setup_html: function() {
		var map_container = jQuery('#' + this.map_container_id);
		if (map_container.length > 0) {
			map_container.css({
				width: this.opts.map_width,
				height: this.opts.map_height
			});
	
		    var script = document.createElement("script");
		    script.type = "text/javascript";
		    
		    if (jQuery('div.language-es').length > 0) {
			    script.src = "http://maps.google.com/maps/api/js?sensor=false&language=es&callback=directions.map_init";
		    } else {
			    script.src = "http://maps.google.com/maps/api/js?sensor=false&callback=directions.map_init";
			}
		    document.body.appendChild(script);
	   	}
	},
	
	map_init: function() {
		var myLatlng = new google.maps.LatLng(this.opts.location_lat, this.opts.location_long);
		var myOptions = {
			zoom: this.opts.location_zoom,
			center: myLatlng,
			mapTypeId: google.maps.MapTypeId.ROADMAP
		}
		
		this.map_dir_service = new google.maps.DirectionsService();
		this.map = new google.maps.Map(document.getElementById(this.map_container_id), myOptions);
		this.map_dir =  new google.maps.DirectionsRenderer();
		
		this.map_dir.setMap(this.map);
		
		this.update();
	},
	
	update: function() {
		var start = jQuery(this.input_start).val();
		var end = jQuery(this.input_end).val();	
		
		if (start == 'Type your address here' || start == 'Escriba su dirección aquí')
			start = "Chicago, IL"; 					
		
	    var request = {
	        origin:start, 
	        destination:end,
	        travelMode: google.maps.DirectionsTravelMode.DRIVING
	    };
	    this.map_dir_service.route(request, function(response, status) {
			if (status == google.maps.DirectionsStatus.OK) {
				directions.map_dir.setDirections(response);
				
				var html = "";
				if (response.routes && response.routes.length > 0) {
					var routes = response.routes[0];
					if (routes.legs && routes.legs.length > 0) {
						var legs = routes.legs[0];
						if (legs.steps && legs.steps.length > 0) {
							var steps = legs.steps;
							for(var i=0; i<steps.length; i++)
								html += "<li>" + steps[i].instructions + "</li>";
						}
					}
				}
				jQuery("#directions").html(html);
			}
	    });
	}
};
jQuery(document).ready(function() {
	var opts = {
		map_width: 732,
		map_height: 300,
		location_lat: 41.855242,
		location_long: -87.680511,
		location_zoom: 12
	};
	
	directions.setup(opts);
	
	jQuery("#transit").click(function() {
		var start = jQuery('#directions-start').val();
		var end = jQuery('#directions-end').val();	
		
		if (start == 'Type your address here' || start == 'Escriba su dirección aquí')
			start = "Chicago, IL"; 	
			
		var url = "http://maps.google.com/?saddr=" + escape(start) + "&daddr=" + escape(end) + "&dirflg=r";
		window.open(url);
	});
});

