function showCalendar( m, y, d )
{
    var url =   urlElementos + "&elemento=calendario" +
                "&day=" + d + 
                "&month=" + m + 
                "&year=" + y;
    var xa  =   new Ajax( url,
        {
            method : 'get',
            update : 'componente_content_1'
        }
    ).request();
}
/**
 *  Muestra las actividades de la fecha enviada y el detalle del segundo argumento
 */
function show_activities( date )
{
    if( !$( 'componente_content_3' ) )
    {
        goTo('act_hoy',false,{'date' : date, 'acti_id' : arguments[1]});
    }
    else
    {
        var url = urlProcesos + "&action=getTareas&fecha=" + date;
        var ja  =   new Ajax( url,
            {
                method : 'get',
                update : $( 'componente_content_3' )
            }
        ).request();
        if( arguments[1] )
        {
         activitie_deta( arguments[1] );
        }
    }
}
/**
 * Detalle de actividad 
 */
function activitie_deta( acti_id )
{
    if( $( 'acti_deta_div' ) )
    {
        $( 'acti_deta_div' ).remove();
    }
    var pos = getAvailablePosition();
    var aDiv = new Element( 'div',{'id':'acti_deta_div'}).inject(document.body);
    $( 'acti_deta_div' ).setStyles(
        {   'position'  :   'absolute',
            'left'      :   pos.x + 'px',
            'top'       :   pos.y + 'px'
        }
    );
    var aja = new Ajax( urlProcesos,
        {   'method'    :   'get',
            'data'      :   {   'action'    :   'get_acti_deta',
                                'acti_id'   :   acti_id,
                                'display'   :   ( arguments[ 1 ] ? arguments[ 1 ] : 'none' )
                            },
            'update'    :   $( 'acti_deta_div' ),
            'onComplete' :  function()
            {
                showOver( 'acti_deta_div' );
            }
        }
    ).request();
}
function create_activities( date )
{
    alert( 'agreagr actividad para la fecha' + date );
}

/*
 *  Formulario para capturar comentarios de alguna actividad
 */
function addActiComment( acti_id )
{
    if( $( 'acti_comment_div' ) )
    {
        $( 'acti_comment_div' ).remove();
    }
    var pos = getAvailablePosition();
    var DIV = new Element( 'div', { 'id' : 'acti_comment_div' } ).inject( document.body );
    $( 'acti_comment_div' ). setStyles(
        {   'position'  :   'absolute',
            'left'      :   pos.x + 'px',
            'top'       :   pos.y + 'px'
        }
    );
    var xa = new Ajax( urlProcesos,
        {   'method'    :   'get',
            'data'      :   {   'action'    :   'add_acti_comment',
                                'acti_id'   :   acti_id,
                                'responseTo' : ( arguments[1] ? arguments[1] : 0 )
                            },
            'update'    :   $( 'acti_comment_div' ),
            'onComplete' :   function()
            {   showOver( 'acti_comment_div' );}
        }
    ).request();

}

/*
 *  Guardando comentario en DB
 */
function saveActiComment()
{
    var jsn = new Json.Remote(
        urlProcesos + '&action=save_acti_comment' +
        '&comentario_acti_id=' + $( 'comentario_acti_id' ).value +
        '&comentario_para_user_id=' + $( 'comentario_para_user_id' ).value +
        ( $( 'comentario_id' ).value != '' ? '&comentario_id=' + $( 'comentario_id' ).value : '' ) +
        '&comentario_text=' + escape( $( 'comentario_text' ).value ),
        {
            onComplete : function( obj )
            {
                if( obj.complete == 'ok' )
                {
                    updateActiCommentList( $( 'comentario_acti_id' ).value );
                }
                else
                {
                    alert( obj.error );
                }
            }
        }
    ).send();
}
/*
 *  Actualiza listado de comentarios
 */

function updateActiCommentList( acti_id )
{
    var xa = new Ajax( urlProcesos,
        {   'method'    :   'get',
            'data'      :   { 'action' : 'update_acti_comment_list', 'acti_id' :  acti_id },
            'update'    :   $( 'acti_comment_list_' + acti_id ),
            'onComplete'    :   function()
            {
                if( $( 'acti_comment_div' ) ) $( 'acti_comment_div' ).remove();
                if( $( 'acti_comment_deta_div' ) ) $( 'acti_comment_deta_div' ).remove();
            }
        }
    ).request();
}


/*
 *  Mostrando detalle de comentarios de actividad
 */

function displayActiComment( comentario_id, responseTo )
{
    if( $( 'acti_comment_deta_div' ) ) $( 'acti_comment_deta_div' ).remove();
    var pos = getAvailablePosition();
    var DIV = new Element( 'div',{'id':'acti_comment_deta_div'}).inject(document.body);
    $( 'acti_comment_deta_div' ).setStyles(
        {   'position' : 'absolute',
            'left'      :   pos.x + 'px',
            'top'       :   pos.y + 'px'
        }
    );
    
    var xa = new Ajax(  urlProcesos,
        {   'method'    :   'get',
            'data'      :   {   'action' : 'comentario_acti',
                                'comentario_id' : comentario_id,
                                'responseTo' : responseTo
                            },   
            'update'    :   $( 'acti_comment_deta_div' ),
            'onComplete' : function()
            {
                showOver( 'acti_comment_deta_div' );
            }
        }
    ).request();

}

/*
 *  Responder mensajes
 */

function responseComment( comentario_id, comentario_status )
{
    var responseTo = arguments[2] ? arguments[2] : false;
    var jsn = new Json.Remote( 
        urlProcesos + '&action=set_acti_comment_status' +
        '&comentario_id=' + comentario_id +
        '&comentario_acti_id=' + $( 'comentario_acti_id' ).value +
        '&comentario_status=' + comentario_status,
        {   'onComplete' : function( obj )
            {
                if( responseTo > 0 && $( 'comentario_acti_id' ) )
                {
                    addActiComment( $( 'comentario_acti_id' ).value, responseTo );
                }
                else
                {
                    updateActiCommentList( $( 'comentario_acti_id' ).value ); 
                }
                chkComments();
            }
        }
    ).send();
}

function chkInvita()
{
    var jsn = new Json.Remote( urlProcesos + '&action=chk_invita',
        {   'onComplete'    :   function( obj )
            {
                if( obj.invita > 0 )
                {
                    displayNewAlert( 'Invita');
                }
                else
                {
                    if( $( 'newInvitaAlertDiv' ) ) $( 'newInvitaAlertDiv' ).remove();
                }
            }
        }
    ).send();
}
function chkComments()
{
    var jsn = new Json.Remote( urlProcesos + '&action=chk_new_comments',
        {   'onComplete' :  function( obj )
            {   
                if( obj.newComments > 0 )
                {
                    displayNewAlert( 'Comments');
                }
                else
                {
                    if( $( 'newCommentsAlertDiv' ) ) $('newCommentsAlertDiv' ).remove();
                }
            }
        }
    ).send();
}
function displayNewAlert( tipo, arr )
{
    var isNew = false;
    if( !$( 'new' + tipo + 'AlertDiv' ) )
    {
        isNew = true;
        var position = getAvailablePosition();
        var nCD = new Element( 'div', { 'id' : 'new' + tipo + 'AlertDiv' } ).inject( document.body );
        $('new' + tipo + 'AlertDiv').setStyles(
            {   'position'  :   'absolute',
                'top'       :   position.y + 'px',
                'left'      :   position.x + 'px'
            }
        );
    }
    var xa = new Ajax(
        urlProcesos,
        {   'method'    :   'get',
            'data'      :   {   'action' : 'display_new_'+ tipo + '_list' },
            'update'    :   $( 'new' + tipo + 'AlertDiv' )
        }
    ).request();
    if( isNew )
    {
        showOver( 'new' + tipo + 'AlertDiv' );
    }
}

