var SYSTEM_MAIN_URL =  'www.itnews.pp.ru'; var client = navigator.userAgent.toLowerCase();var IE = ((client.indexOf("msie") != -1) && (client.indexOf("opera") == -1));var helpleft = 0;var helptop = 0;var openerqr = document.getElementById("messageform");var imageTag = false;var theSelection = false;var clientPC = navigator.userAgent.toLowerCase();var clientVer = parseInt(navigator.appVersion);var is_ie = ((clientPC.indexOf("msie") != -1) && (clientPC.indexOf("opera") == -1));var is_nav = ((clientPC.indexOf('mozilla')!=-1) && (clientPC.indexOf('spoofer')==-1)&&(clientPC.indexOf('compatible') == -1) && (clientPC.indexOf('opera')==-1)&&(clientPC.indexOf('webtv')==-1) && (clientPC.indexOf('hotjava')==-1));var is_moz = 0;var is_win = ((clientPC.indexOf("win")!=-1) || (clientPC.indexOf("16bit") != -1));var is_mac = (clientPC.indexOf("mac")!=-1);bbcode = new Array();
bbtags = new Array('[b]','[/b]','[i]','[/i]','[u]','[/u]', '[s]', '[/s]', '[left]', '[/left]', '[center]', '[/center]', '[right]', '[/right]', '[ul]\n [li]\n[/li]\n', '[/ul]', '[ol]\n [li]\n[/li]\n', '[/ol]', '[quote]', '[/quote]', '[code]', '[/code]');
imageTag = false;
function getarraysize(thearray) {for (i = 0; i < thearray.length; i++) {if ((thearray[i] == "undefined") || (thearray[i] == "") || (thearray[i] == null))return i;}return thearray.length;}
function do_window(src, w, h) {
 win=window.open('http://www.itnews.pp.ru/'+src,'window','toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width='+w+',height='+h+',top=100');
 win.focus();
}
function AJAX() {
	this.http = false;
	this.init = function() { 
    if (window.XMLHttpRequest) {
        try {
            this.http = new XMLHttpRequest();
        } catch (e){}
    } else if (window.ActiveXObject) {
        try {
            this.http = new ActiveXObject('Msxml2.XMLHTTP');
        } catch (e){
            try {
                this.http = new ActiveXObject('Microsoft.XMLHTTP');
            } catch (e){}
        }
    }
  }   
  this.s_send = function (url, data, method) { 
	if (this.http) {
		this.http.open(method, url, false);
		if (method == 'post') {this.http.setRequestHeader('Content-Type',  'application/x-www-form-urlencoded');}
        this.http.send(data);
		if(this.http.status == 200) {
			return this.http.responseText;
		} else {
				return false;
		}
	}
  }
}
function dark() {
	var el = document.createElement('div');
	document.body.appendChild(el);
	el.id = 'dark';
	el.innerHTML = '<img src="http://www.itnews.pp.ru/files/templates/tgf_malavita/images/ajax_loading_big.gif" style="top: 50%; left: 50%; position: fixed;" alt="Загрузка" title="Загрузка">';}
function undark() {
	document.body.removeChild(document.getElementById('dark'))
}
var draged;
function make_dragle(div){
	if(!div) return;
	document.getElementById(div.id).onmouseup = function() {
		window.clearInterval(draged);
		return false;
	}
	document.getElementById(div.id).onmousedown = function() {
		drag(document.getElementById(div.id));
		return false;
	}
}
function drag(e) {
	var pos = absoliute_pos(e);
	var y = pos['left'];
	var x = pos['top'];
	var mouse_y = helpleft;
	var mouse_x = helptop;
	draged = window.setInterval(function () {e.style.top = (helptop - mouse_x + x) + 'px'; e.style.left = (helpleft - mouse_y + y) + 'px';},24);
}
function absoliute_pos(e) {
	var ret = new Array();
	var top = e.offsetTop;
	var left = e.offsetLeft;
	while(e.offsetParent) {
		e = e.offsetParent;
		top+= e.offsetTop;
		left+= e.offsetLeft;
	}
	ret['top'] = top;
	ret['left'] = left;
	return ret;
}
var div = document.createElement('div');
function create_window(inner,margin,title,id) {
	document.body.appendChild(div);
	div.style.position = 'fixed';
	div.style.top = '50%';
	div.style.left = '50%';
	div.id = 'window'+id;
	div.innerHTML = '<style type="text/css" />.ar1, .ar2, .ar3, .ar5 {display: block;height: 1px;overflow: hidden;font-size: 1px; background: rgb(221, 221, 221);}.ar5 {margin: 0 5px;}.ar3 {margin: 0 3px;}.ar2 {margin: 0 2px;}.ar1 {margin: 0 1px;}.content {border-top-width: 1px;border-top-style: solid;border-top-color: rgb(249, 249, 249);}.buttons {background: rgb(221, 221, 221);border-bottom-width: 1px;border-bottom-style: solid;border-bottom-color: rgb(204, 204, 204);}</style><div id="window'+id+'"><b style="cursor: move; opacity: 0.7;" class="ar5"></b><b style="cursor: move; opacity: 0.7;" class="ar3"></b><b style="cursor: move; opacity: 0.7;" class="ar2"></b><b style="cursor: move; opacity: 0.7;" class="ar1"></b><div style="box-shadow: rgb(0, 0, 0) 1px 1px 20px; -moz-box-shadow: rgb(0, 0, 0) 1px 1px 20px; -webkit-box-shadow: rgb(0, 0, 0) 1px 1px 20px; border-right: 1px solid rgb(204, 204, 204); border-left: 1px solid rgb(204, 204, 204); background: #FFFFFF;"><div style="cursor: move; opacity: 0.7;" class="buttons"><table style="width: 100%; height:10px"><tr><td align="left" style="text-shadow: rgb(255, 255, 255) 0px 1px 0px; font: normal normal bold 100%/normal">'+title+'</td><td align="right"><img style="margin-right: 3px" id="close-btn" style="cursor: pointer;" src="http://www.itnews.pp.ru/files/templates/tgf_malavita/images/close1.png" onmouseover="this.src = \'http://www.itnews.pp.ru/files/templates/tgf_malavita/images/close2.png\';" onmouseout="this.src = \'http://www.itnews.pp.ru/files/templates/tgf_malavita/images/close1.png\';" onclick="document.body.removeChild(document.getElementById(\'window'+id+'\'))"></td></tr></table></div><div id="content" style="padding: 7px 0.75em">'+inner+'</div></div><b style="opacity: 0.7;" class="ar1"></b><b style="opacity: 0.7;" class="ar2"></b><b style="opacity: 0.7;" class="ar3"></b><b style="opacity: 0.7;" class="ar5"></b></div>';
	make_dragle(div);
}
var ajax = new AJAX();
ajax.init();
function load_ajax_hidden(imgid, elid,url) {
	if (document.getElementById(elid).innerHTML.length>1) {
		document.getElementById(elid).innerHTML = '';
		document.getElementById(imgid).src = 'http://www.itnews.pp.ru/files/templates/tgf_malavita/images/plus.png';		
		document.getElementById(imgid).alt = '+';		
		document.getElementById(imgid).title = '+';		
	} else {
			jsframe(url, elid);
			document.getElementById(imgid).src = 'http://www.itnews.pp.ru/files/templates/tgf_malavita/images/minus.png';		
			document.getElementById(imgid).alt = '-';		
			document.getElementById(imgid).title = '-';		
	}
	return false;
}
function dashboard_change(el) {
	items = new Array('li_quik_add_content','li_quik_add_catt','li_quik_add_link', 'li_statistic', 'li_quik_error_log');
	items_main = new Array('quik_add_content','quik_add_catt','quik_add_link', 'dashboard_main', 'error_log');
	for(var i=0; i<5; i++) {
		if (items[i] == el.id) {
			if (document.getElementById(items_main[i]).style.display == 'block') {
				document.getElementById(items_main[i]).style.display = 'none';
				document.getElementById(items[i]).style.background = '#E9E9E9';
			} else {
					document.getElementById(items_main[i]).style.display = 'block';
					document.getElementById(items[i]).style.background = '#D2D2D2';
			}
		} else {
				document.getElementById(items_main[i]).style.display = 'none';
				document.getElementById(items[i]).style.background = '#E9E9E9';
		}
	}
	return false;
}
function settings_tab_change(el) {
	items = new Array('li_server', 'li_meta');
	items_main = new Array('settings_server','settings_main');
	for(var i=0; i<2; i++) {
		if (items[i] == el.id) {
			if (document.getElementById(items_main[i]).style.display == 'block') {
				document.getElementById(items_main[i]).style.display = 'none';
				document.getElementById(items[i]).style.background = '#E9E9E9';
			} else {
					document.getElementById(items_main[i]).style.display = 'block';
					document.getElementById(items[i]).style.background = '#D2D2D2';
			}
		} else {
				document.getElementById(items_main[i]).style.display = 'none';
				document.getElementById(items[i]).style.background = '#E9E9E9';
		}
	}
	return false;
}
function jsframe(url, element_id) {
	dark();
    var element = document.getElementById(element_id);
	var tmp = element.innerHTML;
	var ret = ajax.s_send(url, null,'get');
	if (ret != false) {
		element.innerHTML = ret;
	} else {
			element.innerHTML = tmp;
	}
	undark();
}
function addyoutube() {
	var prm = prompt('URL: ');
	if (prm) {
		addtext('[youtube='+prm+'/]');
	}
	return false;
}
var time;
function FitToContent() {
	var text = document.getElementById('messageform');
	if (!text) return;
	if (text.scrollHeight>200) {
		text.style.height = text.scrollHeight + "px";
	}
}
function arraypush(thearray,value){thearray[getarraysize(thearray)] = value;}
function arraypop(thearray) {
	thearraysize = getarraysize(thearray);
	retval = thearray[thearraysize - 1];
	delete thearray[thearraysize - 1];
	return retval;
}
document.onmousemove = mouse_pos;
var mouse_position = document.createElement('div');
function help(helptext) {
	document.body.appendChild(mouse_position);
	mouse_position.id = 'help';
	mouse_position.innerHTML = helptext;
}
function unhelp() {
	document.body.removeChild(document.getElementById('help'))
}
function mouse_pos(e) {
	if (IE) {
		helptop = event.clientY + document.body.scrollTop;
		helpleft = event.clientX + document.body.scrollLeft;
	} else {
			helptop = e.pageY;
			helpleft = e.pageX;
	}
	mouse_position.style.top = helptop + 'px';
	mouse_position.style.left = helpleft + 'px';
	return true;
}
function emoticon(text,id) {
	var txtarea = document.getElementById(id);
	text = ' ' + text + ' ';
	if (txtarea.createTextRange && txtarea.caretPos) {
		var caretPos = txtarea.caretPos;
		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? caretPos.text + text + ' ' : caretPos.text + text;
		txtarea.focus();
	} else {
		txtarea.value  += text;
		txtarea.focus();
	}
}
function emoticon_wospaces(text) {
	var txtarea = document.getElementById("messageform");
	if (txtarea.createTextRange && txtarea.caretPos) {
		var caretPos = txtarea.caretPos;
		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? caretPos.text + text + ' ' : caretPos.text + text;
		txtarea.focus();
	} else {
		txtarea.value  += text;
		txtarea.focus();
	}
}
function bbstyle(bbnumber,id) {
	var txtarea = document.getElementById(id);
	txtarea.focus();
	donotinsert = false;
	theSelection = false;
	bblast = 0;
	if (bbnumber == -1) {
		while (bbcode[0]) {
			butnumber = arraypop(bbcode) - 1;
			txtarea.value += bbtags[butnumber + 1];
		}
		imageTag = false;
		txtarea.focus();
		return;
	}
	if ((clientVer >= 4) && is_ie && is_win) {
		theSelection = document.selection.createRange().text; // Get text selection
		if (theSelection) {
			document.selection.createRange().text = bbtags[bbnumber] + theSelection + bbtags[bbnumber+1];
			txtarea.focus();
			theSelection = '';
			return;
		}
	}
	else if (txtarea.selectionEnd && (txtarea.selectionEnd - txtarea.selectionStart > 0)) {
		mozWrap(txtarea, bbtags[bbnumber], bbtags[bbnumber+1]);
		return;
	}
	for (i = 0; i < bbcode.length; i++) {
		if (bbcode[i] == bbnumber+1) {
			bblast = i;
			donotinsert = true;
		}
	}
	if (donotinsert) {
		while (bbcode[bblast]) {
				butnumber = arraypop(bbcode) - 1;
				txtarea.value += bbtags[butnumber + 1];
				imageTag = false;
			}
			txtarea.focus();
			return;
	} else {		
		txtarea.value += bbtags[bbnumber];
		arraypush(bbcode,bbnumber+1);
		txtarea.focus();
		return;
	}
	storeCaret(txtarea);
}

function mozWrap(txtarea, open, close) {
	var selLength = txtarea.textLength;
	var selStart = txtarea.selectionStart;
	var selEnd = txtarea.selectionEnd;
	if (selEnd == 1 || selEnd == 2) 
		selEnd = selLength;

	var s1 = (txtarea.value).substring(0,selStart);
	var s2 = (txtarea.value).substring(selStart, selEnd)
	var s3 = (txtarea.value).substring(selEnd, selLength);
	txtarea.value = s1 + open + s2 + close + s3;
	return;
}

function storeCaret(textEl) {
	if (textEl.createTextRange) textEl.caretPos = document.selection.createRange().duplicate();
}

var selection = false;

function putname(name) { 
	emoticon_wospaces('[b]'+name+'[/b]\n');
	document.getElementById('messageform').focus(); 
	return true; 
}
function addtext(name) { 
	emoticon_wospaces(name + '\n'); 
	document.getElementById('messageform').focus(); 
	return true; 
}
