


/*** LIKES ***/

// TODO: cange like/unlike
function shortactionup_like(elem_id) {
  if((shortactionup_points == 1) && (shortactionup_points_rememberchoice == 0)) {
	mooFaceboxExShow("", "user_shortactionup_points.php?task=points&action_id="+elem_id, 370);
	return;
  }
  new WENJRMods.NewsfeedPlusPoster(elem_id).post_like(1);
}


function shortactionup_like_and_give(action_id) {
  var points = WENJRMods.B.ge('shortactionup_givepoints').value;
  // "other"
  if(points == "") {
	points = WENJRMods.B.ge('shortactionup_pointsother').value;
  }
  var remember_choice = WENJRMods.B.ge('shortactionup_remember').checked ? 1 : 0;
  parent.shortactionup_like_withpoints(action_id, points, remember_choice);
}


function shortactionup_like_withpoints(action_id, points, remember_choice) {
  shortactionup_points_rememberchoice = remember_choice;
  new WENJRMods.NewsfeedPlusPoster(action_id).post_like(1,points,remember_choice);
}


function shortactionup_givepoints_onChange() {
  var points = WENJRMods.B.ge('shortactionup_givepoints').value;
  if(points == "") {
	WENJRMods.B.hide("shortactionup_givepoints");
	WENJRMods.B.show("shortactionup_pointsother_holder");
	WENJRMods.B.ge("shortactionup_pointsother").focus();
  }
}

function shortactionup_unlike(elem_id) {
  new WENJRMods.NewsfeedPlusPoster(elem_id).post_unlike();
}


function shortactionup_load_likes(elem_id) {
  new WENJRMods.NewsfeedPlusPoster(elem_id).load_likes(elem_id);
}

function shortactionup_load_comments(elem_id) {
  new WENJRMods.NewsfeedPlusPoster(elem_id).load_comments(elem_id);
}

function shortactionup_comment_onFocus(elem_id) {
  var input_elem = WENJRMods.B.ge("shortactionup_comment_" + elem_id);
  if(input_elem.value == shortactionup_lang_writecomment) {
	WENJRMods.B.ge("shortactionup_comment_" + elem_id).value = "";;
	WENJRMods.B.ge("shortactionup_comment_" + elem_id).className = "textarea shortactionup_comment_active";
  }
  
  controller = shortactionup_autogrow('shortactionup_comment_' + elem_id);
  WENJRMods.B.show('shortactionup_commentpost_' + elem_id);
  if(controller) {
	controller.onFocus();
  }
}


function shortactionup_comment_onBlur(elem_id) {
  var input_elem = WENJRMods.B.ge("shortactionup_comment_" + elem_id);
  if(input_elem.value == "") {
	WENJRMods.B.hide('shortactionup_commentpost_' + elem_id);
	input_elem.value = shortactionup_lang_writecomment;
	WENJRMods.B.ge("shortactionup_comment_" + elem_id).className = "textarea shortactionup_comment";
  }
}


function shortactionup_cancelcomment(elem_id) {
  WENJRMods.B.hide('shortactionup_commentpost_' + elem_id);
  WENJRMods.B.hide('shortactionup_commentpost_' + elem_id);
  WENJRMods.B.ge("shortactionup_comment_" + elem_id).value = shortactionup_lang_writecomment;
  WENJRMods.B.ge("shortactionup_comment_" + elem_id).className = "textarea shortactionup_comment";
}

function shortactionup_comment(elem_id) {
  //var input_elem = WENJRMods.B.ge("shortactionup_comment_" + elem_id);
  WENJRMods.B.show("shortactionup_comment_" + elem_id);
  WENJRMods.B.ge("shortactionup_comment_" + elem_id).focus();
}



function shortactionup_editcomment(elem_id, comment_id) {
  this.edit_obj = null;
  
  if(this.edit_obj == null) {
	this.edit_obj = WENJRMods.B.ge("shortactionup_editcomment_wrapper");
	this.textarea_obj = WENJRMods.B.ge("shortactionup_editcomment");
	this.textarea_obj.className = "shortactionup_textarea shortactionup_comment";
	controller = shortactionup_autogrow(this.textarea_obj);
  }
  
  WENJRMods.B.hide("shortactionup_comment_body_"+elem_id+"_"+comment_id);

  var comment_elem = WENJRMods.B.ge("shortactionup_comment_body_"+elem_id+"_"+comment_id);
  var wrapper = WENJRMods.B.ge("shortactionup_comment_body_wrapper_"+elem_id+"_"+comment_id);

  this.textarea_obj.value = comment_elem.innerHTML;
  this.textarea_obj.className = "shortactionup_textarea shortactionup_comment_active";
  wrapper.appendChild(this.edit_obj);
  this.edit_obj.style.display = "";
  this.textarea_obj.focus();
}



function shortactionup_deletecomment(elem_id,comment_id) {
  elem = WENJRMods.B.ge("shortactionup_commentitem_" + elem_id + "_" + comment_id);
  if(elem) {
	elem.parentNode.removeChild(elem);
  }
  new WENJRMods.NewsfeedPlusPoster(elem_id).remove_comment(comment_id);
}


function shortactionup_submitcomment(elem_id) {
  var input_elem = WENJRMods.B.ge("shortactionup_comment_" + elem_id);
  var comment = encodeURIComponent(input_elem.value);
  input_elem.value = shortactionup_lang_writecomment;
  input_elem.className = "textarea shortactionup_comment";
  new WENJRMods.NewsfeedPlusPoster(elem_id).post_comment(comment);
}


WENJRMods.NewsfeedPlusEditor = function() {
};


// Singleton
WENJRMods.NewsfeedPlusEditor = {
  elem_id : '',
  instance_id : '',
  action_id : 0,
  comment_id : 0,
  edit_obj : null,
  textarea_obj : null,
  editing : false,
  updating : false,

  edit_comment : function (elem_id, comment_id) {
	
	if(this.updating) {
	  return;
	}
	
	this.cancel_comment();
	
	this.elem_id = elem_id;
	//this.action_id = shortactionup_get_action_id(elem_id);
	if (elem_id.indexOf('_') != -1) {
	  this.action_id = elem_id.split('_')[0];
	  this.instance_id = elem_id.split('_')[1];
	} else {
	this.action_id = elem_id;
	}

	this.comment_id = comment_id;
	this.editing = true;
	
	edit_obj = WENJRMods.NewsfeedPlusEditor.edit_obj;
	
	if(edit_obj == null) {
	  edit_obj = WENJRMods.B.ge("shortactionup_editcomment_wrapper");
	  textarea_obj = WENJRMods.B.ge("shortactionup_editcomment");
	  textarea_obj.className = "shortactionup_textarea shortactionup_comment";
	  controller = shortactionup_autogrow(textarea_obj);
	  WENJRMods.NewsfeedPlusEditor.textarea_obj = textarea_obj;
	  WENJRMods.NewsfeedPlusEditor.edit_obj = edit_obj;
	}
	
	textarea_obj = WENJRMods.NewsfeedPlusEditor.textarea_obj;
	
	WENJRMods.B.hide("shortactionup_comment_body_"+elem_id+"_"+comment_id);
  
	WENJRMods.NewsfeedPlusEditor.comment_elem = WENJRMods.B.ge("shortactionup_comment_body_"+elem_id+"_"+comment_id);
	WENJRMods.NewsfeedPlusEditor.wrapper = WENJRMods.B.ge("shortactionup_comment_body_wrapper_"+elem_id+"_"+comment_id);
  
	textarea_obj.disabled = false;
	textarea_obj.value = WENJRMods.NewsfeedPlusEditor.comment_elem.innerHTML;
	textarea_obj.className = "shortactionup_textarea shortactionup_comment_active";
	WENJRMods.NewsfeedPlusEditor.wrapper.appendChild(edit_obj);
	WENJRMods.B.show("shortactionup_editcomment_post");
	edit_obj.style.display = "";
	textarea_obj.focus();
  },

  cancel_comment : function () {
	if(WENJRMods.NewsfeedPlusEditor.editing) {
	  WENJRMods.NewsfeedPlusEditor.wrapper.removeChild(WENJRMods.NewsfeedPlusEditor.edit_obj);
	  WENJRMods.B.show(WENJRMods.NewsfeedPlusEditor.comment_elem);
	  WENJRMods.NewsfeedPlusEditor.editing = false;
	}
  },
  
  update_comment : function () {
	WENJRMods.NewsfeedPlusEditor.updating = true;
	
	var comment = encodeURIComponent(WENJRMods.NewsfeedPlusEditor.textarea_obj.value);
	WENJRMods.NewsfeedPlusEditor.textarea_obj.disabled = true;

	WENJRMods.B.hide('shortactionup_editcomment_post');

	var ajax = new WENJRMods.Ajax(WENJRMods.NewsfeedPlusEditor.onUpdateSuccess, WENJRMods.NewsfeedPlusEditor.onUpdateFail);
	var params = "task=updatecomment&comment="+comment+"&action_id="+WENJRMods.NewsfeedPlusEditor.action_id+"&comment_id="+WENJRMods.NewsfeedPlusEditor.comment_id;
	ajax.post(shortactionup_actionurl,params);
  },
  
  onUpdateSuccess : function( obj, responseText ) {

    var r = [];
    try {
      r = eval('(' + responseText + ')')
    } catch(e) {
      r.status = 1;
    };
    if (r.status == 0) {
	  WENJRMods.NewsfeedPlusEditor.comment_elem.innerHTML = WENJRMods.NewsfeedPlusEditor.textarea_obj.value;
    } else {
      //error
	  // apps_show_error_message(r.err_msg ? r.err_msg: '')
    }

	WENJRMods.NewsfeedPlusEditor.updating = false;
	WENJRMods.NewsfeedPlusEditor.cancel_comment();
	
  },

  onUpdateFail : function( obj, responseText ) {
	WENJRMods.NewsfeedPlusEditor.updating = false;
	WENJRMods.NewsfeedPlusEditor.cancel_comment();
  }
  
}

function shortactionup_get_action_id(elem_id) {
  if (elem_id.indexOf('_') != -1) {
    return elem_id.split('_')[0];
  }
  return elem_id;
}


WENJRMods.NewsfeedPlusPoster = function(elem_id) {
  this.elem_id = elem_id;
  //this.action_id = shortactionup_get_action_id(elem_id);
  if (elem_id.indexOf('_') != -1) {
    this.action_id = elem_id.split('_')[0];
    this.instance_id = elem_id.split('_')[1];
  } else {
    this.action_id = elem_id;
  }

};


WENJRMods.NewsfeedPlusPoster.prototype = {
  elem_id : '',
  instance_id : '',
  action_id : 0,
  
  post_comment : function(comment) {
	WENJRMods.B.hide('shortactionup_commentpost_' + this.elem_id);
	WENJRMods.B.hide('shortactionup_comment_' + this.elem_id);
	WENJRMods.B.show('shortactionup_commentpost_progress_' + this.elem_id);

	var ajax = new WENJRMods.Ajax(this.onPostSuccess.bind(this), this.onPostFail.bind(this));
	var params = "task=postcomment&comment="+comment+"&action_id="+this.action_id+"&instance_id="+this.instance_id;
	ajax.post(shortactionup_actionurl,params);
  },
  
  remove_comment : function(comment_id) {
	//var ajax = new WENJRMods.Ajax(this.onPostSuccess.bind(this), this.onPostFail.bind(this));
	var ajax = new WENJRMods.Ajax();
	var params = "task=deletecomment&comment_id="+comment_id+"&action_id="+this.action_id+"&instance_id="+this.instance_id;
	ajax.post(shortactionup_actionurl,params);
  },
  
  onPostSuccess : function( obj, responseText ) {
	WENJRMods.B.hide('shortactionup_commentpost_progress_' + this.elem_id);
	WENJRMods.B.show('shortactionup_comment_' + this.elem_id);
	
    var r = [];
    try {
      r = eval('(' + responseText + ')')
    } catch(e) {
      r.status = 1;
    };
    if (r.status == 0) {
	  var elem = document.createElement("DIV");
	  elem.innerHTML = r.html;
	  WENJRMods.B.ge('shortactionup_comments_'+this.elem_id).appendChild(elem);

	  // try all other instances - not good - incorrect instance
	//  if(this.instance_id != '') {
	//	for(var i=0;i<=10;i++) {
	//	  if(WENJRMods.B.ge('shortactionup_comments_'+this.action_id + '_' + i)) {
	//		elem = document.createElement("DIV");
	//		elem.innerHTML = r.html;
	//		WENJRMods.B.ge('shortactionup_comments_'+this.action_id + '_' + i).appendChild(elem);
	//	  }
	//	}
	//  }
	  
    } else {
      //error
	  // apps_show_error_message(r.err_msg ? r.err_msg: '')
    }
	
  },
  
  onPostFail : function( obj, responseText ) {
	WENJRMods.B.hide('shortactionup_commentpost_progress_' + this.elem_id);
	WENJRMods.B.show('shortactionup_comment_' + this.elem_id);
  },
  
  
  

  post_like : function(like, points, remember_choice) {
	var ajax = new WENJRMods.Ajax(this.onPostLikeSuccess.bind(this), this.onPostLikeFail.bind(this));
	var params = "task=like&like="+like+"&action_id="+this.action_id+"&instance_id="+this.instance_id;
	if(typeof points != "undefined") {
	  params += '&points='+points+'&remember='+remember_choice;
	}
	ajax.post(shortactionup_actionurl,params);
  },


  
  onPostLikeSuccess : function( obj, responseText ) {
	
    var r = [];
    try {
      r = eval('(' + responseText + ')')
    } catch(e) {
      r.status = 1;
    };
    if (r.status == 0) {
	  var elem = document.createElement("DIV");
	  elem.innerHTML = r.html;
	  WENJRMods.B.ge('shortactionup_likes_'+this.elem_id).appendChild(elem);

	  elem = WENJRMods.B.ge('shortactionup_unlike_'+this.elem_id);
	  if(elem) {
		elem.style.display = "";
	  }
	  WENJRMods.B.hide('shortactionup_ilike_'+this.elem_id);

	  // try all other instances - not good - incorrect instance
	  if(this.instance_id != '') {
		for(var i=0;i<=10;i++) {
		  if($('shortactionup_unlike_'+this.action_id + '_' + i)) {
			$('shortactionup_unlike_'+this.action_id + '_' + i).style.display = "";
			WENJRMods.B.hide('shortactionup_ilike_'+this.action_id + '_' + i);
		  }
		}
	  }
	  
    } else {
      //error
	  // apps_show_error_message(r.err_msg ? r.err_msg: '')
    }
	
  },
  
  onPostLikeFail : function( obj, responseText ) {
  },




  post_unlike : function(like) {

	var ajax = new WENJRMods.Ajax(this.onPostUnLikeSuccess.bind(this), this.onPostUnLikeFail.bind(this));
	var params = "task=unlike"+"&action_id="+this.action_id+"&instance_id="+this.instance_id;
	ajax.post(shortactionup_actionurl,params);
  },
  
  onPostUnLikeSuccess : function( obj, responseText ) {
	
    var r = [];
    try {
      r = eval('(' + responseText + ')')
    } catch(e) {
      r.status = 1;
    };
    if (r.status == 0) {
	  
	  elem = WENJRMods.B.ge('shortactionup_likeitem_'+this.elem_id+'_'+r.user_id);
	  if(elem) {
		elem.parentNode.removeChild(elem);
	  }
	  WENJRMods.B.hide('shortactionup_unlike_'+this.elem_id);
	  elem = WENJRMods.B.ge('shortactionup_ilike_'+this.elem_id);
	  if(elem) {
		elem.style.display = "";
	  }

	  if(this.instance_id != '') {
		for(var i=0;i<=10;i++) {
		  if($('shortactionup_ilike_'+this.action_id + '_' + i)) {
			$('shortactionup_ilike_'+this.action_id + '_' + i).style.display = "";
			WENJRMods.B.hide('shortactionup_unlike_'+this.action_id + '_' + i);
		  }
		}
	  }
	  
    } else {
      //error
	  // apps_show_error_message(r.err_msg ? r.err_msg: '')
    }
	
  },
  
  onPostUnLikeFail : function( obj, responseText ) {
  },




  load_likes : function(action_id) {

	var ajax = new WENJRMods.Ajax(this.onLoadLikesSuccess.bind(this), this.onLoadLikesFail.bind(this));
	var params = "task=loadlikes"+"&action_id="+this.action_id+"&instance_id="+this.instance_id;
	ajax.post(shortactionup_actionurl,params);
  },
  
  onLoadLikesSuccess : function( obj, responseText ) {
	
    var r = [];
    try {
      r = eval('(' + responseText + ')')
    } catch(e) {
      r.status = 1;
    };
    if (r.status == 0) {
	  WENJRMods.B.ge('shortactionup_likes_'+this.elem_id).innerHTML = r.html;
    } else {
      //error
	  // apps_show_error_message(r.err_msg ? r.err_msg: '')
    }
	
  },
  
  onLoadLikesFail : function( obj, responseText ) {
  },
  

  load_comments : function(action_id) {

	var ajax = new WENJRMods.Ajax(this.onLoadCommentsSuccess.bind(this), this.onLoadCommentsFail.bind(this));
	var params = "task=loadcomments"+"&action_id="+this.action_id+"&instance_id="+this.instance_id;
	ajax.post(shortactionup_actionurl,params);
  },
  
  onLoadCommentsSuccess : function( obj, responseText ) {
	
    var r = [];
    try {
      r = eval('(' + responseText + ')')
    } catch(e) {
      r.status = 1;
    };

    if (r.status == 0) {

	  WENJRMods.B.ge('shortactionup_comments_'+this.elem_id).innerHTML = r.html;

    } else {
      //error
	  // apps_show_error_message(r.err_msg ? r.err_msg: '')
    }
	
  },
  
  onLoadCommentsFail : function( obj, responseText ) {
  }
  
  
  
}


function shortactionup_autogrow(elementid) {
  var el = WENJRMods.Browser.ge(elementid);
  if(!el) WENJRMods.Utils.debug("textarea_autogrow(): element not found");
  if(el && !el._controlled) {
	el._controlled = true;
	el._controller = new WENJRMods.TextAreaControl(el);
	el._controller.setAutoGrow(true);
	return el._controller;
  }
  return false;
};

