/*
 * JavaScript interface to the SB network.
 * class SB.comment - comments management
 *
 * Requirements: jQuery library 1.4.2 and above
 * Copyright (C) 2010 Tomas Hnilica.   All Rights Reserved.
 */

/** CONVENIENCE FUNCTIONS*/
function addComment(id) {
  var c = new SB.comment(id);
  c.addComment();
}



function deleteComment(id, com_id) {
  var c = new SB.comment(id);
  c.deleteComment(com_id);
}


function initWallComment(id) {
  var c = new SB.comment(id);
  c.initOnWall();
}



SB.comment = function(cid) {
	this.DOM = $("#" + cid);
	this.id = cid;
	this.topic = $("#" + cid).attr('topic');
	this.type = $("#" + cid).attr('type');
	this.size = $("#" + cid).attr('size');
	this._textarea = this.DOM.find("textarea");
	
}

/** initialize*/
SB.comment.prototype.init = function() {
	if (this._textarea ) {
  	/*
  	this._textarea.autoResize({
		    onResize : function() {
		        $(this).css({opacity:0.8});
		    },
		    animateCallback : function() {
		        $(this).css({opacity:1});
		    },
		    animateDuration : 100,
		    extraSpace : 30
		});
		*/
		
		this._textarea.val("Napište komentář...");
	  this._textarea.css('color','#777777');
		this._textarea.focus(function() {
	     	  if ($(this).val() == "Napište komentář...") $(this).val('');
	     	  $(this).css('color','#000000');
			});
	  
	  /*
	  if (this.type == 'wall') {
	  	 var context = this;
	     this._textarea.focusout(function(){
	     	   context.wallAppend.hide('slow');
	     	});
	  }
	  */
		
  }
}

/**
 differnet initialization for wall posts
*/
SB.comment.prototype.initOnWall = function() {
  this.wallAppend = this.DOM.find(".comment-wall-post");
  this.wallAppend.toggle('slow');
  this.init();
  
}

SB.comment.prototype.getComments = function() {
 	var params = {}
	params["ws"] = "getComment";
	params["id"] = this.id;
	params["topic"] = this.topic;
	params["type"] = this.type;
	params["size"] = this.size;
  var context = this;
	$.get("/ws/", params, function(data){
	   context.DOM.replaceWith(data);
	   $('.jq-button').button();
	});  
};


SB.comment.prototype.addComment = function() {
	
	var params = {}
	params["id"] = this.id;
	params["text"] = this._textarea.val();
  var context = this;
  if (params["text"] == "") return;
	$.post("/ws/?ws=addComment", params, function(data){
     if (SBuser && context.type != 'wall') {
			 if (data && data.ap) {
			    var sbp = new SB.profile(SBuser.uid, false);
			    sbp.informAboutPoints(data)
			 }
       var link = jQuery.url.attr("path"); 
       //SBuser.addActivityDlg("přidal komentář k:  [[a href="+link+"]]"+context.topic+"[[/a]]", "comment", context.id , link);
       SBuser.addActivity("přidal komentář k:  [[a href="+link+" ]]"+context.topic+"[[/a]]", "comment", context.id, link);
     }
     context.getComments();
	},"json");	

}

SB.comment.prototype.deleteComment = function(cid) {
  var context = this;
  thConfirm("Opravdu chcete smazat Váš komentář?","Smazat komentář?", function(r){
	 	if (!r) return;
	 	var params = {}
		params["ws"] = "deleteComment";
		params["id"] = cid;
		$.get("/ws/", params, function(data){
		   context.getComments();
		});  
     
  });


}

