(function() {

    YAHOO.util.Event.onContentReady("NLSignUp", function(evt) {

        var NLSignUpEl = document.getElementById("NLSignUp");
        var NLSignUpBodyEl = document.getElementById("NLSignUpBody");
        var NLSignUpList = document.getElementById("NLSignUpList");
        var expandHeight = NLSignUpList.offsetHeight + 10;

        //Setup the exand animation for ther Sign up control.  The expandHeight must be set by the code behind
        //in order to allow the animation to know how far to expand.
        var nlExpandAnim = new YAHOO.util.Anim(NLSignUpBodyEl, {
            height: { from: 1, to: expandHeight }
        });
        nlExpandAnim.duration = 1;
        nlExpandAnim.method = YAHOO.util.Easing.easeOutStrong;


        //Setup the close animation for the sign up control
        var nlCloseAnim;

        //IE seems to have an issue with determining clientHeight when a element is display:none which is required by IE
        //to get the need hidden element effect for this expanding control. This creates a cloase animation now based on browser.
        //So when IE closes it doesn't go to 0;
        var isLegacy = false;
        if (typeof (nlSignUpLegacyMode) !== 'undefined') {
            isLegacy = nlSignUpLegacyMode;
        }

        var isIE = /*@cc_on!@*/false;
        if (isLegacy & isIE) {

            nlCloseAnim = new YAHOO.util.Anim(NLSignUpBodyEl, {
                height: { to: 7 }
            });

        } else {

            nlCloseAnim = new YAHOO.util.Anim(NLSignUpBodyEl, {
                height: { to: 0 }
            });

        }
        nlCloseAnim.duration = 1;
        nlCloseAnim.method = YAHOO.util.Easing.easeOutStrong;

        var hasExpanded = false;

        //Expand the sign up body to display a list of newsletters
        var expand = function() {
            YAHOO.util.Event.removeListener(NLSignUpEl, "click", this);
            nlExpandAnim.animate();
            hasExpanded = true;
        };

        //Close the sign up control only if the user has moused out for more then 1 secound
        var close = function(ev) {

            //The current mouse position
            var currentMousePos = new YAHOO.util.Point(YAHOO.util.Event.getPageX(ev), YAHOO.util.Event.getPageY(ev));

            var NLSignUpRegion = YAHOO.util.Region.getRegion(NLSignUpEl);

            if (!NLSignUpRegion.intersect(currentMousePos)) {

                YAHOO.util.Event.removeListener(document.body, "mousemove", this);
                nlCloseAnim.animate();
                emailAddrTextbox.blur();
            }
        };

        //Assign expand and close animations to events
        YAHOO.util.Event.on(NLSignUpEl, 'click', expand);

        nlExpandAnim.onComplete.subscribe(function(e) {
            YAHOO.util.Event.on(document.body, 'mousemove', close);
        });

        nlCloseAnim.onComplete.subscribe(function(e) {
            YAHOO.util.Event.on(NLSignUpEl, 'click', expand);
        });



        //Clear email textbox when user selects the email textbox
        var emailAddrTextbox = YAHOO.util.Dom.getElementsByClassName('emailAddress', 'input', NLSignUpEl)[0];
        var hasBeenCleared = false;

        YAHOO.util.Event.on(emailAddrTextbox, 'click', function() {
            if (!TOWNHALL.util.Validator.isValidEmail(emailAddrTextbox.value)
				& !hasBeenCleared) {
                emailAddrTextbox.value = "";
                hasBeenCleared = true;
            }
        });

        var sumbitButton = YAHOO.util.Dom.getElementsByClassName('signupSubmit', 'input', NLSignUpEl)[0];

        //When the user submits the page, perform validation
        YAHOO.util.Event.on(sumbitButton, 'click', function(e) {

            if (!TOWNHALL.util.Validator.isValidEmail(emailAddrTextbox.value)) {
                YAHOO.util.Event.stopEvent(e);
                alert('Please enter a valid email address.');
                return;
            }

            var newsletterCheckBoxes = YAHOO.util.Dom.getElementsByClassName('newsletterCheckBox', 'input', NLSignUpList);
            var hasCheckedSignup = false;

            for (var nodeCount = 0; nodeCount < newsletterCheckBoxes.length; nodeCount++) {
                if (newsletterCheckBoxes[nodeCount].checked) {
                    hasCheckedSignup = true;
                }
            }

            if (!hasCheckedSignup) {
                YAHOO.util.Event.stopEvent(e);

                //If the user has not had the control expand for them, expand the control instead of throughing a 
                //validation error
                if (!hasExpanded) {
                    expand();
                } else { 
                    alert('Please select a newsletter.');                
                }
                return;
            }

        });
    });
})();

