$(document).ready(function(){   
	
	$("#addCommentFB").validationEngine({
		unbindEngine: false,
        success :  function() {commentClass.saveAction('#addCommentFB')},
        failure : function() {}
       });
	
	
	$("#addComment").validationEngine({
		unbindEngine: false,
        success :  function() {commentClass.saveAction('#addComment')},
        failure : function() {}
       });
       
    $('.commentsreadmore').live('click', function () {
    	var rel = $(this).attr('rel');
    	$('#' + rel).hide('fast');
    	$('#full' + rel).show('slow');
    });
    
    $('.commentsreadless').live('click', function () {
    	var rel = $(this).attr('rel');
    	$('#full' + rel).hide('fast');
    	$('#' + rel).show('slow');
    });
})	





function commentClass() {
	var host;

	return {
		$ : function() {
			return host = this;
		},

		loadAction : function(page) {
			genericPrototype(host.urlLoad+'/page/'+page, 'loadcommentAction', 'post');
		},
		saveAction : function(formid) {
			genericPrototype(host.urlSubmit, 'savecommentAction', 'post',$(formid).serialize());
		},
		urlLoad :null,
		urlSubmit :null
	}.$();
}

function loadcommentAction(transport, textStatus) {
	if (textStatus == 'success') {
		response = $.evalJSON(transport);
		if (empty(response.error)) {		
			$('#commentContainer').html(response.content);
		} else {
			failureFunc(response);
		}
	} else {
		failureFunc(response);
	}
	if($('#commentContainer')[0]){
	//FB.XFBML.Host.parseDomElement($('#comments')[0]);
	}
}


function savecommentAction(transport, textStatus) {
	
	// generic json
	genericJson(transport, textStatus);
	
	// generic success
	if (textStatus == 'success') {
		response = $.evalJSON(transport);
		if (empty(response.error)) {		
			if (response.publishFB == 1) {
				var attachment = {'media':[{'type':'image','src':unescape(response.thumbnailURL),'href':unescape(response.URL)}],
				                  'caption':'Site - '+response.Title,
				                  'description':response.Description}; 
				var actionLinks = [{"text": "Website ", "href": unescape(response.siteURL)}];
				FB.Connect.streamPublish(response.commentText, attachment, actionLinks, null, response.Description, function(){$('html,body').animate({scrollTop: '+=-' + divOffset + 'px'}, 1000)});
			} 
			$('#comment').val('');
			$('#comment_fb').val('');
		}
	} 
	
	// FBML
}