var last_opened;
var last_from;
var citate_on_comment_id = 0; 

String.prototype.replaceAll = function(search, replace){
	  return this.split(search).join(replace);
	}


function come_to_daddy(babie){
	if ($.browser.msie) {
		var p = $(babie).attr('parentNode');
		while ($(p).attr('tagName') != 'DIV'){
			p = $(p).attr('parentNode');
		}
		return p;
	}
	else {
		var p = babie.parentNode;
		while (p.tagName != 'DIV'){
			p = p.parentNode;
		}
		return p;
	}
}

function find_babie(daddy){
	for(i=0; i < daddy.childNodes.length; i++){
		if (daddy.childNodes[i].tagName == 'DIV'){
			break;
		}
	}
	return daddy.childNodes[i];
}

function close_open(from, comment_id){
	citate_on_comment_id = comment_id;
	$("#citate_link").show();
	$("#id_body").attr('value', '');
	parent_div = come_to_daddy(from);
	var babie = find_babie(parent_div);
	var cf = document.getElementsByTagName('DIV')['comment-form'];
	if (babie.style.display == 'none'){
		if (last_opened) {
			$(last_opened).hide('slow');
			last_from.innerHTML = 'ответить';
			babie.innerHTML = last_opened.innerHTML;
			last_opened.innerHTML = '';
		}
		else {
			$(cf).hide('slow');
			from.innerHTML = 'ответить';
			babie.innerHTML = cf.innerHTML;
			cf.innerHTML = '';
		}
		$(babie).show('slow');
		from.innerHTML = 'отменить';
		last_opened = babie;
		last_from = from;
	}
	else{
		$(babie).hide('slow');
		from.innerHTML = 'ответить';
		cf.innerHTML = last_opened.innerHTML;
		babie.innerHTML = '';
		$(cf).show('slow');
		last_opened = null;
	}
}

function GetCitate() {
	var text = $("#text_of_comment_for_citate_"+citate_on_comment_id);
	set_html = text.html();
	set_html = set_html.replaceAll('&gt;', '>');
	set_html = set_html.replaceAll('[n]', '\n');
	$("#id_body").attr('value', set_html);
}

function cf_submit(){
	if ($.browser.msie) {
		//var cf_form = document.all.getElementsByTagName('FORM')['comment-form-itself'];
		var cf_form = $("#comment-form-itself").attr('elements');
	}
	else {
		var cf_form = document.getElementsByTagName('FORM')['comment-form-itself'];
	}
	
    parent_div = come_to_daddy(cf_form);
    if (parent_div.id != "comment-form"){
        cf_form.elements['comment_on'].value = parent_div.id;
    }
    cf_form.submit();
}

function getTop(inputObj)
{
  var returnValue = inputObj.offsetTop + inputObj.offsetHeight;
  while((inputObj = inputObj.offsetParent) != null)returnValue += inputObj.offsetTop;
  return returnValue;
}

function getLeft(inputObj)
{
  var returnValue = inputObj.offsetLeft;
  while((inputObj = inputObj.offsetParent) != null)returnValue += inputObj.offsetLeft;
  return returnValue;
}


var cur_x = 0;
var cur_y = 0;
var t;

function trig_close_control(){
	author_control.style.display = "none";
}

function trig_author_control(control, ip, email, username, id){
	clearTimeout(t);
	var form = document.getElementsByTagName('FORM')['banform'];
	form.elements['ip'].value = ip;
	//form.elements['email'].value = email;
	form.elements['username'].value = username;
	form.elements['next'].value = '/admin/comments/commentnode/' + id + '/delete/';
	form.reset();
	var author_control = document.getElementsByTagName('DIV')['author_control'];
	author_control.style.left = getLeft(control) + "px";
	author_control.style.top = getTop(control) + "px";
	if (!(author_control.style.display) || (author_control.style.display == "none")){
		author_control.style.display = "block";
	}
	t = setTimeout('trig_close_control()', 2000);
}