var DMC_selField  = "message";
var DMC_fombj = document.getElementById( 'chat-form' );

function Chat_UserMenu(a, b, c, d, e, f) {
	var g = new Array();
	g[0] = '<a onclick="Chat_QuoteName(\'' + e + '\'); return false;" href="javascript:quote(' + e + ');">Ответить</a>';
	g[1] = '<a ' + a + ' >' + menu_profile + '</a>';
	g[2] = '<a href="' + dle_root + 'index.php?do=pm&doaction=newpm&user=' + b + '">' + menu_send + '</a>';
	if (c == '1' || c == '2') {
		g[3] = '<a onclick="return Chat_MessDel(' + d + ');" href="javascript:messages_delete(' + d + ');">Удалить</a>'
	}
	return g
}
function Chat_GastMenu(a, b, c, d, e) {
	var f = new Array();
	f[0] = '<a onclick="Chat_QuoteName(\'' + d + '\'); return false;" href="javascript:quote(' + d + ');">Ответить</a>';
	f[1] = '<a href="mailto:' + a + '">' + menu_send + '</a>';
	if (b == '1' || b == '2') {
		f[2] = '<a onclick="return Chat_MessDel(' + c + ');" href="javascript:messages_delete(' + c + ');">Удалить</a>'
	}
	return f
}

function Chat_MessDel(b) {
    $.post(dle_root + "system/ajax/chat.php", { mdelete: b, refresh: 1, skin: dle_skin}, function(data){
           $("#chat").html(data);
   	});
}
function Chat_Refresh() {
    $.post(dle_root + "system/ajax/chat.php", { skin: dle_skin}, function(data){
           $("#chat").html(data);
   	});
}
function Chat_QuoteName(a) {
	document.getElementById('message').value += "[b]" + a + "[/b]:";
	$("#message").focus()
}
function Chat_AddSmile(a, b, c) {
	document.getElementById(b).value += ":" + a + ":";
	$("#" + c).fadeOut("slow");
	$("#" + b).focus()
}
function Chat_Display(a, b) {
	if ($("#" + a).css("display") === "none") {
		$("#" + a).show(b)
	} else {
		$("#" + a).hide(b)
	}
}
function AWclear(a, b) {
	var c = a.value;
	if (c == b) {
		a.value = ""
	}
	$(a).attr('onblur', "if(this.value=='')this.value='" + b + "';")
}

function SendMessage(){
    if($.trim(document.chat_form.message.value) == ''){
        alert('Введите сообщение!');
    return false;
    };

	var message = document.getElementById('message').value;

	$.post(dle_root + "system/ajax/chat.php", { message: message, skin: dle_skin}, function(data){
        $("#chat").html(data);
        $('#message').attr('value', '');
	});
    return false;
};

//BBCOdes

var uagent    = navigator.userAgent.toLowerCase();
var is_safari = ( (uagent.indexOf('safari') != -1) || (navigator.vendor == "Apple Computer, Inc.") );
var is_ie     = ( (uagent.indexOf('msie') != -1) && (!is_opera) && (!is_safari) && (!is_webtv) );
var is_ie4    = ( (is_ie) && (uagent.indexOf("msie 4.") != -1) );
var is_moz    = (navigator.product == 'Gecko');
var is_ns     = ( (uagent.indexOf('compatible') == -1) && (uagent.indexOf('mozilla') != -1) && (!is_opera) && (!is_webtv) && (!is_safari) );
var is_ns4    = ( (is_ns) && (parseInt(navigator.appVersion) == 4) );
var is_opera  = (uagent.indexOf('opera') != -1);
var is_kon    = (uagent.indexOf('konqueror') != -1);
var is_webtv  = (uagent.indexOf('webtv') != -1);

var is_win    =  ( (uagent.indexOf("win") != -1) || (uagent.indexOf("16bit") !=- 1) );
var is_mac    = ( (uagent.indexOf("mac") != -1) || (navigator.vendor == "Apple Computer, Inc.") );
var ua_vers   = parseInt(navigator.appVersion);

var b_open = 0;
var i_open = 0;
var u_open = 0;
var s_open = 0;
var quote_open = 0;
var code_open = 0;
var sql_open = 0;
var html_open = 0;
var left_open = 0;
var center_open = 0;
var right_open = 0;
var hide_open = 0;
var color_open = 0;
var ie_range_cache = '';
var DMC_bbtags   = new Array();

function DMC_stacksize(thearray)
{
	for (i = 0; i < thearray.length; i++ )
	{
		if ( (thearray[i] == "") || (thearray[i] == null) || (thearray == 'undefined') )
		{
			return i;
		}
	}

	return thearray.length;
};

function cstat()
{
	var c = DMC_stacksize(DMC_bbtags);

	if ( (c < 1) || (c == null) ) {
		c = 0;
	}

	if ( ! DMC_bbtags[0] ) {
		c = 0;
	}


};

function DMC_simpletag(thetag)
{
	var tagOpen = eval(thetag + "_open");


		if (tagOpen == 0)
		{
			if(DMC_doInsert("[" + thetag + "]", "[/" + thetag + "]", true))
			{
				eval(thetag + "_open = 1");
				document.getElementById( 'b_' + thetag ).className = 'editor_buttoncl';

				pushstack(DMC_bbtags, thetag);
				cstat();

			}
		}
		else
		{
			lastindex = 0;

			for (i = 0 ; i < DMC_bbtags.length; i++ )
			{
				if ( DMC_bbtags[i] == thetag )
				{
					lastindex = i;
				}
			}

			while (DMC_bbtags[lastindex])
			{
				tagRemove = popstack(DMC_bbtags);
				DMC_doInsert("[/" + tagRemove + "]", "", false);


				if ( (tagRemove != 'font') && (tagRemove != 'size') )
				{
					eval(tagRemove + "_open = 0");
					document.getElementById( 'b_' + tagRemove ).className = 'editor_button';
				}
			}

			cstat();
		}

};



function DMC_doInsert(ibTag, ibClsTag, isSingle)
{
	var isClose = false;
	var obj_ta = eval('DMC_fombj.'+ DMC_selField);

	if ( (ua_vers >= 4) && is_ie && is_win)
	{
		if (obj_ta.isTextEdit)
		{
			obj_ta.focus();
			var sel = document.selection;
			var rng = ie_range_cache ? ie_range_cache : sel.createRange();
			rng.colapse;
			if((sel.type == "Text" || sel.type == "None") && rng != null)
			{
				if(ibClsTag != "" && rng.text.length > 0)
					ibTag += rng.text + ibClsTag;
				else if(isSingle)
					ibTag += rng.text + ibClsTag;

				rng.text = ibTag;
			}
		}
		else
		{
				obj_ta.value += ibTag + ibClsTag;

		}
		rng.select();
	ie_range_cache = null;

	}
	else if ( obj_ta.selectionEnd )
	{
		var ss = obj_ta.selectionStart;
		var st = obj_ta.scrollTop;
		var es = obj_ta.selectionEnd;

		if (es <= 2)
		{
			es = obj_ta.textLength;
		}

		var start  = (obj_ta.value).substring(0, ss);
		var middle = (obj_ta.value).substring(ss, es);
		var end    = (obj_ta.value).substring(es, obj_ta.textLength);

		if (obj_ta.selectionEnd - obj_ta.selectionStart > 0)
		{
			middle = ibTag + middle + ibClsTag;
		}
		else
		{
			middle = ibTag + middle + ibClsTag;
		}

		obj_ta.value = start + middle + end;

		var cpos = ss + (middle.length);

		obj_ta.selectionStart = cpos;
		obj_ta.selectionEnd   = cpos;
		obj_ta.scrollTop      = st;


	}
	else
	{
		obj_ta.value += ibTag + ibClsTag;
	}

	obj_ta.focus();
	return isClose;
};

function getOffsetTop(obj)
{
	var top = obj.offsetTop;

	while( (obj = obj.offsetParent) != null )
	{
		top += obj.offsetTop;
	}

	return top;
};

function getOffsetLeft(obj)
{
	var top = obj.offsetLeft;

	while( (obj = obj.offsetParent) != null )
	{
		top += obj.offsetLeft;
	}

	return top;
};
