var Rotator = new Class(
{
    period:          4000,
    idleTime:        10000,
    container:       undefined,                  // jeweils letzter href aus loadContent(href)
    items:           [],
    autoRotate:      undefined,
    navigator1:      undefined,
    navigator2:      undefined,
    navigator2Image: [],
    navigator2HoverImage: [],
    timerAuto:       undefined,
    timeIdle:        undefined,
    active:          undefined,

    initialize: function(el, l1Navi, l2Navi, startID, period, idleTime)
    {
//alert(l1Navi);
//alert(l2Navi);
        if (period != undefined) this.period = period;
        if (idleTime != undefined) this.idleTime = idleTime;
		if (startID != undefined) this.startID = startID;
		else this.startID = 0;

        this.container = el;
        this.items = this.container.getChildren('div');
        this.items.each(function (el, index)
        {
            if (index == this.startID)
            {
                this.active = index;
            }
            else
            {
                this.items[index].fade('hide');
            }

        }, this);

        if (this.items.length > 1)              // nur, wenns was zu rotieren gibt
        {
            this.buildNavigation(l1Navi, l2Navi);
//            this.startRotation();
        }
    },

    buildNavigation: function (l1Navi, l2Navi)
    {
        if (l1Navi)
        {
            this.navigator1 = l1Navi.getElements('a');
            this.items.each(function (el, index)
            {
                this.navigator1[index].addEvent('mouseover', this.navigatorClick.bind(this, index));

                if (index == this.active)
                {
                    this.navigator1[index].addClass('active');
                }
            }, this);
        }
        if (l2Navi)
        {
            this.navigator2 = l2Navi.getElements('a');
            this.items.each(function (el, index)
            {
                this.navigator2Image[index] = this.navigator2[index].getElement('img').getProperty('src');
                this.navigator2HoverImage[index] = this.getHoverImageName(this.navigator2Image[index]);
                this.navigator2[index].addEvent('mouseover', this.navigatorClick.bind(this, index));

                if (index == this.active)
                {
                    this.navigator2[index].addClass('active');
                    this.changeImage(this.navigator2[index].getElement('img'), this.navigator2HoverImage[this.active]);
                }
            }, this);
        }
//alert(this.navigator2Image);
//alert(this.navigator2HoverImage);
    },

    startRotation: function ()
    {
        this.timerAuto = this.showNext.periodical(this.period, this);
        this.autoRotate = true;
    },

    stopRotation: function ()
    {
        $clear(this.timerAuto);
        $clear(this.timerIdle);
        this.timerIdle = this.startRotation.delay(this.idleTime, this);
        this.autoRotate = false;
    },

    navigatorClick: function (index)
    {
//        this.stopRotation();
        this.show(index);
    },

    showNext: function ()
    {
//alert(this.active);
        var show = (this.active < (this.items.length - 1)) ? this.active + 1 : 0;
        this.show(show);
    },

    show: function (index)
    {
//alert(index);
        this.navigator1[this.active].removeClass('active');
        this.navigator2[this.active].removeClass('active');
        this.changeImage(this.navigator2[this.active].getElement('img'), this.navigator2Image[this.active]);
        this.items[this.active].fade('out');
        this.items[index].fade('in');
        this.active = index;
        this.navigator1[this.active].addClass('active');
        this.navigator2[this.active].addClass('active');
        this.changeImage(this.navigator2[this.active].getElement('img'), this.navigator2HoverImage[this.active]);
    },
    
    changeImage: function(img, newUri)
    {
        img.src = newUri;
    },

    getHoverImageName: function(src)
    {
            var newImg = '';
            var uriPart = src.split('.');
            var length = uriPart.length;

            for (var ii=0; ii<length-2; ii++)
            {
                    newImg += uriPart[ii] + ".";
            }

            newImg += uriPart[length-2] + '_f2.' + uriPart[length-1];
            return newImg;
    }
});
