var coordinates = new Array();

$(document).ready(function () {
    initMap();

});

function initMap() {
    if ($("#location-map") == null)
        return;

    loadCoordinates();
    wireUpLocations();
    wireUpMap();

}

function loadCoordinates() {
    var locations = $("#location-map-locations").children(".location");

    $.each(locations, function (index, value) {
        value = $(value);

        coordinates[index] = new Coordinate(
                    value.children("a:first").html(),
                    $.trim(value.children(".coordinate-img:first").html()),
                    parseFloat($.trim(value.children(".coordinate-x:first").html())),
                    parseFloat($.trim(value.children(".coordinate-y:first").html())),
                    value.children("a:first"));

    });

}

function wireUpLocations() {
    var locations = $("#location-map-locations a")

    $.each(locations, function (index, value) {
        value = $(value);

        value.mouseenter(function () {
            showMarker(getCoordinateByCity($(this).html()));

        });

        value.mouseleave(function () {
            hideMarker();

        });

    });

}

function wireUpMap() {
    var map = $("#location-map-map");

    map.mousemove(function (e) {
        var x = parseFloat(e.pageX) - parseFloat($(this).offset().left);
        var y = parseFloat(e.pageY) - parseFloat($(this).offset().top);

        var xy = getCoordinateByCoordinate(x, y);

        if (xy == null) {
            hideMarker();
            clearAState();

            return;

        }

        showMarker(xy);
        setAState(xy.getA())

    });

    map.mouseout(function (e) {
        clearAState();

    });

    map.mousedown(function (e) {
        var x = parseFloat(e.pageX) - parseFloat($(this).offset().left);
        var y = parseFloat(e.pageY) - parseFloat($(this).offset().top);

        var xy = getCoordinateByCoordinate(x, y);

        if (xy == null)
            return;

        window.location = xy.getHref();

    });

}

function getCoordinateByCity(city) {
    for (var i = 0; i < coordinates.length; i++) {
        if (coordinates[i].getCity() == city)
            return coordinates[i];

    }

}

function getCoordinateByCoordinate(x, y) {
    var c;
    var cX;
    var cY;

    x = Math.floor(x); 
    y = Math.floor(y); 

//alert("X = " + x);
//alert("Y = " + y);

    for (var i = 0; i < coordinates.length; i++) {
        c = coordinates[i];
        cX = c.getX();
        cY = c.getY();

        if (x >= cX && y >= cY && x <= cX + 12 && y <= cY + 12)
            return c;

    }

    return null;

}

function setAState(a) {
    clearAState();
    a.addClass("hovered");

}

function clearAState() {
    var a = $("#location-map-locations").find("a");

    $.each(a, function(index, value) { 
        $(value).removeClass("hovered");
    });
}

function showMarker(xy) {
    if (xy == null)
        return;

    setBackground("url(" + xy.getImg() + ")");
    setCursor("pointer");

}

function hideMarker() {
    setBackground("none");
    setCursor("default");

}

function setBackground(background) {
    var map = $("#location-map-map");

    map.css("background-image", background);

}

function setCursor(cursor) {
    var map = $("#location-map-map");

    map.css("cursor", cursor);

}

function Coordinate(city, img, x, y, a) {
    this.getImg = function () {
        return img;

    }

    this.getCity = function () {
        return city;

    }

    this.getX = function () {
        return x;

    }

    this.getY = function () {
        return y;

    }

    this.getA = function () {
        return a;

    }

    this.getHref = function () {
        return a.attr("href");

    }

}
