window.core = window.core || {};
window.core.bio = window.core.bio || {};
window.core.bio.page = window.core.bio.page || {};


core.bio.page.botd = function() {

    function init() {
        emailer();
        dateSearches();
    }

    function daysInMonth(month) {
        //2000 picked a leap year so that feburary would have 29 days...        
        return 32 - new Date(2000, month, 32).getDate();
    }

    function dateSearches() {
        $("form.botd-search").each(function() {
            var $this = $(this);

            //Insert Watermarks/hints and select them
            $("select.day", $this).prepend($("<option>").attr("value", "-1").text("Day")).val("-1");
            $("select.month", $this).prepend($("<option>").attr("value", "-1").text("Month")).val("-1");

            $("select.month", $this).bind("change", function() {

                var days = daysInMonth(parseInt($(this).val()) - 1);
                var val = $('select.day', $this).val();

                $('select.day option[value="-1"]', $this).remove();
                $('select.month option[value="-1"]', $this).remove();
                if (val <= 0 || days < val) {
                    
                    //$('select.day option', $this).val("1");
                    $('select.day', $this).val("1");
                }

                var daysInPrevMonth = $('select.day > option', $this).size();
                if (days < daysInPrevMonth) {
                    $('select.day option:gt(' + (days - 1) + ')', $this).remove();
                } else {
                    //alert(daysInPrevMonth);
                    daysInPrevMonth = $('select.day > option', $this).size()

                    for (var i = daysInPrevMonth + 1; i <= days; i++) {
                        $('select.day', $this).append($("<option>").attr("value", i).text(i));
                    }
                }                
                
            });

          

            //Ensure Date Sure Request is valid, ie does not include watermarks/hints
            $("button.submit", $this).click(function() {
                if ($("select.day", $this).val() < 1 && $("select.month", $this).val() < 1) {
                    alert("Please Enter a Valid Month and Day");
                    return false;
                }
            });

        });
    }

    function emailer() {
        $("p.botd-email a").click(function(e) {
            e.preventDefault();
            e.stopPropagation();

            window.open($(this).attr("href"), 'emailmgr', 'width=600, height=500,scrollbars=yes');

            return false;
        });
    }

    return function() {
        $(document).ready(function() { init(); });
    } ();
} ();
