/*
 * clear the form
 */

function prismaajaxcomment_clear_form( form )
{
    form.Message.value=''
    if ( form.Subject )
    {
      form.Subject.value=''
    }
    if ( form.Author )
    {
      form.Author.value=''
    }
}

/*
 * count characters in message
 */
function prismaajaxcomment_counter( field, countfield, max )
{
    if ( max > 0)
    {
        if ( field.value.length > max )
        {
            field.value = field.value.substring( 0, max )
        }
        else 
        {
            if ( countfield !=null )
            {
                countfield.value = max - field.value.length
            }
        }
    }
}

/*
 * Display a message
 */

function prismaajaxcomment_display_message( message, message_class )
{
    $('prismaajaxcomment_message').set( 'html', message );
    $('prismaajaxcomment_message').setProperty( 'class', message_class );
    
}

/*
 * Display the form
 */
function prismaajaxcomment_display_form()
{
    $('prismaajaxcomment_form').setStyle( 'display', 'block' )
}

/*
 * Pre-validate the form
 */
function prismaajaxcomment_validate( form )
{
    //check author
    if ( settings['ask_for_author'] && form.Author.value=='' )
    {
        prismaajaxcomment_display_message( i18n['author_empty'], 'warning')
        form.Author.focus()
        return false
    }
    //check author
    if ( settings['ask_for_subject'] && form.Subject.value=='' )
    {
        prismaajaxcomment_display_message( i18n['subject_empty'], 'warning')
        form.Subject.focus()
        return false
    }
    //check message
    if ( form.Message.value=='' )
    {
        prismaajaxcomment_display_message( i18n['message_empty'], 'warning')
        form.Message.focus()
        return false
    }
    return true
}

/*
 * Display comment list
 */
function  prismaajaxcomment_display_list( comments )
{
    if ( comments.length )
    {
      html = ''
      for(i=0;i<comments.length;i++)
      {
        html += prismaajaxcomment_comment_tpl( comments[i], i%2 );
      }
      $('prismaajaxcomment_list').set( 'html', html )
    }
}

function prismaajaxcomment_display_navigator( navigator )
{
    if ( navigator['page_count'] > 1 )
    {
        var html = prismaajaxcomment_navigator_tpl( navigator );
        $('prismaajaxcomment_navigator').set( 'html', html )
    }
}

/*
 * Load list comments and check if user can submit a comment
 */
function prismaajaxcomment_load( page )
{
    if ( page == 'last' )
    {
        the_url = settings['last_url']
    }
    else
    {
        the_url = settings['list_url'] + '/' + page
    }
    
    var jsonRequest = new Request.JSON({url: the_url, onComplete: function(result){prismaajaxcomment_on_load_complete(result)}}).get()
}

/*
 *
 */
function prismaajaxcomment_on_load_complete( result )
{
    if ( result )
    {
        if ( result['can_create'] )
        {
            prismaajaxcomment_display_form()
	        if ( !result['comments'].length )
	        {
	            prismaajaxcomment_display_message( i18n['no_comment'], 'feedback' )
	        }
        }
        else
        {
            prismaajaxcomment_display_message( i18n['can_not_create'], 'feedback' )
        }
        prismaajaxcomment_display_list( result['comments'] );
        
        prismaajaxcomment_display_navigator( result['navigator'] )
        
    }
    else
    {
        prismaajaxcomment_display_message( i18n['ajax_error'], 'warning' )
    }
}

/*
 * send the form
 */
function prismaajaxcomment_send( form )
{
    prismaajaxcomment_display_message( i18n['sending'], 'feedback' )
    form.set('send', {url: settings['add_url'], method: 'post', onComplete: function(result){prismaajaxcomment_on_send_complete(result,form)}})
    form.send()
}

/*
 *
 */
function prismaajaxcomment_on_send_complete( result, form )
{
    if ( !result )
    {
        prismaajaxcomment_display_message( i18n['ajax_error'], 'warning' );
        return;
    }
    
    result = JSON.decode( result );
    if ( !settings['publish_hidden'] )
    {
        prismaajaxcomment_display_message( i18n['sended'], 'feedback' )
    }
    else
    {
        prismaajaxcomment_display_message( i18n['sended_waiting'], 'feedback' )
    }
    prismaajaxcomment_clear_form( form );
    if ( result['comments'] ) //data reload
    {
        prismaajaxcomment_display_list( result['comments'] )
    }

    if ( result['navigator'] ) //data reload
    {
        prismaajaxcomment_display_navigator( result['navigator'] )
    }
}


/*
 * Count number of comment for a node_id
 */
function prismaajaxcomment_on_count_complete(result, id)
{
    if ( !result )
    {
        return;
    }
    
    var html = prismaajaxcomment_count_tpl(result['count']);
    $$('.nb_comment_'+id).each(function(res){res.set('html', html)});
}

function prismaajaxcomment_count( id )
{
    var jsonRequest = new Request.JSON({url: '/prismaajaxcomment/count/'+ id, onComplete: function(result){prismaajaxcomment_on_count_complete(result, id)}}).get()
}
