$().ready(function() {

	// Hide original form
	//$("#add-comment-form").hide();
    // Initialize comment form
    
    // Adjust form html
    $("#add-comment-form").find("form").attr("id", "commentForm-0");	
    $("#add-comment-form").find("legend").html('<a href="" onclick="TCF(this);	return false;">Add Comment</a>');
        
    // Set reply value
    $('#add-comment-form [name=reply]').val(0);
    
    // Bind onclick event
    $("#add-comment-form [name=submit]").click(function(){
        CSF("commentForm-"+0);
        return false;
    });
	
	// Create new ajax form
	//replyComment(0);

    //CommentBindEvents(0);
});

function CSF(form_id){
	
	hasError = false;	
	
	// Grab Form Values
	page = $("#"+form_id+" [name=page_type]").val();
	id = $("#"+form_id+" [name=page_id]").val();
	reply = $("#"+form_id+" [name=reply]").val();
	comment = $("#"+form_id+" [name=comment]").val();
	
	var errorMessage = '';
	
	// Validate values
	if(comment == ''){
		errorMessage += 'Please add a comment \n';
	}	
	
	// Anon comment
	if($("#"+form_id+" [name=name]").length > 0){
		// Get anon values
		name = $("#"+form_id+" [name=name]").val();
		email = $("#"+form_id+" [name=email]").val();
		site = $("#"+form_id+" [name=site]").val();
		
		if(name == '')
			errorMessage += 'Please give a name \n';
		
		if(email == '')
			errorMessage += 'Please give an email \n';
	}
	
	if(errorMessage ==''){
		$("#"+form_id+' [name=submit]').hide();
		$("#"+form_id).append('<img id="saving-comment-'+id+'"src="/assets/img/indicator.gif"/>');
	
		$.post("/comment/form", 
				$("#"+form_id).serialize(), 
				function(data) {
					if(data != "fail"){					   
						if(reply == 0){
							$('.comments-list').prepend(data);
							$("#"+form_id+" [name=comment]").val('');							
						}else{
							$('#'+form_id).parents('.indent').prepend(data);
							$('#'+form_id).parent().parent().remove();
						}					   
					}else{
					   
					}    				
					
					$("#saving-comment-"+id).remove();
					$("#"+form_id+' [name=submit]').show();					
				}
		);
	}else{
		alert(errorMessage);
	}
}

function TCF(link){
	$(link).parents('fieldset').find('.fieldset-wrapper').slideToggle("fast");
}

function replyComment(cid, name){   

    // Check if the reply form already exists
    if($("#reply-container-"+cid).length > 0){
        $("#reply-container-"+cid).slideToggle("fast", function(){$(this).parent().remove();});
    }else{
        // Get comment form and set fields
        comment_form = $("#add-comment-form").clone();
		comment_form_title = $(comment_form).find("legend").html();
        
        // Adjust form html
        $(comment_form).attr("id", "reply-container-"+cid);
        $(comment_form).find("form").attr("id", "commentForm-"+cid);		
		if(cid != 0)
			$(comment_form).find(".comment-text-label").html("Replying to " + name);
		else
			$(comment_form).find("legend").html('<a href="" onclick="TCF(this);	return false;">'+comment_form_title+'</a>');
			
		// Set reply value
		$('[name=reply]', comment_form).val(cid);
        
        // Insert
        if(cid == 0)
            $(comment_form).insertAfter(document.getElementById("comment-"+cid));
        else
            $(comment_form).insertAfter(document.getElementById("comment-"+cid)).wrap("<div class='indent'></div>");
            
        $(comment_form).find(".fieldset-wrapper").show();
        $(comment_form).css("display", "none").slideDown("fast");        
		
		// Bind onclick event
		$("#commentForm-"+cid+' [name=submit]').click(function(){
			CSF("commentForm-"+cid);
			return false;
		});
    }
    
    return false;
}

function showThread(cid, pid, type, num){
    
	if($(".comment-"+cid+"-replies").html() != ''){
		$(".comment-"+cid+"-replies").slideToggle("fast"); 
	}else{
		$("#show-thread-for-"+cid).prepend('<img id="loading-thread-'+cid+'"src="/assets/img/indicator.gif"/>');
		$.get("/comment/thread/"+pid+"/"+type+"/"+num, function(data){
			$(".comment-"+cid+"-replies").html(data);			
			$("#loading-thread-"+cid).remove();
		});		
	}
}
