(function ($) {
    $.fn.ivalbum = function (options) {
        var defaults = {
            url: "imagen.php",
            bigpath: "images/album/",
            thumbpath: "images/thumbs/",
            classname: "album",
            callback: "",
            speed: 400,
            easing: "linear"
        };
        var options = $.extend(defaults, options);
        var theurl = options.url;
        var theclass = options.classname;
        var thethumbs = "." + theclass + "-thumbs";
        var theprev = "." + theclass + "-prev";
        var thenext = "." + theclass + "-next";
        var theclose = "." + theclass + "-close";
        var theview = "." + theclass + "-view";

        var thebigpath = options.bigpath;
        var thethumbpath = options.thumbpath;
        var thumbwide;
        var thecallback = options.callback;

        var speed = options.speed;
        var easing = options.easing;

        var appendable = this;

        var whereto = theurl + ((theurl.indexOf("?") >= 0) ? "&class=" : "?class=") + theclass;
        var imggone = false;
        $.ajax({
            type: "GET",
            url: whereto,
            success: function (data) {
                var readimg = []
                var s = 0;
                $(data).find(thethumbs + " img").each(function () {
                    readimg[s] = $(this).attr("src");
                    s += 1;
                });
                if ($("." + theclass).length == 0) {
                    appendable.append(data);
                    $(theview).bind("click", function () {
                        if ($(thethumbs + " ." + theclass + "-active").next().length > 0) {
                            $(thethumbs + " ." + theclass + "-active").next().trigger("click");
                        } else {
                            $(thethumbs + ">span").first().trigger("click");
                        }
                    });
                }
                appendable.find(thethumbs).empty();
                var z = 0;
                $.each(readimg, function () {
                    $("<span><span></span><img src='' alt='' /></span>").appendTo(thethumbs).find("img").bind("load", function () {
                        z += 1;
                        if (z == readimg.length) {
                            loaded();
                        }
                    }).attr("src", this);
                });
                theend();
            }
        });
        var thumbmax = 0;
        function loaded() {
            thumbwide = $(thethumbs).width() * 0.9;
            $(thethumbs).animate({ scrollLeft: "+=1000000" }, 0, function () {
                thumbmax = $(thethumbs).scrollLeft();
                $(thethumbs).scrollLeft(0);
                if (thumbmax > 0 && $(thenext).length == 0) {
                    $("." + theclass).append("<span class='" + theclass + "-next'></span><span class='" + theclass + "-prev'></span>");
                    $(thenext).bind("click", function () {
                        $(thethumbs).stop().animate({ scrollLeft: "+=" + thumbwide }, 500);
                    });
                    $(theprev).bind("click", function () {
                        $(thethumbs).stop().animate({ scrollLeft: "-=" + thumbwide }, 500);
                    });
                } else if (thumbmax == 0) {
                    $(thenext + ", " + theprev).remove();
                }
            });
            setupThumbs();
            $(thethumbs + ">span").first().trigger("click");
        }
        function setupThumbs() {
            $(thethumbs + ">span").bind("click", function () {
                $(theview + " img").stop(true, false).fadeTo(speed, 0, easing, function () {
                    appendimg();
                });
                $(this).addClass(theclass + "-active").stop().fadeTo(50, 1).siblings().stop().removeClass(theclass + "-active").fadeTo(300, .5);

                var src = $(this).children("img").attr("src").split(thethumbpath)[1];
                $("<img class='reeufnwditb' src='" + thebigpath + src + "' style='position:absolute;height:1px;width:1px;top:-100px;' />").bind("load", function () {
                    imggone = true;
                }).appendTo("body");
                var position = $(this).position();
                if (position.left + $(this).width() >= thumbwide || position.left < 0) {
                    $(thethumbs).stop().animate({ scrollLeft: "+=" + ((position.left < 0) ? position.left - thumbwide : position.left) }, 800);
                }
            }).bind("mouseenter", function () {
                if (!$(this).hasClass(theclass + "-active")) {
                    $(this).stop().fadeTo(50, 1);
                }
            }).bind("mouseleave", function () {
                if (!$(this).hasClass(theclass + "-active")) {
                    $(this).stop().fadeTo(300, .5);
                }
            });
        }
        function appendimg() {
            if (imggone == false) {
                setTimeout(appendimg, 50);
            } else {
                $(theview + " img").remove();
                if ($(".reeufnwditb").length > 1) {
                    $(".reeufnwditb:not(:last-child)").remove();
                }
                imggone = false;
                $(".reeufnwditb").appendTo(theview).removeClass(".reeufnwditb").css({ "position": "relative", "height": "", "width": "", "top": "" }).css("opacity", 0).fadeTo(speed, 1, easing);
            }
        }
        function theend() {
            if (typeof thecallback == 'function') {
                thecallback(appendable, theurl);
            }
        }
        return ($(this));
    };
})(jQuery);
