﻿var etPretitulo;
var etTitulo;
var etBreve;
var anchoFoto;
var verNot;
var cantNot;
var subseccion = "";
var btnsel = 'btn0';
var notNro = 0;
var busco = true;
var ind = 0
var contenedores = ['colIzqDoble', 'colDerDoble'];

$(document).ready(function () {
    etPretitulo = "p";
    etTitulo = "h2";
    etBreve = "p";
    anchoFoto = 240;
    verNot = "";
    var seccion;
    var tn = getUrlVars()["TipoNoticia"];
    if (tn == null) {
		GetNoticias('H1')
        seccion = 'H1';
    } else if (tn == "TI") {
        tn = tn.replace("#", "");
        GetNoticias(tn)
        seccion = tn;
    } else {
        tn = tn.replace("#", "");
        GetNoticias(tn)
        seccion = tn;
    }
    CargarSubsecciones(seccion);
});



function getUrlVars() {
    var vars = {};
    var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function (m, key, value) {
        vars[key] = value;
    });
    return vars;
}


function decodificar(cad) {
    if (cad != null) {
        cad = cad.replace("%E1", "á");
        cad = cad.replace("%E9", "é");
        cad = cad.replace("%ED", "í");
        cad = cad.replace("%F3", "ó");
        cad = cad.replace("%FA", "ú");
        cad = cad.replace("%FC", "ü");
        cad = cad.replace("%C1", "Á");
        cad = cad.replace("%C9", "É");
        cad = cad.replace("%CD", "Í");
        cad = cad.replace("%D3", "Ó");
        cad = cad.replace("%DA", "Ú");
        cad = cad.replace("%DC", "Ü");
        cad = cad.replace("%F1", "ñ");
        cad = cad.replace("%D1", "Ñ");
        cad = cad.replace("%BA", "º");
        cad = cad.replace("%26", "&");
        cad = cad.replace("%20", " ");
        cad = cad.replace("%28", "(");
        cad = cad.replace("%3D", "=");
        cad = cad.replace("%29", ")");
        cad = cad.replace("%2C", ",");
        cad = cad.replace("%A1", "¡");
        cad = cad.replace("%21", "!");
        cad = cad.replace("%3B", ";");
        cad = cad.replace("%3A", ":");
        cad = cad.replace("%BF", "¿");
        cad = cad.replace("%3F", "?");
        cad = cad.replace("%24", "$");
        cad = cad.replace("%25", "%");
        cad = cad.replace("%22", "\"");
        cad = cad.replace("%23", "#");
        return unescape(cad)
    }
}


function mostrarMas() {
    cantNot = cantNot + 6;
    if (getUrlVars()["TipoNoticia"] == null)
        ObtenerNoticias(['colIzqDoble', 'colDerDoble'], 'H1', cantNot);
    else ObtenerNoticias(['colIzqDoble', 'colDerDoble'], getUrlVars()["TipoNoticia"], cantNot);
}


function GetNoticias(seccion) {
    var i;
    var link = "";
    var img;
    var strCadenas = new Array(contenedores.length);
    for (i = 0; i < contenedores.length; i++) {
        strCadenas[i] = "";
        $("." + contenedores[i]).html("");
    }
    var ppal = obtenerPrincipal(seccion, subseccion);
    if (ppal == null) cont = 0;
    else cont = 1;
    var strPpal = "";
    while (busco) {
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "Archivos_Gestor_Noticias/GestorNoticias.asmx/ObtenerTitulosyBrevesNoticiasPorSeccion",
            data: "{ strCodSeccion:'" + seccion + "' , nro: " + notNro + ", strCodSubseccion: '" + subseccion + "' }",
            dataType: "json",
            async: false,
            success: function (data) {
                c = eval(data.d);
                if (c.length != 0) {
                    if (c[0].Cod_Noticia != ppal) {
                        if (c[0].ImgPpal == "") {
                            img = "";
                        } else {
                            ruta = "Archivos_Gestor_Noticias-Datos/ImagenesNoticias/" + c[0].Cod_Noticia.toString() + "/Principal/" + c[0].ImgPpal; ;
                            img = '<img src= "' + ruta + '" alt="" width="' + anchoFoto + '"  />'
                        }
                        if (cont == contenedores.length) {
                            cont = 0;
                        }
                        strCadenas[cont] += '<' + etPretitulo + '>' + unescape(c[0].PreTitulo) + '</' + etPretitulo + '><a href="noticia.html" onclick="verNoticia(' +
                                        c[0].Cod_Noticia + ')">' + img + '</a><' + etTitulo + '><a href="noticia.html" onclick="verNoticia(' + c[0].Cod_Noticia +
                                        ')">' + unescape(c[0].Titulo) + '</a></' + etTitulo + '><p><' + etBreve + '>' + unescape(c[0].Breve) + '</' + etBreve +
                                        '><br /> <a href="noticia.html" onclick="verNoticia(' + c[0].Cod_Noticia + ')">' + verNot + '</a> </p><br/>';
                        $("." + contenedores[cont]).html(strCadenas[cont]);
                        cont = cont + 1;
                    } else {
                        if (c[0].ImgPpal == "") {
                            img = "";
                        } else {
                            ruta = "Archivos_Gestor_Noticias-Datos/ImagenesNoticias/" + c[0].Cod_Noticia.toString() + "/Principal/" + c[0].ImgPpal; ;
                            img = '<img src= "' + ruta + '" alt="" width="' + anchoFoto + '"  />'
                        }
                        strPpal += '<' + etPretitulo + '>' + unescape(c[0].PreTitulo) + '</' + etPretitulo + '><a href="noticia.html" onclick="verNoticia(' +
                                c[0].Cod_Noticia + ')">' +
                                img + '</a><' + etTitulo + '><a href="noticia.html" onclick="verNoticia(' + c[0].Cod_Noticia + ')">' + unescape(c[0].Titulo) +
                                '</a></' + etTitulo + '><p><' + etBreve + '>' + unescape(c[0].Breve) + '</' + etBreve +
                                '><br /> <a href="noticia.html" onclick="verNoticia(' + c[0].Cod_Noticia + ')">' + verNot + '</a> </p><br/>';
                    }
                    notNro = notNro + 1
                } else {busco = false }
            }
        });
    }
    if (ppal != null) {
        strCadenas[0] = strPpal + strCadenas[0];
        $("." + contenedores[0]).html(strCadenas[0]);
    }
        $("." + contenedores[cont - 1]).html(strCadenas[cont - 1]);
    
}



function ObtenerNoticias(seccion) {
    var c;
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "Archivos_Gestor_Noticias/GestorNoticias.asmx/ObtenerTitulosyBrevesNoticiasPorSeccion",
        data: "{ strCodSeccion:'" + seccion + "' , nro: " + notNro + ", strCodSubseccion: '" + subseccion + "' }",
        dataType: "json",
        async: false,
        success: function (data) {
            c = eval(data.d);
            if (c.length != 0) {
                mostrarNoticias(c, contenedores, seccion);
                notNro = notNro + 1
            }else busco = false
        }
    });
};


function CargarSubsecciones(seccion) {
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "Archivos_Gestor_Noticias/GestorNoticias.asmx/ObtenerSubseccionesPorSeccion",
        data: "{ CodSeccion:'" + seccion + "' }",
        dataType: "json",
        async: true,
        success: function (data) {
            c = eval(data.d);
            strCadena = '<div class="ges-btn-archivos-sel" id="btn0"><a href="#" onclick="javascript:ObtNoticiasSubseccion(0, \''+seccion+'\')" title="Principal">Principal</a></div>';
            for (i = 0; i < c.length; i++) {
                strCadena += '<div class="ges-btn-archivos" id="btn' + c[i].ID + '"><a href="#" onclick="javascript:ObtNoticiasSubseccion(' + c[i].ID + ', \'' + seccion + '\')" title="' + decodificar(c[i].Nombre) + '">' + decodificar(c[i].Nombre) + '</a></div>';
            }
            $("#nav").html(strCadena);
        }
    });
    
}


function ObtNoticiasSubseccion(idSub, seccion) {
    $('#btn' + idSub).addClass('ges-btn-archivos-sel');
    $('#' + btnsel).removeClass('ges-btn-archivos-sel');
    $('#' + btnsel).addClass('ges-btn-archivos');
    btnsel = "btn" + idSub;
    if (idSub == 0) idSub = "";
    subseccion = idSub;
    ObtenerNoticias(seccion, 0);
}


function mostrarNoticias(data, contenedores, IdSecc) {
    var i;
    var link = "";
    var img;
    var strCadenas = new Array(contenedores.length);
/*    for (i = 0; i < contenedores.length; i++) {
        strCadenas[i] = "";
        $("." + contenedores[i]).html("");
    }*/
    var ppal = obtenerPrincipal(IdSecc, subseccion);
    if (ppal == null) cont = 0;
    else cont = 1;
    var strPpal = "";
    for (i = 0; i < data.length; i++) {
        if (data[i].Cod_Noticia != ppal) {
            if (data[i].ImgPpal == "") {
                img = "";
            } else {
                ruta = "Archivos_Gestor_Noticias-Datos/ImagenesNoticias/" + data[i].Cod_Noticia.toString() + "/Principal/" + data[i].ImgPpal; ;
                img = '<img src= "' + ruta + '" alt="" width="' + anchoFoto + '"  />'
            }
            if (cont == contenedores.length) {
                cont = 0;
            }
            strCadenas[cont] += '<' + etPretitulo + '>' + unescape(data[i].PreTitulo) + '</' + etPretitulo + '><a href="noticia.html" onclick="verNoticia(' + data[i].Cod_Noticia + ')">' +
                                img + '</a><' + etTitulo + '><a href="noticia.html" onclick="verNoticia(' + data[i].Cod_Noticia + ')">' + unescape(data[i].Titulo) + '</a></' + etTitulo +
                                '><p><' + etBreve + '>' + unescape(data[i].Breve) + '</' + etBreve + '><br /> <a href="noticia.html" onclick="verNoticia(' + data[i].Cod_Noticia + ')">' + verNot + '</a> </p><br/>';
            $("." + contenedores[cont]).html(strCadenas[cont]);
            cont = cont + 1;
        } else {
            if (data[i].ImgPpal == "") {
                img = "";
            } else {
                ruta = "Archivos_Gestor_Noticias-Datos/ImagenesNoticias/" + data[i].Cod_Noticia.toString() + "/Principal/" + data[i].ImgPpal; ;
                img = '<img src= "' + ruta + '" alt="" width="' + anchoFoto + '"  />'
            }
            strPpal += '<' + etPretitulo + '>' + unescape(data[i].PreTitulo) + '</' + etPretitulo + '><a href="noticia.html" onclick="verNoticia(' + data[i].Cod_Noticia + ')">' +
                                img + '</a><' + etTitulo + '><a href="noticia.html" onclick="verNoticia(' + data[i].Cod_Noticia + ')">' + unescape(data[i].Titulo) + '</a></' + etTitulo +
                                '><p><' + etBreve + '>' + unescape(data[i].Breve) + '</' + etBreve + '><br /> <a href="noticia.html" onclick="verNoticia(' + data[i].Cod_Noticia + ')">' + verNot + '</a> </p><br/>';
        }
    }
    if (ppal != null) {
        strCadenas[0] = strPpal + strCadenas[0];
        $("." + contenedores[0]).html($("." + contenedores[0]).html() + strCadenas[0]);
    }
    if (data.length == cantNot) {
        strCadenas[cont - 1] += '<br /><div align="right"><a href="#" onclick="javascript:mostrarMas()">Ver mas</a></div>';
        $("." + contenedores[cont - 1]).html($("." + contenedores[0]).html() + strCadenas[cont - 1]);
    }
};



function obtenerPrincipal(IdSecc, IdSubs) {
    if (IdSubs == "") IdSubs = 0;
    var pri;
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "Archivos_Gestor_Noticias/GestorNoticias.asmx/ObtenerPrincipal",
        data: "{Id_Seccion: '" + IdSecc + "', Id_Subseccion: " + IdSubs + "}",
        dataType: "json",
        async: false,
        success: function (data) {
            var c = eval(data.d);
            pri = c;
        }
    });
    return pri;
}



function verNoticia(id) {
    $.cookie("cod", id);
}
