var WebtvComments;

WebtvComments = function ()
{
	this.init = function ()
	{
		try{
			Event.observe('comments-show-form', 'click', function(){ $('comments-form').show(); $('comments-toolbar-0').hide(); $('comments-toolbar-1').show(); });
			Event.observe('comments-close-form', 'click', function(){ $('comments-form').hide(); $('comments-toolbar-1').hide(); $('comments-toolbar-0').show(); });
			Event.observe('comments-send-comment', 'click', this.post.bind(this));
			Event.observe('comment-text', 'change', this.check_length.bind(this));
			Event.observe('comment-text', 'keyup', this.check_length.bind(this));
		}
		catch(e){}
		Wlog('webtv.comments:: init');
	};
	
	this.comment = function ()
	{
		try{
			$('comments-container').show();
			this.load();
		} catch(e){}
	};
	
	this.load = function ()
	{
		$('comments-content').update('<i>Kommentare werden geladen ...</i>');
		new Ajax.Request(settings.base+'wdata/'+settings.curr_clip.uri_prefix+'comments/'+settings.curr_clip.uri, {
			method: 'get',
			onSuccess: function (tr){
				var resp;
				try{ resp = tr.responseText.evalJSON(); } catch(e) {}
				Wlog(resp.comments);
				if(resp && resp.comments)	$('comments-content').update(resp.comments);
				else {
					$('comments-content').update('<i>Es gibt noch keine Kommentare zu diesem Beitrag.</i>');
					Wlog('webtv.comments::load:: unable to parse comments (or no comments)');
				}
			},
			onFailure: function (){
				$('comments-content').update('&nbsp;');
				Wlog('webtv.comments::load:: unable to retrieve comments');
			}
		});
	};
	
	this.post = function ()
	{
		var name, text;
		name = $F('comment-name');
		text = $F('comment-text');
		$('comments-form').hide();
		$('comments-toolbar-0').show();
		$('comments-toolbar-1').hide();
		$('comments-content').update('<i>Ihr Kommentar wird gespeichert ...</i>');
		new Ajax.Request(settings.base+'wdata/'+settings.curr_clip.uri_prefix+'comment/'+settings.curr_clip.uri, {
			method: 'post',
			parameters: Object.toQueryString({'name':name,'text':text}),
			onSuccess: function (tr){
				$('comment-name').value = '';
				$('comment-text').value = '';
				this.load();
			}.bind(this),
			onFailure: function ()
			{
				$('comments-form').show();
				$('comments-toolbar-0').hide();
				$('comments-toolbar-1').show();
				alert('Das Kommentar konnte nicht gespeichert werden, bitte versuchen Sie es erneut!');
				Wlog('webtv.comments::load:: unable to save comment');
				this.load();
			}.bind(this)
		});
		
	};
	
	this.check_length = function ()
	{
		var len = $F('comment-text').length, maxlen = 320;
		if(len > maxlen){ $('comment-text').value = $F('comment-text').substr(0,maxlen); this.check_length(); return; }
		$('comment-textrest').update(maxlen-len);
	};
};

try{
	Webtv.prototype.comments = new WebtvComments();
	Webtv_reg.push('comments');
	Wlog('webtv.comments:: bound');
}
catch(e){
	Wlog('webtv.comments:: could not bind myself');
}


/* player bindings */
fpComment = function () {
	try {
		webtv.comments.comment();
	} catch (e) {}
};

