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();

        //
        quoteOfTheDay();

    }

    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 fieldset").each(function() {
            var $this = $(this);


            if ($("input.date", $this).size() > 0) {
                $this.append('<div class="text-mask" ></div>');
                $("div.text-mask", $this).bind("click", function(e) { $("img.ui-datepicker-trigger",$this).trigger("click"); } );
                $("input.date", $this).each(function() {
                    $(this).datepicker({ dateFormat: 'm/d', showAnim: 'slideDown', showOn: 'both', constrainInput: true, buttonImage: '/images/botd2/icon.calendar.jpg', buttonImageOnly: true }).attr("disabled","disabled");
                });
                $("input.date",$this).val($("input.date",$this).val().replace("_","/") );
            } else {
                //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;
                } else { 
                    if ( ! /[\d]{1,2}[\/][\d]{1,2}/.test($("input.date",$this).val() ) ) {                                                                
                        return false;
                    } else { 
                        
                        $("input.date",$this).removeAttr("disabled").val($("input.date",$this).val().replace("/","_") );
                    }
                }
            });
        });
    }

    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;
        });
    }

    function quoteOfTheDay() {
        $("div.quote-of-day").each(function() {
            var quoteData = null;
            var $qotd = $(this);
            jQuery.ajax({
                url: "/js/data/core.bio.page.botd.data.xml",
                timeout: "10000",
                success: function(data) {
                    quoteData = data;
                },
                complete: function(XMLHttpRequest, textStatus) {
                    var now = new Date();
                    var results = $(quoteData).find("month[id='" + (now.getMonth() + 1) + "']:first day[id='" + now.getDate() + "']");
                    if ($(results).size() <= 0) {
                        $("blockquote", $qotd).text("Unable to Load Quote For Today");
                    } else {
                        $(results).each(function() {
                            var url = $(this).children("articleURL").text();
                            $("blockquote", $qotd).text($(this).children("quote").text());
                            $("cite", $qotd).html('<a href="' + url + '"> ' + $(this).children("name").text() + '</a>');
                        });
                    }
                }
            });
        });
    }

    return function() {
        $(document).ready(function() { init(); });
    } ();
} ();
