﻿$(function () {
    $.ajaxSetup({ cache: false });
    initPopupButtons();
    initSermonPopup();
    initEventsPage();
});

function initSermonPopup() {
    $("a[href*='/Sermons/Listen']").not("#topMenu a").click(function () {
        var dialog = $(".modal");
        dialog
            .load($(this).attr("href") + "/popup")
            .dialog({
                title: $(this).html(),
                width: '600px'
            });

        return false;
    });
}

function initPopupButtons() {
    $("#addButton").click(function () {
        var dialog = $(".modal");
        dialog
            .load($(this).attr("href"))
            .dialog({
                title: $(this).html(),
                width: '600px'
            });

        return false;
    });

    $(".editButton").click(function () {
        var dialog = $(".modal");
        dialog
            .load($(this).attr("href"))
            .dialog({
                title: $(this).html() + " Edit",
                width: '600px'
            });

        return false;
    });

    $(".deleteButton").click(function () {
        if (confirm("Are you sure you want to delete this item?")) {
            $.post($(this).attr("href"), function (data) {
                location.reload(true);
            });
        }

        return false;
    });
}

function initEventsPage() {
    $("#When").live("mousedown", function () {
        $(this).datepicker();
    });
    $("#StartTime").live("mousedown", function () {
        $(this).timepicker({
            ampm: true,
            hourMin: 8,
            hourMax: 16
        });
    });
    $("#EndTime").live("mousedown", function () {
        $(this).timepicker({
            ampm: true,
            hourMin: 8,
            hourMax: 16
        });
    });
}
function reloadCallback() {
    location.reload(true);
}

