
	/**
	 * Load GoogleMaps API
	 */
	$(function(){
		script = document.createElement('script');
		script.type = 'text/javascript';
		script.src = 'http://maps.google.com/maps/api/js?sensor=false&callback=handleApiReady';
		document.body.appendChild(script);
	});

	/**
	 * Show map once GoogleMaps API is ready
	 */
	function handleApiReady() {
		if ( $("#map_canvas").length > 0 ) {
			var latlng = $("#store_lat_long").html();
			var details = latlng.split(',');
			initialize(Number(details[0]), Number(details[1]), 'map_canvas');
		}
	}

	/**
	 * Display map based on latitude and longitude
	 * @param		Float		latitude
	 * @param		Float		longitude
	 * @param		String		map_canvas_id
	 */
	function initialize(latitude, longitude, map_canvas_id) {
		var latlng = new google.maps.LatLng(latitude, longitude);

		var myOptions = {
			zoom : 15,
			center : latlng,
			navigationControl : true,
			navigationControlOptions : {
				style : google.maps.NavigationControlStyle.ZOOM_PAN
			},
			mapTypeControl : false,
			scaleControl : true,
			mapTypeId : google.maps.MapTypeId.ROADMAP
		};

		var map = new google.maps.Map(document.getElementById(map_canvas_id), myOptions);

		var marker = new google.maps.Marker({
			position: latlng,
			map: map
		});

		if ( map_canvas_id == 'map_canvas' ) {
			$('.details_store_image').fadeOut();
		}
	}

	

	var num_stores = 1;
	var currentTitle = "";
	var isUS = false;
	
	$(document).ready(function() {
        $('#stores').hide();
        $('#details').hide();

        handleStoreDetailsReady();

        if ($('#stores-menu').length) {
            $('#stores').fadeIn('slow');
        }

        if ($('#store-details').length) {
            setTimeout(function() {
                $('#details').fadeIn('slow');
            }, 300);
        }

		/**
		 * Action for click on 'VIEW MAP' link
		 */
		$('a#view_map').live('click', function() {
			if ( $('#details #map_canvas').html() != '' ) {
				return false;
			}

			if ( $('#details #map_canvas').length > 0 ) {
				var latlng = $('#details #store_lat_long').html();
				var details = latlng.split(',');
				initialize(Number(details[0]), Number(details[1]), 'map_canvas');
			}
			
			return false;
		});

		$('.map_go_button').live('click', function(){
			ReissGoogleMap.getDirections();
		});

		var ReissGoogleMap = {
			// HTML Nodes
			dirContainer: null,

			latitude: null,
			longitude: null,

			// API Objects
			dirService: null,
			dirRenderer: null,
			map: null,
			
			// For retrying with leading digits stripped
			tryAgain: true,

			showDirections: function(dirResult, dirStatus) {
				if ( dirStatus != google.maps.DirectionsStatus.OK ) {	
					if (ReissGoogleMap.tryAgain) {
						ReissGoogleMap.tryAgain = false;
						ReissGoogleMap.getDirections(true);
					} else {
						ReissGoogleMap.tryAgain = true;
						alert("Sorry, your location could not be found. Please try again.");
					}
					return;
				}

				// Show directions
				ReissGoogleMap.dirRenderer.setMap(ReissGoogleMap.map);
				ReissGoogleMap.dirRenderer.setPanel(ReissGoogleMap.dirContainer);
				ReissGoogleMap.dirRenderer.setDirections(dirResult);
			},

			getSelectedTravelMode: function() {
				var elem = $('.travel-mode-input.current');

				if ( $(elem).hasClass('driving') ) {
					ReissGoogleMap.dirContainer = document.getElementById('driving_pane');
					return google.maps.DirectionsTravelMode.DRIVING;
				}
				else if ( $(elem).hasClass('walking') ) {
					ReissGoogleMap.dirContainer = document.getElementById('walking_pane');
					return google.maps.DirectionsTravelMode.WALKING;
				}
				else {
					alert('Unsupported travel mode.');
				}

				return false;
			},
			
			getDirections: function(stripStreetNumber) {
				
				if (stripStreetNumber == null) {
					stripStreetNumber = false;
				}
				
				var fromStr = $('#from-input').val();
				
				if (stripStreetNumber) {
					// Strip whitespace and digits from start of string
					fromStr = fromStr.replace(/\d+/g, '');
					fromStr = fromStr.replace(/\s+/i, '');
					ReissGoogleMap.tryAgain = false; 
				}
				
				if ( fromStr == '' ) {
					return false;
				}
				
				var dirRequest = {
					origin      : fromStr,
					destination : ReissGoogleMap.latitude + ',' + ReissGoogleMap.longitude,
					travelMode  : ReissGoogleMap.getSelectedTravelMode(),
					unitSystem  : google.maps.DirectionsUnitSystem.METRIC,
					provideRouteAlternatives : true
				};
				
				ReissGoogleMap.dirService.route(dirRequest, ReissGoogleMap.showDirections);
			},

			init: function() {
				var details = $('#details #store_lat_long').html().split(',');
				
				ReissGoogleMap.latitude = Number(details[0]);
				ReissGoogleMap.longitude = Number(details[1]);

				ReissGoogleMap.dirService = new google.maps.DirectionsService();
				ReissGoogleMap.dirRenderer = new google.maps.DirectionsRenderer();

				var latlng = new google.maps.LatLng(ReissGoogleMap.latitude, ReissGoogleMap.longitude);

				var myOptions = {
					zoom : 15,
					center : latlng,
					navigationControl : true,
					navigationControlOptions : {
						style : google.maps.NavigationControlStyle.ZOOM_PAN
					},
					mapTypeControl : false,
					scaleControl : true,
					mapTypeId : google.maps.MapTypeId.ROADMAP
				};

				ReissGoogleMap.map = new google.maps.Map(document.getElementById('map_canvas_full_screen'), myOptions);

				var marker = new google.maps.Marker({
					position: latlng,
					map: ReissGoogleMap.map
				});
			}
		};


		/**
		 * Handle store details
		 */
        function handleStoreDetailsReady() {
			$('#details a.fancybox').fancybox({
				'overlayOpacity' : '0.4',
				'overlayColor'   : '#000000',
				'autoScale'      : false,
				'centerOnScroll' : true,
				'transitionIn'   : 'fade',
				'transitionOut'  : 'fade',
				'type'           : 'ajax',
				'onComplete'     : function() {
					ReissGoogleMap.init();

					$('.travel-mode-input').click(function(){
						$('.travel-mode-input.current').removeClass('current');
						$(this).addClass('current');

						if ( $(this).hasClass('driving') ) {
							$('.walking_pane').hide();
							$('.driving_pane').show();
						}
						else {
							$('.driving_pane').hide();
							$('.walking_pane').show();
						}

						ReissGoogleMap.getDirections();

						return false;
					});
				}
			});

			if ( $('#details .details_store_image').length == 0 ) {
				if ( $('#details #view_map').length > 0 ) {
					$('#details #view_map').trigger('click');
				}
			}
		}


	});
	

