﻿function galx(id) {
    this.id = id;

    this.obj = $("gal_" + this.id);
    this.obj_images = $("gal_" + this.id + "_images");
    this.obj_images_scroll = $("gal_" + this.id + "_images_scroll");
    this.obj_scroll = $("gal_" + this.id + "_scroll");
    this.obj_scroll2 = $("gal_" + this.id + "_scroll2");
    this.obj_scroll_po = $("gal_" + this.id + "_scroll_po2");
    this.obj_scroll_po2 = $("gal_" + this.id + "_scroll_po");
    this.obj_image = $("gal_" + this.id + "_image");
    this.obj_text = $("gal_" + this.id + "_text");
    this.obj_back = $("gal_" + this.id + "_back");
    this.obj_next = $("gal_" + this.id + "_next");

    this.arr_images = Array(0);
    this.arr_cat = Array(0);

    this.slider = null;
    this.scroll = null;

    this.width_i = 0;
    this.id_sel = null;

    this.is_scrolable = 0;

    this.auto_step_id = 0;

    this.add_cat = function() {
        var l = this.arr_cat.length;
        this.arr_cat[l] = new galx_cat(l, this.id);
        return l;
    }

    this.add_image = function(cat_id, src_big, text) {
        var l = this.arr_images.length;
        if (text == undefined)
            text = '';
        this.arr_images[l] = new galx_image(l, this.id, src_big, cat_id, text);
        return l;
    }

    this.init = function() {
        this.width_i = 110 * this.arr_images.length;
        gg.obj_images_scroll.setStyle("width", this.width_i);

        this.slider = new Slider(this.obj_scroll2, this.obj_scroll_po, {
            steps: 100,
            wheel: true,
            onChange: function(step) {
                this.set(step);
                var p = (parseInt(gg.obj_images_scroll.offsetWidth) - parseInt(gg.obj_images.offsetWidth)) / 100;
                //gg.obj_images_scroll.setStyle("left", (-1) * Math.ceil(step * p));
                //gg.obj_scroll_po2.setStyle("left", gg.obj_scroll_po.getStyle("left"));

                if (gg.is_scrolable == 0) {
                    var myFx = new Fx.Tween(gg.obj_images_scroll);
                    myFx.start('left', gg.obj_images_scroll.getStyle('left').toInt(), ((-1) * Math.ceil(step * p)));

                    var myFx = new Fx.Tween(gg.obj_scroll_po2);
                    myFx.start('left', gg.obj_scroll_po2.getStyle("left").toInt(), gg.obj_scroll_po.getStyle("left").toInt());
                }
                else {
                    gg.obj_images_scroll.setStyle("left", (-1) * Math.ceil(step * p));
                    gg.obj_scroll_po2.setStyle("left", gg.obj_scroll_po.getStyle("left"));
                }
            },
            onComplete: function(step) {
                var p = (parseInt(gg.obj_images_scroll.offsetWidth) - parseInt(gg.obj_images.offsetWidth)) / 100;
                //gg.obj_images_scroll.setStyle("left", (-1) * Math.ceil(step * p));
                //gg.obj_scroll_po2.setStyle("left", gg.obj_scroll_po.getStyle("left"));

                if (gg.is_scrolable == 0) {
                }
                else {
                    gg.obj_images_scroll.setStyle("left", (-1) * Math.ceil(step * p));
                    gg.obj_scroll_po2.setStyle("left", gg.obj_scroll_po.getStyle("left"));
                }

            }
        }).set(0);

        this.obj_scroll_po.addEvent("mousedown", function(a) {
            return function() {
                a.is_scrolable = 1;
            }
        } (this));

        this.obj_scroll_po.addEvent("mouseup", function(a) {
            return function() {
                a.is_scrolable = 0;
            }
        } (this));

        this.obj_images_scroll.setStyle("left", 0);

        this.obj_back.addEvent("click", function(a) {
            return function() {
                if (a.id_sel == null || a.id_sel == 0)
                    return;

                a.select(a.id_sel - 1);

	        	clearInterval(a.auto_step_id)

                //var sr = Math.ceil(a.id_sel * 100 / (a.arr_images.length - 1));
                ////a.slider.set(sr);
            }
        } (this));

        this.obj_next.addEvent("click", function(a) {
            return function() {
                if (a.id_sel == null || a.id_sel == (a.arr_images.length - 1))
                    return;

                a.select(a.id_sel + 1);

	        	clearInterval(a.auto_step_id)

                //var sr = Math.ceil(a.id_sel * 100 / (a.arr_images.length - 1));
                //a.slider.set(sr);
            }
        } (this));

        if (this.arr_images.length > 0)
            this.select(0);
        
        this.auto_step_id = setInterval('gg.auto_step();', 3000); 
    }

    this.auto_step = function() {
       if (this.id_sel == (this.arr_images.length - 1))
       {
       	   clearInterval(this.auto_step_id)
       }
       else
       {
           this.select(this.id_sel + 1);
	   }
	}
	
    this.select = function(id) {
        if (this.id_sel != null) {
            this.arr_images[this.id_sel].obj.removeClass("gal_small_images_sel");
        }

        this.arr_images[id].obj.addClass("gal_small_images_sel");

        var old_id = this.id_sel;
        if (old_id == null)
            old_id = 0;

        this.id_sel = id;

        this.obj_image.src = this.arr_images[id].src_big;
        this.obj_text.innerHTML = this.arr_images[id].text;

        if (old_id > id) {
            var sr = Math.ceil(this.id_sel * 100 / (this.arr_images.length - 1));
            this.slider.set(sr);
        }
        else if (old_id < id) {
            var sr = Math.ceil(this.id_sel * 100 / (this.arr_images.length - 1));
            this.slider.set(sr);
        }
    }
}

function galx_cat(id, pid) {
    this.id = id;
    this.pid = pid;

    this.obj = $("gal_" + this.pid + "_cat_" + this.id);
}

function galx_image(id, pid, src_big, cat_id, text) {
    this.id = id;
    this.pid = pid;
    this.src_big = src_big;
    this.cat_id = cat_id;
    this.text = text;

    this.obj = $("gal_" + this.pid + "_image_" + this.id);

    this.obj.addEvent("click", function(a, b) {
        return function() {
            gg.select(b);
        	clearInterval(gg.auto_step_id)
        }
    } (this.src_big, this.id));
}
