
var localSearch = new GlocalSearch();

var homepoint = "";

//this variable will collect the html which will eventualkly be placed in the side_bar
var side_bar_html = "";
var gmarkers = [];
//arrays to hold copies of the markers used by the side_bar
//because the function closure trick doesnt work there

var htmls = [];
//arrays to hold variants of the info window html with get direction forms open
var to_htmls = [];
var from_htmls = [];
var gdir = "";

var pbIcon = new GIcon();
pbIcon.image = 'images/mapicons/pin_plumbase/image.png';
pbIcon.shadow = 'images/mapicons/pin_plumbase/shadow.png';
pbIcon.iconSize = new GSize(18, 18);
pbIcon.shadowSize = new GSize(27, 18);
pbIcon.iconAnchor = new GPoint(0, 18);
pbIcon.infoWindowAnchor = new GPoint(9, 0);
pbIcon.printImage = 'images/mapicons/pin_plumbase/printImage.gif';
pbIcon.mozPrintImage = 'images/mapicons/pin_plumbase/mozPrintImage.gif';
pbIcon.printShadow = 'images/mapicons/pin_plumbase/printShadow.gif';
pbIcon.transparent = 'images/mapicons/pin_plumbase/transparent.png';
pbIcon.imageMap = [17, 0, 17, 1, 17, 2, 17, 3, 17, 4, 17, 5, 17, 6, 17, 7, 17, 8, 17, 9, 17, 10, 17, 11, 17, 12, 17, 13, 17, 14, 17, 15, 17, 16, 17, 17, 0, 17, 0, 16, 0, 15, 0, 14, 0, 13, 0, 12, 0, 11, 0, 10, 0, 9, 0, 8, 0, 7, 0, 6, 0, 5, 0, 4, 0, 3, 0, 2, 0, 1, 0, 0];


// This function picks up the click and opens the corresponding info window
function myclick(i) {
    map.getInfoWindow().show();
    map.getInfoWindow().hide();

    gmarkers[i].openInfoWindowHtml(htmls[i]);
}

//functions that open the directions forms
function tohere(i) {
    map.getInfoWindow().show();
    map.getInfoWindow().hide();

    gmarkers[i].openInfoWindowHtml(to_htmls[i]);
}
function fromhere(i) {
    map.getInfoWindow().show();
    map.getInfoWindow().hide();

    gmarkers[i].openInfoWindowHtml(from_htmls[i]);
}

// A function to create the marker and set up the event window
function createHomeMarkerPB(point) {
    var marker = new GMarker(point, pbIcon);
    return marker;
}

//===== request the directions =====
function getDirections() {
    //map.getInfoWindow().show();
    //map.getInfoWindow().hide();
    //==== Set up the walk and avoid highways options ====
    //var opts = {};
    //if (document.getElementById("walk").checked) {
    //    opts.travelMode = G_TRAVEL_MODE_WALKING;
    //}
    //if (document.getElementById("highways").checked) {
    //    opts.avoidHighways = true;
    //}
    // ==== set the start and end locations ====
    var saddr = document.getElementById("saddr").value
    var daddr = document.getElementById("daddr").value

    gdir.load("from: " + saddr + " to: " + daddr);
}


function createHomeMarker(point) {
    var marker = new GMarker(point);
    return marker;
}



// A function to create the marker and set up the event window
function createMarker(point, name, details, hoursDetails, conDetails, html) {
    var marker = new GMarker(point, pbIcon);

    var i = gmarkers.length;

    //The info window version with the "to here" form open
    to_htmls[i] = html + '<form action="javascript:getDirections()">' +
           '<input type="hidden" name="saddr" id="saddr" value="' + homepoint + '" /><br>' +
           '<INPUT value="Get Directions" TYPE="SUBMIT"><br>' +

           '<input type="hidden" id="daddr" value="' + name + "@" + point.lat() + ',' + point.lng() +
           '"/></form>';
    //The info window version with the "from here" form open
    //    from_htmls[i] = html + '<form action="javascript:getDirections()">' +
    //           '<input type="hidden" name="daddr" id="daddr" value="" /><br>' +
    //           '<INPUT value="Get Directions not used" TYPE="SUBMIT"><br>' +

    //           '<input type="hidden" id="saddr" value="' + name + "@" + point.lat() + ',' + point.lng() +
    //           '"/></form>';

    //    // The inactive version of the direction info
    html = html + '<br>Directions: <a href="javascript:document.goto' + i + '.submit();">To here<\/a> - <a href="javascript:fromhere(' + i + ')">From here<\/a>';

    GEvent.addListener(marker, "click", function() {

        map.getInfoWindow().show();
        map.getInfoWindow().hide();
        marker.openInfoWindowHtml(to_htmls[i]);
    });
    //save the info we need to use later for the side_bar
    gmarkers.push(marker);
    htmls[i] = html;

    htmls[i] = to_htmls[i];

    //add a line to the side_bar html
    side_bar_html += '<div class="branch" onClick="javascript:myclick(' + (gmarkers.length - 1) + ')">'
    side_bar_html += '<h5><a href="javascript:myclick(' + (gmarkers.length - 1) + ')">' + name + ' &raquo;<\/a></h5>';
    side_bar_html += details + "<br/>";
    side_bar_html += "<p>" + hoursDetails + "</p>";
    side_bar_html += "<p>" + conDetails + "</p>";
    side_bar_html += '<p><a href="javascript:myclick(' + (gmarkers.length - 1) + ')">highlight on map &raquo;<\/a></p></div>';
    return marker;

}






function usePointFromPostcode(postcode, callbackFunction) {

    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);
		        callbackFunction(point);
		    } else {
		        alert("Postcode not found!");
		    }
		});

    localSearch.execute(postcode + ", UK");
}


function loaddata(point) {

    GDownloadUrl("/branchesToXml.asp?lat=" + point.lat() + "&lng=" + point.lng(), function(doc) {
        map.clearOverlays();

        //Place the you are here marker

        var marker = createHomeMarker(point);
        map.addOverlay(marker);
        homepoint = point;

        var bounds = new GLatLngBounds();
        side_bar_html = "";
        //GDownloadUrl("example.aspx", function(doc) {
        var xmlDoc = GXml.parse(doc);
        var markers = xmlDoc.documentElement.getElementsByTagName("marker");


        for (var i = 0; i < markers.length; i++) {

            // obtain the attribues of each marker
            var lat = parseFloat(markers[i].getAttribute("lat"));
            var lng = parseFloat(markers[i].getAttribute("lng"));
            var point1 = new GLatLng(lat, lng);
            var html = markers[i].getAttribute("html");
            var label = markers[i].getAttribute("label");
            var details = markers[i].getAttribute("details");
            var hoursDetails = markers[i].getAttribute("openHours");
            var conDetails = markers[i].getAttribute("contact");

            // create the marker
            var marker = createMarker(point1, label, details, hoursDetails, conDetails, html);
            map.addOverlay(marker);
            bounds.extend(point1);
        }

        // put the assembled side_bar_html contents into the side_bar div
        document.getElementById("side_bar").innerHTML = side_bar_html;

        //map.setCenter(point, 10, G_HYBRID_MAP);
        // ===== determine the zoom level from the bounds =====
        map.setZoom(map.getBoundsZoomLevel(bounds));
        // ===== determine the centre from the bounds ======
        map.setCenter(bounds.getCenter());


    })
}




function loadAllData(point) {

    GDownloadUrl("/data/BranchesAll.xml", function(doc) {
        map.clearOverlays();

        //Place the you are here marker

        var marker = createHomeMarker(point);
        map.addOverlay(marker);

        var bounds = new GLatLngBounds();
        side_bar_html = "";
        //GDownloadUrl("example.aspx", function(doc) {
        var xmlDoc = GXml.parse(doc);
        var markers = xmlDoc.documentElement.getElementsByTagName("marker");


        for (var i = 0; i < markers.length; i++) {

            //            // obtain the attribues of each marker
            var lat = parseFloat(markers[i].getAttribute("lat"));
            var lng = parseFloat(markers[i].getAttribute("lng"));
            var point1 = new GLatLng(lat, lng);
            //            var html = markers[i].getAttribute("html");
            //            var label = markers[i].getAttribute("label");
            //            var details = markers[i].getAttribute("details");

            // create the marker
            var marker = createHomeMarkerPB(point1);
            map.addOverlay(marker);
            bounds.extend(point1);

        }

        // put the assembled side_bar_html contents into the side_bar div

        //document.getElementById("side_bar").innerHTML = side_bar_html;
        //map.setCenter(point, 10, G_HYBRID_MAP);

        // ===== determine the zoom level from the bounds =====
        map.setZoom(map.getBoundsZoomLevel(bounds));

        // ===== determine the centre from the bounds ======
        map.setCenter(bounds.getCenter());
    })

}  
       
