// $Id: comments.js 2503 2009-03-01 21:36:06Z coryking $

var spin_comment_start = function(last_cid){
	try {
		if(last_cid == null) {
			last_cid='';	
		}
		Element.show('cspin'+last_cid);
		var pb;

		if (last_cid) {
			pb = $('bpost' + last_cid);
			pc = $('bc' + last_cid);
			pc.disabled=true;
		} else {
			pb= $('bpost');
		}
		pb.disabled=true;
	} catch(e) {
		console.error('%o', e);
	}

};

var spin_comment_end = function(last_cid) {
	try {
		if(last_cid == null) {
			last_cid='';	
		}
		Element.hide('cspin'+last_cid);
		var pb;

		if (last_cid) {
			pb = $('bpost' + last_cid);
			pc = $('bc' + last_cid);
			pc.disabled=false;
		} else {
			pb= $('bpost');
		}
		pb.disabled=false;
	} catch(e) {
		console.error('%o', e);
	}

};

function attach_ratings() {
	try {
		if($('story_id_hdn') == null) {
			return;	
		}
		var story_id=$('story_id_hdn').value;
		var comment_type=$('comment_type_hdn').value;
		var conversation_id=$('conversation_id_hdn').value;
			$$('img.rec_c').each(function(s) {
			s.setStyle({cursor: 'pointer'});
				var cid = s.readAttribute('rel');
				s.observe('click', function(){
					rec_comment(story_id, cid, comment_type,conversation_id)
				});
			});	
			$$('img.norec_c').each(function(s) {
				s.setStyle({cursor: 'pointer'});
				var cid = s.readAttribute('rel');
				s.observe('click', function(){
	
					norec_comment(story_id, cid, comment_type,conversation_id)
				});
			});	
	} catch(e) {
		// nothin...	
	}
}
function detach_ratings() {
	
	var story_id=$('story_id_hdn').value;
	var comment_type=$('comment_type_hdn').value;
	var conversation_id=$('conversation_id_hdn').value;
		$$('img.rec_c').each(function(s) {
			var cid = s.readAttribute('rel');
			s.stopObserving('click', function(){
				rec_comment(story_id, cid,comment_type,conversation_id)
			});
		});	
		$$('img.nounrec_c').each(function(s) {
				var cid = s.readAttribute('rel');
			s.stopObserving('click', function(){
				norec_comment(story_id, cid, comment_type,conversation_id)
			});
		});	
}

function update_comments_callback(t) {
	var response =  t.responseText.evalJSON(true);
	
// Create an instance of the object type and throw it


	if (response.error) {
		hide_editor(response.data.sid,response.data.comment_id,response.data.comment_type,response.data.conversation_id);
		raiseErrorBox(response.error.error, response.error.title,1);
	} else {
		var commentList = $('content'+response.data.valid_sid);
		//deactivateEditors();

		Element.update(commentList, response.data.comments);
		attach_ratings();
		//activateEditor('tm_');
		//tinyMCE.addMCEControl(tm, 'tm');
		//tinyMCE.execCommand('mceAddControl', false, 'tm_');

		if(typeof pageTracker != 'undefined') {
			pageTracker._trackPageview('/comments/update/' + response.data.sid);
//			ajaxTracker._trackEvent("Post Comment", story_id);
		}

	}
}

var old_comment;

function show_comment_editor(t) {
	var response =  t.responseText.evalJSON(true);
	
	if (response.error) {
		hide_editor(response.data.sid,response.data.comment_id,response.data.comment_type,response.data.conversation_id);
		raiseErrorBox(response.error.error, response.error.title);
	} else {
		var comment_id=response.data.comment_id;
		Element.hide('comment_body_'+ comment_id);
		var commentEditor = $('comment_editbox_'+ comment_id);
		var comment = $('comment_' + comment_id);
		var ct = $('commentTable');
		var title = $('comment_dotitle_' + comment_id);
		if(title != null) {
			title.href="javascript:void(save_comment('" + escape(response.data.sid) + "', '" + comment_id + "', true, '" + response.data.comment_type + "', '" + response.data.conversation_id + "'))";
		}
		Element.update(commentEditor,response.data.edit_form);
		//activateEditor('tm_' + comment_id);
		var tm = $('tm' + comment_id);
		
			//tinyMCE.addMCEControl(tm, 'tm' + comment_id);		
		Element.show(commentEditor);
		
		tm.setStyle({
			width:(commentEditor.getWidth() -36) + 'px',
			height:commentEditor.getHeight() + 'px'
		});
		tinyQueue.loadEditor('tm' + comment_id, {
			setup:	function(ed){
				ed.onInit.add(function(ed){
					ed.focus();
			
				});	
			}
		});
	
	

	}

}
function hide_editor(story_id, comment_id, comment_type, conversation_id)
{	
	try {
		spin_comment_end(comment_id);
		if(comment_id != null)
		{
			tinyMCE.remove('tm' + comment_id);
			Element.remove('tm' + comment_id);
			Element.hide('comment_editbox_'+ comment_id);
			Element.show('comment_body_'+ comment_id);
			var title = $('comment_dotitle_' + comment_id);
			title.href="javascript:void(edit_comment('" + escape(story_id) + "', '" + comment_id + "', '" + comment_type + "', '" + conversation_id + "'))";
		} else {
			Element.show('tm');	
			tinyQueue.loadEditor('tm');
			
		}
	} catch(e) {
	}	
		

}
function save_comment(story_id, comment_id, cancel,comment_type, conversation_id) {
	var comment = $('comment_' + comment_id);
	
	var valid_sid=make_valid_sid(unescape(story_id));
	var post_form = $('form_' + comment_id);
	try {
		if(cancel) {
			hide_editor(story_id,comment_id,comment_type,conversation_id);
		} else {
			// call the server with the new shit, yo...
			tinyMCE.triggerSave();
			var params = Form.serialize(post_form, getHash=true);
			//tinyMCE.removeMCEControl('tm' + comment_id);
			//tinyMCE.removeMCEControl('tm');
			tinyMCE.execCommand('mceRemoveControl', false, 'tm' + comment_id);
			tinyMCE.execCommand('mceRemoveControl', false, 'tm');
			params.method='save';
			params.comment_id=comment_id;
			params.comment_type=comment_type;
			params.conversation_id=conversation_id;
			Element.remove('tm' + comment_id);
			Element.remove('tm');
			new Ajax.Request('/comments/ajax', {
				parameters:params,
				onSuccess:update_comments_callback,
				onFailure:function() {
					spin_comment_end(comment_id);
					hide_editor(story_id,comment_id,comment_type,conversation_id);
					callback_error(comment_id);
				},
				onLoading:spin_comment_start(comment_id),
				onComplete:spin_comment_end(comment_id)	
			});
			
		}
	} catch (e) {
		logTryCatch('save_comment', e);
		hide_editor(story_id,comment_id,comment_type);
	}
	//deactivateEditors();

	
	
	//Element.remove('tm_' + comment_id);


}

function post_comment(story_id, comment_type, conversation_id) {
	var valid_sid=make_valid_sid(story_id);
	var post_form = $('form_' + valid_sid);
	//var request = Form.toObject(post_form);
	tinyMCE.triggerSave();
	var request = Form.serialize(post_form, getHash=true);
	request.conversation_id=conversation_id;
	request.sid=story_id;
	request.comment_type=comment_type;
	//request.message = encodeURIComponent(request.message);
	tinyMCE.execCommand('mceRemoveControl', false, 'tm');
	new Mozi.Ajax(base_server + '/comments/ajax/ajax', 'post', request, {
		onSuccess:update_comments_callback,
		onFailure:function() {
					spin_comment_end();
					hide_editor(story_id,null,comment_type,conversation_id);
					callback_error();
		},
		onLoading:spin_comment_start(),
		onComplete:spin_comment_end()

	});
	Element.hide('tm');


}

function rec_comment(story_id, comment_id, comment_type,conversation_id) {
	var request = {};
	request.comment_id=comment_id;
	request.sid=story_id;
	request.conversation_id=conversation_id;
	request.comment_type=comment_type;
	$('rb'+comment_id).hide();
	$('rw'+comment_id).show();

	new Mozi.Ajax('/comments/ajax/ajax', 'rec', request, {
		onSuccess:update_comments_callback,
		onFailure:callback_error
	});
	
}
function norec_comment(story_id, comment_id, comment_type,conversation_id) {
	var request = {};
	request.comment_id=comment_id;
	request.conversation_id=conversation_id;
	request.sid=story_id;
	request.comment_type=comment_type;
	$('rb'+comment_id).hide();
	$('rw'+comment_id).show();
	new Mozi.Ajax('/comments/ajax/ajax', 'norec', request, {
		onSuccess:update_comments_callback,
		onFailure:callback_error
	});
	
}

function clear_comment(story_id, comment_id, comment_type,conversation_id) {
	var request = {};
	request.comment_id=comment_id;
	request.sid=story_id;
	request.conversation_id=conversation_id;
	request.comment_type=comment_type;
	
	new Mozi.Ajax('/comments/ajax/ajax', 'clear_rec', request, {
		onSuccess:update_comments_callback,
		onFailure:callback_error
	});
	
}



function hide_comment(story_id, comment_id, reason,comment_type,conversation_id) {
	var request = {};
	request.comment_id=comment_id;
	request.sid=story_id;
	request.reason=reason;
	request.comment_type=comment_type;
	request.conversation_id=conversation_id;
	new Mozi.Ajax('/comments/ajax/ajax', 'hide', request, {
		onSuccess:update_comments_callback,
		onFailure:callback_error
	});
	
}

function edit_comment(story_id, comment_id,comment_type,conversation_id) {
	var request = {};
	request.comment_id=comment_id;
	request.sid=story_id;
	request.conversation_id=conversation_id;
	request.comment_type=comment_type;
	if(typeof ajaxTracker != 'undefined') {
		ajaxTracker._trackEvent("Edit Comment", story_id);
	}

	new Mozi.Ajax('/comments/ajax/ajax', 'edit', request, {
		onSuccess:show_comment_editor,
		onFailure:function() {
				spin_comment_end(comment_id);
				hide_editor(story_id,comment_id,comment_type,conversation_id);
				callback_error();
		}
	});
}

addLoadEvent(function(){
	attach_ratings();
	
});