function addComment( entryID){
	var comments = $("Comments-" + entryID);
	if( comments == undefined)	return ;
	
	if(comments.commentOpen != undefined ){
		if( comments.commentOpen ){
			toggelCommentForm( entryID);
		}		
	}
	else{
		return openComment( entryID , true);
	}
	return;
}

function showComment( entryID){
	var comments = $("Comments-" + entryID);
	if( comments == undefined)	return ;
	
	if( comments.commentOpen != undefined  ){
		if( comments.commentOpen ){
			hideComment( entryID );
		}		
		else{
			displayComment(entryID);
		}
	}
	else{
		return openComment( entryID , false);
	}
	return;
}

function openComment( entryID , showForm){
	var url = 'http://yuroyoro.com/mt/mt-comments.cgi';
	var pars = 'entry_id=' + entryID;
	var comments = $("Comments-" + entryID);
	var commnetAjax = new Ajax.Request( url,{
					method : "get",
					parameters: pars,

					onSuccess: function(httpObj){
						comments.innerHTML = httpObj.responseText;
						displayComment(entryID , showForm );
					}
					
					}
	);
}

function toggelCommentForm( entryID  ){
	var form = $("comment_form_block-" + entryID);
	if( form == undefined  ) return;
	
	if( form.openForm != undefined && !form.openForm){
		new Effect.SlideDown( "comment_form_block-" + entryID ,
			{ duration: 0.65 } );
		form.openForm = true;
		changeCommentFormLinkText( entryID , false);
	}
	else{
		new Effect.SlideUp( "comment_form_block-" + entryID ,
			{ duration: 0.65 } );
		form.openForm = false;
		changeCommentFormLinkText( entryID , true);
	}
	
}
function changeCommentFormLinkText( entryID , add ){
	var link = $("CommentFormLink-" + entryID);
	if( link == undefined ) return;
	
	if( add ){
		link.innerHTML= "■ Add Comment";	
	}
	else{
		link.innerHTML= "■ Hide Form";
	}
	
}
function changeCommentLinkText( entryID , add ){
	var link = $("CommentLink-" + entryID);
	if( link == undefined ) return;
	
	if( add ){
		link.innerHTML= "■ Show Comments";	
	}
	else{
		link.innerHTML= "■ Hide Comments";
	}
	
}
function displayComment( entryID , showForm ){

		Element.show( "comment_posted-" + entryID );
		var form = $("comment_form_block-" + entryID);
		if( showForm ){
			Element.show("comment_form_block-" + entryID );
			form.openForm = true;
			changeCommentFormLinkText( entryID , false);
		}
		else{
			Element.hide("comment_form_block-" + entryID );
			form.openForm = false;
			changeCommentFormLinkText( entryID , true);
		}

		new Effect.SlideDown( "Comments-" + entryID ,
			{ duration: 0.65, 
			  afterFinish : function() { 
			  	new Effect.Highlight( "Comments-" + entryID ,
					{ duration: 0.65 })
			  }
			}
		);
		var comments = $("Comments-" + entryID);
		changeCommentLinkText(entryID , false);
		comments.commentOpen = true;
}

function hideComment( entryID){
	var comments = $("Comments-" + entryID);
	new Effect.SlideUp( "Comments-" + entryID ,
			{ duration: 0.65 } );
	comments.commentOpen = false;
	changeCommentLinkText(entryID , true);
}

function reportError(request){
		alert('Sorry. There was an error.');
}



function ajax_post( entryID )
{

	var url = 'mt/mt-comments.cgi';
	var pars = Form.serialize('comments_form_' + entryID)+'&post=Post';
	var myAjax = new Ajax.Updater( 'Comments-' + entryID, url, { 
																method: 'post', 
																parameters: pars,
																onComplete: function(){
																	highlight_comment(entryID);
																	hideLoadingImage();
																}

																});	
	
	
	return false;
}

function highlight_comment( entryID )
{
	var err = $("comment_error-" + entryID );
	var el = null; 
	if( err != undefined){
		el = err;
	}
	else{
		var posted = $('comment_posted-' + entryID);
		alert( $A(posted.getElementsByTagName("div")).last());
		el = $A(posted.getElementsByTagName("div")).last().parentNode;

	}
	new Effect.Fade( el , { duration: 0.2, queue:'end' });
	new Effect.Appear(el, { duration: 0.5, queue:'end' } );
	new Effect.Highlight(el,{ startcolor: '#FF9933', duration: 2.0 });
}



function ReloadTextDiv( entryID)
{
	if( !isPrevewCreated( entryID)){
		createPreview( entryID , this );
		return;
	}
	
	var NewText = document.getElementById("text-"+entryID).value;
	splitText = NewText.split(/\n/);
	var DivElement = document.getElementById("PreviewText-" +entryID);
	var i = 0;
	DivElement.innerHTML = '';
	for(i = 0; i < splitText.length; i++) {
	  if(splitText[i].length > 0 ) {
		DivElement.innerHTML += splitText[i] + "<br />";
	  }
	}
}

function isPrevewCreated( entryID ){
	var preview = $("Preview-" + entryID );
	return preview != undefined;
}
function createPreview( entryID ){

	var comments = $("comment_posted-" + entryID );
	if( comments == undefined ) return;
	
	var countObj = $("CommentCount-" + entryID );
	var count = new Number( countObj.value );
	count += 1;
	countObj.value = count;
		
	var preview = document.createElement( "div");
	preview.setAttribute("id" ,"Preview-" + entryID );
	preview.setAttribute("class","CommentBody");
	var html = '<span id="PreviewText-' + entryID +'">&nbsp;</span>';
	html += '<div class="CommentFooter">';
	
	html += '[<span id="PreviewNo-' + entryID + '">' + count +'</span>]';
	html += 'Posted by <a id="PreviewURL-' + entryID + '" href=""><span id="PerviewAuthor-' + entryID + '"></span></a>'
	html += ': ' + getCommentDate() + '</div>';		
	
	preview.innerHTML = html;
	comments.appendChild( preview );
	
	ReloadNameDiv(entryID);
	ReloadWebDiv(entryID);
}

function getCommentDate(){
	var d = new Date();
	return d.toLocaleString();
}

function ReloadNameDiv(entryID)
{
	if( !isPrevewCreated( entryID)){
		createPreview( entryID  );
	}
	var NewText = document.getElementById("author-" + entryID).value;
	var DivElement = document.getElementById("PerviewAuthor-"+entryID);
	DivElement.innerHTML = NewText;
}

function ReloadWebDiv(entryID)
{
	if( !isPrevewCreated( entryID)){
		createPreview( entryID , this );
		return;
	}
	var NewText = document.getElementById("url-" + entryID).value;
	var aElement = document.getElementById("PreviewURL-" + entryID);
	aElement.setAttribute("href" , NewText);
	
}

