﻿//----------------------------------------------------------------------------------
//DESNEA.Util.Templates
//Desarrollos NEA | www.desarrollosnea.com
//----------------------------------------------------------------------------------



registerNS("DESNEA.Util.Templates");


//----------------------------------------------------------------------------------
//Trim
//----------------------------------------------------------------------------------
DESNEA.Util.templateArmar = function (templConfig) {
    return templateArmar(templConfig);
}
templateArmar = function (templConfig) {

    DESNEA.Util.templateInicializar(templConfig);

    $.getJSON(templConfig.url,
          null,
          function (datos) {
              //              $.each(data.items, function (i, item) {
              //                  $("<img/>").attr("src", item.media.m).appendTo("#images");
              //                  if (i == 3) return false;
              //              });

              $(function () {

                  templConfig.datos = datos;

                  $(templConfig.destinoSelector).html('');

                  $(templConfig.templateSelector)
		            .tmpl(datos)
		            .appendTo(templConfig.destinoSelector);

                  DESNEA.Util.templateCargadoEvent(templConfig);
              });
          });

}

DESNEA.Util.templateInicializar = function (templConfig) {
    return templateInicializar(templConfig);
}
templateInicializar = function (templConfig) {
    //Cargando
    var cargandoMensajeDefault = "Cargando..."
    if (templConfig.cargandoMensaje != null) {
        templConfig.cargandoMensaje = cargandoMensajeDefault;
    }
    $(templConfig.destinoSelector).html = templConfig.cargandoMensaje;
}


DESNEA.Util.templateVacio = function (templConfig) {
    return templateVacio(templConfig);
}
templateVacio = function (templConfig) {
    //Cargando
    var sinDatosMensajeDefault = "[Sin datos]"
    if (templConfig.sinDatosMensaje != null) {
        templConfig.sinDatosMensaje = sinDatosMensajeDefault;
    }
    $(templConfig.sinDatosMensajeDefault).html = templConfig.sinDatosMensaje;
}


DESNEA.Util.templateCargadoEvent = function (templConfig) {
    return templateCargadoEvent(templConfig);
}
templateCargadoEvent = function (templConfig) {

    var sinDatosMensajeDefault = "[Sin datos]"
    if (templConfig.cargadoFuncion != null) {
        eval(templConfig.cargadoFuncion);

        try {
            if (cargadoFuncion == true) {
                DESNEA.Util.templateVacio(templConfig);
                $(templConfig.destinoSelector).html(templConfig.sinDatosMensajeDefault);
            }
        } catch (e) {

        }
    }
}

