document.observe("dom:loaded", function () {
    if ($('home_page')) {
        var banners = '';
        var bannercount = '';
        $('coloana_centru').childElements().each(function (e) {
            if (!($(e).hasClassName('coloana_centru_inner'))) {
                $(e).addClassName('poza_banner');
                $(e).id = '';
            }
        });
        $('coloana_centru').select('.poza_banner').each(function (e) {
            bannercount++;
            banners = banners + ($(e).innerHTML);
            $(e).remove();
        });
        var slidecontrols = '';
        for (i = 1; i <= bannercount; i++) {
            slidecontrols += '<dd id="dd' + i + '"><a href="javascript:void(0);"';
            if (i == 1) {
                slidecontrols += ' class="selected"';
            }
            slidecontrols += '>' + i + '</a></dd>';
        }
        template = '<div id="boxa_slide">' + '<div id="panou_slide">' + '<div id="secventa">' + banners + '</div>' + '</div>' + '<dl class="butoane_slide">' + slidecontrols + '</dl>' + '</div>';
        $('coloana_centru').insert({
            top: template
        });
        var slidecount = 1;
        $('secventa').select('a, div').each(function (e) {
            $(e).addClassName('slide');
            $(e).id = 'slide' + slidecount;
            slidecount++;
        });
        new smPanelEffects({
            effect: 'panel',
            autoscrolldelay: 7,
            speed: 0.4,
            hoverpause: true
        });
    }
});
var currentPanel = 1;
var animating = false;
var automating = false;
var timeout;
var totalpanels;
var eventQueue = [];
var smPanelEffects = Class.create({
    initialize: function (options) {
        this.options = {
            effect: 'fade',
            controls: 'butoane_slide',
            controltag: 'dd',
            controltagid: 'dd',
            contEvent: 'click',
            skipbutton: 'skip',
            skipnext: 'next',
            skipprev: 'previous',
            hoverpause: false,
            playpause: 'playpause',
            pause: 'pause',
            play: 'play',
            controlsel: 'selected',
            container: 'secventa',
            panels: 'slide',
            panelid: 'slide',
            initpanel: 'slide1',
            speed: 0.7,
            transition: Effect.Transitions.sinoidal,
            auto: true,
            autoscrolldelay: 4,
            eventQueue: true,
            DEBUG: false,
            extend: null
        }
        Object.extend(this.options, options || {});
        totalpanels = $$('.' + this.options.panels).length;
        if (this.options.effect == 'panel') {
            var fullwidth = ($(this.options.initpanel).getWidth()) * totalpanels;
            $(this.options.container).setStyle({
                width: fullwidth + 'px'
            });
        }
        if (this.options.hoverpause) {
            $(this.options.container).observe("mouseover", function () {
                this.doPlayPause("pause")
            }.bindAsEventListener(this));
            $(this.options.container).observe("mouseout", function () {
                this.doPlayPause("play")
            }.bindAsEventListener(this));
        }
        if ($(this.options.playpause)) this.clickPlayPause();
        $$('.' + this.options.controls + ' a').each(function (e) {
            $(e).observe(this.options.contEvent, function () {
                this.animateAction(e, '');
            }.bindAsEventListener(this));
        }.bindAsEventListener(this))
        $$('.' + this.options.skipbutton).each(function (elmnt) {
            $(elmnt).observe('click', function () {
                this.buttonAction(elmnt);
            }.bindAsEventListener(this));
        }.bindAsEventListener(this))
        timeout = setTimeout(function () {
            this.animateAuto();
        }.bindAsEventListener(this), (this.options.autoscrolldelay * 1000));
    },
    buttonAction: function (e) {
        var skipTo;
        if ($(e).hasClassName(this.options.skipnext)) {
            skipTo = ((currentPanel + 1) <= totalpanels) ? (currentPanel + 1) : 1;
        } else {
            skipTo = ((currentPanel - 1) != 0) ? (currentPanel - 1) : totalpanels;
        }
        this.doShowHidePlayPause("pause");
        this.clearAuto();
        this.animateAction('', skipTo);
    },
    animateAuto: function () {
        if (this.options.auto) {
            if (this.options.DEBUG) console.log('totalpanels: ' + totalpanels);
            automating = true;
            newp = (currentPanel < totalpanels) ? (currentPanel + 1) : 1;
            if (this.options.DEBUG) console.log('newpanel: ' + newp);
            this.animateAction('', newp);
            timeout = setTimeout(function () {
                this.animateAuto();
            }.bindAsEventListener(this), (this.options.autoscrolldelay * 1000));
        }
    },
    clearAuto: function () {
        automating = false;
        if (typeof(timeout) == 'number') clearTimeout(timeout);
        this.doShowHidePlayPause("pause");
    },
    animateAction: function (e, v) {
        var newpanel;
        if (!animating) {
            if (e) {
                this.clearAuto();
                newpanel = parseInt($(e).up(this.options.controltag).id.replace(this.options.controltagid, ""));
            } else {
                newpanel = v;
            }
            var diff = (currentPanel - newpanel);
            if (this.options.DEBUG) console.log('currentPanel: ' + currentPanel + ' newpanel: ' + newpanel + ' |diff: ' + diff);
            animating = true;
            if (this.options.effect == 'panel') {
                var distance = diff * ($(this.options.initpanel).getWidth());
                if (this.options.DEBUG) console.log('distance: ' + distance);
                new Effect.Move($(this.options.container), {
                    x: distance,
                    y: 0,
                    mode: 'relative',
                    duration: this.options.speed,
                    transition: this.options.transition,
                    afterFinish: function () {
                        animating = false;
                        this.checkEventQueue();
                    }.bindAsEventListener(this)
                });
            } else {
                if (currentPanel != newpanel) {
                    new Effect.Fade(this.options.panelid + currentPanel, {
                        duration: this.options.speed
                    });
                    new Effect.Appear(this.options.panelid + newpanel, {
                        duration: this.options.speed,
                        afterFinish: function () {
                            animating = false;
                            this.checkEventQueue();
                        }.bindAsEventListener(this)
                    });
                } else {
                    if (this.options.DEBUG) console.log('ignoring click');
                    animating = false;
                    this.checkEventQueue();
                }
            }
            currentPanel = newpanel;
            this.setSelected(newpanel);
            if (this.options.DEBUG) console.log(' --------- ');
        } else {
            if (this.options.eventQueue) {
                if (this.options.DEBUG) console.log('adding to event queue');
                if (e) {
                    newpanel = parseInt($(e).up(this.options.controltag).id.replace(this.options.controltagid, ""));
                } else {
                    newpanel = v;
                }
                eventQueue.push(newpanel);
            }
        }
    },
    checkEventQueue: function () {
        if (this.options.DEBUG) console.log('checking event queue');
        if (eventQueue.length > 0) {
            if (this.options.DEBUG) console.log('using the event queue');
            var newp = eventQueue.shift();
            this.animateAction('', newp);
        }
    },
    clickPlayPause: function () {
        if (($(this.options.pause).visible()) && ($(this.options.play).visible())) {
            var val = (this.options.auto == true) ? 'play' : 'pause';
            this.doShowHidePlayPause(val);
        }
        $(this.options.pause).observe("click", function () {
            this.doPlayPause("pause");
        }.bind(this));
        $(this.options.play).observe("click", function () {
            this.doPlayPause("play");
        }.bind(this));
    },
    doPlayPause: function (status) {
        this.doShowHidePlayPause(status);
        if (this.options.DEBUG) console.log(status);
        if (status == 'pause') {
            this.clearAuto();
        } else {
            this.options.auto = true;
            this.clearAuto();
            timeout = setTimeout(function () {
                this.animateAuto();
            }.bindAsEventListener(this), (this.options.autoscrolldelay * 1000));
        }
        this.doShowHidePlayPause(status);
    },
    doShowHidePlayPause: function (status) {
        if (status == 'play') {
            if ($(this.options.play)) $(this.options.play).hide();
            if ($(this.options.pause)) $(this.options.pause).show();
        } else {
            if ($(this.options.pause)) $(this.options.pause).hide();
            if ($(this.options.play)) $(this.options.play).show();
        }
    },
    setSelected: function (sel) {
        if (this.options.DEBUG) console.log("setting selected " + sel);
        $$('.' + this.options.controls + ' a').each(function (el) {
            var ddid = parseInt($(el).up(this.options.controltag).id.replace(this.options.controltagid, ""));
            if (ddid == sel) {
                $(el).addClassName(this.options.controlsel);
            } else {
                if ($(el).hasClassName(this.options.controlsel)) {
                    $(el).removeClassName(this.options.controlsel);
                }
            }
        }.bindAsEventListener(this));
    }
});
