/**
 * SCRIPT PADRAO INICIALIZADO.
 * AS FUNCOES DEVERAO SER CHAMADAS NO SCRIPT LAYOUT 
 */

jQuery(function(){
    // MASCARAS DE CAMPOS
    jQuery.mask.options.autoTab = false;                
    jQuery('input:text').setMask();       
    jQuery('[alt=time]').change(function(){ // valida hora mask
        var str = $(this).val();
        var h = str.substr(0, 2);
        var m = str.substr(3, 2)
        var s = str.substr(6, 2)
        if (parseInt(h) > 24) {
            $(this).val('');
        }
        if (parseInt(m) > 60) {
            $(this).val('');
        }
        if (parseInt(s) > 60) {
            $(this).val('');
        }
        return true;
    });
                
    // TRATA MULTIPLOS SUBMITS
    jQuery(':submit').click(function(){
        $(':submit').attr('disabled','disabled'); 
        var name = $(this).attr('name');
        if ( name != '' &&  name != undefined) {        
            var input = '<input type="hidden" name="' + $(this).attr('name') + '" value="'+ $(this).attr('value') +'" />';
            $(this).parents('form').append(input);
        }
        $(this).parents('form').submit();
    }); 
    
    // CORRIJE LABELFORM NO SUBMIT
    jQuery('form').submit(function(){
        jQuery(this).find('label input').each(function(idx) {
            var cp = $(this);
            var tx = $(cp).parents('label:first');
            if ($(cp).val()== $(tx).attr('title')) {
                $(cp).val('');
            }
        });
        return true;            
    });

    // LABLE SUGEST
    jQuery('label').each(function() {
        var tx = this.title;
        jQuery(this).find('input[value=""]')
        .val(tx)
        .css('color', '#999')
        .focus(function() {
            if (jQuery(this).val() == tx) {
                jQuery(this).css('color', '').val('');
            }
        })
        .blur(function() {
            if (jQuery(this).val() == '') {
                jQuery(this).val(tx).css('color', '#999');
            }
        });
    });//label.each


    // SET ROWOVER TABLE GRID
    jQuery('table tbody.ui-widget-content tr').hover(
        function() {
            jQuery(this).addClass('rowHover');
        },
        function() {
            jQuery(this).removeClass('rowHover');
        });


    // BUTTON STYLE
    jQuery('.button').button();

});



//FUNCOES=======================================================================
/**
 * DESABILITA ESTRUTRA FORMULARIO PADRAO.
 */
function setDisabledForm(aDomainPropertyName) {
    var propname = (aDomainPropertyName != null ? '.ref-' + aDomainPropertyName : '')
    jQuery('div' + propname + ' > input').attr('disabled','disabled').css("background-color", "#fff");
    jQuery('div' + propname + ' > select').attr('disabled', 'disabled').css("background-color", "#fff");
    jQuery('div' + propname + ' > textarea').attr('disabled', 'disabled').css("background-color", "#fff");

    // auto complete
    jQuery('div' + propname + ' div > input').attr('disabled', 'disabled').css("background-color", "#fff");
    jQuery('div' + propname + ' div span> input').attr('disabled', 'disabled').css("background-color", "#fff");
}

/**
 * AJUSTA O CONTAINER MIOLO DO LAYOUT EM 100%
 */
function ajusta100percent(aContainerMiolo, aTopoHeight){ //default (#container-miolo, 230)
    if (aContainerMiolo==null || aContainerMiolo==undefined) {
        aContainerMiolo= '#container-miolo';
    }    
    if (aTopoHeight==null || aTopoHeight==undefined) {
        aTopoHeight= 230;
    }
    
    var h = $(document).height() - aTopoHeight;
                
    if ( $(aContainerMiolo).height() < h) {
        $(aContainerMiolo).height(h);
    }                                
}




