/**
 * plugin-left-right-controls.js
 * =============================
 * Developer: David G. Daniel
 *
 *
 * The main point of this file is to provide a central store for configuring and setting up left/right controls for an
 * element that requires its contents to go left and right.
 */

var DEFAULT_ITEMS_PER_VIEW = 5;

function setup_next_control(gallery, items_per_view)
{
    if (items_per_view == undefined)
        items_per_view = DEFAULT_ITEMS_PER_VIEW;

    gallery = $(gallery);
    control = $(gallery.find(".link-next")[0]);
    gallery = $(gallery.find(".main-gallery")[0]);
    gallery.attr('next_counter', 0);

    control.click(function(e)
    {
        e.preventDefault();
        elements = $(gallery.find('li'));
        /*
        $(gallery).hide("slide", { direction : 'right' }, 'slow', function()
        {
            // move the elements around
            var visible_elements = elements.splice(0, 5);
            elements = $("<ul />").append(elements).append(visible_elements);

            // bring it back
            $(this).html("");
            $(this).append(elements);
            $(this).show('slide', { direction : 'left' }, 'slow');
        });
        */
        var element_array = $.makeArray(elements);
        var last = element_array.pop();
        element_array.unshift(last);


        $(elements).each(function(index, value)
        {
            var final = $("<ul></ul>").append(element_array);
            $(gallery.children('ul').html(final));
            /*$(this).css('position', 'relative').animate({ direction : 'left', left : "+=195" }, 'slow');*/
        });
    });
}

function setup_prev_control(gallery, items_per_view)
{
    if (items_per_view == undefined)
        items_per_view = DEFAULT_ITEMS_PER_VIEW;
    
    gallery = $(gallery);
    control = $(gallery.find(".link-prev")[0]);
    gallery = $(gallery.find(".main-gallery")[0]);
    gallery.attr('prev_counter', 0);
    
    control.click(function(e)
    {
        e.preventDefault();
        elements = $(gallery.find('li'));
        /*
        $(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);
            
            $(this).html("");
            $(this).append(elements);
            $(this).show("slide", { direction : 'right' }, 'slow');
        });
        */
        var element_array = $.makeArray(elements);
        var first = element_array.shift();
        element_array.push(first);
        
        $(elements).each(function(index, value)
        {
            var final = $("<ul></ul>").append(element_array);
            $(gallery.children('ul').html(final));
            /*$(this).css('position', 'relative').animate({ direction : 'left', left : "+=195" }, 'slow');*/
        });
    });
}
