/* Faz o hover/out de imagens
 * Precisa botar em um atributo 'toggle' a nova imagem
 * @author Mateus Haas
 * @return void
 */
$.fn.hover2Change = function() {
    $(this).hover(function() {
        //setando variaveis
        var o   = $(this).attr('src');
        var n   = $(this).attr('toggle');
        var img = $(this);

        //do
        img.attr('src',n);
        img.attr('toggle',o);

    });
}

/* Método usado para ler imagens antes de usálas (por exemplo, em menus que quando o mouse fica em cima, ele bota uma outra imagem)
 * @author Mateus Haas
 * @return void
 * @param array
 */
function preLoad(array) {
    //variaveis
    $('<div id="preload"></div>').appendTo('body');
    var count   = array.length;
    var div     = $('#preload');

    //esconde a div
    $(div).hide();

    // loop do array
    for (i = 0; i < count; i++) {
        $(div).append('<img src="'+ array[i] +'" />');
    }

    //remove div, ou seja, soh eh usada pra ler as imagens antes
    $(div).remove();
}

/* Método usado para validar campos
 * @author Mateus Haas
 * @return void
 * @param array
 */
$.fn.validate = function(ref) {
    var $aqui = true;
    var inputs = $(ref,this);
    inputs.each(function() {
        if ($(this).is(':text') || $(this).is(':password')) {
            if ($.trim($(this).val()) == '') {
                $aqui = false;
                alert('Campo Obrigatório !');
                $(this).focus();
                return false;
            }
        }
        else if ($(this).is('select')) {
            if ($(this).val() == '0') {
                $aqui = false;
                alert('Campo Obrigatório !');
                $(this).focus();
                return false;
            }
        }
    });
    if ($aqui == false) {
        return false;
    }
    else {
        return true;
    }
}

/*
 * on focus, remove the text
 */
$.fn.focusRemove = function() {
    //on focus
    $(this).unbind('focus').bind('focus',function() {
        if ($(this).is('input') || $(this).is('textarea')) {
            if ($(this).val() == this.defaultValue) {
                $(this).val('');
            }
        }
    });

    //on blur
    $(this).unbind('blur').bind('blur',function() {
        if ($(this).is('input') || $(this).is('textarea')) {
            if ($(this).val() == '') {
                $(this).val(this.defaultValue);
            }
        }
    });
}
