$(function() {
    
    $(".numeric").livequery(function(){
        $(this).numeric();
    });

    $("input").livequery(function(){
        $(this).attr("autocomplete","off");
    })
    
    $(".accordion").livequery(function(){
        $(this).accordion(

        {
                clearStyle: true,
                navigation: true,
                collapsible: true,
                active: false
            })
        return true;
    });

    $('.accordion2 > .titulo').click(function() {
        $(this).next().toggle('blind');
        $(this).toggleClass('unfold');
        return false;
    }).next().hide();

    $('.tabs').livequery(function(){
        $(this).tabs({
            ajaxOptions: {
                beforeSend: placeLoader
            }
        });
    });

    $('.uibutton').livequery(function(){
        $(this).button();
    });

    $('.datepicker').livequery(function(){
        $(this).datepicker({
            option : $.datepicker.regional['es'],
            dateFormat: 'yy-mm-dd',
            showAnim: 'drop'
        })
    });

    $('.tipsyN').tipsy({
        title: 'alt',
        live:   true,
        gravity: 'n'
    });

    $('.tipsyW').tipsy({
        title: 'alt',
        live:   true,
        gravity: 'w'
    });

    $('.tipsy').tipsy({
        title: 'alt',
        live:   true
    });


    $(".fancybox").livequery(function(){
        $(this).fancybox({
            'transitionIn'		: 'fade',
            'autoDimensions'        : true,
            'transitionOut'		: 'fade',
            'autoscale'             : true,
            'overlayOpacity'        : 0.7,
            'titleShow'             : false
        })
    });
        
});

/**
 *Funcion que redondea un numero para tener dec decimales
 **/
function roundNumber(num, dec) {
    var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
    return result;
}


/*Funcion equivalente a setflash de cake
 *message: mensaje a mostrar
 *error: parametro opcional, define si es un error, true o false
 *id: el string del selector sobre el cual se va a hacer el flash
 **/
function setFlash(message){
    var error = (arguments[1] ? arguments[1] : false);
    var id = (arguments[2] ? arguments[2] : "#flashSession");
    var flash = $(id);
    flash.html(message).show("blind");
    if(error){
        flash.addClass("flashError");
        flash.removeClass("flashNotice");
    } else {
        flash.removeClass("flashError");
        flash.addClass("flashNotice");
    }
    flash.show(200,function(){
        $.fancybox.resize();
    });
    
    if(!error){
        setTimeout(function(){
            flash.hide("blind",200)
        }, 5000);
    } else {
        flash.click(function(){
            flash.queue("fx", []).hide("blind",200)
        });
    }
}

$(document).ajaxStart(function(){
    $('#cargandoz').fadeIn();
});
$(document).ajaxStop(function(){
    $.fancybox.resize();
    $('#cargandoz').fadeOut();
});

/*funcion vacía para asignar luego el gif de loading a las pestañas*/
function placeLoader(trigger){
}

/*funcion que clona un objeto*/
function cloneObj(obj){
    if(obj == null || typeof(obj) != 'object')
        return obj;
    var temp = new obj.constructor();
    for(var key in obj)
        temp[key] = cloneObj(obj[key]);
    return temp;
}

/*transforma las propiedades que pueden ser floats de un objeto a float*/
function floatJson(obj){
    if(!isNaN(parseFloat(obj)) && typeof(obj) == 'string'){
        return parseFloat(obj);
    } else if(typeof(obj) == 'string') {
        return obj;
    }
    if(obj == null) return null;
    if (typeof(obj) == 'array' || typeof(obj) == 'object'){
        var temp = obj.constructor(); // changed (twice)
        for(var key in obj){
            temp[key] = floatJson(obj[key]);
        }
        return temp;
    }
    return obj;
}

/**
* Function : dump()
* Arguments: The data - array,hash(associative array),object
*    The level - OPTIONAL
* Returns  : The textual representation of the array.
* This function was inspired by the print_r function of PHP.
* This will accept some data as the argument and return a
* text that will be a more readable version of the
* array/hash/object that is given.
*/
function dump(arr,popup,level) {
    var breaker;
    if(popup) {
        breaker = "\n";
    } else {
        breaker = "<br/>";
    }
    var dumped_text = "";
    if(!level) level = 0;

    //The padding given at the beginning of the line.
    var level_padding = "";
    for(var j=0;j<level+1;j++) level_padding += "    ";

    if(typeof(arr) == 'object') { //Array/Hashes/Objects
        for(var item in arr) {
            var value = arr[item];

            if(typeof(value) == 'object') { //If it is an array,
                dumped_text += level_padding + "'" + item + "' ..."+breaker;
                dumped_text += dump(value,popup,level+1);
            } else {
                dumped_text += level_padding + "'" + item + "' => '" + value +"'" +breaker;
            }
        }
    } else { //Stings/Chars/Numbers etc.
        dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
    }
    return dumped_text;
} 
