var west_most = 113.2; // west-most longitude - Steep Point, WA
var north_most = -10; // noth-most latitude - Cape York, QLD. including a margin for the top-most plot.
var east_most = 154; // Byron Bay, NSW
var south_most = -43.7; // Tassie somewhere

var map_width = 685;
var map_height = 660;

var map_width_in_degrees = east_most - west_most;
var pixels_per_degree_longitude = map_width/map_width_in_degrees;

var map_height_in_degrees = north_most - south_most;
var pixels_per_degree_latitude = map_height/map_height_in_degrees;

var x_pos = west_most;
var y_pos = north_most;

var centre_x = 240;
var centre_y = 300;

window.pixelMapper = {
    
    init: function() {
        // If pixel locations need to be plotted
        if (journey_pixel_maps) {
            pixelMapper.drawPoints(journey_pixel_maps);
            // If url is .../journeys/...
            if (window.location.pathname.indexOf('journeys') == 1)
                pixelMapper.focusOnMeanLocation(journey_pixel_maps);
        }
            
    },
    
    drawPoints: function(store) {
        // sequentially insert all points into the page as dots
        for (name in store) {
            var point = store[name];
            draw_point(point.x, point.y, point.order, name, point.url, name)
        }
    },
    
    focusOnMeanLocation: function(store) {
        // focus on the average point between all the pixel locations
        var x = 0, y = 0, count = 0;
        for (name in store) {
            count++;
            x += store[name].x
            y += store[name].y
        }
        x = Math.round(x / count);
        y = Math.round(y / count);
        animate_map_to_point(x, y);
    },
    
};

function dot_over(id, animate) {
    $("#item-" + id + " span.dot").addClass("hover");
    $("#item-" + id).addClass("hover");
    
    $("#dot-" + id).addClass("hover");
    $("#dot-" + id).css("z-index", 99999); //gosh
    
    loc_id = $("#location-item-" + id).parent().parent().parent().attr("id");
    $("#"+loc_id).trigger('click');
    
    if (animate) {
        if($("#"+loc_id).length > 0) {
            animate_map_to_point($("#"+loc_id).css("left").replace("px", ""), $("#"+loc_id).css("top").replace("px", ""));
        }
        if($("#dot-" + id).length > 0) {
            animate_map_to_point($("#dot-" + id).css("left").replace("px", ""), $("#dot-" + id).css("top").replace("px", ""));
        }
    }
};

function get_centre_x() {
    if($("#embedded_journey").length != 0) {
        return $("#embedded_journey").width()/2
    }
    return centre_x;
}

function get_centre_y() {
    if($("#embedded_journey").length != 0) {
        return $("#embedded_journey").height()/2
    }
    return centre_y;
}


function animate_map_to_point(x, y) {
    x_animate = get_centre_x() - x;
    y_animate = get_centre_y() - y;

    $("#movablemap").animate( { top: y_animate, left:x_animate }, { queue:true, duration:500 } )
};

function animate_map_to_coordinate(lat, lng) {
    x_animate = get_centre_x() - longitude_to_x(lng);
    y_animate = get_centre_y() - latitude_to_y(lat);
    $("#movablemap").animate( { top: y_animate, left:x_animate }, { queue:true, duration:500 } )
};

function dot_out(id) {
    $("#item-" + id + " span.dot").removeClass("hover");
    $("#item-" + id).removeClass("hover");
    $("#dot-" + id).removeClass("hover");
    $("#dot-" + id).css("z-index", 1);
};

function draw_point(x, y, id, title, url, location) {
    // Be careful! New firefox crashes JS on the .console log
    // when firebug is not activated.
        
    var text = "";
    var url = url;
    // draw a text label if it's off the map, or if this is an embed widget
    if (! ( (x >=0 && x <= map_width) && (y >= 0 && y <= map_height ) ) )
    {   text = '<span class="label">' + location + '</span>';   }
    
    if ($("#embedded_journey").length != 0)
    {   
        text = '<span class="label">' + location + '</span>';
        url = "http://www.curiousworks.org.au" + url;
    }
    
    dot = '<a href="' + url + '" title="' + title +'" alt="' + title + '"><span id="dot-' + id + '" class="dot new" style="position:absolute;top:' + y + 'px;left:' + x + 'px;padding-left:16px;">' +  text + '</span></a>';
    $('#movablemap').append(dot);
    if($.bindDot)
        $.bindDot(id);
}

function plot_point(lat, lng, id, title, url, location) {
    draw_point(longitude_to_x(lng), latitude_to_y(lat), id, title, url, location);
}

function longitude_to_x(lng) {
    return (lng - x_pos)*pixels_per_degree_longitude;
}

function latitude_to_y(lat) {
    return (Math.abs(lat) - Math.abs(y_pos))*pixels_per_degree_latitude;
}

/* call this function to calibrate the points on the map */
/* may need future work on WGS84 for accuracy */
function plot_calibration_points() {
    plot_point(-10.7, 142.5, 0, "N Cape York, QLD", "/");
    plot_point(-28.6, 153.6, 0, "E Byron Bay, NSW", "/");
    plot_point(-43.7, 146.9, 0, "S Tassie somewhere", "/");
    plot_point(-25.7, 113.2, 0, "W Steep Point, WA", "/");
    plot_point(-33, 138, 0, "Port Augusta", "/");
    plot_point(-33.8, 115.3, 0, "Naturaliste, WA near Margaret River", "/");
    plot_point(-34.4, 115.1, 0, "Leeuwin, WA near Margaret River", "/");
    plot_point(-11.6, 130.7, 0, "near Darwin", "/");
    plot_point(-18.1, 140.4, 0, "south-most point of Arafura Sea", "/");
    plot_point(-35, 135.7, "south-most point, near Port Lincoln", "/");
}

function create_location(longitude, latitude, id) {
    $("#movablemap").append('<div id="location-' + id + '" class="location-box hidden shadow collapsed"><span id="location-open-' + id + '" class="item-count">0</span><div class="list hidden" id="location-list-' + id + '">At this location:<span id="close-location-' + id + '" class="close">Close X</span><ul></ul></div></div>');
    
    $("#location-" + id).css("top", latitude_to_y(latitude)+"px");
    $("#location-" + id).css("left", longitude_to_x(longitude)+"px");
    
    $("#location-open-" + id).click(function() {
        $(this).parent().removeClass("collapsed");
        $(this).next(".list").removeClass("hidden");
    });
    
    $("#close-location-" + id).click(function() {
        $("#location-list-"+id).addClass("hidden");
        $("#location-"+id).addClass("collapsed");
    });
}

function plot_item(item_id, group_id, title) {
    if ( $("#location-" + group_id).hasClass("hidden")) {
        $("#location-" + group_id).removeClass("hidden");
    }
    count = $("#location-" + group_id + ">.item-count").text();
    $("#location-" + group_id + ">.item-count").text(parseInt(count)+1);
    $("#location-" + group_id + ">div>ul").append('<li id="location-item-' + item_id + '" class="location-item">' + title + '</li>');
    
    $(".location-item").click(function() {
        id = $(this).attr("id").replace("location-item-", "");
        dot_over(id, true);
        $('html,body').queue([]).stop();  //we clear the queue first.
        $.scrollTo($("#item-"+id).stop(1), { duration:500, offset:-40, queue: false });
    });
}

