/*!
 * Robo Gallery
 *
 * Copyright (c) 2010 Jeremy Dombrowski (robopixel.com)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */

var robo_gallery = {
    animating : false,
    gallery_scroll : function(direction, caller){
        var next_element;
        var width = 634;
        var position = {};

        //Stop if animating
        if(robo_gallery.animating === true){
            return false;
        }
        robo_gallery.animating = true;

        //Stop auto-scroll timer if caller is the user
        if(caller == 'user'){
            robo_gallery.timer.stop();
        }

        //Get the element to scroll in
        var current = $('#gallery_slides .current');
        if(direction == 'left'){
            next_element = current.prev();
            if(next_element.length < 1){
                next_element = $('#gallery_slides .slide:last');
            }
        } else if(direction == 'right'){
            next_element = current.next();
            if(next_element.length < 1){
                next_element = $('#gallery_slides .slide:first');
            }
        }
        //console.log(next_element.attr('id'));

        //Animate Slides
        position.next_elem = (direction == 'left') ? width *-1 : width;
        position.slider_anim = (direction == 'left') ? '+=' + width + 'px' : '-=' + width + 'px';

        next_element.css('left', position.next_elem).css('display', 'block');
        $('#gallery_slides .slide').animate({
            "left": position.slider_anim
        }, 'slow', 'swing', function(){
            $('#gallery_slides').css('left', '0').children().css('left', '0');

            //Finish Up
            $('#gallery_slides .current').removeClass('current').css('display', 'none');
            next_element.addClass('current').css('display', 'block');
            robo_gallery.animating = false;
        });

        //Set display markers
        $('#gallery_selector li').removeClass('current');
        $('#' + next_element.attr('id') + '_marker').addClass('current');

        return true;
    },

    //Auto scroll timer activated on pageload, deativated on interaction.
    timer : {
        timer_var : false, //Setup Timer Holder
        start : function(){
            robo_gallery.timer.timer_var = window.setInterval('robo_gallery.gallery_scroll("right", "timer")', 3500);
        },
        stop : function(){
            window.clearInterval(robo_gallery.timer.timer_var);
        }
    }
};

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

    //Start auto-scroll timer
    robo_gallery.timer.start();

    //Event Triggers
    $("div#gallery_button_left").click(function(){
        robo_gallery.gallery_scroll('left', 'user');
    });
    $("div#gallery_button_right").click(function(){
        robo_gallery.gallery_scroll('right', 'user');
    });


});

//Key Bindings
// $(document).keydown(function(e){
//     if (e.keyCode == 37) { //Left Arrow Key
//         robo_gallery.gallery_scroll('left', 'user');
//         return false;
//     } else if(e.keyCode == 39) { //Right Arrow Key
//         robo_gallery.gallery_scroll('right', 'user');
//         return false;
//     }
// });
