if (window.drivve === undefined)
{
    drivve = {};
}

drivve.slideshow =
{
    current_index: -9999,

    initialize: function(name)
    {
        var root = jQuery('.slideShow.' + name);
        root.find(".quit").bind('click', drivve.slideshow.dismiss);
        root.find(".toLeft").bind('click', function() { return drivve.slideshow.scrollTo(root, drivve.slideshow.current_index - 1, true); } );
        root.find(".toRight").bind('click', function() { return drivve.slideshow.scrollTo(root, drivve.slideshow.current_index + 1, true); } );

        root.find("img").css({opacity: 0.3, marginLeft:0, marginRight:320, marginTop:120, width: 320, height: 213});

        drivve.slideshow.scrollTo(root, 0, false);
    },

    scrollTo: function(root, index, animate)
    {
        var img_count = root.find("img").length;
        if ((index < 0) || (index >= img_count))
        {
            return false;
        }

        var ul_params = 
        {
            left: 61 - (index * 680)
        };

        var img_params = 
        {
            opacity: 0.3, 
            marginLeft: (index > drivve.slideshow.current_index) ? 320 : 0, 
            marginRight: (index > drivve.slideshow.current_index) ? 0 : 320, 
            marginTop: 120, 
            width: 320, 
            height: 213
        };

        var next_img_params = 
        {
            opacity: 1.0, 
            marginLeft: 0, 
            marginRight: 0, 
            marginTop: 0, 
            width: 640, 
            height: 426
        };

        if (animate)
        {
            root.find("ul").animate(ul_params);
            root.find("img:eq(" + drivve.slideshow.current_index + ")").animate(img_params);
            root.find("img:eq(" + index + ")").animate(next_img_params);
        }
        else
        {
            root.find("ul").css(ul_params);
            root.find("img:eq(" + drivve.slideshow.current_index + ")").css(img_params);
            root.find("img:eq(" + index + ")").css(next_img_params);
        }

        drivve.slideshow.current_index = index;
        if (drivve.slideshow.current_index == 0)
        {
            root.find(".toLeft").hide();
            root.find(".toRight").show();
        }
        else if (drivve.slideshow.current_index+1 >= img_count )
        {
            root.find(".toLeft").show();
            root.find(".toRight").hide();
        }
        else
        {
            root.find(".toLeft").show();
            root.find(".toRight").show();
        }

        return false;
    },

    display: function(evt)
    {
        var name = jQuery(this).attr('href');
        var slideShow = jQuery('.slideShow.' + name);

        slideShow.fadeIn();

        return false;
    },

    dismiss: function(evt)
    {
        var slideShow = jQuery(this).parents('.slideShow');
        slideShow.fadeOut();

        return false;
    }
};

jQuery(document).ready(function()
{
    var initialized = {};
    jQuery("a.triggerSlideShow").each(function()
    {
        var name = jQuery(this).attr('href');
        if (initialized[name] === undefined)
        {
            drivve.slideshow.initialize(name);
            initialized[name] = true;
        }

        jQuery(this).bind('click', drivve.slideshow.display);
    });
});
