$(function () {
    String.prototype.toProperCase = function () {
        return this.toLowerCase().replace(/\w+/g, function (s) {
            return s.charAt(0).toUpperCase() + s.substr(1);
        });
    };
    // open on click, close on click anywhere but menu
    $('.cal_service_links ul').hide();
    $('.add_to_cal_head a').attr("href", "#");
    $('.add_to_cal_head').one('click', openMenu);
    $('.cal_subscribe').css({
        position: "absolute",
        left: "175px",
        top: "2px"
    });
    function openMenu(e) {
        var button = $(this);
        var menu = $(this).parent().children("ul");
        menu.click(function (e) {
            e.stopPropagation();
        }).show('fast', function () {
            $('.add_to_cal_head').one('click', {
                button: button,
                menu: menu,
                returnval: false
            }, closeMenu);
            $(document).one('click', {
                button: button,
                menu: menu,
                returnval: true
            }, closeMenu);
        });
        return false;
    }
    function closeMenu(e) {
        e.data.menu.hide('slow');
        e.data.button.one('click', openMenu);
        return e.data.returnval;
    }

    $(".date_field").datepicker({
        dateFormat: 'm/d/yy',
        changeMonth: true,
        changeYear: true,
        minDate: '-547'
    });

    $("#events_all_categories").click(function () {
        if (this.checked)
            $(".catbox").removeAttr("checked");
    });

    $(".catbox").click(function () {
        if (this.checked)
            $("#events_all_categories").removeAttr("checked");
    });
    $("#events_keyword").focus(function () {
        this.select();
    });
    $('#events_start_date_img').click(function () {
        $('#events_start_date').focus();
    });
    $('#events_end_date_img').click(function () {
        $('#events_end_date').focus();
    });

    if ($('#more_categories input:checked').length == 0) {
        $('#more_categories').hide();
    }
    else {
        $('#more_categories_link').text('Less');
    }
    $('#more_categories_link').click(function () {
        if ($('#more_categories').css('display') == 'none')
            $('#more_categories_link').text('Less');
        else
            $('#more_categories_link').text('More');
        $('#more_categories').toggle("slow");
        return false;
    });

    if ($("div.location").length > 10) {
        $("div.location:gt(9)").hide();
        $("div.location:last").after('<p><a href="#" id="more_locations">More Locations</a></p>');
    }

    $("#more_locations").toggle(function () {
        $("div.location:gt(9)").slideDown();
        $("#more_locations").text("Close");
    }, function () {
        $("div.location:gt(9)").slideUp();
        $("#more_locations").text("More Locations");
    });

    $('#tabs').tabs();
    $('#related_links').corner();
});

