$(document).ready(function() {
    //alert("comments");
    $('#comment_name').focus(function () {
        
        if($('#comment_name').val()=='Name'){
           $('#comment_name').val(''); 
        }
    }) 
    $('#comment_body').focus(function () {
        if($('#comment_body').val()=='Comment'){
           $('#comment_body').val(''); 
        }
    })
    $('#comment_submit').click(function () {
      
        if(($('#comment_body').val()=='' || $('#comment_body').val()=='Comment') || ($('#comment_name').val()=='' || $('#comment_name').val()=='Name')){        
            alert("Please enter a name and comment");
        }else{
            // populated form so submit comment
            var comment_name = $('#comment_name').val();
            var comment_body = $('#comment_body').val();   
            $.post("/ajax/comments_add.php", { conf_id: conf_id, item_id: current_item_id, item_type: item_type, comment_name: comment_name, comment_body: comment_body },
                function(data){
                    if(data.result!='success'){
                        if(data.errors){
                            alert(data.errors);
                        }else{
                            alert("You comment did not add. Please try again");
                        }
                    }else{
                        // comment added fine so reset
                        $('#comments_right_add_entry').hide('fast');
                        $('#comment_body').val('Comment');
                    }
                   
                },"json");            
        }
    
    })         

});

function start_comments_feed(item_id,item_type){
    comments_feed(item_id,item_type);
    $(document).everyTime(30000, function() {
       comments_feed(item_id,item_type);
    });    
}

function comments_feed(item_id,item_type){
  
    $.getJSON("/ajax/comments_json.php", { conf_id: conf_id, item: item_id, item_type: item_type, since_id : latest_comment_id },
        function(data){
            var html = '';
            var x = 0;
            $.each(data, function(i,item){
                if(x==0){
                    latest_comment_id = item.id;
                }
                x++;
                html+='<div class="updates_item" id="comment_' + item.id + '"><div class="updates_inner"><div class="updates_heading">';
                html+=item.friendly_time + ' : <strong>' + item.comment_name + '</strong>';
                html+='</div>';
                html+='<div class="updates_body">' + item.comment_body + '</div></div></div>';


              });
            $("#comments_updates_inner").prepend(html);
    });    
}

function show_add_comment(){
    if($('#comments_right_add_entry').css("display")=='none'){
        $('#comments_right_add_entry').show('fast');
    }else{
        $('#comments_right_add_entry').hide('fast');
    }
    


    
}
