﻿
var utils = {};

utils.calendar = {};

utils.calendar.months = {};

utils.calendar.callCalendar = function(y, m, d, calendarDivId, currentY, currentM) {
    var div = document.getElementById(calendarDivId);
    var monthName = y + "" + m;
    var curMonthName = currentY + "" + currentM;
    if (!utils.calendar.months[curMonthName]) {
        utils.calendar.months[curMonthName] = document.getElementById(calendarDivId).innerHTML;
    }
    if (utils.calendar.months[monthName]) {
        document.getElementById(calendarDivId).innerHTML = utils.calendar.months[monthName];
        return false;
    }
    var load = utils.controls.createLoadingMessage("calLoad", div);
    //div.appendChild(load);

    $.get("services/calendar.aspx", { Y: y, M: m, D: d }, function(data) {
        document.getElementById(calendarDivId).innerHTML = data;
        utils.calendar.months[monthName] = data;
        load.parentNode.removeChild(load);
    }
    );

}

utils.calendar.showDetail = function(detailTitle, detailTextId, calendarDivId) {
    var detailText = document.getElementById(detailTextId).innerHTML;
    if (detailText == '') {
        detailText = '<p>Nessun evento previsto.</p><p>Vuoi inserire il tuo?<br/><a class=\'lnkSegnala\' href=\'segnala.aspx\'>Compila il modulo!</a></p>';
    }
    document.getElementById(calendarDivId + '_text').innerHTML = detailText;
    document.getElementById(calendarDivId + '_title').innerHTML = detailTitle;

}

utils.gallery = {};

utils.gallery.swapFotoProperty = function(idFoto, property) {
    var div = document.getElementById("dv_" + property + "_" + idFoto);
    div.innerHTML = "...";
    $.get('../services/GalleryButton.aspx', { Tipo: property, IdFoto: idFoto },
        function(res) {
            div.innerHTML = res;
        });
};

utils.gallery.browseSlideShow = function(idOldPhoto, idNewPhoto) {
    document.getElementById(idOldPhoto).style.display = "none";
    document.getElementById(idNewPhoto).style.display = "block";
};

utils.controls = {};

utils.controls.createLoadingMessage = function(id, dest, customText) {
    if (!customText) {
        customText = "caricamento in corso";
    }
    var d = document.createElement("DIV");
    d.className = "dvInfoPanel";
    var s = document.createElement("DIV");
    s.className = "loadingIcon";
    d.appendChild(s);
    var p = document.createElement("P");
    p.innerHTML = customText;
    d.appendChild(p);
    d.id = id;
    var w = dest.clientWidth - 10;
    var x = $(dest).offset().left + 5;
    var h = 38;
    var y = (dest.clientHeight - h) / 2 + $(dest).offset().top;
    d.style.width = w + "px";
    d.style.height = h + "px";
    d.style.overflow = "hidden";
    d.style.position = "absolute";
    d.style.display = "block";
    d.style.left = x + "px";
    d.style.top = y + "px";
    document.body.appendChild(d);
    return d;
}
