/// <reference path="../../libraries/jquery/js/jquery-1.3.2-vsdoc2.js"/>

$(function()
{
    var locations =
    [
		["", new GLatLng(-24.902381, 134.809569)],
					["birkdale", new GLatLng(-27.496344, 153.228866)],
					["sunnybankhills", new GLatLng(-27.606733, 153.055889)],
					["pullenvale", new GLatLng(-27.506262, 152.854354)],
					["sorrento", new GLatLng(-31.826104, 115.758792)],
					["aspley", new GLatLng(-27.365438, 153.011997)],
					["narrewarren", new GLatLng(-38.047827, 145.305367)],
					["stgeorgesbasin", new GLatLng(-35.085788, 150.598052)],
					["tweedheads", new GLatLng(-28.173415, 153.542948)],
					["dalyellup", new GLatLng(-33.397357, 115.611835)],
					["capalaba", new GLatLng(-27.551408, 153.200955)],
		    ];

    var map = new GMap2($("#map")[0]);
    var geocoder = new GClientGeocoder();
    var directions = new GDirections(map);

    map.setUIToDefault();
    map.disableScrollWheelZoom();
    map.setCenter(locations[0][1], 3);
    geocoder.setBaseCountryCode(".com.au");

    for (var i = 1; i < locations.length; i++)
    {
        var marker = new GMarker(locations[i][1], { title: $("#studio option[value='"+locations[i][0]+"']").text() });
        marker.bindInfoWindowHtml($(".info-container."+locations[i][0]).html());

        locations[i][2] = marker;

        map.addOverlay(marker);
    }

    $("#studio").change(function()
    {
        for (var i = 0; i < locations.length; i++)
        {
            if (locations[i][0] == $(this).val())
            {
                //directions.clear();

                map.setCenter(locations[i][1], (i == 0) ? 3 : 13);
                locations[i][2].openInfoWindowHtml($(".info-container."+locations[i][0]).html());

                return;
            }
        }
    });

    if ($("#studio").val() != "")
        $("#studio").change();

    $("#button-match").click(function()
    {
        var postcode = $("#postcode").val().trim();

        if (postcode.length != 4)
        {
            alert("Please enter a valid postcode to use this feature.");
            $("#postcode").focus();

            return;
        }

        var address = postcode + " Australia";

        geocoder.getLatLng
        (
            address,
            function(latlng)
            {
                if (latlng != null)
                {
                    var closestIndex = 0;
                    var closestDist = Number.MAX_VALUE;

                    for (var i = 1; i < locations.length; i++)
                    {
                        var dist = latlng.distanceFrom(locations[i][1]);

                        if (dist < closestDist)
                        {
                            closestIndex = i;
                            closestDist = dist;
                        }
                    }

                    /*var listener = GEvent.addListener
                    (
                        directions,
                        "load",
                        function()
                        {
                            if (directions.getStatus().code == G_GEO_SUCCESS)
                            {
                                $("#studio").val(locations[closestIndex][0]);
                                $("#studio").change();
                            }
                            else
                                alert("Unfortunately we couldn't find a match for you.\nPlease choose a studio manually.");

                            GEvent.removeListener(listener);
                        }
                    );

                    if (closestIndex > 0)
                        directions.load(latlng.toUrlValue() + " to " + locations[closestIndex][1].toUrlValue());*/

                    if (closestIndex > 0)
                    {
                        $("#studio").val(locations[closestIndex][0]);
                        $("#studio").change();
                    }
                }
                else
                    alert("Unfortunately we couldn't locate you.\nPlease choose a studio manually.");
            }
        );
    });
});

