// 
//  googleMaps.js
//  brycefarrah.com
//  
//  Created by Mr Edwards on 2008-07-12.
//  Copyright 2008 Mr Edwards. All rights reserved.
// 
$(document).ready(function() {
    setGoogleMaps();
});


//Holds the reference to the marker.
var Gmarker;
//Holds the reference to the point on the map for the marker.
var point = new GLatLng(51.28660, -0.83526);

//Name of the marker.
var name = 'bryce:FARRAH';
//Address to be shown.
var address = '<p>bryce<span class="bfOrange">:</span>FARRAH</p>';


/**
 * Generates the google maps content
 *
 * @author Rob Edwards
 */
function setGoogleMaps() {
    //Check that the browser is compatible
    if (GBrowserIsCompatible()) {
        
        //Create the map div and create a Google maps instance
        var map = new GMap2(document.getElementById("map"));
        
        //Enable special zoom on browsers which support it
        map.enableContinuousZoom();
        
        //Add controls to the map
        map.addControl(new GLargeMapControl());
        map.addControl(new GMapTypeControl());
        
        //Set the start location of the map and the zoom level
        map.setCenter(new GLatLng(51.28660, -0.83526), 6);
        
        //Create the marker and append it to the Gmarker variable
        createMarker(point, name, address);
        
        //Add the marker to the map
        map.addOverlay(Gmarker);
        
        //Do this through a time out to allow for the map to load
        window.setTimeout(function() {
            //Want to have the map so it shows where we are. Following the load zoom in
            map.zoomIn();map.zoomIn();map.zoomIn();map.zoomIn();map.zoomIn();
            
            window.setTimeout(function() {
                //Following the zoom, display the info box
                Gmarker.openInfoWindowHtml(htmls);
            }, 500);
        //End first time out
        }, 3000);
    }
}


/**
 * Use this function to create the marker. It takes the address HTML and adds the
 * buttons to allow for the user to get directions 'to' or 'from' the point set.
 *
 * @author Rob Edwards
 */
function createMarker(point, name, address) {
    
    //Create the marker
    var marker = new GMarker(point);

    // The inactive version of the direction info
    html = address + '<p>Directions: <a href="javascript:tohere()">To here</a> - <a href="javascript:fromhere()">From here</a></p>';

    //Add the click functionality to the marker
    GEvent.addListener(marker, "click", function() {
      marker.openInfoWindowHtml(html);
    });
    
    //Store a reference to the marker and corrosponding html
    htmls = html;
    
    Gmarker = marker;    
}


/**
 * Sets the content for when the user clicks directions to here
 *
 * @author Rob Edwards
 */
function getToHtml() {
    // The info window version with the "to here" form open
    html = address + 
        '<p>Directions:<span class="bfOrange">To here</span> - <a href="javascript:fromhere()">From here</a></p>' +
        '<p>Start address:<form action="http://maps.google.com/maps" method="get" target="_blank">' +
        '<input type="text" size=40 maxlength=40 name="saddr" id="saddr" value="" /></p><br />' +
        '<p><input  value="Get Directions" type="submit" /></p>' +
        '<input type="hidden" name="daddr" value="' + point.lat() + ',' + point.lng() + '" /></form><br /><br /><br />';
        
    return html;
}


/**
 * Allows for the user to get directions from.
 *
 * @author Rob Edwards
 */
function getFromHtml() {
    // The info window version with the "to here" form open
    html = address +
        '<p>Directions: <a href="javascript:tohere()">To here</a> - <span class="bfOrange">From here</span></p>' +
        '<p>End address:<form action="http://maps.google.com/maps" method="get" target="_blank">' +
        '<input type="text" size=40 maxlength=40 name="daddr" id="daddr" value="" /></p><br />' +
        '<p><input id="getDirections" value="Get Directions" type="submit" /></p>' +
        '<input type="hidden" name="saddr" value="' + point.lat() + ',' + point.lng() + '" /></form><br /><br /><br />';
    return html;
}


/**
 * When the link is clicked displays the address with the to here form
 *
 * @author Rob Edwards
 */
function tohere() {
    Gmarker.openInfoWindowHtml(getToHtml());
}


/**
 * When the link is clicked displays the address with the from here form
 *
 * @author Rob Edwards
 */
function fromhere() {
    Gmarker.openInfoWindowHtml(getFromHtml());
}