﻿jQuery.Eir = function (imageList, linkList, targetList, duration, activeImage, inactiveImage, stopRotationOnClick) {

    var currentImage = -1;
    var images = imageList;
    var links = linkList;
    var targets = targetList;
    var displayDuration = duration;

    // preload the images
    var cache = [];
    for (var i = 0; i < images.length; i++) {
        var cacheImage = document.createElement('img');
        cacheImage.src = images[i];
        cache.push(cacheImage);
    }

    // setup the buttons list
    var listText = "";
    for (i = 0; i < images.length; i++) {
        listText += '<li><img src="' + inactiveImage + '" alt="" /><img src="' + activeImage + '" alt="" /></li>';
    }
    $("#img-rotator-buttons ul").html(listText);

    // set the button mouseovers
    $("#img-rotator-buttons li").mouseenter(function () {
        highlight($(this).index());
    })
    .mouseleave(function () {
        if ($(this).index() != currentImage) {
            lowlight($(this).index());
        }
    })
    .click(function () {
        if ($(this).index() != currentImage) {
            //interactedWith = true;
            clearInterval(autoRotate);

            setActive($(this).index());
            lowlight(currentImage);
            highlight($(this).index());
            currentImage = $(this).index();
            // Reset the auto rotation playback after selection
            if (!stopRotationOnClick) {
                setRotate();
            }
        }
    });

    // pause rotator on mouse hover
    $("#img-rotator")
        .mouseenter(function ()
        {
            if (autoRotate) clearInterval(autoRotate);
        })
        .mouseleave(function ()
        {
            setRotate();
        })
    ;

    // animation methods
    function highlight(itemNumber) {
        $("img:eq(1)", "#img-rotator-buttons li").stop().animate({ opacity: 0 }, 200);
        $("#img-rotator-buttons li:eq(" + itemNumber + ") img:eq(1)").stop().animate({ opacity: 1 }, 200);
        $("#img-rotator-buttons li:eq(" + currentImage + ") img:eq(1)").stop().animate({ opacity: 1 }, 200);
    }

    function lowlight(itemNumber) {
        $("#img-rotator-buttons li:eq(" + itemNumber + ") img:eq(1)").stop().animate({ opacity: 0 }, 200);
    }

    function setActive(itemNumber) {
        if (itemNumber != currentImage) {
            if (currentImage > 1) lowlight(currentImage);
            currentImage = itemNumber;
            $("#img-rotator-hidden").attr("src", images[currentImage]).stop().animate({ opacity: 0 }, 0).animate({ opacity: 1 }, 300);
            $("#img-rotator").attr("src", $("#img-rotator-hidden").attr("src"));
            if (links[currentImage] != null && links[currentImage].length > 0) $("#img-rotator-link").attr("href", links[currentImage]);
            else $("#img-rotator-link").attr("href", "javascript:void(0);");
            if (targets[currentImage]) $("#img-rotator-link").attr("target", targets[currentImage]);
        }
    }

    // show the current image
    setActive(0);
    highlight(currentImage);

    var interactedWith = false;
    var autoRotate;
    setRotate();

    function setRotate() {
        if (images.length > 1) {
            autoRotate = setInterval(function () {
                //alert("rotate");
                if (interactedWith) {//-- was causing loop issues so move clear to the function that interactedWith autoRotate
                    clearInterval(autoRotate);
                    interactedWith = false;
                    setRotate();
                }
                else {
                    nextImage = currentImage + 1;
                    if (nextImage > images.length - 1) nextImage = 0;
                    setActive(nextImage);
                    highlight(currentImage);
                }
            }, duration * 1000);
        } else {
            // Set the active image and do not allow the autoRotation
            $("#img-rotator").attr("src", $("# img-rotator-hidden").attr("src"));
            // Remove the selector bar indicator
            $("#img-rotator-buttons ul").html("");
        }
    }
}
