var currentSlide = 0;
var interval = '';
document.carouselBaseImageUrl = document.carouselBaseImageUrl ? document.carouselBaseImageUrl : '';
document.carouselPointImage = document.carouselPointImage ? document.carouselPointImage : document.carouselBaseImageUrl + '/static/images/einvite/UI/point.png';

function rotate() {
    currentSlide++;
    currentSlide = document.compare(currentSlide + 1, 'gt', document.imageUrls.length) ? 0 : currentSlide;
    $('carousel-image').down('a').writeAttribute('href', document.imageLinks[currentSlide]);
    $('carousel-image').down('a').appendChild(new Element('img', {'id': 'bottom', 'src': document.carouselBaseImageUrl + document.imageUrls[currentSlide], 'style': 'position: absolute; z-index: 0; left: 0; top: 0;'}));
    if ($('point')) {
        new Effect.Move('point', {x: 183, y: 0, mode: 'relative', transition: Effect.Transitions.linear, duration: 1, afterFinish: function() {
            if (currentSlide == 0) {
                $('point').setStyle({'left': '0'});
                new Effect.Move('point', {x: 87, y: 0, mode: 'relative', transition: Effect.Transitions.linear, duration: .5});
            }
        }});
    }
    new Effect.Opacity('top', {from: 1, to: 0, duration: .5, afterFinish: function() {
        $('top').remove();
        $('bottom').writeAttribute('id', 'top');
        $('top').setStyle({'zIndex': '1'});
        if (document.carouselType == 'jump') {
            $('carousel-teaser').update(document.teaserTexts[currentSlide]);
            $('carousel-jumpers').childElements().each(function(e) {
                e.removeClassName('curr');
            });
            $('jump' + currentSlide).addClassName('curr');
        } else {
            $('carousel-image').down('a').writeAttribute('href', document.imageLinks[currentSlide]);
            $('carousel-image').down('a').writeAttribute('alt', document.altTexts[currentSlide]);
            $('carousel-tab').childElements().each(function(e) {
                e.removeClassName('curr');
            });
            $('tab' + currentSlide).addClassName('curr');
        }
    }});
}

function jump(n) {
    clearInterval(interval);
    currentSlide = n - 1;
    rotate();
    interval = setInterval('rotate()', document.carouselDelayInSeconds * 1000);
}

document.carouseller = function() {
    $('carousel-image').down('a').writeAttribute('href', document.imageLinks[0]);
    $('carousel-image').down('a').appendChild(new Element('img', {'id': 'top', 'src': document.carouselBaseImageUrl + document.imageUrls[currentSlide], 'style': 'position: absolute; z-index: 1; left: 0; top: 0;'}));
    if (document.carouselType == 'jump') {
        $('carousel-controls').appendChild(new Element('div', {'id': 'carousel-teaser'}));
        $('carousel-teaser').addClassName('teaser');
        $('carousel-teaser').update(document.teaserTexts[0]);
        $('carousel-controls').appendChild(new Element('div', {'id': 'carousel-jumpers'}));
        $('carousel-jumpers').setStyle({'letter-spacing': '2px'});
        for (j = 0; document.compare(j, "lt", document.imageUrls.length); j++) {
            $('carousel-jumpers').appendChild(new Element('a', {'id': 'jump' + j, 'href': document.imageLinks[j], 'onclick': 'jump(' + j + '); return false;'}).update(j + 1));
        }
        $('jump0').addClassName('curr');
    } else {
        $('carousel-image').appendChild(new Element('div', {'id': 'point'}));
        $('point').setStyle({'position': 'absolute', 'top': '242px', 'left': '87px', 'z-index': '10', 'zIndex': '10', 'color': '#fff'});
        $('point').appendChild(new Element('img', {'src': document.carouselPointImage}));

        $('carousel-tab').appendChild(new Element('div', {'id': 'tab0'}));
        $('tab0').appendChild(new Element('a', {'href': document.imageLinks[0]}).update(document.captions[0]));
        $('tab0').addClassName('curr');
        $('carousel-image').down('a').writeAttribute('href', document.imageLinks[0]);
        $('carousel-image').down('a').writeAttribute('alt', document.altTexts[0]);
        $('carousel-tab').appendChild(new Element('div', {'id': 'tab1'}));
        $('tab1').appendChild(new Element('a', {'href': document.imageLinks[1]}).update(document.captions[1]));
        $('carousel-tab').appendChild(new Element('div', {'id': 'tab2'}));
        $('tab2').appendChild(new Element('a', {'href': document.imageLinks[2]}).update(document.captions[2]));
    }
    interval = setInterval('rotate()', document.carouselDelayInSeconds * 1000);
};

document.waitForIt = function() {
    if (document.compare && document.imageUrls) {
        document.carouseller();
    } else {
        setTimeout("document.waitForIt();", 500);
    }
}

document.waitForIt();
