//<![CDATA[
// Google Map Maker script v.1.1
// (c) 2006 Richard Stephenson http://www.donkeymagic.co.uk
// Email: donkeymagic@gmail.com
// http://mapmaker.donkeymagic.co.uk
var map;
var icon0;
var newpoints = new Array();
var directions;

function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function'){
        window.onload = func
    } else {
        window.onload = function() {
            oldonload();
            func();
        }
    }
}

var coordinates = {
    wellington: Array(-41.26426336839788, 174.78698194026947),
    picton_terminal: Array(-41.28553836111379, 174.00513410568237),
    picton_vehicle: Array(-41.288932,174.000778)
};

/*Wellington Terminal*/

function loadMapWellington() {
    map = new GMap2(document.getElementById("map_wellington"));
    map.addControl(new GLargeMapControl());
    map.addControl(new GMapTypeControl());
    map.setCenter(new GLatLng(coordinates.wellington[0], coordinates.wellington[1]), 15);
//    map.setMapType(G_MAP_TYPE);

    icon0 = new GIcon();
    icon0.image = fullyQualifiedPath + "/images/marker_wellington.png";
    icon0.iconSize = new GSize(138, 73);
    icon0.iconAnchor = new GPoint(0, 34);
    icon0.infoWindowAnchor = new GPoint(0, 0);
    
    addPointsWellington();

}

function addPointsWellington() {
    newpoints[0] = new Array(coordinates.wellington[0], coordinates.wellington[1], icon0, 'Interislander', '');

    for(var i = 0; i < newpoints.length; i++) {
        var point = new GPoint(newpoints[i][1],newpoints[i][0]);
        var popuphtml = newpoints[i][4] ;
        var marker = createMarker(point,newpoints[i][2],popuphtml);
        map.addOverlay(marker);
    }
}

/*Picton Terminal*/

function loadMapPicton() {
    map = new GMap2(document.getElementById("map_picton"));
    map.addControl(new GLargeMapControl());
    map.addControl(new GMapTypeControl());
    map.setCenter(new GLatLng( -41.28553836111379, 174.00513410568237), 15);
    //map.setMapType(G_MAP_TYPE);

    icon0 = new GIcon();
    icon0.image = fullyQualifiedPath + "/images/marker_picton.png";
    icon0.iconSize = new GSize(138, 73);
    icon0.iconAnchor = new GPoint(0, 34);
    icon0.infoWindowAnchor = new GPoint(0, 0);

    icon1 = new GIcon();
    icon1.image = fullyQualifiedPath + "/images/marker_picton_vehicle.png";
    icon1.iconSize = new GSize(88, 52);
    icon1.iconAnchor = new GPoint(80, 26);
    icon1.infoWindowAnchor = new GPoint(0, 0);

    addPointsPicton();
    
    document.getElementById("rdoPictonTerminal").checked = "checked";
}

function addPointsPicton() {
    newpoints[0] = new Array(coordinates.picton_terminal[0], coordinates.picton_terminal[1], icon0, 'Interislander', '');
    newpoints[1] = new Array(coordinates.picton_vehicle[0], coordinates.picton_vehicle[1], icon1, 'Vehicle', '');

    for(var i = 0; i < newpoints.length; i++) {
        var point = new GPoint(newpoints[i][1],newpoints[i][0]);
        var popuphtml = newpoints[i][4] ;
        var marker = createMarker(point,newpoints[i][2],popuphtml);
        map.addOverlay(marker);
    }
}


function createMarker(point, icon, popuphtml) {
    //var popuphtml = "<div id=\"popup\">" + popuphtml + "<\/div>";
    var marker = new GMarker(point, icon);
    GEvent.addListener(marker, "click", function() {
        //marker.openInfoWindowHtml(popuphtml);
    });
    return marker;
}
//]]>


$(document).ready(function() {
    $("#directions_wellington").keyup(function(event) {
        if(event.keyCode == 13) {
            find_directions("wellington");            
        }
    });
    
    $("#directions_picton").keyup(function(event) {
        if(event.keyCode == 13) {
            find_directions("picton");            
        }
    });    
});

function get_picton_radio_value() {
    return $('input:radio[name=picton_destination]:checked').val();    
}

function find_directions(place) {   
    text = $("#directions_" + place).val();
    
    if(text != "") {
        if(place == "picton") {
            if(value = get_picton_radio_value()) {
                place = "picton_" + value;
            } else {
                return false;                
            }
        }
            
        coord = eval("coordinates." + place);            
        
        if(directions != undefined) directions.clear();
        
        directionsPanel = document.getElementById("route_info");
        directions = new GDirections(map, directionsPanel);        
        
        GEvent.addListener(directions, "load", onGDirectionsLoad);
        GEvent.addListener(directions, "error", handleErrors);
                
        $("#loading_icon").show();        
        $("#route_info").html("");           
        
        query = "from: " + text + " to: @" + coord[0] + "," + coord[1];  
        directions.load(query, { "locale": "en_NZ", "travelMode": G_TRAVEL_MODE_DRIVING });
    } else {
        error = "<p class='directions_error'>Please enter an address on the box above.</p>";
        
        $("#loading_icon").hide();
        $("#route_info").html(error);        
    }
    
    
    return false;
}

function handleErrors(){
    error = "";
    
    if (directions.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
      error = "Sorry, your search did not return any valid results. Please note that if Google has multiple listings for your street address, we are unable to provide directions.";

    else if (directions.getStatus().code == G_GEO_SERVER_ERROR)
      error = ("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + directions.getStatus().code);
    
    else if (directions.getStatus().code == G_GEO_MISSING_QUERY)
      erro = ("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + directions.getStatus().code);
      
    else if (directions.getStatus().code == G_GEO_BAD_REQUEST)
      error = ("A directions request could not be successfully parsed.\n Error code: " + directions.getStatus().code);
     
    else error = ("An unknown error occurred.");
    
    if(error!="") error = "<p class='directions_error'>"  + error + "</p>";
    
    $("#loading_icon").hide();
    $("#route_info").html(error);
    
 }

 function onGDirectionsLoad(){ 
     $("#loading_icon").hide();
 }

