//AJAX specifc functions =======================
var comment_ajaxURL	   = "./?c=api&m=post";


function comment_doPost(origin) {
    // Global variable
    postArray = new Array();
    
	// Get our variables
	postArray['origin']      = origin;
	postArray['post']        = $("#commentInput").val();
	postArray['cid']         = $("#commentCID").val();
	postArray['title']       = $("#commentTitle").val();
	postArray['description'] = $("#commentDescription").val();
	postArray['image']       = $("#commentImage").val();
	postArray['view']        = $("#commentView").val();
	postArray['link']        = $("#commentLink").val();
	postArray['fbid']        = $("#commentFBID").val();
	
	//Clear the comment area for another post
	$('#commentInput').val('');	
	//Reset the counter
	textCounter('commentInput', 'commentCharCount', display_comment_length);	
	
	// Facebook
	// fbid is if user has pre authorized Thoora to post
	if($('#postFacebook').attr('checked') && postArray['fbid']==0 ) {
		facebook_post(postArray['title'], postArray['description'], postArray['link'], postArray['image'], postArray['post'], comment_doAjax);
	}else{
		comment_doAjax();
	}

}

function comment_doAjax() {

	// Facebook post
	var postFacebook = 0;
	if($('#postFacebook').attr('checked')) {		
		postFacebook = 1;
	}
		
	// Twitter post
	var postTwitter = 0 ;
	if($('#postTwitter').attr('checked')) {
	    postTwitter = 1;
	}

	$.getJSON( comment_ajaxURL, { view: postArray['view'], postTwitter: postTwitter, postFacebook: postFacebook, post: postArray['post'], cid: postArray['cid'], title: postArray['title'], origin: postArray['origin'] }, function(json){
	    if(!json) {
	        $("#comment-container").html('Post failed');
	    } else {		
		    if(json.twitterLink && json.twitterLink.length > 1) {				   
		        // Store some analytics
		        eval(json.analytics);
		     
		        // Redirect to Twitter
		        location.href = json.twitterLink;
		        return 1;
		    } else if(json.postHTML) {		
		    
				// Store some analytics
		        eval(json.analytics);			    
			    	  
				//Fadeout no comments box if it exists
				$("#noComments").hide();
				
				// Get the rendered view object back and display it
				$("#comment-container").prepend(json.postHTML);					 					
				
				$('#newcomment'+json.postID).slideDown('slow');								
			}		        
	    }
	});
}


function textCounter(field,cntfield,maxlimit) {
 $(document).ready(function(){
	 // Not in DOM if not logged in.
	 if($("#"+cntfield).length != 0) {
	    $("#"+cntfield).html(maxlimit - $("#"+field).val().length);
		
		if ($("#"+field).val().length > maxlimit) { 
		    // if too long, color red
		    $("#"+cntfield).addClass("comment-neg-count");	
		}else{
		    $("#"+cntfield).removeClass("comment-neg-count");	
		}
	}
 });
}

function markPost(id) {
 $(document).ready(function(){
	$.getJSON("./?c=api&m=markPost", { postID: id }, function(json) {
	    
	});
 });
}

function removePost(id) {
 $(document).ready(function(){
	var answer=confirm("Are you sure you want to remove this post?");
	if(answer) {    
		$.getJSON("./?c=api&m=removePost", { postID: id }, function(json){
		    if(json.error) {
		        // Nothing happens if we have an error for now.
		        alert("Sorry we could not remove your post.");
		    }else{
		    	$("#post_"+id).parent().slideUp('slow');
		    }
		});
	}		
 });
}

function ismaxlength(obj) {
	var mlength = obj.getAttribute?parseInt(obj.getAttribute("maxlength")):"";
	
	if(obj.getAttribute && obj.value.length>mlength)
		obj.value=obj.value.substring(0,mlength);
}

function readMore(pid) {
    var post = $('#post_root_'+pid).html() + $('#post_show_'+pid).html();

    $('#post_root_'+pid).html(post);
	$('#post_hide_'+pid).hide();
}