window.addEvent('domready', function(){
	var comment = new FormCheck('comment-form', {
    	display : {
    		showErrors: 1,
    		checkValueIfEmpty: 1,
    		tipsPosition: 'left',
	    	tipsOffsetX: -23
    	}
    });
    if($('comment-form'))
    {
    	 $('comment-form').get('send').addEvent('success', function(rsp){
		var json = JSON.decode(rsp);
		
		if(json.status == "ok")
		{
			$('comment-form').setStyle('display', 'none');
			if(json.comment.url != 0)
			{
				var author = '<a href="'+json.comment.url+'">'+json.comment.author+'</a>';
			}
			else
			{
				var author = json.comment.author;
			}
			var comment = new Element('div', {
					'class': 'comment', 
					html: '<p>'+json.comment.comment+'</p><div class="meta">by <span class="author">'+author+' </span><span class="date">'+json.comment.date_added+'</span></div><div class="dialog-triangle"></div>'
				});
			comment.inject($('comment-list'), 'bottom');
			$('comment-form-title').set('html', 'Thanks! Your comment has been added.');
		}
		else
		{
			$('comment-form').getElement(".alert").set('html', json.msg).addClass('negative');;
		}
	});
	
	    $('comment-form').getElement('.submit').addEvent('click', function(e){
	    	e.stop();
	    	if(comment.isFormValid())
			{
	    		$('comment-form').getElement(".alert").set('html', 'Sending...');
	 			$('comment-form').send();
	 		}
	 	});
		
		$('comment-list').getElements('.comment .author a').each(function(item){
			if(item.get('href') == 0)
			{
				item.getParent('span').set('html', item.get('html'));
				item.destroy();
			}
		});
		$('comment-list').getElements('.comment:even').addClass('even');
    }
});
