﻿$(document).ready(function() {
    $(".calendar-event-title-link").each(function() { var url = $(this).attr("href"); $(this).attr("eventurl", url); $(this).removeAttr("href"); });
    $(".calendar-event-title-link").bind("click", function() { viewEventDetails($(this).attr("eventurl")); });

    if ($("#lnk-request-event").size() > 0) {
        $("#lnk-request-event").bind("click", eventPostingRequest);
    }
});

function createOverlayAlertBannerDialog(clientName, alerts) {

    if (alerts && alerts.length > 0) {
        var img = $.create("img", { src: "/images/icons/ajax-loader.gif", alt: "" });
        var wait = $.create("span", "One moment please...");
        var container = $.create("div", { id: "overlay-alert-container", className: "content" },[img,wait]);
        var detailsDialog = $.create("div", { id: "overlay-alert-dialog", style: "display:none;" }, container);
        detailsDialog.appendTo("body");

        $("#overlay-alert-dialog").dialog({ autoOpen: true, draggable: false, width: 600, height: 550, modal: true, overlay: { opacity: 0.5, background: "black" }, title: clientName + " Notification", close: function() { $("#overlay-alert-dialog").dialog("destroy"); $("#overlay-alert-dialog").remove(); } });

        $("#overlay-alert-container").load("/alerts/active/list?modalBannersOnly=true");
    }
}

function create_modal_alerts(){
    var img = $.create("img", { src: "/images/icons/ajax-loader.gif", alt: "" });
    var wait = $.create("span", "One moment please...");
    var container = $.create("div", { id: "overlay-alert-container", className: "content" },[img,wait]);
    var detailsDialog = $.create("div", { id: "overlay-alert-dialog", style: "display:none;" }, container);
    detailsDialog.appendTo("body");

    $("#overlay-alert-dialog").dialog({ autoOpen: true, draggable: false, width: 600,
        height: 550, modal: true,
        overlay: { opacity: 0.5, background: "black" },
        title: "Nutley, NJ Notification",
        open: function(){ $("#overlay-alert-container").load("/alerts/modal_banners");$(".ui-dialog").css("z-index",999999999999999);},
        close: function() { $("#overlay-alert-dialog").dialog("destroy"); $("#overlay-alert-dialog").remove(); }
    });
    $("#overlay-alert-dialog").dialog("open");

}

var department;

function loadFullCalendarView(month, year) {
    $("#calendar-loader").ajaxStart(function() { $("#full-page-calendar-container").html(""); $(this).show(); }).ajaxComplete(function() { $(this).unbind("ajaxStart").hide(); });
    var calUrl = "/calendar/";
    if (department){
        calUrl += "department/" + department + "/";
    }
    calUrl += month + "/" + year;
    
    $.get(calUrl, function(response) { $("#full-page-calendar-container").html(response).removeAttr("class"); }, "html");
}

function wireFullCalendarControl() {
    $("#lnkNextMonth").bind("click", function() { goToAnotherMonth($(this).attr("gotomonth")); });
    $("#lnkPreviousMonth").bind("click", function() { goToAnotherMonth($(this).attr("gotomonth")); });

    $(".calendar-event").each(function() { var url = $(this).attr("href"); $(this).attr("eventurl", url); $(this).removeAttr("href"); });
    $(".calendar-event").bind("click", function() { viewEventDetails($(this).attr("eventurl")); });
    $(".more-events-for-date-link").bind("click", showEventsForDate);
}

function showEventsForDate() {
    var selectedDate = $(this).attr("selecteddate");

    var img = $.create("img", { src: "/images/icons/ajax-loader.gif", alt: "" });
    var wait = $.create("span", "One moment please...");
    var container = $.create("div", { id: "event-details-container", className: "content" }, [img, wait]);
    var detailsDialog = $.create("div", { id: "event-details-dialog", style: "display:none;" }, container);
    detailsDialog.appendTo("body");

    $("#event-details-dialog").dialog({ autoOpen: true, draggable: false, width: 800, height: 550, modal: true, overlay: { opacity: 0.5, background: "black" }, title: "Events for Date", close: function() { $("#event-details-dialog").dialog("destroy"); $("#event-details-dialog").remove(); } });
    $("#event-details-container").load("/calendar/date/" + selectedDate);
    $("#event-details-dialog").show().dialog("open");
}

function goToAnotherMonth(gotomonth) {
    var month = gotomonth.split("-")[0];
    var year = gotomonth.split("-")[1];
    loadFullCalendarView(month, year);
}

function loadEventsCalendar(calendar) {
    var month = calendar.formatDate(calendar.get_visibleDate(), "MM");
    var year = calendar.formatDate(calendar.get_visibleDate(), "yyyy");
    loadFullCalendarView(month, year, true);
}
function viewEventDetails(eventHref) {

    var img = $.create("img", { src: "/images/icons/ajax-loader.gif", alt: "" });
    var wait = $.create("span", "One moment please...");
    var container = $.create("div", { id: "event-details-container", className: "content" }, [img, wait]);
    var detailsDialog = $.create("div", { id: "event-details-dialog", style: "display:none;" }, container);
    detailsDialog.appendTo("body");

    $("#event-details-dialog").dialog({ autoOpen: true, draggable:false,width: 600, height: 550, modal: true, overlay: { opacity: 0.5, background: "black" }, title: "Event Details", close: function() { $("#event-details-dialog").dialog("destroy"); $("#event-details-dialog").remove(); } });
    $("#event-details-container").load(eventHref);
    $("#event-details-dialog").show().dialog("open");
}

function getDrivingDirections(locationUuId) {

    var img = $.create("img", { src: "/images/icons/ajax-loader.gif", alt: "" });
    var wait = $.create("span", "One moment please...");
    var container = $.create("div", { id: "driving-directions-container", className: "content" }, [img, wait]);
    var detailsDialog = $.create("div", { id: "driving-directions-dialog", style: "display:none;" }, container);
    detailsDialog.appendTo("body");

    $("#driving-directions-dialog").dialog({ autoOpen: true, width: 800, height: 650, modal: true, overlay: { opacity: 0.5, background: "black" }, title: "Driving Directions", close: function() { $("#driving-directions-dialog").remove(); } });
    $("#driving-directions-container").load("/" + clientNameKey + "/maps/directions/to/" + locationUuId);
    $("#driving-directions-dialog").show().dialog("open");
}

function eventPostingRequest() {
    var img = $.create("img", { src: "/images/icons/ajax-loader.gif", alt: "" });
    var wait = $.create("span", "One moment please...");
    var container = $.create("div", { id: "event-request-container", className: "content" }, [img, wait]);
    var detailsDialog = $.create("div", { id: "event-request-dialog", style: "display:none;" }, container);
    detailsDialog.appendTo("body");

    $("#event-request-dialog").dialog({ autoOpen: true, draggable: false, width: 600, height: 550, modal: true, overlay: { opacity: 0.5, background: "black" }, title: "Request an Event Posting", close: function() { $("#event-request-dialog").dialog("destroy"); $("#event-request-dialog").remove(); } });
    $("#event-request-container").load("/event/posting/request");
    $("#event-request-dialog").show().dialog("open");
}
