﻿if (typeof (tapijttegelcentrale) == "undefined") {
	tapijttegelcentrale = {}
}

tapijttegelcentrale.Spotlight = function() {
    var strContainerID = "";
    var maxCount = 0;
    var iCount = 0;
    var animationInterval;
    var animationSwitchspeed;

	function animateLink(index) {
        jQuery(strContainerID + " div.spotLightNavigation a").each(function(i) {
		if (jQuery(this).attr("class").indexOf("spotLightOrb") != -1) {
                if (i != index) {
                    jQuery(this).removeClass("active");
                } else {
                    jQuery(this).addClass("active");
                }
            }
        });
    }
	
	function setLinks() {
        jQuery(strContainerID + " div.spotLightNavigation a").each(function(i) {
            if (jQuery(this).attr("class").indexOf("spotLightOrb") != -1) {
                jQuery(this).click(function() {
                    stopTimer();

                    setImages(i, true);

                    startTimer(i);
                });
            }
        });

        jQuery(strContainerID + " div.spotLightNavigation a.nextLink").click(function() {
            stopTimer();

            var index = iCount + 1;

            if (index > maxCount) {
                index = 0;
            }

            setImages(index, true);

            startTimer(index);
        });
    }
	
    function setZIndex() {
        var zIndex = (maxCount + 1) * 10;

        jQuery(strContainerID + " li").each(function(i) {
            jQuery(this).css("z-index", zIndex);
            zIndex = zIndex - 10;
        });
    }

    function animateImage(opacity, animate, listItem) {
        if (opacity == 1) {
            listItem.show();
        }

        if (animate) {
            listItem.animate({ "opacity": opacity }, animationSwitchspeed);
        } else {
            listItem.css("opacity", opacity);
        }

        if (opacity == 0) {
            listItem.hide();
        }
    }

    function setImages(index, animate) {
		animateLink(index);
        jQuery(strContainerID + " li").each(function(i) {
            if (i != index) {

                animateImage(0, animate, jQuery(this));

            } else {

                animateImage(1, animate, jQuery(this));

            }
        });

        index++;
    }

    function startTimer(index) {
        iCount = index;

        jQuery(strContainerID).everyTime(animationInterval, "spotlight", function(i) {
            if (iCount >= maxCount) {
                iCount = 0;
            } else {
                iCount++;
            }

            setImages(iCount, true);
        });
    }
	
	function stopTimer() {
        jQuery(strContainerID).stopTime("spotlight");
    }

    /* Start public */

    /**
    * nl.fontysmer.Spotlight.Init()
    */
    this.Init = function(containerID, iAnimationSwitchspeed, iAnimationInterval, iDelay) {
        strContainerID = containerID.toString();

        animationSwitchspeed = iAnimationSwitchspeed;
        animationInterval = iAnimationInterval;

        maxCount = (jQuery(strContainerID + " li").length - 1);

        setZIndex();
		
        setImages(0, false);
		
		setLinks();

        jQuery(strContainerID).oneTime(iDelay, "delay", function() {
            startTimer(0);
        });
    }
    /* End public */
};
