var n_cache = new Array();
var c_cache = new Array();
var comm_id;
var comm_edit_id;
var s_id;
var e_id;
var is_adding_com = false;

function IPMenu(m_ip, l1, l2, l3) {
	var menu = new Array();
	menu[0] = '<a href="http://www.nic.ru/whois/?ip=' + m_ip + '" target="_blank">' + l1 + '</a>';
	menu[1] = '<a href="' + dle_root + dle_admin + '?mod=iptools&ip=' + m_ip + '" target="_blank">' + l2 + '</a>';
	menu[2] = '<a href="' + dle_root + dle_admin + '?mod=blockip&ip=' + m_ip + '" target="_blank">' + l3 + '</a>';
	return menu;
};

function ajax_cancel_for_edit(news_id) {
	if (n_cache[news_id] != "") {
		$("#news-id-" + news_id).html(n_cache[news_id]);
	}

	return false;
};

function ajax_save_for_edit(news_id, event) {
	var allow_br = 0;
	var news_txt = '';
	e_id = news_id;

	if (document.getElementById('allow_br_' + news_id).checked) {
		allow_br = 1;
	}

	if (quick_wysiwyg == "1") {

		news_txt = $('#dleeditnews' + news_id).html();

	} else {

		news_txt = $('#dleeditnews' + news_id).val();

	}

	var news_title = $('#edit-title-' + news_id).val();
	var reason = $('#edit-reason-' + news_id).val();

	$.post(dle_root + "engine/ajax/editnews.php", {
		title: news_title,
		news_txt: news_txt,
		id: news_id,
		allow_br: allow_br,
		reason: reason,
		field: event,
		action: "save"
	}, function (data) {

		n_cache[e_id] = '';
		$("#news-id-" + news_id).html(data);

	});

	return false;
};


function ajax_prep_for_edit(news_id, event) {
	if (!n_cache[news_id] || n_cache[news_id] == '') {
		n_cache[news_id] = $('#news-id-' + news_id).html();
	}

	s_id = news_id;

	$.get(dle_root + "engine/ajax/editnews.php", {
		id: news_id,
		field: event,
		action: "edit"
	}, function (data) {

		$("#news-id-" + news_id).html(data);

		setTimeout(function () {
			$("html:not(:animated)" + (!$.browser.opera ? ",body:not(:animated)" : "")).animate({
				scrollTop: $("#news-id-" + s_id).position().top - 70
			}, 700);
		}, 100);

	});

	return false;
};


function MenuCommBuild(m_id) {
	var menu = new Array();
	menu[0] = '<a onclick="ajax_comm_edit(\'' + m_id + '\'); return false;" href="#">' + menu_short + '</a>';
	menu[1] = '<a href="' + dle_root + '?do=comments&action=comm_edit&id=' + m_id + '">' + menu_full + '</a>';
	return menu;
};

function ajax_comm_edit(c_id, area) {
	if (!c_cache[c_id] || c_cache[c_id] == '') {
		c_cache[c_id] = document.getElementById('comm-id-' + c_id).innerHTML;
	}
	comm_id = c_id;
	var data = {
		id: c_id,
		area: area,
		action: 'edit'
	};

	$.get(dle_root + "engine/ajax/editcomments.php", data, function (data) {
		$('#comm-id-' + c_id).html(data);
		
		setTimeout(function () {
			$("html:not(:animated)" + (!$.browser.opera ? ",body:not(:animated)" : "")).animate({
				scrollTop: $("#comm-id-" + c_id).position().top - 70
			}, 700);
		}, 100);
		
	});

	return false;
};


function ajax_cancel_comm_edit(c_id) {
	if (n_cache[c_id] != "") {
		document.getElementById('comm-id-' + c_id).innerHTML = c_cache[c_id];
	}
	return false;
};


function ajax_save_comm_edit(c_id, area) {
	var comm_txt = '';
	comm_edit_id = c_id;

	if (dle_wysiwyg == "yes") {

		comm_txt = $('#dleeditcomments' + c_id).html();

	} else {

		comm_txt = $('#dleeditcomments' + c_id).val();

	}

	$.post(dle_root + "engine/ajax/editcomments.php", {
		id: c_id,
		comm_txt: comm_txt,
		area: area,
		action: "save"
	}, function (data) {

		c_cache[comm_edit_id] = '';
		$("#comm-id-" + c_id).html(data);

	});
	return false;
};

function doFavorites(fav_id, event) {
	$.post(dle_root + 'engine/ajax/favorites.php?fav_id=' + fav_id + '&action=' + event + '&skin=' + dle_skin, function (data) {
		$("#fav-id-" + fav_id).html(data);
	});
	return false;
};

function ShowBild(sPicURL) {
	window.open(dle_root + 'engine/modules/imagepreview.php?image=' + sPicURL, '', 'resizable=1,HEIGHT=200,WIDTH=200, top=0, left=0, scrollbars=yes');
};

function doAddComments() {
	var form = document.getElementById('dle-comments-form');
	var options = {
		comments: form.comments.value,
		name: form.name.value,
		mail: form.mail.value,
		skin: dle_skin,
		post_id: form.post_id.value
	};

	if (form.comments.value == '' || form.name.value == '') {
		alert(dle_req_field);
		return false;
	}

	if (form.sec_code) {
		options.sec_code = form.sec_code.value;
	}

	if (is_adding_com == false) {
		is_adding_com = true;
		$("#adc").attr('disabled', 'disabled').addClass('process');
		$.post(dle_root + "engine/ajax/addcomments.php", options, function (data) {
			$('#dle-ajax-comments').html(data);
			is_adding_com = false;
			$("#adc").removeClass('process').removeAttr('disabled');
		});
	}
	return false;
};

function dle_copy_quote(qname) {
	dle_txt = '';
	if (document.getSelection) {
		dle_txt = document.getSelection();
	} else if (document.selection) {
		dle_txt = document.selection.createRange().text;
	}
	if (dle_txt.replace(" ", "") != "") {
		dle_txt = '[quote=' + qname + ']' + dle_txt + '[/quote]\n';
	}
};

function dle_ins(name) {
	var input = document.getElementById('dle-comments-form').comments;
	var finalhtml = "";
	if (dle_wysiwyg == "no") {
		if (dle_txt != "") {
			input.value += dle_txt;
		} else {
			input.value += "[b]" + name + "[/b]," + "\n";
		}
	} else {
		if (dle_txt != "") {
			finalhtml = dle_txt;
		} else {
			finalhtml = "<b>" + name + "</b>," + "<br />";
		}
		tinyMCE.execInstanceCommand('comments', 'mceInsertContent', false, finalhtml, true)
	}
};

function SpoilerShowOrHide(d1, d2) {
	if (d1 != '') {
		DoDiv(d1);
	}
	if (d2 != '') {
		DoDiv(d2);
	}
};

function ShowOrHide(d1) {
	if (d1 != '') DoDiv(d1);
};

function DoDiv(id) {
	var item = null;
	if (document.getElementById) {
		item = document.getElementById(id);
	} else if (document.all) {
		item = document.all[id];
	} else if (document.layers) {
		item = document.layers[id];
	}
	if (!item) {} else if (item.style) {
		if (item.style.display == "none") {
			item.style.display = "";
		} else {
			item.style.display = "none";
		}
	} else {
		item.visibility = "show";
	}
};

function ckeck_uncheck_all() {
	var frm = document.pmlist;
	for (var i = 0; i < frm.elements.length; i++) {
		var elmnt = frm.elements[i];
		if (elmnt.type == 'checkbox') {
			if (frm.master_box.checked == true) {
				elmnt.checked = false;
			} else {
				elmnt.checked = true;
			}
		}
	}
	if (frm.master_box.checked == true) {
		frm.master_box.checked = false;
	} else {
		frm.master_box.checked = true;
	}
};

function confirmDelete(url) {
	var agree = confirm(dle_del_agree);
	if (agree) document.location = url;
};

function dle_smiley(text) {
	doInsert(' ' + text + ' ', '', false);
	document.getElementById('dle_emo').style.visibility = "hidden";
	document.getElementById('dle_emo').style.display = "none";
	ie_range_cache = null;
};

function DlePageCompleted() {
	hideBusyLayer();
	var post_main_obj = document.getElementById('dle-content');
	var post_box_top = _get_obj_toppos(post_main_obj);
	if (post_box_top) {
		scroll(0, post_box_top - 70);
	}
};

function setNewField(which, formname) {
	if (which != selField) {
		allcleartags();
		fombj = formname;
		selField = which;
	}
};

function UserMenu(url, m_id, group) {
	var menu = new Array();
	menu[0] = '<a ' + url + ' >' + menu_profile + '</a>';
	menu[1] = '<a href="' + dle_root + 'index.php?do=pm&doaction=newpm&user=' + m_id + '">' + menu_send + '</a>';
	menu[2] = '<a href="' + dle_root + 'index.php?do=lastcomments&userid=' + m_id + '">' + menu_fcomments + '</a>';
	if (group == '1') {
		menu[3] = '<a onclick="window.open(\'' + dle_root + dle_admin + '?mod=editusers&action=edituser&id=' + m_id + '\', \'User\',\'toolbar=0,location=0,status=0, left=0, top=0, menubar=0,scrollbars=yes,resizable=0,width=540,height=500\'); return false;" href="#">' + menu_uedit + '</a>';
	}
	return menu;
};

function UserNewsMenu(url, news_url, m_id, group) {
	var menu = new Array();
	menu[0] = '<a ' + url + ' >' + menu_profile + '</a>';
	menu[1] = '<a ' + news_url + ' >' + menu_fnews + '</a>';
	menu[2] = '<a href="' + dle_root + 'index.php?do=pm&doaction=newpm&username=' + m_id + '">' + menu_send + '</a>';
	if (group == '1') {
		menu[3] = '<a onclick="window.open(\'' + dle_root + dle_admin + '?mod=editusers&action=edituser&user=' + m_id + '\', \'User\',\'toolbar=0,location=0,status=0, left=0, top=0, menubar=0,scrollbars=yes,resizable=0,width=540,height=500\'); return false;" href="#">' + menu_uedit + '</a>';
	}
	return menu;
};

function dle_news_delete(m_id) {
	var agree = confirm(dle_del_agree);
	if (agree) document.location = dle_root + 'index.php?do=deletenews&id=' + m_id + '&hash=' + dle_login_hash;
};

function MenuNewsBuild(m_id, event) {
	var menu = new Array();
	menu[0] = '<a onclick="ajax_prep_for_edit(\'' + m_id + '\', \'' + event + '\'); return false;" href="#">' + menu_short + '</a>';
	menu[1] = '<a href="' + dle_root + dle_admin + '?mod=editnews&action=editnews&id=' + m_id + '" target="_blank">' + menu_full + '</a>';
	if (allow_dle_delete_news) {
		menu[2] = '<a onclick="dle_news_delete (\'' + m_id + '\'); return false;" href="#">' + dle_del_news + '</a>';
	}
	return menu;
};


function show_comment(id, ukaz) {
	var obj = document.getElementById(id);
	obj.style.display = 'block';
	ukaz.style.display = 'none';
}

function change_method(id) {
	var hdd = document.getElementById('hddfoto');
	var url = document.getElementById('urlfoto');
	if (id == 'hddfoto') {
		hdd.style.display = 'block';
		url.style.display = 'none';
	} else {
		url.value = '';
		hdd.style.display = 'none';
		url.style.display = 'block';
	}
}

function add_mirror() {
	var element = document.createElement('div');
	element.innerHTML = '<input type="text" name="mirrors[]">';
	document.getElementById('miri').appendChild(element);
}

function mir_check() {
	var main = document.getElementById('main_mir');
	if (main.value == '') {
		main.focus();
		return false;
	} else {
		return true;
	}
}

function sh_miradd() {
	var block = document.getElementById('addform').style;
	if (block.display == 'none') {
		block.display = 'block';
	} else {
		block.display = 'none';
	}
}

function mir_delete(id) {
	$.post(dle_root + 'engine/ajax/mirror_del.php', {
		id: id
	}, function (data) {
		$('tr#' + id).fadeOut('fast');
	});
}

function DeletePm(pmid) {
	$.post(dle_root + 'engine/ajax/delete_pm.php', {
		pmid: pmid
	}, function (data) {
		$('#pm' + pmid).fadeOut('fast');
	});
}

function ChangeRating(go_rate, news_id, rate) {
	if (go_rate == 1) {
		rate--;
	} else if (go_rate == 2) {
		rate++;
	} else if (go_rate == 3) {
		rate++;
		rate++;
	} else {
		rate;
	}
	$('#ratig-layer-' + news_id).html('<b>' + rate + '</b>');
	$.post(dle_root + 'engine/ajax/rating.php', {
		'go_rate': go_rate,
		'news_id': news_id
	});
}

function doComent(rate, id, author) {
	$.post(dle_root + 'engine/ajax/comrate.php', {
		'com_id': id,
		'author': author,
		'rate': rate
	}, function (data) {
		$('#comrate_' + id).html(data);
	});
}

function topajax(cat_id) {
	$.post(dle_root + 'engine/ajax/topajax.php', {
		'cat_id': cat_id
	}, function (data) {
		$('#topajax').html(data);
	});
}

function CheckLogin() {
	$.post(dle_root + 'engine/ajax/registration.php', {
		'name': $("#name").val()
	}, function (data) {
		$('#result-registration').html(data);
	});
}
