﻿(function($) {

    $.fn.albumsAnim = function(options) {

        // default configuration properties
        var defaults = {
            prevId: 'deckLeft',
            nextId: 'deckRight',
            speed: 800
        };

        var options = $.extend(defaults, options);

        return this.each(function() {

            obj = $(this);
            var currentDeck = 0;
            var maxDecks = $("ul:first", this).children().length / 7;

            var objRef = "#" + obj.attr("id");

            $("#" + options.nextId).hover(function() {
                $("#" + options.nextId).addClass("active");
            }, function() {
                $("#" + options.nextId).removeClass("active");
            });

            $("#" + options.nextId).click(function() {
                if (currentDeck < (maxDecks - 1)) {
                    var deckPos = ((currentDeck + 1) * -596);
                    $("ul:first", objRef).animate({ marginLeft: deckPos }, 1800);
                    currentDeck++;
                }
            });

            $("#" + options.prevId).hover(function() {
                $("#" + options.prevId).toggleClass("active");
            }, function() {
                $("#" + options.prevId).toggleClass("active");
            });

            $("#" + options.prevId).click(function() {
                if (currentDeck > 0) {
                    var deckPos = ((currentDeck - 1) * -596);
                    $("ul:first", objRef).animate({ marginLeft: deckPos }, 1800);
                    currentDeck--;
                }
            });

        });

    };

    $.fn.albumPreview = function(options) {

        // default configuration properties
        var defaults = {};

        var options = $.extend(defaults, options);

        return this.each(function() {

            obj = $(this);
            var objRef = "#" + obj.attr("id");

            $("li", this).each(function() {

                $(this).bind("click", function() {
                    var imgSrc = $("img:first", this).attr("src");
                    var fileName = imgSrc.split('/').pop();

                    $(".previewcontainer a").each(function() {
                        if (!$(this).hasClass("unvisiblebox")) {
                            $(this).addClass("unvisiblebox");
                            $(this).removeClass("visiblebox");
                        }
                    });

                    WebForm_DoCallback("__Page", fileName, ActOnPreview, "", null, false);
                });
            });

            //Hide all thumbs in the thumbs list
            var thumbArray = new Array();
            var midArray = new Array();

            $("#deck img").each(function() {
                url = $(this).attr("src");
                $(this).attr("src", "");
                thumbArray.push(url);
            });
            $(".previewcontainer img").each(function(index) {
                if (index > 0) {
                    url = $(this).attr("src");
                    $(this).attr("src", "");
                    midArray.push(url);
                }
            });

            $("#deck ul").children().hide();
            thumbArray.reverse();
            midArray.reverse();

            //Load images after document ready event
            $(window).bind("load", function() {

                $(".previewcontainer img").each(function(index) {
                    if(index > 0) 
                        $(this).attr("src", midArray.pop());
                });

                $("#deck img").each(function(index) {
                    $(this).attr("src", thumbArray.pop());
                    $(this).parent().show();
                });
            });

            ActOnPreview('');
        });
    };

    function ActOnPreview(arg) {
        if (arg != '') {
            $('#' + arg).removeClass("unvisiblebox");
            $('#' + arg).addClass("visiblebox");
        }

        var imgWidth = $(".visiblebox img").css("width");
        var imgHeight = $(".visiblebox img").css("height");
        var restHeight = 350 - imgHeight.replace("px", "");

        if (restHeight > 0)
            $(".visiblebox img").css("margin-top", restHeight / 2);

        $(".previewcontainer").width(imgWidth);
    }

})(jQuery);
