/**
 * @fileOverview All JS-code for domain.nl
 * @author name
 * @since 1.0 - 2010-8-6
 * @version 1.0 - 2010-8-6
 */




if (typeof(nl) == "undefined") {
	var nl = {}
}
if (typeof(nl.agroenco) == "undefined") {
    nl.agroenco = {}
}






/**
 * @namespace Boiler plate class
 */
nl.agroenco.Spotlight = (function() {
    var maxCount = 0;
    var iCount = 0;

    function animateLink(index) {
        //alert(index);
        jQuery("li.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 animateImage(opacity, animate, listItem) {
        if (opacity == 1) {
            listItem.show();
        }

        if (animate) {
            listItem.animate({ "opacity": opacity }, 600);
        } else {
            listItem.css("opacity", opacity);
        }

        if (opacity == 0) {
            listItem.hide();
        }
    }

    function setImages(index, animate) {
        animateLink(index);

        jQuery("div.containerSpotLight ul li").each(function(i) {
            if (jQuery(this).attr("class").indexOf("spotLightItem") != -1) {
                if (i != index) {

                    animateImage(0, animate, jQuery(this));

                } else {

                    animateImage(1, animate, jQuery(this));

                }
            }
        });

        index++;
    }

    function setLinks() {
        jQuery("li.spotLightNavigation a").each(function(i) {
            if (jQuery(this).attr("class").indexOf("spotLightOrb") != -1) {
                jQuery(this).click(function() {
                    stopTimer();

                    setImages(i, true);

                    startTimer(i);
                });
            }
        });

        jQuery("li.spotLightNavigation a.nextLink").click(function() {
            stopTimer();

            var index = iCount + 1;

            if (index > maxCount) {
                index = 0;
            }

            setImages(index, true);

            startTimer(index);
        });
    }

    function startTimer(index) {
        iCount = index;

        jQuery("div.containerSpotLight ul").everyTime(8000, "spotlight", function(i) {
            if (iCount >= maxCount) {
                iCount = 0;
            } else {
                iCount++;
            }

            setImages(iCount, true);
        });
    }

    function stopTimer() {
        jQuery("div.containerSpotLight ul").stopTime("spotlight");
    }

    /* Start public */
    return {
        /**
        * nl.agroenco.Spotlight.Init()
        **/
        Init: function() {
            maxCount = (jQuery("div.containerSpotLight ul li").length - 2);

            setImages(0, false);

            setLinks();

            startTimer(0);
        }
    }
    /* End public */
})();





nl.agroenco.SubscriptionForm = (function() {
    var config = {
        introText: "Ik wil me aanmelden voor de gratis digitale nieuwsbrief.",
        watermark: "e-mailadres",
        defaultInputValue: "Subscriber"
    }

    function insertIntroText(subscriptionForm) {
        subscriptionForm.prepend("<span class=\"sf_newsletterSubscriptionIntro arrowLeft\">" + config.introText + "</span>");
    }

    function hideFields(ul) {
        ul.find("li").each(function(i) {
            var input = jQuery(this).find("input");
            if ((input.attr("name") != "email") && (input.attr("type") != "submit") && (jQuery(this).attr("class") != "sf_emailValidation")) {
                input.val(config.defaultInputValue);
                jQuery(this).hide();
            } else {
                jQuery(this).css({
                    position: "absolute",
                    top: "0px",
                    left: "0px"
                });

                setClassAndWaterMark(input, i);
            }

            if (input.attr("type") == "submit") {
                jQuery(this).css({
                    position: "absolute",
                    top: "0px",
                    right: "0px",
                    left: "auto",
                    width: "72px"
                });

                input.attr("class", "sf_subscribeBtn");

                input.click(function() {
					var newHeight = (3 + jQuery("span.sf_newsletterSubscriptionIntro.arrowLeft").height() + jQuery(this).height());
					ul.height(newHeight);
					ul.find("input.sf_subscribeTxt.formInputField").focus();
                });
            }

            if (jQuery(this).attr("class") == "sf_emailValidation") {
                jQuery(this).css({
                    position: "absolute",
                    top: "22px",
                    left: "0px"
                });
            }
        });
    }

    function setClassAndWaterMark(input, i) {
        if (i == 0) {
            var labelText = input.parent().find("label").text();

            if (labelText != "") {
                config.watermark = labelText.replace(":", "").toLowerCase();
            }

            input.addClass("formInputField");

            input.focus(function() {
                focus(jQuery(this));
            });

            input.blur(function() {
                blur(jQuery(this));
            });

            input.val(config.watermark);
        }
    }

    function focus(el) {
        if (el.val() == config.watermark) el.val("");
    }

    function blur(el) {
        if (el.val() == "") el.val(config.watermark);
    }

    /* Start public */
    return {
        /**
        * nl.agroenco.Spotlight.Init()
        **/
        Init: function() {
            var subscriptionForm = jQuery("span.sf_newsletterSubscriptionForm fieldset");
            var ul = subscriptionForm.find("ul");

            insertIntroText(subscriptionForm);

            hideFields(ul);
        }
    }
    /* End public */
})();
