var url_root = "http://eugene.adamcoddington.net/";
var slider_count = 30;
var slider_offscreen = 10;
var final_slider_count = 30;
var square_width = 94;
var natural_offset = (0 - (square_width * slider_offscreen));
var in_progress = false;
var drag_in_progress = false
var debug_div = "#column_left"
var debug_div_important = "#column_center"

function debug(message, important)
{
    $(debug_div).prepend(message + "<br />")
    if(important !== undefined)
    {
        $(debug_div_important).prepend(message + "<br />")
    }
}
function jump_to_id(id)
{
    $("#entry_display").slideUp()
    $("#entry_display").html("")
    $("#photo_display").load(
        "/slider/get_photo_display/" + id + "/", {}, function()
        {
            $("#photo_display").slideDown()
        }
    )
}
function get_entry(id)
{
    $("#photo_display").slideUp()
    entry_display = $("#entry_display")
    entry_display.load(
        "/slider/get_entry/" + id + "/", {}, function(){
            entry_display.slideDown()
        })
}
function slider_mark_active(id)
{
    debug("Marking " + id + " as active.")
    matches = $("#photo_slider img");
    match_count = matches.length;
    match_curr = 0;
    while(match_curr < match_count)
    {
        if($(matches[match_curr]).attr("rel") == id)
        {
            $(matches[match_curr]).animate(
                {
                    opacity: 0.3
                }
            )
        }
        else
        {
            match = $(matches[match_curr])
            if(match.css("opacity") < 1)
            {
                match.animate(
                    {
                        opacity: 1
                    }
                )
            }
        }
        match_curr = match_curr + 1;
    }
}
function slider_jump_to_id(id)
{
    if(! drag_in_progress)
    {
        debug("Jumping to id " + id)
        slider_mark_active(id);
        matches = $("#photo_slider img");
        match_count = matches.length;
        match_curr = 0;
        if(! in_progress)
        {
            in_progress = true
            setTimeout(function(){in_progress = false}, 500)
            jump_to_id(id)
            in_progress = false
        }
    }
    else
    {
        debug("Drag is in progress.  Cannot jump.");
    }
}
function advance_slider(callback)
{
    slider_count = final_slider_count;
    debug("Advancing slider...")
    min_id = get_min_slider_id()
    max_id = get_max_slider_id()
    $.ajax({
        url: "/slider/advance/" + min_id + "/" + max_id + "/",
        success: function(data)
            {
                matches = $("#photo_slider div")
                slider = $("#photo_slider");
                new_element = $(data).css("width", "0px")
                old_element = $(matches[matches.length - 1])
                $("#photo_slider").prepend(new_element);
                new_element.animate({width: 94}, 250)
                slider.animate({left: slider.position().left + 94}, 250);
                old_element.animate(
                    {
                        width: 0
                    }, 250, function(){
                        debug("Removing last slider element.")
                        old_element.remove();
                        if(callback !== undefined)
                        {
                            callback()
                        }
                    }
                );
            }
        })
}
function retreat_slider(callback)
{
    slider_count = final_slider_count;
    debug("Retreating slider...");
    min_id = get_min_slider_id();
    max_id = get_max_slider_id();
    $.ajax({
        url: "/slider/retreat/" + min_id + "/" + max_id + "/",
        success: function(data)
            {
                matches = $("#photo_slider div");
                slider = $("#photo_slider");
                new_element = $(data).css("width", "0px")
                old_element = $(matches[0])
                $("#photo_slider").append(new_element);
                new_element.animate({width: 94}, 250);
                slider.animate({left: slider.position().left - 94}, 250);
                old_element.animate(
                    {
                        width: 0
                    }, 250, function(){
                        debug("Removing first slider element.")
                        old_element.remove();
                        if(callback !== undefined)
                        {
                            callback();
                        }
                    }
                );
            }
        })
}
function get_min_slider_id()
{
    matches = $("#photo_slider img");
    return $(matches[matches.length - 1]).attr("rel")
}
function get_max_slider_id()
{
    matches = $("#photo_slider img");
    return $(matches[0]).attr("rel");
}
function get_current_id()
{
    return $("#photo_display img").attr("rel");
}
