s_categorias = new Array( 3 );
categorias   = null;
left         = null;


function $id( id ) {
    return document.getElementById( id );
}

function contact( form, url ) {
    $id( 'loader' ).style.display = 'block';

    new Ajax.Request( url, {
        method: 'post',
        onFailure: function() {
            alert('Houve um erro na tentativa de enviar seu formulário.');
            $id( 'loader' ).style.display = 'none';
        },
        onSuccess: function( cnt ) {
           if ( cnt.responseText.toString().indexOf("false") > -1 ) alert('Erro na tentativa de enviar seu formulário.');
           else                                                     alert('Sua mensagem foi enviada com sucesso. Assim que possível, entraremos em contato.');
           
           $id( 'contact_form' ).reset();
           $id( 'loader' ).style.display = 'none';
        },
        parameters: form
    });
}

function hide_menuProdutos() {
    $id('menu_produtos').style.display        = 'none';
    $id('btn_produtos').style.backgroundColor = '#005244';
}

function marquee_scam( elem, velo ) {
    elem.scrollAmount = velo;
}

function move( side ) {
    var spee = 120;
    var left = $id('panel').scrollLeft;
    var fina = left-spee;
    
    if ( fina < 0 ) fina = 0;
    
    if ( side == 'L' ) {
        if ( left > 0 ) $jq( '#panel' ).scrollTo( fina, 125 );
    } else if ( side == 'R' ) {
        if ( left >= 0 ) $jq( '#panel' ).scrollTo( left+spee, 125 );
    }
}

function produtos_load( act, div, categoria, produto ) {
    $( div ).innerHTML = '<div id="loader" style="margin-top: 35px; text-align: center;"><img src="image/ajax_loader.gif" /></div>';
    
    new Ajax.Request( 'include/produtos.php', {
        method:     'get',
        onComplete: function( t ) {
            $( div ).innerHTML = t.responseText;
            
            if ( act != 'BREA' && categoria != '' ) {
                $('esquerda_col').setStyle({ display: 'block' });
                $('direita').setStyle({ display: 'block' });
            }
        },
        parameters: { action:    act
                    , categoria: categoria
                    , id:        produto }
    } );
}

function produtos_load_() {
    produtos_load( 'BREA', '_bread', '', '' );
    produtos_load( 'CATE', '_categorias', '', '' );
    produtos_load( 'PROD', '_produtos', '', '' );
    
    $('categoria').value = '';
    $('esquerda_col').setStyle({ display: 'none' });
    $('direita').setStyle({ display: 'none' });
}

function produtos_load_c( categoria ) {
    produtos_load( 'BREA', '_bread', categoria, '' );
    produtos_load( 'CATE', '_categorias', categoria, '' );
    produtos_load( 'PROD', '_produtos', '', '' );
}

function produtos_load_p( categoria, produto, elm ) {
    produtos_load( 'BREA', '_bread', categoria, produto );
    produtos_load( 'PROD', '_produtos', categoria, produto );
    
    var element  = $( elm ).ancestors()[ 0 ];
    var brothers = element.siblings();
    
    for ( var x = 0; x < brothers.length; x++ ) {
        if ( $( brothers[x] ).hasClassName( 'selected' ) ) {
            $( brothers[x] ).removeClassName( 'selected' );
        }
    }
    
    $( element ).addClassName( 'selected' );
}

function redirect( url ) {
    location = url;
}

function representantes() {
    $('loader').show();

    var error = false;
    var mands = ['estado'];
        
    for ( var x = 0; x < mands.length && !error; x++ ) {
        if ( $id( mands[x] ).value.length < 1 ) {
            alert('O campo "'+mands[x]+'" é obrigatório.'+$id( mands[x] ).value);
            $id( mands[x] ).focus();
            
            error = true;
            break;
        }
    }
    
    if ( !error ) {
        new Ajax.Request( 'include/representantes.php', {
            method: 'post',
            onComplete: function( t ) { 
                $id( 'representante' ).innerHTML = t.responseText;
                $( 'cidade' ).focus();
                $( 'loader' ).hide();
            },
            parameters: { estado: $id( mands[0] ).value
                        , cidade: $id( 'cidade' ).value }
        });
    } else {
        $('loader').hide();
    }
}

function rollDown( posX, posY ) {
    if ( categorias[ posX ][ posY ] != s_categorias[ posX ] ) {
        $id( 'categoria'+s_categorias[ posX ] ).style.backgroundColor       = '#ECF2F2';
        $id( 'categoria'+categorias[ posX ][ posY ] ).style.backgroundColor = '#FFFFFF';
        
        animatedcollapse.hide( 'image'+s_categorias[ posX ] );
        animatedcollapse.show( 'image'+categorias[ posX ][ posY ] );
        
        s_categorias[ posX ] = categorias[ posX ][ posY ];
    }
}

function show_menuProdutos() {
    $id('menu_produtos').style.display        = 'block';
    $id('btn_produtos').style.backgroundColor = '#007562';
}

function text() {
    if ( navigator.appName.indexOf( 'Microsoft' ) > -1 ) {
        $id( 'text' ).style.marginTop = "-2px";
    }
}

function validate() {
    var mands = ['nome', 'email', 'assunto', 'mensagem'];
    
    for ( var x = 0; x < mands.length; x++ ) {
        if ( $id( mands[x] ).value.length < 1 ) {
            alert('O campo "'+mands[x]+'" é obrigatório.'+$id( mands[x] ).value);
            $id( mands[x] ).focus();
            
            return false;
        }
    }
    
    return true;
}

function verify_estado() {
    if ( $id('estado').value.length != 2 ) $id( 'estado' ).focus();
}