/*
name    : SimpleSlide.js
build   : 2008.04.14
update  : 2009.07.31
outer   : flossy 
URL     : http://5five.net
lisence : http://www.gnu.org/licenses/lgpl-3.0.txt GNU LGPL 3.0
*/

var SimpleSlide = new Class({
    Implements: [Options, Events],
    options: {
        separat: 1,
        childTag: "div",
        duration: 1000,
        autoPlay: true,
        control: true,
        peri: 8000,
        mode: "slider",
        transition: Fx.Transitions.Quart.easeInOut
    },
    initialize: function (b, a) {
        this.setOptions(a);
        this.listWrap = $(b);
        this.caseNum = 0;
        this.build()
    },
    build: function () {
        this.sliders = [];
        this.pages = [];
        this.list = this.listWrap.getChildren();
        this.wrapCase = new Element("div", {
            id: "slideCase",
            styles: {
                position: "relative"
            }
        }).inject(this.listWrap.getParent());
        this.wrap = new Element("div", {
            "class": "sliderBox clearfix"
        }).inject(this.wrapCase);
        this.fx = new Fx.Tween(this.wrap, {
            duration: this.options.duration,
            wait: false,
            transition: this.options.transition
        });
        this.naviCase = new Element("div", {
            id: "pageNavi"
        }).inject(this.wrapCase);
        if (this.options.childTag == "li") {
            this.slider = "ul"
        } else {
            this.slider = "div"
        }
        this.list.each(function (c, b) {
            if ((b % this.options.separat) == 0) {
                var a = b / this.options.separat;
                this.sliders.unshift(new Element(this.slider, {
                    "class": "slider"
                }).setStyles({
                    "float": "left"
                }));
                this.addPage(a)
            }
            c.inject(this.sliders[0])
        },
        this);
        this.fin()
    },
    fin: function () {
        this.size = this.listWrap.getParent().getSize();
        this.caseSize = this.size.x.toInt();
        this.wrap.setStyle("width", this.sliders.length * this.caseSize + "px");
        this.listWrap.destroy();
        this.sliders.reverse();
        this.sliders.each(function (d, c) {
            d.setStyle("width", this.caseSize).inject(this.wrap)
        },
        this);
        this.selected(0);
        var b = 1;
        if (this.options.autoPlay) {
            var a = function () {
                this.pages[b].fireEvent("click");
                b++;
                if (b >= this.pages.length) {
                    b = 0;
                }
            }.bind(this);
            if (this.pages.length > 1) a.periodical(this.options.peri);
        }
        if (!this.options.control) {
            this.naviCase.setStyle("display", "none");
        }
    },
    addPage: function (a) {
        var b = new Element("a", {
            "class": "pages",
            href: "#"
        }).addEvent("click", this.page.bindWithEvent(this, a)).set("html", a + 1).inject(this.naviCase);
        this.pages.push(b);
        return this
    },
    page: function (b, a) {
        this.selected(a);
        return false
    },
    selected: function (a) {
        if (this.options.mode == "slider") {
            this.fx.start("margin-left", -(this.caseSize) * a)
        } else {
            this.sliders.each(function (c, b) {
                if (a == b) {
                    c.fade("in").setStyle("display", "block");
if(this.options.control){if($('pageNavi').getSize().y > c.getSize().y) c.setStyle('height',$('pageNavi').getSize().y+'px')};
                } else {
                    c.fade("out").setStyle("display", "none")
                }
            },
            this)
        }
        this.pages.each(function (c, b) {
            if (a == b) {
                c.addClass("selected")
            } else {
                c.removeClass("selected")
            }
        },
        this)
    }
});


