﻿/*

    Title:          GoogleAPI.js
    Author:         Gary Hodge
    Date:           08/08/07

    Synopsis:       javscript objects for googlemaps API implementation
    
    last Mod:       17/07/08 JD (fixed balloon not showing on marker click)

*/

// globals
var map;
var gdir;
var geocoder = null;
var addressMarker;
var i = 0;
var points = new Array();

// create map object, render and set center point
function load(storeAddress, postCode, mapID)
{
    //var postcode = getStorePostcode();

    if (GBrowserIsCompatible())
    {      
        map = new GMap2(document.getElementById(mapID));
        map.addControl(new GSmallMapControl());
        map.addControl(new GMapTypeControl());
        
        gdir = new GDirections(map, document.getElementById("directions"));
        
        GEvent.addListener(gdir, "load", onGDirectionsLoad);
        GEvent.addListener(gdir, "error", handleErrors);
        
        setPoint(postCode, postCode, "en_UK", storeAddress);
        map.zoomOut();
    }
}

// create map object
function loadprint(storeAddress, postCode, mapID)
{
    //var postcode = getStorePostcode();

    if (GBrowserIsCompatible())
    {      
        map = new GMap2(document.getElementById(mapID));
        map.addControl(new GSmallMapControl());
        map.addControl(new GMapTypeControl());
    }
}

// set single direction point (FROM)
function setPoint(fromAddress, toAddress, locale, storeAddress)
{
    var localSearch = new GlocalSearch();
    
    localSearch.setSearchCompleteCallback(null,function(){if (localSearch.results[0]){var resultLat = localSearch.results[0].lat;var resultLng = localSearch.results[0].lng;var point = new GLatLng(resultLat,resultLng);drawPoint(point,storeAddress);}else{alert("Postcode not found!");}});  
    localSearch.execute(fromAddress + ", UK");
    
    return false;
}

// show single point (FROM)
function drawPoint(point, storeAddress)
{   
    map.setCenter(point, 14);
    var marker = new GMarker(point);
    map.addOverlay(marker);
    marker.openInfoWindowHtml(storeAddress);
    
    GEvent.addListener(marker, "click", function() {  marker.openInfoWindowHtml(storeAddress);});    
    
}

// set the points used for directions (TO and FROM)
function setPoints(fromAddress, toAddress, local)
{
    fromAddress = fromAddress.replace('%20',''); 
    toAddress = toAddress.replace('%20',''); 

    var localSearch = new GlocalSearch();
    localSearch.setSearchCompleteCallback(null,function(){if (localSearch.results[0]){var resultLat = localSearch.results[0].lat;var resultLng = localSearch.results[0].lng;var point = new GLatLng(resultLat,resultLng);drawRoute(point);}else{alert("Postcode not found!");}});  
    localSearch.execute(fromAddress + ", UK");
    localSearch.execute(toAddress + ", UK");
    return false;
}

// draw the route between waypoints (TO and FROM when both available)
function drawRoute(point)
{
    points[i++] = point;

    if (i >= 2)
    {
        map.clearOverlays();
        gdir = new GDirections(map, document.getElementById("directions"));
        gdir.loadFromWaypoints(points);
        i = 0;
    }
}

// error reporting
function handleErrors()
{
    // get error code from gm onject
    var err = gdir.getStatus().code;
    
    // generate appropriate alert
    switch(err)
    {
        case G_GEO_UNKNOWN_ADDRESS:
            alert("No corresponding geographic location could be found for one of the specified addresses.\nThis may be due to the fact that the address is relatively new, or it may be incorrect.\n\nError code: " + err);
            break;
            
        case G_GEO_SERVER_ERROR:
            alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n\nError code: " + err);        
            break;
            
        case G_GEO_MISSING_QUERY:
            alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input.\nFor directions requests, this means that no query was specified in the input.\n\nError code: " + err);
            break;
            
        case G_GEO_BAD_KEY:
            alert("The given key is either invalid or does not match the domain for which it was given.\n\nError code: " + err);
            break;
            
        case G_GEO_BAD_REQUEST:
            alert("A directions request could not be successfully parsed.\n\n Error code: " + err);
            break;
            
         default:
            alert("An unknown error occurred.");
            break;
    }
}

// not implemented - 08/08/07 GH
function onGDirectionsLoad() {}

// reset show the directions div
function setVisible()
{
    document.getElementById("directions").innerHTML = "";
    document.getElementById("route").style.display = "block";
    document.getElementById("route").style.visibility = "visible";
}
