var RETRIEVE_STORIES_URL = "/stories/story/ajax/retrieve_media/";

// ================================================================================================ UTILITY FUNCTIONS ==
// http://blog.freelancer-id.com/index.php/2009/03/26/scroll-window-smoothly-in-jquery
function scrollWin(anchor)
{
    $('html, body').animate({
        scrollTop: $(anchor).offset().top
        }, 1000);
}

// ================================================================================================= ACTION FUNCTIONS ==
function scrollToLightbox()
{
    scrollWin(".lightbox-content");
}

function setup_close(scroll_to)
{
    $(".close").click(function(event)
    {
        event.preventDefault();
        $(".lightbox_home").fadeOut("fast", function()
        {
            $(".lightbox_home").remove();
        });
    });
}

function setup_lightswitch()
{
    $(".light-switch a").each(function(index, value)
    {
        $(value).click(function(event)
        {
            event.preventDefault();
            var selected_story = "#storymedia" + index;
            $(".light-switch li").removeClass("active");
            $(this).parent().addClass("active");
            $(".light-gallery li").fadeOut("slow", function()
            {
                $(selected_story).fadeIn("slow");
            });

        });
    });
}

function load_stories(e)
{
    $(this).css("cursor", "pointer");
    var story_id = $(this).find("a").attr('href').substring(1);
    var STORIES_URL = RETRIEVE_STORIES_URL + story_id;
    var oldvalue = $(this).html();
    var me = $(this);
    // me.html("Loading...");

    $.ajax({
        url : STORIES_URL,
        success: function(data)
        {
            $("body").append("<div class='lightbox_home'>" + data + "</div>");
            // scrollToLightbox();
            // $(".lightbox img").width("100%");
            setup_lightswitch();
            setup_close($(e.currentTarget));
            me.html(oldvalue);
            $(".lightbox_home").offset({ top: $("body").scrollTop() , left : 0 });
            $('html,body').animate({scrollTop: $(".lightbox_home").offset().top},'slow');
        },
        error: function(jqXHR, textStatus, errorThrown)
        {
            if (jqXHR.status == 404)
            {
                alert("ERROR-404: This story was not found in the database.");
            }
            else
            {
                alert("ERROR-500: This story could not be loaded due to an internal error.");
            }
            me.html(oldvalue);
        }
    });
}

function get_gallery_elements(e)
{
    return $(".main-gallery li");
}

// ===================================================================================== GALLERY NAVIGATION FUNCTIONS ==
/*
function setup_gallery_next(e)
{
    e.preventDefault();
    var elements = get_gallery_elements();
    
    // reenter contents into the DOM
    var gallery = $(".main-gallery");

    $(gallery).hide("slide", { direction : 'right' }, 'slow', function()
    {
        // move the elements around
        var visible_elms = elements.splice(0,5);
        elements = $("<ul></ul>").append(elements).append(visible_elms);

        gallery.html("");
        gallery.append(elements);

        gallery.show("slide", { direction : 'left' }, 'slow');
    });
}

function setup_gallery_prev(e)
{
    e.preventDefault();
    var elements = get_gallery_elements();

    // reenter contents into the DOM
    var gallery = $(".main-gallery");
    $(gallery).hide("slide", { direction : 'left' }, 'slow', function()
    {
        // move the elements around
        var visible_elms = elements.splice(elements.length - 5,5);
        elements = $("<ul></ul>").append(elements).prepend(visible_elms);

        gallery.html("");
        gallery.append(elements);
        gallery.show("slide", { direction : 'right' }, 'slow');
    });
}*/


// ============================================================================================================= MAIN ==
$(document).ready(function()
{
    $(".storyplugin .story-box").parent().click(load_stories);
    $(".storyplugin .box-narrow-content").parent().click(load_stories);
    //$(".link-prev").click(setup_gallery_prev);
    //$(".link-next").click(setup_gallery_next);
    setup_prev_control(".storyplugin");
    setup_next_control(".storyplugin");
})
