$(document).ready(function(){
    $.ajax({
        url: "script/indicadores_economicos.php",
        async:true,
        beforeSend: function(){
            $("#cont_uf").html("<img src='img/index/load_index.gif'/>");
            $("#cont_dolar").html("<img src='img/index/load_index.gif'/>");
            $("#cont_ipc").html("<img src='img/index/load_index.gif'/>");
        },
        success: function(dato){
            var resp = new Array();
            resp = dato.split("-");
            $("#cont_uf").html(resp[1]);
            $("#cont_dolar").html(resp[3]);
            $("#cont_ipc").html(resp[0]);
        }
    });
    $("#editar_index").click(function(){
        rand = Math.random();
        $("#cuerpo").load("script/html_edit_index.php?_="+ rand +"",function(){
            var button = $('#img1'), interval;
            new AjaxUpload('#img1', {
                action: 'script/ajax/imagen.php?dato=index1_',
                onSubmit : function(file , ext){
                if (! (ext && /^(jpg|png|jpeg)$/.test(ext))){
                    // extensiones permitidas
                    alert('Error: No se permiten ese tipo de archivos');
                   // cancela upload
                    return false;
                } else {
                   //button.text('Subiendo...');
                    this.disable();
                }
            },
            onComplete: function(file, response){
                //button.text('Subido');
                // enable upload button
                this.enable();
                img1 = 1;
                // Agrega archivo a la lista
                $("#cont_peq1").empty();
                $("#cont_peq1").css("background","transparent url(script/timthumb.php?src="+ response +"&h=109&w=293&zc=1) center center no-repeat");
            }
            });
            var button = $('#img2'), interval;
            new AjaxUpload('#img2', {
                action: 'script/ajax/imagen.php?dato=index2_',
                onSubmit : function(file , ext){
                if (! (ext && /^(jpg|png|jpeg)$/.test(ext))){
                    // extensiones permitidas
                    alert('Error: No se permiten ese tipo de archivos');
                   // cancela upload
                    return false;
                } else {
                   //button.text('Subiendo...');
                    this.disable();
                }
            },
            onComplete: function(file, response){
                //button.text('Subido');
                // enable upload button
                this.enable();
                img1 = 1;
                // Agrega archivo a la lista
                $("#cont_peq2").empty();
                $("#cont_peq2").css("background","transparent url(script/timthumb.php?src="+ response +"&h=109&w=293&zc=1) center center no-repeat");
            }
            });
            var button = $('#img3'), interval;
            new AjaxUpload('#img3', {
                action: 'script/ajax/imagen.php?dato=index3_',
                onSubmit : function(file , ext){
                if (! (ext && /^(jpg|png|jpeg)$/.test(ext))){
                    // extensiones permitidas
                    alert('Error: No se permiten ese tipo de archivos');
                   // cancela upload
                    return false;
                } else {
                   //button.text('Subiendo...');
                    this.disable();
                }
            },
            onComplete: function(file, response){
                //button.text('Subido');
                // enable upload button
                this.enable();
                img1 = 1;
                // Agrega archivo a la lista
                $("#cont_peq3").empty();
                $("#cont_peq3").css("background","transparent url(script/timthumb.php?src="+ response +"&h=109&w=293&zc=1) center center no-repeat");
            }
            });
            
            $("input,textarea").click(function(){
                if($(this).val() == "T\u00EDtulo" || $(this).val() == "Descripci\u00F3n" || $(this).val() == "Sucursal" ){
                   $(this).val("");
                }
            });
        });
        return false;
    });
    
    $("#editar_index_edit").live("click",function(){
        var listo = 0;
        $("#cp_izq_editar *,#cp_cen_editar *,#cp_der_editar *").each(function(){
            if($(this).val() == "" || $(this).val() == "T\u00EDtulo" || $(this).val() == "Descripci\u00F3n" || $(this).val() == "Sucursal"){
               $(this).addClass("borde-rojo");
               //listo = 0;
            }
            else{
               $(this).removeClass("borde-rojo");
               listo++;
            }
        });
        bimg1 = $("#cont_peq1").css("background-image");
        bimg2 = $("#cont_peq2").css("background-image");
        bimg3 = $("#cont_peq3").css("background-image");
        if(bimg1 == "none"){
            $("#cont_peq1").parent().css("border","1px solid #FF0000");
            //listo = 0;
        }
        else{
            $("#cont_peq1").parent().css("border","1px solid #CDCEC9");
            listo++;
        }
        if(bimg2 == "none"){
            $("#cont_peq2").parent().css("border","1px solid #FF0000");
            //listo = 0;
        }
        else{
            $("#cont_peq2").parent().css("border","1px solid #CDCEC9");
            listo++;
        }
        if(bimg3 == "none"){
            $("#cont_peq3").parent().css("border","1px solid #FF0000");
            //listo = 0;
        }
        else{
            $("#cont_peq3").parent().css("border","1px solid #CDCEC9");
            listo++;
        }
        
        if(listo == 10){
            var titulo1 = $.trim($("#cp_izq_editar .caja_titulo_editar input").val());
            var titulo2 = $.trim($("#cp_cen_editar .caja_titulo_editar input").eq(0).val());
            var titulo3 = $.trim($("#cp_der_editar .caja_titulo_editar input").val());
            var sucursal = $.trim($("#cp_cen_editar .caja_titulo_editar input").eq(1).val());
            var txt1 = $.trim($("#cp_izq_editar .caja_texto_editar textarea").val());
            var txt2 = $.trim($("#cp_cen_editar .caja_texto_editar textarea").val());
            var txt3 = $.trim($("#cp_der_editar .caja_texto_editar textarea").val());
            $.ajax({
                url: "script/add_index.php",
                async:true,
                cache:false,
                type: "POST",
                data: ({tit1:titulo1,tit2:titulo2,tit3:titulo3,suc:sucursal,txt1:txt1,txt2:txt2,txt3:txt3}),
                beforeSend: function(){
                    
                },
                success: function(dato){
                    alert("Guardado");
                    location.reload();
                }
            });
        }
    });
    
    
    /********** ROTAR IMAGENES INDEX ********/

    img1 = new Image();
    img1.src = "img/index/impresa.png";
    img2 = new Image();
    img2.src = "img/index/caravan.png";
    img3 = new Image();
    img3.src = "img/index/mercedes.png";
    img4 = new Image();
    img4.src = "img/index/Hilux.png";
    img5 = new Image();
    img5.src = "img/auto.png";
    
    var miArray = new Array(4)
    
    miArray[1] = img1.src;
    miArray[2] = img2.src;
    miArray[3] = img3.src;
    miArray[4] = img4.src;
    miArray[5] = img5.src;
    
    var al = Math.ceil(Math.random()*5);
    resp = miArray[al];
    var i = 0;
    $("#auto_index").css("background","transparent url("+ resp +") center bottom no-repeat");
    
    setInterval(function() {
        al++;
        if(al >= 6) al = 1;
        resp = miArray[al];
        $("#auto_index").fadeOut(800,function(){
           $("#auto_index").fadeIn("slow").css("background","transparent url("+ resp +") center bottom no-repeat"); 
        });
        }, 5000);
    
   jQuery.fn.cambioImagen = function() {
        al++;
        if(al >= 6) al = 1;
        resp = miArray[al];
        $("#auto_index").fadeOut("slow");
        $("#auto_index").fadeIn("slow").css("background","transparent url("+ resp +") center center no-repeat");
    };
});
