/**
 * @author Ton Sharp <forma@66ton99.org.ua>
 */

var wall_global_image_id = 0;
var wall_comment_global_bj_id = 0;

function URLEncode(c) {
	var o='';
	var x=0;
	c=c.toString();
	var r=/(^[a-zA-Z0-9_.]*)/;
	var s=/(^[?&+]*)/;
	while (x<c.length) {
		var m=r.exec(c.substr(x));
		var ss=s.exec(c.substr(x));
		if (m!=null && m.length>1 && m[1]!='') {
			o+=m[1];x+=m[1].length;
		} else {
			if(c[x]==' ') { 
				o+='+';
			} else {
			  var d=c.charCodeAt(x);
			  if (d < 128) {
  				var h=d.toString(16);
  				o+='%'+(h.length<2?'0':'')+h.toUpperCase();
  			} else {
  			  o+=c.charAt(x);
  			}
			}
			x++;
		}
	}
	return o;
}

function switchText() {
  if (jQuery(this).val() == jQuery(this).attr('title')) {
    jQuery(this).val('').removeClass('exampleInputText');
  } else if (jQuery.trim(jQuery(this).val()) == '') {
    jQuery(this).addClass('exampleInputText').val(jQuery(this).attr('title'));
  }
}

function remove_comment(comment_data, comment_resp) {
  if ("success" != comment_resp ||
      !comment_data.result ||
      'OK' != comment_data.result ||
      !comment_data.id)
  {
    return false;
  }
  jQuery('#comment_' + comment_data.id).remove();
  jQuery('#comment_separatorLine_' + comment_data.id).remove();
}

function delete_comment(e) {
  if (!confirm(jQuery(e).attr('title') + '?')) return false;
  
  jQuery.ajax({
    type: "POST",
    url: jQuery(e).attr('href') + '?sf_format=js',
    data: "",
    dataType: "json",
    success: remove_comment
  });
  
  return false;
}

function remove_wall_record(wall_row_data, wall_row_resp) {
  if ("success" != wall_row_resp ||
      !wall_row_data.result ||
      'OK' != wall_row_data.result ||
      !wall_row_data.id)
  {
    return false;
  }
  jQuery('#wall_row_' + wall_row_data.id).remove();
}

function delete_wall_record(e) {
  if (!confirm(jQuery(e).attr('title') + '?')) return false;
  
  jQuery.ajax({
    type: "POST",
    url: jQuery(e).attr('href') + '?sf_format=js',
    data: "",
    dataType: "json",
    success: remove_wall_record
  });
  
  return false;
}

function wall_comment_displayMessage(wall_comment_data, wall_comment_resp)
{
  if ("success" != wall_comment_resp || -1 >= wall_comment_data.indexOf('<!--OK-Responce-->')) return false;
  jQuery("#comment_mess_" + wall_comment_global_bj_id).remove();
  jQuery("#comment_content_" + wall_comment_global_bj_id).prepend(wall_comment_data);
}

function wall_comment_click_fn(obj_id_param) {
  wall_comment_global_bj_id = obj_id_param;
  current_obj_id = "#wall_comment_text_" + wall_comment_global_bj_id;
  wall_comment_text = jQuery(current_obj_id).val();
  
  if (!wall_comment_text || wall_comment_text == jQuery(current_obj_id).attr('title')) {
    return false;
  }
  jQuery.ajax({
    type: "POST",
    url: wall_comment_post_action_url + "?sf_format=js&ob=" + wall_comment_global_bj_id,
    data: "text=" + URLEncode(wall_comment_text),
    dataType: "html",
    success: wall_comment_displayMessage
  });
  
  jQuery(current_obj_id).val(jQuery(current_obj_id).attr('title'));
  jQuery(current_obj_id).addClass('exampleInputText');
  return false;
}

function wall_displayMessage(wall_data, wall_resp)
{
  if ("success" != wall_resp || -1 >= wall_data.indexOf('<!--OK-Responce-->')) return false;
  jQuery("#wall_form").after(wall_data);
  jQuery('#wall_image').css('display', 'none');
  jQuery('#wall_image').attr('src', '');
  wall_global_image_id = 0;
}

jQuery(document).ready(function() {
  if (jQuery("#wall_message").length) {
    jQuery("#wall_message").limit(512);
    jQuery("#wall_message").each(function() {
      if (jQuery.trim(jQuery(this).val()) == '') jQuery(this).val(jQuery(this).attr('title'));
      if (jQuery(this).val() == jQuery(this).attr('title')) jQuery(this).addClass('exampleInputText');
    }).focus(switchText).blur(switchText);
  }
  jQuery("#wall_submit").click(function() {
    wall_message_text = jQuery("#wall_message").val();
    if ((!wall_message_text || wall_message_text == jQuery("#wall_message").attr('title')) && !wall_global_image_id) {
      return false;
    }
    //alert(URLEncode(wall_message_text));
    jQuery.ajax({
      type: "POST",
      url: wall_post_action_url + "?sf_format=js",
      data: "message=" + URLEncode(wall_message_text) + "&owner=" + wall_member_id + "&mi=" + wall_global_image_id, 
      dataType: "html",
      success: wall_displayMessage
    });
    jQuery("#wall_message").val(jQuery("#wall_message").attr('title'));
    jQuery("#wall_message").addClass('exampleInputText');
    return false;
  });
  
  jQuery("#wall_image_button").click(function() {
    mcImageManager.browse({select_img : true, insert_filter : function(data) {
      wall_global_image_id = data.custom.mediaitem_id;
      jQuery('#wall_image').css('display', 'block');
      jQuery('#wall_image').attr('src', data.custom.thumbnail_default + '?p=' + (new Date().getTime()));
      
    }});
  });
  
  jQuery(".wall_more_link").click(function() {
    wall_more_records_id = jQuery(this).attr('more_id');
    jQuery("#wall_more_records_" + wall_more_records_id).css('display', 'block');
    jQuery(this).css('display', 'none');
  });
  
  jQuery(".wall_comments_more_link").click(function() {
    wall_more_comments_id = jQuery(this).attr('more_id');
    jQuery("#wall_more_comments_" + wall_more_comments_id).css('display', 'block');
    jQuery(this).css('display', 'none');
  });
  
});
