
var feed_show = new Array;
feed_show['comment'] = true;
feed_show['tweet'] = true;

$(document).ready(function() {


    $('#hide_twitter').click(function(){
        $('.twitter_item').toggle();
    });

    $('#hide_comments').click(function(){
        $('.comments_item').toggle();
    });


    $('#comment_name').focus(function () {  
        if($(this).val()=='Name'){
            $(this).val('');
        }
    });

    $('#question_from').focus(function () {  
        if($(this).val()=='From (Organisation/Role)'){
            $(this).val('');
        }
    });

    $('#question_speaker').focus(function () {  
        if($(this).val()=='To'){
            $(this).val('');
        }
    });	



    $('#comment_body').focus(function () { 
        if($(this).val()=='Comment'){
            $(this).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: 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_name').val('Name');
                    $('#comment_body').val('Comment');
                    $('#comment_confirm').html("COMMENT ADDED").fadeIn('slow',function(){
                        $(this).fadeOut('slow',function(){
                            if(is_full_screen){ // if full screen then comments form is laid out differently
                                $('#comment_form_holder').hide();
                            }
                        });
                    });



                    comments_feed(item_id,item_type);
                }

            },"json");            
        }


    });

    if(item_id && item_type){
        start_comments_feed(item_id,item_type);
        //alert('comments started');
    }



});


function show_feed(feed_type,show){

    feed_show[feed_type] = show;

    if(show){
        $('.' + feed_type).show();
        $('#show_' + feed_type).attr('href',"javascript:show_feed('" + feed_type + "',false);");
        $('#show_' + feed_type + ' span').html('Hide');
    }else{
        $('.' + feed_type).hide();
        $('#show_' + feed_type).attr('href',"javascript:show_feed('" + feed_type + "',true);");
        $('#show_' + feed_type + ' span').html('Show');		
    }
}


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){
    //alert("Checking" + item_id + " TYPE " + item_type);
    if(conf_id && item_id){



        $('#comments_html').load('/ajax/all_comments_html.php?item=' + item_id + "&item_type=" + item_type + "&conf_id=" + conf_id);
    }
}


function show_add_comment(){
    $('#comment_form_holder').show();
    $('#question_form_holder').hide();
}

