var WebPartGalleryV1Images_v2 = new Class({
    Implements: Options,
    options: {
        ContentLeftBut: ' < ',
        ContentRightBut: ' > ',
        Display: 0
    },
    Obj: null,
    ObjImages: null,
    ObjTexts: null,
    Images: Array(),
    Texts: Array(),
    Items: Array(),
    ObjControls: null,
    ObjControlsBg: null,
    ObjLeft: null,
    ObjRight: null,
    ObjItems: null,
    TimerHide: 0,
    initialize: function(Obj, options) {
        this.setOptions(options);

        this.Obj = Obj;
        this.ObjImages = this.Obj.getElement(".WebPartGalleryV1Images_v2_images");
        this.ObjTexts = this.Obj.getElement(".WebPartGalleryV1Images_v2_texts");

        this.Images = this.ObjImages.getElements("a");
        this.Texts = this.ObjTexts.getElements("div");

        if (this.Images.length == this.Texts.length && this.Images.length > 0) {
            this.CreateUI();
            this.ShowImage(0);
        }

        this.ObjImages.addEvent("mousemove", function(a) {
            return function() {
                a.ObjControls.setStyle("display", "block");
                a.Refresh();
                clearTimeout(a.TimerHide);
            }
        } (this));
        this.ObjControls.addEvent("mousemove", function(a) {
            return function() {
                clearTimeout(a.TimerHide);
                a.ObjControls.setStyle("display", "block");
                a.Refresh();
            }
        } (this));
        this.ObjImages.addEvent("mouseout", function(a) {
            return function() {
                a.TimerHide = setTimeout(function(b) {
                    return function() {
                        b.ObjControls.tween("display", "none");
                        b.Refresh();
                    }
                } (a), 200);
            }
        } (this));
        this.StartTimer();
    },
    StartTimer: function() {
        clearInterval(this.timerT);
        this.timerT = setInterval(function(a) {
            return function() {
                if (a.options.Display < (a.Images.length - 1)) {
                    a.ShowImage(a.options.Display + 1);
                }
                else
                    a.ShowImage(0);
            }
        } (this), 4000);
    },
    StopTimer: function() {
        clearInterval(this.timerT);
    },
    CreateUI: function() {
        this.ObjControls = new Element("div", { "class": "WebPartGalleryV1Images_v2_controls" });
        this.ObjControlsBg = new Element("div", { "class": "WebPartGalleryV1Images_v2_controlsbg" });
        this.ObjControlsBg.inject(this.ObjControls);
        var bg_opacity = new Element("div", { "class": "WebPartGalleryV1Images_v2_controlsbgopacity" });
        bg_opacity.setStyle("opacity", "0.5");
        bg_opacity.inject(this.ObjControlsBg);

        this.ObjLeft = new Element("div", { "class": "WebPartGalleryV1Images_v2_left", "html": this.options.ContentLeftBut });
        this.ObjRight = new Element("div", { "class": "WebPartGalleryV1Images_v2_right", "html": this.options.ContentRightBut });
        this.ObjItems = new Element("div", { "class": "WebPartGalleryV1Images_v2_items" });

        this.ObjLeft.addEvent("click", function(a) {
            return function() {
                if (a.options.Display == 0) {
                    a.ShowImage(a.Images.length - 1);
                }
                else {
                    a.ShowImage(a.options.Display - 1);
                }
                a.StartTimer();
            }
        } (this));

        this.ObjRight.addEvent("click", function(a) {
            return function() {
                if (a.options.Display == (a.Images.length - 1)) {
                    a.ShowImage(0);
                }
                else {
                    a.ShowImage(a.options.Display + 1);
                }
                a.StartTimer();
            }
        } (this));

        var i = 0;
        for (i = 0; i < this.Images.length; i++) {
            this.Images[i].setStyle("opacity", "0");
            this.Images[i].setStyle("display", "block");
            var item = new Element("div");
            item.addEvent("click", function(a, b) {
                return function() {
                    a.ShowImage(b);
                    a.StartTimer();
                }
            } (this, i));
            item.addEvent("mousemove", function(a) {
                return function() {
                    a.ObjControls.setStyle("display", "block");
                    a.Refresh();
                    clearTimeout(a.TimerHide);
                }
            } (this));
            item.inject(this.ObjItems);

            this.Items[this.Items.length] = item;
        }
        this.ObjLeft.inject(this.ObjControlsBg);
        this.ObjItems.inject(this.ObjControlsBg);
        this.ObjRight.inject(this.ObjControlsBg);

        this.ObjControls.setStyle("display", "none");

        this.ObjControls.inject(this.ObjImages);

        this.Refresh();
    },
    Refresh: function() {
        var size = this.ObjImages.getSize();
        this.ObjControls.setStyle("margin-left", ((size.x - this.ObjControlsBg.getSize().x) / 2) + "px");
    },
    ShowImage: function(index) {
        if (this.options.Display != null) {
            this.Images[this.options.Display].fade("out");
            this.Texts[this.options.Display].setStyle("display", "none");
            this.Items[this.options.Display].removeClass("WebPartGalleryV1Images_v2_sel");
        }
        this.Images[index].fade("in");
        this.Texts[index].setStyle("display", "block");
        this.Items[index].addClass("WebPartGalleryV1Images_v2_sel");

        this.options.Display = index;

        this.Refresh();
    }
});
