/// <reference path="../jquery-1.2.6-vsdoc.js" />
google.setOnLoadCallback(function() {

    $("#first").focus(function() {
        $("#first").select();
    });

    $("#first").keyup(function(event) {

        first = $("#first").val();

        setTimeout(runFirstSearch, 2000);

        if (isValidPostcode(first)) {
            first = formatPostcode(first);
            $("#first").removeClass("oops");
            $("#first").val(first);
        }

    });

    $("#second").focus(function() {
        $("#second").select();
    });

    $("#second").keyup(function(event) {

        second = $("#second").val();

        setTimeout(runSecondSearch, 2000);

        if (isValidPostcode(second)) {
            second = formatPostcode(second);
            $("#second").removeClass("oops");
            $("#second").val(second);
        }

    });

    $("#example1").click(function() {
        return showExample("GU9 0RA", "AB21 0GU");
    });

    $("#example2").click(function() {
        return showExample("N16 0DU", "SE1 8NY");
    });

    $("#example3").click(function() {
        return showExample("WA3 6TY", "YO31 0UD");
    });


    $("#tabs").tabs();

    dp.SyntaxHighlighter.HighlightAll('code');

    $("#first").focus();

    $("ul").removeClass("ui-tabs-hide");

});

var lastfirst = "";
var lastsecond = "";
var first = "";
var second = "";

function showExample(a, b) {
    $("#first").val(a);
    $("#second").val(b);
    first = a;
    second = b;
    $("#second").keyup();
    return false();
}

function runFirstSearch() {

    if ($("#first").val() == first) {

        if (first != lastfirst) {

            lastfirst = first;

            if (isValidPostcode(first)) {

                runSearch();
                
            } else {
                
                $("#first").addClass("oops");
            
            }
        }
    }
}

function runSecondSearch() {

    if ($("#second").val() == second) {

        if (second != lastsecond) {

            lastsecond = second;

            if (isValidPostcode(second)) {

                runSearch();

            } else {

                $("#second").addClass("oops");

            }
        }
    }
}

function runSearch() {

    var url = "/postcode/search.rails?first=" + first + "&second=" + second + "&ticks=" + getTicks();

    $(".loader").show();

    $.getJSON(url, function(data) {

        $(".loader").hide();

        $("#free-the-postcode-a").html(data.freeThePostcodeA);
        $("#free-the-postcode-b").html(data.freeThePostcodeB);

        $("#new-popular-edition-maps-a").html(data.newPopularEditionMapsA);
        $("#new-popular-edition-maps-b").html(data.newPopularEditionMapsB);

        $("#ernest-marples-a").html(data.ernestMarplesA);
        $("#ernest-marples-b").html(data.ernestMarplesB);

        $("#locating-postboxes-a").html(data.locatingPostboxesA);
        $("#locating-postboxes-b").html(data.locatingPostboxesB);

        $("#google-maps-a").html(data.googleMapsA);
        $("#google-maps-b").html(data.googleMapsB);

        if (data.miles > 0) {
            $("#miles").html(data.miles);
            $("#kilometers").html(data.kilometers);
            $(".distance").css("visibility", "visible");
        } else {
            $(".distance").css("visibility", "hidden");
        }

    });

}

function formatPostcode(p) {
    if (isValidPostcode(p)) {
        var postcodeRegEx = /(^[A-Z]{1,2}[0-9]{1,2})([0-9][A-Z]{2}$)/i;
        return p.replace(postcodeRegEx, "$1 $2");
    } else {
        return p;
    }
}

function isValidPostcode(p) {

    if (p == "") return true;

    var postcodeRegEx = /[A-Z]{1,2}[0-9]{1,2} ?[0-9][A-Z]{2}/i;
    return postcodeRegEx.test(p);
}