﻿//
//	gMapLoad()
//	Loading the map and creating the marker.
//
function gMapLoad() {
      
    if (GBrowserIsCompatible()) {
		var mapAddress = $("#map_address");
		var adr = mapAddress.children(".adr:first");       
		var street = adr.children(".street-address:first").html();

		// Get rid of Extra non-street address bits:
		var streetChunks = street.split('<');
		street = streetChunks[0];

		var city = adr.children(".locality:first").text();
		var state = adr.children(".region:first").text();
		var zip = adr.children(".postal-code:first").text();

		var geo = mapAddress.children(".geo:first").attr("title").split(";");

		var lat = geo[0];
		var lon = geo[1];

		var htmlAddress = "<h5 class='map-marker-header'>Address:</h5><p>" + street + "<br />" + city + ", " + state + " " + zip + "</p>";
		var geoAddress = street + ", " + city + ", " + state + ", " + zip;

		var map = createMap();
		
		function searchByAddress() {
			//console.log("Searching based on street address.");
			//console.log("geoAddress: "+ geoAddress);
			
            geocoder.getLatLng(
                geoAddress,
                function(point) {
                    if (!point) {
                        //console.log(geoAddress + " not found");
						searchByLatLong();
                    } else {
                        map.setCenter(point, 13);
                        var marker = new GMarker(point);
                        map.addOverlay(marker);
                        marker.openInfoWindowHtml(htmlAddress);
						getDirections(geoAddress, map);
                    }
                }
            );
		}
		
		function searchByLatLong() {
			//console.log("Searching based on lat/lon");
			//console.log(lat + ", " + lon);
			
            var latLon = new GLatLng(lat, lon);
            map.setCenter(latLon, 13);

            var marker = new GMarker(latLon);
            map.addOverlay(marker);
            marker.openInfoWindowHtml(htmlAddress);

            getDirections(lat + ", " + lon, map);

            return true;
		}
		
		// Search based on street address if available, otherwise use latitude and longitude :
		searchByAddress();
		return true;
    }

    return false;
}

//
//	createMap()
//	To create the map. Called if the map has lat and long OR  address
//
function createMap() {
    $("div#content div#two_col-right div#getting_here-map").css("display", "block");
    $("div#content div#two_col-right div#directions_controls").css("display", "block");

    var map = new GMap2(document.getElementById("getting_here-map"));

    map.addControl(new GLargeMapControl());
    map.enableContinuousZoom();
    map.addControl(new GMapTypeControl());

    geocoder = new GClientGeocoder();

    return map;
}


//
//	getDirections()
//	getting directions function
//
function getDirections(destination, map) {
	
	var mapCurrentZoom;
	var mapDirections;
	var mapAddressValue;	
    var viewPort = $("div#content div#two_col-right div#get_directions");
    var directionsAction = $("div#content div#two_col-right .get_direction");

    directionsAction.click(function() {
        map.savePosition();
        if (typeof mapDirections != "undefined") {
            mapDirections.clear();
        }

        if (mapAddressValue != $("div#content div#two_col-right #user_address").attr("value")) {
            mapAddressValue = $("div#content div#two_col-right #user_address").attr("value");
        } else {return false;}
   
        var directionsPanel = document.getElementById("get_directions");
        mapDirections = new GDirections(map, directionsPanel);
        mapDirections.load("from: " + mapAddressValue + " to: " + destination);
        return false;
    });

 
	//    
	// To clear the default message in the input text box
	//
    var swap_text_boxes = [];

	//Store the default value for each box
	$('input[type=text][value].swaptextbox').each(function() {
		swap_text_boxes[$(this).attr('id')] = $(this).attr('value');
	});

	//Add focus and blur events to set or clear the value
	$('input[type=text][value].swaptextbox').bind('focus', function() {
		if($(this).val() == swap_text_boxes[$(this).attr('id')]) {
			$(this).val('');
		}
	});

	$('input[type=text][value].swaptextbox').bind('blur', function() {
		if($(this).val() == '') {
			$(this).val(swap_text_boxes[$(this).attr('id')]);
		}
	});
}


//
// onload/unload business:
//
$(function() { 
	if($('div#gmap').length) {
		// Unload gmaps when leaving:
	    $(window).unload(function() {
	        GUnload();
	    });
	
	    // Create the map and hide the map address div if successful:
	    if (gMapLoad()) {
	        $("#map_address").css("display", "none");
	    }
	}
});