$(document).ready(function() {
    var idD = new Array();
    var titulo = new Array();
    var texto = new Array();
    var imagen = new Array();
    var fecha = new Array();
    var mes = new Array();
    var dia = new Array();
    var hora = new Array();
    var am_pm = new Array();
    var colTitulo = new Array();
    var colTexto = new Array();
    var countXML = 0;
    var j = 0;
    var next = false;
    var prev = false;
    $('#slider-nav img').hover(function(){
        this.src = this.src.replace('.png','-hover.png');
    },function(){
        this.src = this.src.replace('-hover','');
    });
    $("#slider-destacados").mouseover(function() {
        $("#slider-nav").css("display","block");
    });
    $("#slider-destacados").mouseout(function() {
        $("#slider-nav").css("display","none");
    });
    $.ajax({
        type: "GET",
        url: "destacados.xml",
	contentType: "application/x-www-form-urlencoded; charset=utf-8",
        dataType: "xml",
        success: function(xml) {
            $(xml).find('destacado').each(function(){
                var id = 0;
                id = $(this).attr('id');
                var xidD = $(this).find('idD').text();
                var xTitulo = $(this).find('titulo').text();
                var xTexto = $(this).find('texto').text();
                var xImagen = $(this).find('imagen').text();
                var xFecha = $(this).find('fecha').text();
                var xMes = $(this).find('mes').text();
                var xDia = $(this).find('dia').text();
                var xHora = $(this).find('hora').text();
                var xAm_pm = $(this).find('am_pm').text();
                var xcolTitulo = $(this).find('colTitulo').text();
                var xcolTexto = $(this).find('colTexto').text();
                idD[id]=xidD;
                titulo[id]=xTitulo;
                texto[id]=xTexto;
                imagen[id]=xImagen;
                fecha[id]=xFecha;
                dia[id]=xDia;
                mes[id]=xMes;
                hora[id]=xHora;
                am_pm[id]=xAm_pm;
                colTitulo[id]=xcolTitulo
                colTexto[id]=xcolTexto;
                countXML++;
            });
            $("#item-destacado").html("<div style='display: block; vertical-align: middle; width:32px; height:32px;'><img style='display: block;margin-left: auto;margin-right: auto' src='imagenes/ajax-loader.gif'/></div>");
            $("#item-destacado").html("<img id='img-destacado' style='display: block; margin-left: auto;margin-right: auto' src='"+imagen[0]+"' width='677' height='300'/>");
            $("#titulo").html(titulo[j]);
            $("#titulo").css("color",colTitulo[j]);
            $("#texto").html(texto[j]);
            $("#texto").css("color",colTexto[j]);
            $("#fecha").html(fecha[j]);
            $("#dia").html(dia[j]);
            $("#mes").html(mes[j]);
            $("#hora").html(hora[j]);
            $("#am_pm").html(am_pm[j]);
            $("#trans-titulo").css("position","relative");
            $("#trans-titulo").css("left","0px");
            $("#trans-texto").css("position","relative");
            $("#trans-texto").css("left","500px");
            /*margin_height=$("#trans-titulo").css("height");
			$("#trans-texto").css("top",margin_height);*/
            animar();
            $(".start").click();
        //desanimar();
        //flashealo();
        }
    });
    var active = false;
    $('#slider-nav').find('.start').css("cursor", "pointer").click(function() {
        if (!active) {
            active = !active;
            $("#trans-titulo").css("left","0px");
            $("#trans-texto").css("left","500px");
            $("#item-destacado").everyTime(8000, 'controlled', function() {
                transicion();
            });
        }
    }).end().find('.stop').css("cursor", "pointer").click(function() {
        if (active) {
            active = !active;
            $("#trans-titulo").stop(true,true);
            $("#trans-texto").stop(true,true);
            $("#item-destacado").html("<img id='img-destacado' style='display: block; margin-left: auto;margin-right: auto' src='"+imagen[j]+"' width='677' height='300'/>");
            $(this).parents("div").find('div').stopTime('controlled');
        }
    });

    $("#prev-btn").click(function(){
        accion_nav('prev');
        return false;
    });

    $("#next-btn").click(function(){
        accion_nav('next');
        return false;
    });

    function accion_nav(accion){
        $("#trans-titulo").css("left","325px");
        $("#trans-texto").css("left","325px");
        transicion();
        if(accion=='next'){
            next=true;
            prev=false;
        } else {
            prev=true;
            next=false;
        }
        return false;
    }

    function animar(){
        $("#trans-titulo").animate({
            opacity:0
        },1);
        $("#trans-texto").animate({
            opacity:0
        },1);
        $("#trans-texto").css("z-index","1");
        $("#img-destacado").fadeIn("slow").fadeTo(6200, 1).fadeOut("slow");
        $("#trans-titulo").animate({
            "left": "+=325px",
            opacity: 1
        },1000).fadeTo(4500, 1).animate({
            "left": "-=325px",
            opacity: 0
        },1000);
        $("#trans-texto").animate({
            "left": "-=175px",
            opacity: 1
        },1000).fadeTo(4500, 1).animate({
            "left": "+=175px",
            opacity: 0
        },1000);
        return false;
    }

    function transicion(valor){
        j=valorJ();
        //desanimar();
        $("#item-destacado").html("<img id='img-destacado' src='"+imagen[j]+"' width='677' height='300'/>");
        $("#titulo").html(titulo[j]);
        $("#titulo").css("color",colTitulo[j]);
        $("#texto").html(texto[j]);
        $("#texto").css("color",colTexto[j]);
        $("#fecha").html(fecha[j]);
        $("#dia").html(dia[j]);
        $("#mes").html(mes[j]);
        $("#hora").html(hora[j]);
        $("#am_pm").html(am_pm[j]);
        animar();
        $(".start").click();
        //desanimar();
        return false;
    }

    function valorJ(){
        if(prev==false && next==false && j==0){
            j=1;
            return j;
        }

        if(prev==false && next==false && j>=(countXML-1)){
            j=0;
            return j;
        }
        if(prev==false && next==false && j>=0 && j < (countXML-1)){
            j++;
            return j;
        }

        if(j==0 && prev==true){
            j = countXML-1;
            return j;
        }

        if(j>=0 && j < (countXML-1) && next==true){
            j++;
            return j;
        }

        if(j>=0 && j < (countXML-1) && prev==true){
            j--;
            return j;
        }

        if(j>=(countXML-1) && next==true){
            j=0;
            return j;
        }

        if(j>0 && next==true && j < (countXML-1)){
            j++;
            return j;
        }

        if(j>0 && prev==true && j <= (countXML-1)){
            j--;
            return j;
        }

        return false;
    }
});

