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

function IPMenu( m_ip, l1, l2, l3 ){

var menu=new Array();

menu[0]='<a href="https://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 ] != "" )
	{
		document.getElementById( 'news-id-'+news_id ).innerHTML = n_cache[ news_id ];
	}
	
	return false;
};

function whenCompletedSave(){
n_cache[ e_id ] = '';
};

function ajax_save_for_edit( news_id, event )
{
	var ajax = new dle_ajax();
	var allow_br = 0;
	var news_txt = '';

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

	e_id = news_id;
	ajax.onShow ('');

	if (quick_wysiwyg == "1") {

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

	} else {

		news_txt = ajax.encodeVAR( document.getElementById('dleeditnews'+news_id).value );

	}

	var news_title = ajax.encodeVAR( document.getElementById('edit-title-'+news_id).value );

	var varsString = "news_txt=" + news_txt;
	ajax.setVar("id", news_id);
	ajax.setVar("allow_br", allow_br);
	ajax.setVar("title", news_title);
	ajax.setVar("reason", ajax.encodeVAR( document.getElementById('edit-reason-'+news_id).value ));
	ajax.setVar("field", event);
	ajax.setVar("action", "save");
	ajax.requestFile = dle_root + "engine/ajax/editnews.php";
	ajax.method = 'POST';
	ajax.element = 'news-id-'+news_id;
	ajax.onCompletion = whenCompletedSave;
	ajax.sendAJAX(varsString);

	return false;
};

function whenCompleted(){

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


};

function ajax_prep_for_edit( news_id, event )
{
	if ( ! n_cache[ news_id ] || n_cache[ news_id ] == '' )
	{
		n_cache[ news_id ] = document.getElementById( 'news-id-'+news_id ).innerHTML;
	}

	var ajax = new dle_ajax();
	s_id = news_id;
	ajax.onShow ('');
	var varsString = "";
	ajax.setVar("id", news_id);
	ajax.setVar("field", event);
	ajax.setVar("action", "edit");
	ajax.requestFile = dle_root + "engine/ajax/editnews.php";
	ajax.method = 'GET';
	ajax.element = 'news-id-'+news_id;
	ajax.execute = true;
	ajax.onCompletion = whenCompleted;
	ajax.sendAJAX(varsString);

	return false;
};


function whenCompletedCommentsEdit(){

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


};

function MenuCommBuild( m_id, area ){

var menu=new Array();

menu[0]='<a onclick="ajax_comm_edit(\'' + m_id + '\', \'' + area + '\'); return false;" href="#">' + menu_short + '</a>';
menu[1]='<a href="' + dle_root + '?do=comments&action=comm_edit&id=' + m_id + '&area=' + area + '">' + 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;
	}

	var ajax = new dle_ajax();
	comm_id = c_id;
	ajax.onShow ('');
	var varsString = "";
	ajax.setVar("id", c_id);
	ajax.setVar("area", area);
	ajax.setVar("action", "edit");
	ajax.requestFile = dle_root + "engine/ajax/editcomments.php";
	ajax.method = 'GET';
	ajax.element = 'comm-id-'+c_id;
	ajax.execute = true;
	ajax.onCompletion = whenCompletedCommentsEdit;
	ajax.sendAJAX(varsString);
	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 whenCompletedSaveComments(){
c_cache[ comm_edit_id ] = '';
}

function ajax_save_comm_edit( c_id, area )
{
	var ajax = new dle_ajax();
	var comm_txt = '';

	comm_edit_id = c_id;
	ajax.onShow ('');

	if (dle_wysiwyg == "yes") {

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

	} else {

		comm_txt = ajax.encodeVAR( document.getElementById('dleeditcomments'+c_id).value );

	}

	var varsString = "comm_txt=" + comm_txt;

	ajax.setVar("id", c_id);
	ajax.setVar("area", area);
	ajax.setVar("action", "save");
	ajax.requestFile = dle_root + "engine/ajax/editcomments.php";
	ajax.method = 'POST';
	ajax.element = 'comm-id-'+c_id;
	ajax.onCompletion = whenCompletedSaveComments;
	ajax.sendAJAX(varsString);

	return false;
};

function whenCommentsDeleted( r ) {

	r = parseInt(r);

	if (!isNaN(r)) {

		$("html"+( ! $.browser.opera ? ",body" : "")).animate({scrollTop: $("#comment-id-" + r).position().top - 70}, 700);

		setTimeout(function() { $("#comment-id-" + r).hide('blind',{},1400)}, 700);


	}


};

function DeleteComments(id, hash) {

    DLEconfirm( dle_del_agree, dle_confirm, function () {

		var ajax = new dle_ajax();
		ajax.onShow ('');
		var varsString = "id=" + id;
		ajax.setVar("dle_allow_hash", hash);
		ajax.requestFile = dle_root + "engine/ajax/deletecomments.php";
		ajax.method = 'GET';
		ajax.onCompletion = whenCommentsDeleted;
		ajax.sendAJAX(varsString);

	} );

};

function doFavorites( fav_id, event )
{
	var ajax = new dle_ajax();
	ajax.onShow ('');
	var varsString = "fav_id=" + fav_id;
	ajax.setVar("action", event);
	ajax.setVar("skin", dle_skin);
	ajax.requestFile = dle_root + "engine/ajax/favorites.php";
	ajax.method = 'GET';
	ajax.element = 'fav-id-'+fav_id;
	ajax.sendAJAX(varsString);

	return false;
};

function CheckLogin()
{
	var ajax = new dle_ajax();
	var name = ajax.encodeVAR( document.getElementById('name').value );
	ajax.onShow ('');
	var varsString = "name=" + name;
	ajax.requestFile = dle_root + "engine/ajax/registration.php";
	ajax.method = 'POST';
	ajax.element = 'result-registration';
	ajax.sendAJAX(varsString);

	return false;
};

function doCalendar(month, year, effect){
	var ajax = new dle_ajax();
	ajax.onShow ('');
	var varsString = "";
	ajax.setVar("year", year);
	ajax.setVar("month", month);
	ajax.requestFile = dle_root + "engine/ajax/calendar.php";
	ajax.method = 'GET';
	ajax.effect = effect;
	ajax.element = 'calendar-layer';
	ajax.sendAJAX(varsString);
};

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 doRate( rate, id ) {
	var ajax = new dle_ajax();
	ajax.onShow ('');
	var varsString = "go_rate=" + rate;
	ajax.setVar("news_id", id);
	ajax.setVar("skin", dle_skin);
	ajax.requestFile = dle_root + "engine/ajax/rating.php";
	ajax.method = 'GET';
	ajax.element = 'ratig-layer';
	ajax.sendAJAX(varsString);
};

function dleRate( rate, id ) {
	var ajax = new dle_ajax();
	ajax.onShow ('');
	var varsString = "go_rate=" + rate;
	ajax.setVar("news_id", id);
	ajax.setVar("skin", dle_skin);
	ajax.setVar("mode", "short");
	ajax.requestFile = dle_root + "engine/ajax/rating.php";
	ajax.method = 'GET';
	ajax.element = 'ratig-layer-' + id;
	ajax.sendAJAX(varsString);
};

function doAddComments(){

	var form = document.getElementById('dle-comments-form');
    var dle_comments_ajax = new dle_ajax();

	if (dle_wysiwyg == "yes") {
		document.getElementById('comments').value = $('#comments').html();
		dle_comments_ajax.setVar("editor_mode", 'wysiwyg');
	} 

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

	dle_comments_ajax.onShow ('');
	var varsString = "post_id=" + form.post_id.value;
	dle_comments_ajax.setVar("comments", dle_comments_ajax.encodeVAR(form.comments.value));
	dle_comments_ajax.setVar("name", dle_comments_ajax.encodeVAR(form.name.value));
	dle_comments_ajax.setVar("mail", dle_comments_ajax.encodeVAR(form.mail.value));
	dle_comments_ajax.setVar("skin", dle_skin);

	if ( form.sec_code ) {

	   dle_comments_ajax.setVar("sec_code", form.sec_code.value);

    }

	if ( form.allow_subscribe ) {

		if ( form.allow_subscribe.checked == true ) {
	
		   dle_comments_ajax.setVar("allow_subscribe", "1");

		} else {

		   dle_comments_ajax.setVar("allow_subscribe", "0");

		}

    }

	dle_comments_ajax.requestFile = dle_root + "engine/ajax/addcomments.php";
	dle_comments_ajax.method = 'POST';
	dle_comments_ajax.execute = true;
	dle_comments_ajax.effect = 'blind';
	dle_comments_ajax.element = 'dle-ajax-comments';
	dle_comments_ajax.sendAJAX(varsString);

};

function dle_copy_quote(qname) 
{
dle_txt= '';

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

function dle_ins(name) 
{
	if ( !document.getElementById('dle-comments-form') ) return false;

	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 ShowOrHide( id ) {

	  var item = $("#" + id);

	  if ( document.getElementById('image-'+ id) ) {

		var image = document.getElementById('image-'+ id);

	  } else {

		var image = null;
	  }


	if (item.css("display") == "none") { 

		item.show('blind',{},1500);

		if (image) { image.src = dle_root + 'templates/'+ dle_skin + '/dleimages/spoiler-minus.gif';}

	} else {

		item.hide('blind',{},1500);
		if (image) { image.src = dle_root + 'templates/'+ dle_skin + '/dleimages/spoiler-plus.gif';}
	}

};


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){

    DLEconfirm( dle_del_agree, dle_confirm, function () {
		document.location=url;
	} );
};

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

            }
};

function dle_news_delete( m_id ){
    DLEconfirm( dle_del_agree, dle_confirm, function () {
	    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>';

if (dle_admin != '') {

	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 DLEalert(message, title){

	$("#dlepopup").remove();

	$("body").append("<div id='dlepopup' title='" + title + "' style='display:none'><br />"+ message +"</div>");

	$('#dlepopup').dialog({
		autoOpen: true,
		width: 470,
		buttons: {
			"Ok": function() { 
				$(this).dialog("close");
				$("#dlepopup").remove();							
			} 
		}
	});
};

function DLEconfirm(message, title, callback){

	var b = {};

	b[dle_act_lang[1]] = function() { 
					$(this).dialog("close");
					$("#dlepopup").remove();						
			    };

	b[dle_act_lang[0]] = function() { 
					$(this).dialog("close");
					$("#dlepopup").remove();
					if( callback ) callback();					
				};

	$("#dlepopup").remove();

	$("body").append("<div id='dlepopup' title='" + title + "' style='display:none'><br />"+ message +"</div>");

	$('#dlepopup').dialog({
		autoOpen: true,
		width: 470,
		buttons: b
	});
};

function DLEprompt(message, d, title, callback){

	var b = {};

	b[dle_act_lang[3]] = function() { 
					$(this).dialog("close");						
			    };

	b[dle_act_lang[2]] = function() { 
					if ( $("#dle-promt-text").val().length < 1) {
						 $("#dle-promt-text").addClass('ui-state-error');
					} else {
						var response = $("#dle-promt-text").val()
						$(this).dialog("close");
						$("#dlepopup").remove();
						if( callback ) callback( response );	
					}				
				};

	$("#dlepopup").remove();

	$("body").append("<div id='dlepopup' title='" + title + "' style='display:none'><br />"+ message +"<br /><br /><input type='text' name='dle-promt-text' id='dle-promt-text' class='ui-widget-content ui-corner-all' style='width:97%; padding: .4em;' value='" + d + "'/></div>");

	$('#dlepopup').dialog({
		autoOpen: true,
		width: 470,
		show: 'blind',
		hide: 'blind',
		buttons: b
	});
};

var dle_user_profile = '';
var dle_user_profile_link = '';

function ShowPopupProfile( r )
{
	var b = {};

	b[menu_profile] = function() { 
					document.location=dle_user_profile_link;						
			    };

	if (dle_group != 5) {

		b[menu_send] = function() { 
						document.location=dle_root + 'index.php?do=pm&doaction=newpm&username=' + dle_user_profile;						
				    };
	}

	if (dle_group == 1) {

		b[menu_uedit] = function() {
					$(this).dialog("close");
					window.open('' + dle_root + dle_admin + '?mod=editusers&action=edituser&user=' + dle_user_profile + '', 'User','toolbar=0,location=0,status=0, left=0, top=0, menubar=0,scrollbars=yes,resizable=0,width=540,height=500');					
			    };

	}

	$("#dleprofilepopup").remove();

	$("body").append(r);

	$('#dleprofilepopup').dialog({
		autoOpen: true,
		show: 'fade',
		hide: 'fade',
		buttons: b,
		width: 450
	});
	
	return false;
};

function ShowProfile( name, url )
{

	if (dle_user_profile == name && document.getElementById('dleprofilepopup')) {$('#dleprofilepopup').dialog('open');return false;}

	var ajax = new dle_ajax();
	dle_user_profile = name;
	dle_user_profile_link = url;
	ajax.onShow ('');
	var varsString = "";
	ajax.setVar("name", name);
	ajax.setVar("skin", dle_skin);
	ajax.requestFile = dle_root + "engine/ajax/profile.php";
	ajax.method = 'GET';
	ajax.onCompletion = ShowPopupProfile;
	ajax.sendAJAX(varsString);
	
	return false;
};

function FastSearch()
{
	$('#story').attr('autocomplete', 'off');
	$('#story').blur(function(){
		 	$('#searchsuggestions').fadeOut();
	});

	$('#story').keyup(function() {
		var inputString = $(this).val();

		if(inputString.length == 0) {
			$('#searchsuggestions').fadeOut();
		} else {

			if (dle_search_value != inputString && inputString.length > 3) {
				clearInterval(dle_search_delay);
				dle_search_delay = setInterval(function() { dle_do_search(inputString); }, 600);
			}

		}
	
	});
};

function dle_do_search( inputString )
{
	clearInterval(dle_search_delay);

	$('#searchsuggestions').remove();

	$("body").append("<div id='searchsuggestions' style='display:none'></div>");
	$("#searchsuggestions").css({ 'position' : 'absolute'});
	$("#searchsuggestions").position({
		my: "left top",
		at: "left bottom",
		of: "#story",
		collision: "fit flip"
	});

	$.post(dle_root + "engine/ajax/search.php", {query: ""+inputString+""}, function(data) {
			$('#searchsuggestions').fadeIn(); 
			$('#searchsuggestions').html(data);
		});

	dle_search_value = inputString;

};