﻿var carruselActivo = true;
var contenidoActivo = "";
var isFirstTime = true;
var arrProyectos = "";
var arrNoticias = "";
var indiceActivo = 0;
var globalAgrupacion = "";
var globalFiltro = "";
var seleccion = "";
var sitioActivo = "";
var idbtnActivo = 0;
var paginaActual = 1;
var paginas = 0;
var eltosbypagina = 8;
var paginaActualProducto=0;


function mostrarSubMenu(id) {
    $('#Navegacion-SubSeccion').empty();
    loadContent(id);
    $.get('xml/menu.xml', function(data) {
        $(data).find("item").each(function() {
            var $item = $(this);
            $item.find("subItem").each(function() {
                $sitem = $(this);
                if ($item.attr("id") == id) {
                    var html = "<div  class='subitem' id='" + $sitem.attr("id") + "' categoria='" + $sitem.attr("categoria") + "'";
                    html += " default='" + $sitem.attr("default") + "'>";
                    html += $sitem.attr("name");
                    html += "</div>";

                    $('#Navegacion-SubSeccion').append($(html));
                }
            });
        });
        $('#Navegacion-SubSeccion div:first').css('color', 'black');
        
        var obj = document.getElementById(id);
        var posSubmenu = 0;
        if (obj.offsetLeft < 110) { posSubmenu = obj.offsetLeft; }
        else if (obj.offsetLeft == 110) { posSubmenu = 30; }
        else if (obj.offsetLeft == 220) { posSubmenu = 170; }
        else if (obj.offsetLeft == 440) { posSubmenu = 410; }
        else { posSubmenu = obj.offsetLeft + (obj.offsetLeft / 2); }
        //alert(obj.offsetLeft);

        $('#Navegacion-SubSeccion').css('padding-left', posSubmenu);
        
        $('#Navegacion-SubSeccion div').click(function() {
            $('#Navegacion-SubSeccion div').css('color', '#7B7B7B');
            $(this).css('color', 'black');
            var contenido = $(this).attr('id');
            //if (contenido != contenidoActivo) {
                $("#contenido").empty();
                loadContent(contenido);
                contenidoActivo = contenido;
            //}
        });
    });  
}

function loadContent(id) {
    $.get("xml/contenidos.xml", function(data) {
        $("#contenido").empty();
        $("#contenido").append("<div id='filtro'></div>");
        $(data).find("contenido[id|=" + id + "]").each(function() {
            var contenido = $(this);
            //if (id == $contenido.attr('id')) {
            var pagina = jQuery.trim(contenido.text());
            //$("#contenido").fadeOut('slow', function() {

            $("#contenido").load(pagina, function() {
                

            });
            //});
            //$("#mLeft").css("height", $("#marco").css("height"));
            //$("#mRight").css("height", $("#marco").css("height"));
            //}
        });
    });

    /*$("#contenido").fadeOut('slow');
    if ($.browser.msie) {
        pageload("#" + id);
    } else {
        document.location = "#" + id;
    }
    $("#contenido").fadeIn(2000);*/
}


function sortProcedure(a, b) {
    var value1 = a;
    var value2 = b;

    if (value1 > value2)
    { return 1; } else { return -1; }
}

function loadNavegador(cant) {

    var html = "<span id='fizda' style='margin-left:0px;padding:0px 5px 0px 5px;'>Anterior</span>";
    for (var i = 1; i <= cant; i++) {
        html += "<span pagina=" + i + " style='margin-left:0px;padding:0px 5px 0px 5px;'>" + i + "</span>";
    }
    html += "<span id='fdcha' style='margin-left:0px;padding:0px 5px 0px 5px;'>Siguiente</span>";
    $('#filtro').append($(html));


    $("#filtro span").click(function() {

        var cambiar = false;

        $('#filtro span').css('font-weight', 'normal');

        var p = $(this).attr("id");

        if ($(this).attr("id") == "fdcha") {
            if (parseInt(paginaActual) < parseInt(paginas)) {
                paginaActual++;
                cambiar = true;
            }
        }
        else if ($(this).attr("id") == "fizda") {
            if (parseInt(paginaActual) > 1) {
                paginaActual--;
                cambiar = true;
            }
        }
        else {
            if (paginaActual != parseInt($(this).attr('pagina'))) {
                paginaActual = parseInt($(this).attr('pagina'));
                cambiar = true;
            }
        }

        if (cambiar) {
            $('.hojaproyecto').hide();
            $('#pagina-' + paginaActual).show('slow');
        }
        $("#filtro span[pagina|=" + paginaActual + "]").css('font-weight', 'bold');

    });
	$("#filtro span[pagina|=" + paginaActual + "]").css('font-weight', 'bold');
    //$("#filtro span:eq(" + paginaActual + ")").css('font-weight', 'bold');
}





$(function() {

    $('#Navegacion-Seccion').addClass('Navegacion-Seccion');
    $('#Navegacion-SubSeccion').empty();
    $('#Pie').addClass('Pie');
    $('#mostrar').hide();
    $('#Diapositiva').attr("num", 1);

    function carruselOFF() {

        $("#Diapositiva").fadeOut("slow");
        $("#ocultar").hide();
        $("#mostrar").show();
        $("#botones").css("margin-top", "0px");
        $("#botones").css("filter", "alpha(opacity=80)");
        $("#botones").css("-moz-opacity", ".80");
        $("#botones").css("opacity", ".80");
        $("#botones").css("-ms-filter", "progid:DXImageTransform.Microsoft.Alpha(Opacity=80) !important");
        $("#botones").css("filter", "progid:DXImageTransform.Microsoft.Alpha(Opacity=80) !important");
        $("#Principal").css("min-height", "418px");
        $("#Principal").show();
        $("#Navegacion-SubSeccion").show();
        $("#Pie").css("margin-top", "0px");

        carruselActivo = false;
      
        if (contenidoActivo == "Proyectos") {
            loadProyectos();
        }
    }

    function carruselON() {

        $("#Diapositiva").fadeIn("slow");
        $("#ocultar").show();
        $("#mostrar").hide();
        $("#botones").css("margin-top", "-30px");
        $("#botones").css("filter", "alpha(opacity=50)");
        $("#botones").css("-moz-opacity", ".50");
        $("#botones").css("opacity", ".50");
        $("#botones").css("-ms-filter", "progid:DXImageTransform.Microsoft.Alpha(Opacity=50) !important");
        $("#botones").css("filter", "progid:DXImageTransform.Microsoft.Alpha(Opacity=50) !important");
        $("#Principal").css("min-height", "280px");

        carruselActivo = true;
        
        if (contenidoActivo == "Proyectos") {
            loadProyectos();
        }

    }

    $('#mostrar').click(function() {
        carruselON();
    });

    $('#ocultar').click(function() {
        carruselOFF();
    });



    setInterval(function() {
        var actual = $("#Diapositiva").attr("num");
        cambiarImagenCarruselAuto(actual);
    }, 5000);

    //-------MENU VERTICAL
    $.get("../recursosglobales/xml/menuv.xml", function(data) {
        sitioActivo = $("meta[name=sitio]").attr("content");
        $("#menu").empty();

        $(data).find("item").each(function() {
            var $item = $(this);
            var html = "<div  id='" + $item.attr("id") + "' >";
            html += "<a  href='" + $item.attr("enlace") + "'>";
            html += "<img id='" + $item.attr("name") + "' alt='" + $item.attr("name") + "'";
            html += "src='" + $item.attr("url") + "' src_in='" + $item.attr("url_act") + "'"
            html += "src_out='" + $item.attr("url") + "'/>";
            html += "</a></div>";

            $("#menu").append($(html));
        });

        $("#menu img").hover(function() {
            document.getElementById($(this).attr("id")).src = $(this).attr("src_in");
        }, function() {
            document.getElementById($(this).attr("id")).src = $(this).attr("src_out");
        });
    });

    $.get("xml/menu.xml", function(data) {
        $('#Navegacion-Seccion').empty();
        $(data).find("item").each(function() {
            var $item = $(this);
            var id = "'" + $item.attr("id") + "'";
            var html = "<div class='item' id='" + $item.attr("id") + "'";
            html += " style='background:url(" + $item.attr("boton") + ")'";
            html += " normal='url(" + $item.attr("boton") + ")'";
            html += " over='url(" + $item.attr("boton_ov") + ")'";
            html += " press='url(" + $item.attr("boton_pr") + ")'";
            html += "onclick=mostrarSubMenu(" + id + ")>";
            html += "</div>";

            $('#Navegacion-Seccion').append($(html));
        });

        $('#Navegacion-Seccion div').click(function() {
            $('#Navegacion-Seccion div').each(function() {
                $(this).css('background', $(this).attr('normal'));
            });
            $(this).css('background', $(this).attr('press'));
            seleccion = $(this).attr('id');
            if (isFirstTime == true) {
                carruselOFF();
                isFirstTime = false;
            }
        });

        $('#Navegacion-Seccion div').hover(function() {
            $(this).css('background', $(this).attr('over'));
        }, function() {
            if (seleccion != $(this).attr('id')) {
                $(this).css('background', $(this).attr('normal'));
            }
            else {
                $(this).css('background', $(this).attr('press'));
            }
        });
    });



    //-------------FIN MENU HORIZONTAL

    //-------------Click sobre Menu Horizontal
    /*$('#Navegacion-Seccion div').click(function() {
    $('#Navegacion-Seccion div').css("background", $(this).attr("normal"));
    $(this).css("background", $(this).attr("press"));
    $("#Navegacion-SubSeccion").empty();

        var id = $(this).attr('id');
    
    seleccion = $(this).attr('id');

        
    var contenido = $(this).attr('id');

        if (contenido != contenidoActivo) {
    $("#contenido").empty();
    loadContent(contenido);
    contenidoActivo = contenido;
    }


       

        $.get('xml/menu.xml', function(data) {
    $(data).find("item").each(function() {
    var $item = $(this);
    $item.find("subItem").each(function() {
    $sitem = $(this);
    if ($item.attr("id") == id) {
    var html = "<div  class='subitem' id='" + $sitem.attr("id") + "' categoria='" + $sitem.attr("categoria") + "'";
    html += " default='" + $sitem.attr("default") + "'>";
    html += $sitem.attr("name");
    html += "</div>";

                        $('#Navegacion-SubSeccion').append($(html));
    }
    });
    });
    
    });
    });*/

    /*    $('#Navegacion-Seccion div').css("background", $(this).attr("normal"));
    $(this).css("background", $(this).attr("press"));
    $("#Navegacion-SubSeccion").empty();
    
    
    var id = $(this).attr('id');
    var obj = document.getElementById(id);
    seleccion = $(this).attr('id');

        if (isFirstTime == true) {
    carruselOFF();
    isFirstTime = false;
    }
    var contenido = $(this).attr('id');

        if (contenido != contenidoActivo) {
    $("#contenido").empty();
    loadContent(contenido);
    contenidoActivo = contenido;
    }

        var posSubmenu = 0;
    if (obj.offsetLeft < 110) { posSubmenu = obj.offsetLeft; }
    else if (obj.offsetLeft == 110) { posSubmenu = 30; }
    else if (obj.offsetLeft == 220) { posSubmenu = 170; }
    else { posSubmenu = obj.offsetLeft + (obj.offsetLeft / 2); }

        $("#Navegacion-SubSeccion").css("padding-left", posSubmenu);

        $.get("xml/menu.xml", function(data) {
    $(data).find("item").each(function() {
    var $item = $(this);
    $item.find("subItem").each(function() {
    $sitem = $(this);
    if ($item.attr("id") == id) {
    var html = "<div  class='subitem' id='" + $sitem.attr("id") + "' categoria='" + $sitem.attr("categoria") + "'";
    html += " default='" + $sitem.attr("default") + "'>";
    html += $sitem.attr("name");
    html += "</div>";

                        $("#Navegacion-SubSeccion").append($(html));
    }
    });
    });
    $('#Navegacion-SubSeccion div:first').css('color', 'black');
    });*/

    loadCarrusel();


    function loadCarrusel() {
        $.get("xml/carrusel.xml", function(data) {
            var elementos = $(data).find("imagen").length;
            var id = Math.round(Math.random() * elementos);

            if (id == 0) { id++; }
            $(data).find("imagen").each(function() {
                var $imagen = $(this);
                if ($imagen.attr("id") == id) {
                    $("#Diapositiva").css("background-image", "url(" + $imagen.attr("url") + ")");
                }
            });
            var html = "<div id='btnNavegacion'><img id='fizda' rel='nav' style='margin-top:10px;margin-left:5px;' alt='flecha Izda' src='../grupoproci/images/diapositivas/flecha_izqda.gif'/>";
            for (i = 1; i <= elementos; i++) {
                if (id == i) {
                    html += "<img id='" + i + "' rel='nav' style='margin-top:10px;margin-left:5px;' alt='elemento' src='../grupoproci/images/diapositivas/diapos_activo.gif'/>";
                    idbtnActivo = id;
                }
                else {
                    html += "<img id='" + i + "' rel='nav' style='margin-top:10px;margin-left:5px;' alt='elemento' src='../grupoproci/images/diapositivas/diapos_inactivo.gif'/>";
                }
            }
            html += "<img id='fdcha' rel='nav' style='margin-top:10px;margin-left:5px;' alt='flecha Dcha' src='../grupoproci/images/diapositivas/flecha_dcha.gif'/></div>";

            $("#botones").append($(html));
            $("img[rel='nav']").hover(function() {
                if ($(this).attr("id") != "fdcha" && $(this).attr("id") != "fizda") {
                    var img = document.getElementById($(this).attr("id"));
                    img.src = "../grupoproci/images/diapositivas/diapos_over.gif";
                }
            }, function() {
                if ($(this).attr("id") != "fdcha" && $(this).attr("id") != "fizda") {
                    var img;
                    img = document.getElementById($(this).attr("id"));
                    if (idbtnActivo == $(this).attr("id")) {
                        img.src = "../grupoproci/images/diapositivas/diapos_activo.gif";
                    }
                    else {
                        img.src = "../grupoproci/images/diapositivas/diapos_inactivo.gif";
                    }
                }
            });

            $("img[rel='nav']").click(function() {
                var img = new Image();
                var idBtn = $(this).attr("id");
                if (idBtn >= 1 && id <= elementos) {
                    $("img[rel='nav']").each(function() {
                        img = document.getElementById($(this).attr("id"));
                        if ($(this).attr("id") != "fdcha" && $(this).attr("id") != "fizda") {
                            img.src = "../grupoproci/images/diapositivas/diapos_inactivo.gif";
                        }
                    });
                    idbtnActivo = idBtn;
                    img = document.getElementById(idBtn);
                    img.src = "../grupoproci/images/diapositivas/diapos_activo.gif";
                    cambiarImagenCarruselAuto(idbtnActivo);
                }
                if (idBtn == "fdcha") {
                    if (idbtnActivo >= 1 && id < elementos) {
                        idbtnActivo++;
                        cambiarImagenCarrusel(idbtnActivo);
                    }
                }
                else if (idBtn == "fizda") {
                    if (idbtnActivo > 1 && id <= elementos) {
                        idbtnActivo--;
                        cambiarImagenCarrusel(idbtnActivo);
                    }
                }
                $("img[rel='nav']").each(function() {
                    img = document.getElementById($(this).attr("id"));
                    if ($(this).attr("id") != "fdcha" && $(this).attr("id") != "fizda") {
                        img.src = "../grupoproci/images/diapositivas/diapos_inactivo.gif";
                    }
                });
                img = document.getElementById(idbtnActivo);
                img.src = "../grupoproci/images/diapositivas/diapos_activo.gif";
            });
        });
    }



    function cambiarImagenCarrusel(id) {


        $.get("xml/carrusel.xml", function(data) {

            var elementos = $(data).find("imagen").length;

            $(data).find("imagen").each(function() {

                var $imagen = $(this);

                if ($imagen.attr("id") == id) {

                    $("#Diapositiva").css("background-image", "url(" + $imagen.attr("url") + ")");

                }

            });

        });

    }



    function cambiarImagenCarruselAuto(actual) {

        var mostrar = actual;
        $.get("xml/carrusel.xml", function(data) {
            var elementos = $(data).find("imagen").length;
            if (actual == elementos) {
                mostrar = 1;
            } else {
                mostrar++;
            }

            $(data).find("imagen").each(function() {
                var $imagen = $(this);
                if ($imagen.attr("id") == mostrar) {
                    $("#Diapositiva").css("background-image", "url(" + $imagen.attr("url") + ")");
                    $("#Diapositiva").attr("num", mostrar);
                }
            });

        });

        $("img[rel='nav']").each(function() {
            img = document.getElementById($(this).attr("id"));
            if ($(this).attr("id") != "fdcha" && $(this).attr("id") != "fizda") {
                img.src = "../grupoproci/images/diapositivas/diapos_inactivo.gif";
            }
        });
        img = document.getElementById(actual);
        img.src = "../grupoproci/images/diapositivas/diapos_activo.gif";
    }
});