var Editos = {
    current: 0,
    total: 8,
    totalReel: 0,
    periodicalTime: 5,
    pe: null,
    sendedStat: new Array,

    init: function() {
        if ($('edito_bar')) {
            $('edito_bar').show();
            $('edito_0').show();
            $$('#edito_0_link img')[0].src = '/images/editos/item_selected.png';
            Editos.sendedStat.push($$('#edito_0_link')[0].parentNode.id);

            $('edito_previous').observe('click', function(event){
                Editos.setPreviousCurrentEdito();
                Editos.showEdito();
                Editos.restartPeriodical();
            });

            $$('#edito_previous img')[0].observe('mouseover', function(event){
                e = Event.element(event);
                e.src = '/images/editos/arrow_left_selected.png';
            });

            $$('#edito_previous img')[0].observe('mouseout', function(event){
                e = Event.element(event);
                e.src = '/images/editos/arrow_left.png';
            });

            $('edito_next').observe('click', function(event){
                Editos.setNextCurrentEdito();
                Editos.showEdito();
                Editos.restartPeriodical();
            });

            $$('#edito_next img')[0].observe('mouseover', function(event){
                e = Event.element(event);
                e.src = '/images/editos/arrow_right_selected.png';
            });

            $$('#edito_next img')[0].observe('mouseout', function(event){
                e = Event.element(event);
                e.src = '/images/editos/arrow_right.png';
            });

            for (var i = 0; i < Editos.total; i++) {
                editoLink = $('edito_' + i + '_link');
                if (editoLink) {
                    editoLink.observe('click', function(event){
                        e = Event.element(event);
                        parts = e.parentNode.id.split('_');
                        Editos.showEdito(parts[1]);
                        Editos.restartPeriodical();
                    });
                    Editos.totalReel++;
                } else {
                    break;
                }
            }

            Editos.pe = new PeriodicalExecuter(Editos.editoChange, Editos.periodicalTime);
        }
    },

    showEdito: function(num)
    {
        if (!num) {
            num = Editos.current;
        } else {
            Editos.current = num;
        }

        // show edito
        Editos.hideAllEditos();
        $$('#edito_' + num + '_link img')[0].src = '/images/editos/item_selected.png';
        $('edito_' + num).show();

        // save display stat
        editoId = $$('#edito_' + num + '_link')[0].parentNode.id;
        if (!Editos.sendedStat.include(editoId)) {
            Mxmfo.history.request('/savedisplaystats/2/' + editoId + '/' + (++num), true);
            Editos.sendedStat.push(editoId);
        }
    },

    restartPeriodical: function()
    {
        if (Editos.pe) {
            Editos.pe.stop();
        }
        Editos.pe = new PeriodicalExecuter(Editos.editoChange, Editos.periodicalTime);
    },

    editoChange: function()
    {
        Editos.setNextCurrentEdito();
        Editos.showEdito();
    },

    setPreviousCurrentEdito: function()
    {
        if (Editos.current == 0) {
            Editos.current = Editos.totalReel - 1;

        } else {
            Editos.current--;
        }
    },

    setNextCurrentEdito: function()
    {
        if (Editos.current == Editos.totalReel - 1) {
            Editos.current = 0;

        } else {
            Editos.current++;
        }
    },

    setCurrentEdito: function(num)
    {
        Editos.current = num;
    },

    hideAllEditos: function()
    {
        editosDiv = $$('.edito');

        for (i = 0; i < editosDiv.length; i++) {
            editosDiv[i].hide();
            $$('#' + editosDiv[i].id + '_link img')[0].src = '/images/editos/item.png';
        }
    }
}

Event.onDOMReady(function() {
    Editos.init();
});


