/// <reference path="jquery-1.6.1-vsdoc.js" />

(function($) {
    $.fn.extend({

        GshowPicture: function() {
            $(this).addClass("active").css({ "display": "block", "opacity": 0 }).animate({ "opacity": 1 }, 800);
            return this;
        },
        GhidePicture: function(callback) {
            $(this).removeClass("active").animate({ "opacity": 0 }, 800, function() {
                $(this).css({ "display": "block", "opacity": 0 });
                if (typeof callback == "function") {
                    callback.call(this);
                }
            });
            return this;
        },
        GmoveToFirstPicture: function() {
            $(".picture.active", this).css({ "display": "none", "opacity": 0 }).removeClass("active");
            $(".picture:first", this).css({ "display": "block", "opacity": 1 }).addClass("active");
        },
        // TODO: next/prev picture

        Ggallery: function() {

            $(this).each(function() {
                if ($(this).find(".picture").length == 0) return;

                var gallery = this;
                var animated = false;

                $(".picture:first", gallery).GshowPicture();

                $(".next", gallery).click(function() {
                    if (animated) return false;
                    animated = true;

                    $(".picture.active", gallery).GhidePicture(function() {
                        if ($(this).next(".picture").html() == null)
                            $(".picture:first", gallery).GshowPicture();
                        else
                            $(this).next(".picture").GshowPicture();

                        animated = false;
                    });

                    return false;
                });

                $(".previous", gallery).click(function() {
                    if (animated) return false;
                    animated = true;

                    $(".picture.active", gallery).GhidePicture(function() {
                        if ($(this).prev(".picture").html() == null)
                            $(".picture:last", gallery).GshowPicture();
                        else
                            $(this).prev(".picture").GshowPicture();

                        animated = false;
                    });

                    return false;
                });


                // ocisluju
                var total = $(".picture", gallery).length;
                $(".picture", gallery).each(function(index) {
                    $(this).append($("<span>").addClass("pos").text((index + 1) + "/" + total));
                });

            });
            return this;
        }
    });
})(jQuery);
 
