var teaserAreaElement, teaserAreaLeftHandSideElement, teaserStatusdisplayElement, teaserStatusdisplayElementImages, htmlTeaserElements;
var teaserAreaOuterFrameWidth = 598;
var teaserAreaLeftHandSideElementMarginLeft = -200;
var htmlTeasersNumber = 0;
var animationStarted = false;
var animationStepSize = 3;
var timeoutNoflashIntroAnimation = null;
var timeoutAutomaticSlideshowAnimation = null, automaticSlideshowAnimationDelay = 7000;
var teaserAreaElementMarginLeft, finalMarginLeft;
var direction = 0, slideshowTimeout = 0;

function initializeStaticTeasersNoflash() {
    document.getElementById("teaserAreaLeftHandSide").style.display = "block";
    document.getElementById("teaserAreaRightHandSide").style.display = "block";
    homepageIntroAnimation();

    if (teaserAreaLeftHandSideElementMarginLeft < 0) {
        timeoutNoflashIntroAnimation = setTimeout(initializeStaticTeasersNoflash, 25);
    }
}

function readFirstTeaserCookie() {
    var teaserStartIndex = -1;

    if (navigator.cookieEnabled == false) {
        return teaserStartIndex;
    }

    if (document.cookie) {
        var homepageTeaserCarouselCookieArray = document.cookie.split(";");

        for (var i = 0; i < homepageTeaserCarouselCookieArray.length; i++) {

            if (homepageTeaserCarouselCookieArray[i].split("=")[0] == "TeaserIndex") {
                teaserStartIndex = parseInt(homepageTeaserCarouselCookieArray[i].split("=")[1], 10);
            }

        }

    }

    return teaserStartIndex;
}

function writeFirstTeaserCookie(teaserStartIndex) {
    var currentDate = new Date();
    var expirationDate = currentDate.getTime() + (30 * 24 * 60 * 60 * 1000);
    currentDate.setTime(expirationDate);
    document.cookie = "TeaserIndex=" + teaserStartIndex + "; expires=" + currentDate.toGMTString();
}

function initializeStaticTeasers() {
    teaserAreaElement = document.getElementById("teaserArea");
    teaserAreaElement.style.marginLeft = "-" + teaserAreaOuterFrameWidth + "px";
    teaserStatusdisplayElement = document.getElementById("teaserStatusdisplay");
    teaserStatusdisplayElement.style.left = teaserAreaOuterFrameWidth + "px";
    teaserStatusdisplayElement.style.display = "block";
    teaserStatusdisplayElementImages = teaserStatusdisplayElement.getElementsByTagName("img");
    htmlTeaserElements = teaserAreaElement.getElementsByTagName("table");

    for (var i = 0; i < htmlTeaserElements.length; i++) {
        htmlTeaserElementImages = htmlTeaserElements[i].parentNode.getElementsByTagName("img");
        teaserStatusdisplayElementImages[htmlTeasersNumber].style.display = "block";
        headlineImageSrc = htmlTeaserElementImages[0].src;
        contentImageSrc = htmlTeaserElementImages[1].src;

        //initialize active teasers and status display
        if (htmlTeaserElements[i].parentNode.className.split(" ")[0] == "teaserElement" && (headlineImageSrc.indexOf(".gif") != -1 || headlineImageSrc.indexOf(".jpg") != -1) && (contentImageSrc.indexOf(".gif") != -1 || contentImageSrc.indexOf(".jpg") != -1)) {
            htmlTeasersNumber++;
            teaserStatusdisplayElementImages[htmlTeasersNumber].style.display = "block";
            htmlTeaserElements[i].parentNode.style.display = "block";
            lastActiveHtmlTeaser = htmlTeaserElements[i].parentNode.cloneNode(true);
        }

    }

    teaserAreaElement.insertBefore(lastActiveHtmlTeaser, teaserStatusdisplayElement.nextSibling);

    //read startindex from cookie and adjust teaserposition and statusdisplay
    var elementIdElement = document.getElementById(elementId);
    var flashTeaserElement = true;
    var allTeasersNumber = htmlTeasersNumber + 1;
    var teaserStartIndex = readFirstTeaserCookie();

    if (elementIdElement.nodeName == "DIV" && elementIdElement.getElementsByTagName("img").length == 0) {
        flashTeaserElement = false;
        allTeasersNumber--;
    }

    if (allTeasersNumber % (teaserStartIndex + 2) < allTeasersNumber) {
        teaserStartIndex++;
        //teaserStartIndex = 0;
    } else {
        teaserStartIndex = 0;
        automaticSlideshowAnimationDelay = 10000;
    }

    writeFirstTeaserCookie(teaserStartIndex);
    teaserAreaElementMarginLeft = (teaserAreaOuterFrameWidth + (teaserStartIndex * teaserAreaOuterFrameWidth)) * -1;
    finalMarginLeft = teaserAreaElementMarginLeft;
    var imageSrcTemp = teaserStatusdisplayElementImages[teaserStartIndex].src;
    teaserStatusdisplayElementImages[teaserStartIndex].src = teaserStatusdisplayElementImages[0].src;
    teaserStatusdisplayElementImages[0].src = imageSrcTemp;
    homepageSlideshowAnimation();

    var htmlTeaserElementImages, lastActiveHtmlTeaser, headlineImageSrc, contentImageSrc;

    if (!flashTeaserElement) {
        // in case of no flash teaser available
        elementIdElement.style.display == "none";
        teaserStatusdisplayElementImages[htmlTeasersNumber].style.display = "none";
        htmlTeasersNumber--;
    } else if (document.all) {
        document.getElementById(elementId).style.filter = "alpha(opacity=100)";
        document.getElementById(elementId).style.backgroundColor = "#ffffff";
    } else {
        document.getElementById("FlashMovieDelegateControl_FlashElement").style.opacity = "1";
    }
}

function initializeHomepageIntroAnimation() {
    teaserAreaLeftHandSideElement = document.getElementById("teaserAreaLeftHandSide");
    teaserAreaLeftHandSideElement.style.marginLeft = teaserAreaLeftHandSideElementMarginLeft + "px";
    teaserAreaLeftHandSideElement.style.display = "block";
    document.getElementById("teaserAreaRightHandSide").style.display = "block";
}

function homepageIntroAnimation() {
    if (!animationStarted) {
        clearTimeout(timeoutNoflashIntroAnimation);
        timeoutNoflashIntroAnimation = null;
        initializeStaticTeasers();
        initializeHomepageIntroAnimation();
        animationStarted = true;
    } else if (teaserAreaLeftHandSideElementMarginLeft > 0) {
        document.getElementById("teaserAreaLeftHandSide").style.marginLeft = "0px";
        return;
    } else if (teaserAreaLeftHandSideElementMarginLeft == 0) {
        return;
    } else if (teaserAreaLeftHandSideElementMarginLeft >= -8) {
        animationStepSize = 1;
    } else if (teaserAreaLeftHandSideElementMarginLeft >= -20) {
        animationStepSize = 2;
    } else if (teaserAreaLeftHandSideElementMarginLeft >= -40) {
        animationStepSize = 3;
    } else if (teaserAreaLeftHandSideElementMarginLeft >= -175) {
        animationStepSize = 4;
    } else if (teaserAreaLeftHandSideElementMarginLeft >= -185) {
        animationStepSize = 3;
    } else if (teaserAreaLeftHandSideElementMarginLeft >= -190) {
        animationStepSize = 2;
    } else if (teaserAreaLeftHandSideElementMarginLeft >= -195) {
        animationStepSize = 1;
    }

    teaserAreaLeftHandSideElementMarginLeft += animationStepSize;

    if (teaserAreaLeftHandSideElementMarginLeft >= 0) {
        timeoutAutomaticSlideshowAnimation = setTimeout('initializeHomepageSlideshowAnimation(document.getElementById("teaserAreaRightHandSide"));', automaticSlideshowAnimationDelay);
    }

    teaserAreaLeftHandSideElement.style.marginLeft = teaserAreaLeftHandSideElementMarginLeft + "px";
}

function initializeHomepageSlideshowAnimation(element) {
    clearTimeout(timeoutAutomaticSlideshowAnimation);
    element.id == "teaserAreaLeftHandSide" ? direction = 1 : false;
    element.id == "teaserAreaRightHandSide" ? direction = -1 : false;
    teaserAreaElement = document.getElementById("teaserArea");
    var imageSrcTemp;

    if (direction == -1 && finalMarginLeft == teaserAreaOuterFrameWidth * (htmlTeasersNumber + 2) * -1) {
        teaserAreaElementMarginLeft = 0;
        finalMarginLeft = teaserAreaOuterFrameWidth * -1;
    } else if (teaserAreaElementMarginLeft == finalMarginLeft) {
        finalMarginLeft = teaserAreaElementMarginLeft + (teaserAreaOuterFrameWidth * direction);
    } else if (direction == 1 && teaserAreaElementMarginLeft < finalMarginLeft) {
        return;
    } else if (direction == 1 && teaserAreaElementMarginLeft > finalMarginLeft) {
        finalMarginLeft += teaserAreaOuterFrameWidth;
    } else if (direction == -1 && teaserAreaElementMarginLeft < finalMarginLeft) {
        finalMarginLeft -= teaserAreaOuterFrameWidth;
    } else if (direction == -1 && teaserAreaElementMarginLeft > finalMarginLeft) {
        return;
    }

    if (finalMarginLeft > 0 && direction == -1) {
        finalMarginLeft = 0;
        return;
    } else if (finalMarginLeft > 0 && direction == 1) {
        teaserAreaElementMarginLeft = (htmlTeasersNumber + 1) * teaserAreaOuterFrameWidth * -1;
        finalMarginLeft = teaserAreaElementMarginLeft + teaserAreaOuterFrameWidth;
    } else if (finalMarginLeft < (htmlTeasersNumber + 1) * teaserAreaOuterFrameWidth * -1) {
        teaserAreaElementMarginLeft = 0;
        finalMarginLeft = teaserAreaOuterFrameWidth * -1;
    }

    //update of the status display icons
    if (finalMarginLeft == 0 || (finalMarginLeft == -teaserAreaOuterFrameWidth && direction == -1)) {
        imageSrcTemp = teaserStatusdisplayElementImages[htmlTeasersNumber].src;
        teaserStatusdisplayElementImages[htmlTeasersNumber].src = teaserStatusdisplayElementImages[0].src;
        teaserStatusdisplayElementImages[0].src = imageSrcTemp;
    } else if (Math.abs(finalMarginLeft / teaserAreaOuterFrameWidth) - 1 >= 0 && Math.abs(finalMarginLeft / teaserAreaOuterFrameWidth) <= htmlTeasersNumber + 1) {
        imageSrcTemp = teaserStatusdisplayElementImages[Math.abs(finalMarginLeft / teaserAreaOuterFrameWidth) - 1].src;
        teaserStatusdisplayElementImages[Math.abs(finalMarginLeft / teaserAreaOuterFrameWidth) - 1].src = teaserStatusdisplayElementImages[Math.abs(finalMarginLeft / teaserAreaOuterFrameWidth) + direction - 1].src;
        teaserStatusdisplayElementImages[Math.abs(finalMarginLeft / teaserAreaOuterFrameWidth) + direction - 1].src = imageSrcTemp;
    }

    clearTimeout(slideshowTimeout);
    homepageSlideshowAnimation();
}

function homepageSlideshowAnimation() {

    if ((direction == 1 && teaserAreaElementMarginLeft >= finalMarginLeft) || (direction == -1 && teaserAreaElementMarginLeft <= finalMarginLeft)) {

        if (finalMarginLeft == 0) {
            finalMarginLeft = teaserAreaOuterFrameWidth * (htmlTeasersNumber + 1) * -1;
        } else if (finalMarginLeft == teaserAreaOuterFrameWidth * (htmlTeasersNumber + 1) * -1) {
            finalMarginLeft = 0;
        }

        teaserAreaElementMarginLeft = finalMarginLeft;
        teaserAreaElement.style.marginLeft = teaserAreaElementMarginLeft + "px";
        teaserStatusdisplayElement.style.left = (teaserAreaElementMarginLeft * -1) + "px";
        timeoutAutomaticSlideshowAnimation = setTimeout('initializeHomepageSlideshowAnimation(document.getElementById("teaserAreaRightHandSide"));', 5000);
        return;
    }

    var marginLeftDifference = Math.abs(Math.abs(teaserAreaElementMarginLeft) - Math.abs(finalMarginLeft));

    //slow down at the end of the animation
    (marginLeftDifference <= 10) ? teaserAreaElementMarginLeft += 2 * direction : (marginLeftDifference <= 30) ? teaserAreaElementMarginLeft += 5 * direction : teaserAreaElementMarginLeft += 10 * direction;

    teaserAreaElement.style.marginLeft = teaserAreaElementMarginLeft + "px";
    teaserStatusdisplayElement.style.left = (teaserAreaElementMarginLeft * -1) + "px";
    slideshowTimeout = setTimeout(homepageSlideshowAnimation, 25);
}
