﻿function gallery_scroll(id) {
    this.id = id;

    this.arr = Array(0);

    this.obj = $("gs_" + this.id);
    this.obj_content = $("gs_" + this.id + "_content");
    this.obj_image_big = $("gs_" + this.id + "_image_big");
    this.obj_image_big_i = $("gs_" + this.id + "_image_big_i");
    this.obj_image_big_header = $("gs_" + this.id + "_image_big_header");
    this.obj_image_big_description = $("gs_" + this.id + "_image_big_description");
    this.obj_image_big_close = $("gs_" + this.id + "_image_big_close");
    this.obj_image_big_left = $("gs_" + this.id + "_image_big_left");
    this.obj_image_big_right = $("gs_" + this.id + "_image_big_right");
    this.obj_spane = $("gs_" + this.id + "_spane");
    this.obj_bscroll = $("gs_" + this.id + "_bscroll");
    this.obj_up = $("gs_" + this.id + "_up");
    this.obj_down = $("gs_" + this.id + "_down");

    this.scroll = null;

    this.heights = 0;

    this.sel_id = null;

    this.is_scrolling = 0;

    this.init = function() {
        this.obj_bscroll.setStyle("height", this.arr.length * 106);
        this.heights = this.arr.length * 106 - 349; //parseInt(this.obj_spane.offsetHeight);

        this.obj_up.addEvent("click", function(a) {
            return function() {
                var t = a.obj_bscroll.getStyle("top").toInt();
                if (t == 0)
                    return;
                if (a.is_scrolling == 1)
                    return;
                a.is_scrolling = 1;
                var myEffects = new Fx.Styles(a.obj_bscroll, { duration: 250, transition: Fx.Transitions.Cubic.easeIn });
                myEffects.start({
                    'top': [t, t + 106]
                }).chain(function(b) {
                    return function() {
                        b.is_scrolling = 0;
                    }
                } (a));
            }
        } (this));

        this.obj_down.addEvent("click", function(a) {
            return function() {
                var t = a.obj_bscroll.getStyle("top").toInt(); //*(-1);
                if (a.heights <= (t * (-1)))
                    return;
                if (a.is_scrolling == 1)
                    return;
                a.is_scrolling = 1;
                var myEffects = new Fx.Styles(a.obj_bscroll, { duration: 250, transition: Fx.Transitions.Cubic.easeIn });
                myEffects.start({
                    'top': [t, t - 106]
                }).chain(function(b) {
                    return function() {
                        b.is_scrolling = 0;
                    }
                } (a));
            }
        } (this));

        this.obj_image_big_close.addEvent("click", function(a) {
            return function() {
                a.unselall();
            }
        } (this));

        this.obj_image_big_left.addEvent("click", function(a) {
            return function() {
                a.last();
                var t = a.obj_bscroll.getStyle("top").toInt();
                if (t == 0)
                    return;
                if (a.is_scrolling == 1)
                    return;
                a.is_scrolling = 1;
                var myEffects = new Fx.Styles(a.obj_bscroll, { duration: 250, transition: Fx.Transitions.Cubic.easeIn });
                myEffects.start({
                    'top': [t, t + 106]
                }).chain(function(b) {
                    return function() {
                        b.is_scrolling = 0;
                    }
                } (a));
            }
        } (this));

        this.obj_image_big_right.addEvent("click", function(a) {
            return function() {
                a.next();
                var t = a.obj_bscroll.getStyle("top").toInt(); //*(-1);
                if (a.heights <= (t * (-1)))
                    return;
                if (a.is_scrolling == 1)
                    return;
                a.is_scrolling = 1;
                var myEffects = new Fx.Styles(a.obj_bscroll, { duration: 250, transition: Fx.Transitions.Cubic.easeIn });
                myEffects.start({
                    'top': [t, t - 106]
                }).chain(function(b) {
                    return function() {
                        b.is_scrolling = 0;
                    }
                } (a));
            }
        } (this));

    }

    this.add = function(src_big, header, description) {
        var l = this.arr.length;
        this.arr[l] = new gallery_scroll_item(l, this.id, src_big, header, description);
    }

    this.unselall = function() {
        var i = 0;
        for (i = 0; i < this.arr.length; i++) {
            this.arr[i].obj.removeClass("gallery_div_images_sel");
        }
        this.sel_id = null;
        //this.obj_image_big.setStyle("display", "none");
        this.obj.removeClass("gallery_div_border");

        var myEffects = new Fx.Styles(this.obj_image_big, { duration: 250, transition: Fx.Transitions.Cubic.easeIn });
        myEffects.start({
            'left': [-580, 0],
            'width': [580, 0]
        }).chain(function(a) {
            return function() {
                a.setStyle("display", "none");
            }
        } (this.obj_image_big));
    }

    this.select = function(id) {
        if (this.sel_id == id)
            return;
        if (this.sel_id != null) {
            this.arr[this.sel_id].obj.removeClass("gallery_div_images_sel");
        }
        this.arr[id].obj.toggleClass("gallery_div_images_sel");
        //alert(this.sel_id);
        if (this.sel_id == null) {
            this.obj_image_big.setStyle("width", "0");
            this.obj_image_big.setStyle("left", "0");
            this.obj_image_big.setStyle("display", "block");
            this.obj.addClass("gallery_div_border");

            var myEffects = new Fx.Styles(this.obj_image_big, { duration: 250, transition: Fx.Transitions.Cubic.easeIn });
            myEffects.start({
                'left': [0, -580],
                'width': [0, 580]
            });
        }

        this.sel_id = id;

        this.obj_image_big_i.src = this.arr[this.sel_id].src_big;
        this.obj_image_big_header.innerHTML = this.arr[this.sel_id].header;
        this.obj_image_big_description.innerHTML = this.arr[this.sel_id].description;
    }

    this.last = function() {
        var l = this.arr.length;

        if (this.is_scrolling == 1)
            return;

        if (this.sel_id != 0)
            this.select(this.sel_id - 1);
    }

    this.next = function() {
        var l = this.arr.length;

        if (this.is_scrolling == 1)
            return;

        if (this.sel_id != (l - 1))
            this.select(this.sel_id + 1);
    }
}

function gallery_scroll_item(id, pid, src_big, header, description) {
    this.id = id;
    this.pid = pid;
    this.src_big = src_big;
    this.header = header;
    this.description = description;

    this.obj = $("gs_" + this.pid + "_img_" + this.id);

    this.obj.addEvent("click", function(a) {
        return function() {
            gs.select(a);
        }
    } (this.id));
}