﻿var header_bg = "header-0";
var header_x = 0;

$(document).ready(function() {
    // open external links in their own window

//    $('a[href^="http://"]')
//  .attr({
//    target: "_blank",
//    title: "Opens in a new window"
//  });

    $("a[href*='http://']:not([href*='"+location.hostname+"']),[href*='https://']:not([href*='"+location.hostname+"'])").attr({target:"_blank"});
    $(".photo-gallery-list li a").removeAttr("target");
    $("#header-inner-container").everyTime("7s", function() { header_x = header_x == 4 ? 0 : header_x + 1; $("#header-inner-container").removeClass(header_bg); header_bg = "header-" + header_x; $("#header-inner-container").addClass(header_bg); });

    $(".calendar-event-title-link,.govi-events-list li a").each(function() { var url = $(this).attr("href"); $(this).attr("eventurl", url); $(this).removeAttr("href"); });
    $(".calendar-event-title-link,.govi-events-list li a").bind("click", function() { viewEventDetails($(this).attr("eventurl")); });

    if ($("#lnk-request-event").size() > 0) {
        $("#lnk-request-event").bind("click", eventPostingRequest);
    }

    $(".parent-nav").live("click", function() {
        if (!$("#sub-navigation-container").is(":visible")) {
            $("#sub-navigation-container").show("blind", "slow");
        }
        if (!$($(this).attr("href")).is(":visible")) {
            $(".sub-nav-item").hide("slide");
        }

        $($(this).attr("href")).show("slide");
        return false;
    });

    $(".subnavigation li a, .child-page-navigation li a, .event-listing a").bigTarget();
    $(".town-committee").unbind("bigTarget");

    $("#lnk-close-sub-nav").live("click", function() { $(".sub-nav-item").hide("slide"); $("#sub-navigation-container").hide("blind", "slow"); });
});

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: "Middletown, 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");
}
