
Element.implement({
	sep_nfa_show: function() {
		this.setStyle('display','block');
	},
	sep_nfa_hide: function() {
		this.setStyle('display','none');
	},
	sep_nfa_visible: function() {
		if(this.getStyle('display') == 'none') {
			return false;
		}
		else {
			return true;
		}
	},
	sep_nfa_toggle: function() {
		if(this.getStyle('display') == 'none') {
			this.sep_nfa_show();
		}
		else {
			this.sep_nfa_hide();
		}
	},
	sep_nfa_update: function(html) {
		this.innerHTML = html;
	}
});

function SEP_NewsfeedAdvanced_post(action_id, type) {
	data_string = '';
	if(type == 3) {
		$$('form[id=comment_post_form_'+action_id+']').each(
      function(item, index){
        try{
        data_temp = item.toQueryString();
        if (data_temp.length > 5) data_string = item.toQueryString();
        }catch(err){}
      }
    );
	}

	myreq2 = new Request({
		method: 'post',
		url: 'http://ogeno.com/user_newsfeed_advanced_ajax.php',
		data: '_ajaxReq=1&task=post&type='+type+'&action_id='+action_id+'&' + data_string,
		onSuccess: function(html){
      $$('div[id=ajax_inject_new_post_'+action_id+']').each(function(item,index){
        item.clone().injectBefore(item).sep_nfa_update(html.replace(/\\n/g, '<br>'));
      });
      $$('form[id=comment_post_form_'+action_id+']').each(function(item,index){
        item.reset();
      });
      if(type == 1 || type == 2) {
        $$('span[id=likes_buttons_'+action_id+']').each(function(item,index){
        item.sep_nfa_hide();
      });//.sep_nfa_hide();
      }
    },
		onFailure: function() {alert('Could not save your comment! Please try again!');}
	}).send();

}

function SEP_NewsfeedAdvanced_show_edit_form(id) {
	if($('SEP_NewsfeedAdvanced_Edit_'+id).sep_nfa_visible()) {
		SEP_NewsfeedAdvanced_hide_edit_form(id);
	}
	else {
  	$$('span[id=SEP_NewsfeedAdvanced_Text_'+id+']').each(function(elm){ elm.sep_nfa_hide();});//sep_nfa_hide();
  	$$('div[id=SEP_NewsfeedAdvanced_Edit_'+id+']').each(function(elm){ elm.sep_nfa_show();});//sep_nfa_hide();
  	//$$('textarea[id=SEP_NewsfeedAdvanced_Textarea_Edit_'+id+']').each(function(elm){ elm.focus();});//sep_nfa_hide();
	}
}

function SEP_NewsfeedAdvanced_hide_edit_form(id) {
  $$('div[id=SEP_NewsfeedAdvanced_Edit_'+id+']').each(function(elm){ elm.sep_nfa_hide();});//sep_nfa_hide();
  $$('span[id=SEP_NewsfeedAdvanced_Text_'+id+']').each(function(elm){ elm.sep_nfa_show();});//sep_nfa_hide();
}

function SEP_NewsfeedAdvanced_edit(id, type) {
	data_string = '';
	if(type == 3) {
		$$('form[id=comment_edit_form_'+id+']').each(
      function(item, index){
        try{
        data_temp = item.toQueryString();
        if (data_temp.length > 5) data_string = item.toQueryString();
        }catch(err){}
      }
    );
	}

	myreq2 = new Request({
		method: 'post',
		url: 'http://ogeno.com/user_newsfeed_advanced_ajax.php',
		data: '_ajaxReq=1&task=edit&type='+type+'&id='+id+'&' + data_string,
		onSuccess: function(html){
      $$('span[id=SEP_NewsfeedAdvanced_Text_'+id+']').each(function(elm){ elm.sep_nfa_update(html.replace(/\\n/g, '<br>'));});//sep_nfa_hide();
      $$('div[id=SEP_NewsfeedAdvanced_Edit_'+id+']').each(function(elm){ elm.sep_nfa_hide();});//sep_nfa_hide();
      $$('span[id=SEP_NewsfeedAdvanced_Text_'+id+']').each(function(elm){ elm.sep_nfa_show();});//sep_nfa_hide();
    },
		onFailure: function() {alert('Could not save your changes! Please try again!');}
	}).send();
}


function SEP_NewsfeedAdvanced_delete(id) {
	myreq2 = new Request({
		method: 'post',
		url: 'http://ogeno.com/user_newsfeed_advanced_ajax.php',
		data: '_ajaxReq=1&task=delete&id='+id,
		onSuccess: function(html){
        $$('div[id=SEP_NewsfeedAdvanced_BoxItem_'+id+']').each(function(item,index){
        item.destroy();
      });
    },
		onFailure: function() {alert('Could not delete this comment! Please try again!');}
	}).send();
}

function SEP_NewsfeedAdvanced_report(id) {
	myreq2 = new Request({
		method: 'post',
		url: 'http://ogeno.com/user_newsfeed_advanced_ajax.php',
		data: '_ajaxReq=1&task=report&id='+id,
		onSuccess: function(){
        $$('span[id=SEP_NewsfeedAdvanced_ReportButton_'+id+']').each(function(item,index){
        item.destroy();
      });
      alert('Thank you for reporting this comment!');
    },
		onFailure: function() {alert('Could not send your report! Please try again!');}
	}).send();
}

function SEP_NewsfeedAdvanced_toggle_box(action_id, display) {
		if(display == 'preview') {
			$$('div[id=SEP_NewsfeedAdvanced_Box_Final_'+action_id+']').each(
        function(item,index){
          item.sep_nfa_hide();
        });
			if(SEP_NewsfeedAdvanced_AutoGrow_Instance) {
  			$$('textarea[id=SEP_NewsfeedAdvanced_Textarea_'+action_id+']').each(
          function(item,index){
            item.setStyle('height', SEP_NewsfeedAdvanced_AutoGrow_Instance.textarea_old_height);
          });
			}
			$$('div[id=SEP_NewsfeedAdvanced_Box_Preview_'+action_id+']').each(
        function(item,index){
          item.sep_nfa_show();
        });
		}
		else {
			$$('div[id=SEP_NewsfeedAdvanced_Box_Preview_'+action_id+']').each(
        function(item,index){
          item.sep_nfa_hide();
        });
			$$('div[id=SEP_NewsfeedAdvanced_Box_Final_'+action_id+']').each(
        function(item,index){
          item.sep_nfa_show();
        });
			//$$('SEP_NewsfeedAdvanced_Box_Final_'+action_id).each(function(item,index){item.sep_nfa_show()});//.sep_nfa_show();
      /*
			$$('textare[id=SEP_NewsfeedAdvanced_Textarea_'+action_id+']').each(
        function(item,index){
          item.focus();
        });//.focus();
        */
		}
}

function SEP_NewsfeedAdvanced_ScrollTo(action_id) {
	offset = 300;
	div_position_y = $('SEP_NewsfeedAdvanced_Box_Final_'+action_id).getPosition().y;
	window.scrollTo(0, div_position_y-offset);
}

function SEP_NewsfeedAdvanced_Show_Hidden_Boxes(action_id) {
	$$('div[id=SEP_NewsfeedAdvanced_ShowMoreButton_'+action_id+']').each(function(elm){ elm.sep_nfa_hide();});//sep_nfa_hide();
	$$('div[id=SEP_NewsfeedAdvanced_Box_Hidden_'+action_id+']').each(function(elm){ elm.sep_nfa_show();});
}

function SEP_NewsfeedAdvanced_Check_MaxLength(field_id, max_length) {
	if($(field_id).value.length	> max_length) {
		alert('Your comment is too long!');
		return false;
	}

	return true;
}


function SEP_NewsfeedAdvanced_ItemOptions(mode, id) {
	if(mode == 'show') {
  	$$('div[id=SEP_NewsfeedAdvanced_Box_Header_Right_Inactive_'+id+']').each(function(elm){
      elm.sep_nfa_hide();});//sep_nfa_hide();
  	$$('div[id=SEP_NewsfeedAdvanced_Box_Header_Right_Active_'+id+']').each(function(elm){ elm.sep_nfa_show();});
	}
	else {
  	$$('div[id=SEP_NewsfeedAdvanced_Box_Header_Right_Active_'+id+']').each(function(elm){ elm.sep_nfa_hide();});
  	$$('div[id=SEP_NewsfeedAdvanced_Box_Header_Right_Inactive_'+id+']').each(function(elm){elm.sep_nfa_show();});//sep_nfa_hide();
	}
}



/*
 * Copyright (c) 2009 Gary Glass (www.bookballoon.com)
 * Modified by SEplugins
 */
var SEP_NewsfeedAdvanced_AutoGrow_Instance = null;
var AutoGrow = new Class({

	Implements: [Options, Events],

	options: {
		interval: 333, // update interval in milliseconds
		margin: 30, // gap (in px) to maintain between last line of text and bottom of textarea
		minHeight: 20 // minimum height of textarea
	},

	periodical_timer: null,
	textarea_old_height: 0,

	initialize: function(textarea, options) {
		this.textarea = $(textarea);
		this.options.minHeight = textarea.clientHeight;
		this.setOptions(options);
		this.dummy =  new Element("div", {
			styles:	{
				"overflow-x" : "hidden",
				"position"   : "absolute",
				"top"        : 0,
				"left"       : "-9999px"
			}
		}).setStyles(this.textarea.getStyles("font-size", "font-family", "width", "line-height", "padding")).inject(document.body);
		this.textarea_old_height = this.textarea.getStyle("height");
		this.resize();
		this.start();
	},

	resize: function() {
		var html = this.textarea.get('value').replace(/\n|\r\n/g, '<br>X');
		if (this.dummy.get("html").toLowerCase() != html.toLowerCase()){
			this.dummy.set("html", html);
			var triggerHeight = this.dummy.getSize().y + this.options.margin;
			if (this.textarea.clientHeight != triggerHeight)
			{
				var newHeight = Math.max(this.options.minHeight, triggerHeight);
				this.textarea.tween("height", newHeight);
			}
		}
	},

	start: function() {
		this.periodical_timer = this.resize.periodical(this.options.interval, this);
	},

	stop: function() {
		$clear(this.periodical_timer);
	}



});

