var w = window, d = document;

function $(id){

	if (arguments.length > 1){

		for (var i=0, elements=[], len=arguments.length; i < len; i++) elements.push($(arguments[i]));

		return elements;

	} else return d.getElementById(id);
}

//Object.prototype.$ = $;

function ce(sTag){
	return d.createElement(sTag);
}

function loc(sUrl){
	location.href = sUrl;
}

function dw(sText){
	d.write(sText);
}

function hs(id){

	var obj = $(id);

	obj.style.display = (!obj.style.display)?"none":"";
}

function preloadImages(){

	for (var i=0; src = arguments[i]; i++){

		var img = new Image();
		img.src = src;
	}
}

preloadImages("http://counter.yadro.ru/hit?t21.2;r" + escape(d.referrer) + ((typeof(screen) == "undefined")?"":";s" + screen.width + "*" + screen.height + "*" + (screen.colorDepth?screen.colorDepth:screen.pixelDepth)) + ";u" + escape(d.URL) + ";" + Math.random());

function var_dump(obj){

	var temp = "";

	for (var i in obj){

		var value = eval("obj." + i);

		if (i == "innerHTML" || i == "outerHTML") value = "<pre>" + value.replace(/</g, "&lt;").replace(/>/g, "&gt;") + "</pre>";

		temp += "obj." + i + " = " + value + "<br />";
	}

	d.body.innerHTML += temp;
}

function getAbsolutePos(oElem){

	var _x=0, _y=0;

	if (oElem){

		do{
			_x+=oElem.offsetLeft;
			_y+=oElem.offsetTop;

		} while ((oElem=oElem.offsetParent))
	}

	return {x: _x, y: _y};
}

function getPageSize(){

	var windowWidth, windowHeight;

	if (self.innerHeight){

		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;

	} else if (d.documentElement && d.documentElement.clientHeight){

		windowWidth = d.documentElement.clientWidth;
		windowHeight = d.documentElement.clientHeight;

	} else if (d.body){

		windowWidth = d.body.clientWidth;
		windowHeight = d.body.clientHeight;
	}

	return {width: windowWidth, height: windowHeight};
}

function addBookmark(){

	var url = location.href;

	var title = d.title;

	if (typeof(w.sidebar) == "object" && typeof(w.sidebar.addPanel) == "function")
		w.sidebar.addPanel(title, url, "");

	else if (typeof(window.external) == "object")
		w.external.AddFavorite(url, title);

	else if (w.opera){

		var link = ce("A");

		link.rel = "sidebar";
		link.href = url;
		link.title = title;

		link.click();

	} else alert("Ваш браузер не поддерживает автоматическое добавление в Избранное!");
}

d.onkeydown = function NavigateThrough(event){

	if (!d.getElementById) return;

	if (window.event) event = window.event;

	if (event.ctrlKey == true){

		var code = null;
		var link = null;

		if (event.keyCode)
			code = event.keyCode;
		else if (event.which)
			code = event.which;

		switch (code){

			case 0x27:
				link = $("next_link");
				break;

			case 0x25:
				link = $("prev_link");
				break;

			case 0x26:
				link = $("up_link");
				break;

			case 0x28:
				link = $("down_link");
				break;

			case 0x24:
				link = $("start_link");
				break;
		}

		if (link && link.href)
			loc(link.href);
	}
}

function exValue(obj){

		if (obj.value == obj.defaultValue){

			obj.className = obj.className.replace(" empty", "");
			obj.value = "";

		} else if (!obj.value){

			obj.className = obj.className + " empty";
			obj.value = obj.defaultValue;
		}

}

function check_email(sEmail){

	return (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(sEmail));
}

/*
function check_phone(sPhone){

	return (/^(\d{3}-\d{2}-\d{2})+$/.test(sPhone));
}
*/

function checkColor(obj, postfixFunction){

	if (eval("check_" + postfixFunction + "(obj.value)"))
		obj.style.color = "#79a43b";

	else if (!obj.value)
		obj.style.color = "";

	else
		obj.style.color = "#a77f00";
}

function createScript(sSrc){

	var script = ce("SCRIPT");

	script.src = "/scripts/" + sSrc;

	d.body.appendChild(script);
}

function checkTextLength(obj, nMaxLength){

	var objLength = obj.innerText.length;
	var cNum = $("count_" + obj.name);
	var three = nMaxLength / 3;
	var rest = nMaxLength - objLength;

	if (rest < 0){

		obj.innerText = obj.innerText.substr(0, nMaxLength);
		cNum.style.color = "#c40000";
		cNum.innerHTML = 0;

		return;

	} else if (rest < (three - (three / 2)))
		cNum.style.color = "#c40000";

	else if (rest < ((three * 2) - (three / 2)))
		cNum.style.color = "#ff8f30";

	else
		cNum.style.color = "#4ec400";

	cNum.innerHTML = rest;
}

function addEval(objId, arEval, code, applyEval){

	for (var i in arEval){

		var ev = arEval[i].toLowerCase();

		eval("$('" + objId + "')." + ev + " = function(){" + code + "}");
	}

	if (applyEval)
		eval("$('" + objId + "')." + ev + "();");
}

function GetCorrectWord(iNum, aWords){

	iNum = String(iNum);

	var iLast = Number(iNum.substr(iNum.length-1));
	var sResult;

	if (between(iNum, 5, 20))
		sResult=aWords[2];

	else {

		switch (iLast){
			case 1:  sResult=aWords[0]; break;
			case 2:
			case 3:
			case 4:  sResult=aWords[1]; break;
			default: sResult=aWords[2]; break;
		}
	}

	return sResult;
}

function between(iNum, iMin, iMax, bInclusive){

	if (bInclusive)
		return (iNum >= iMin && iNum <= iMax);
	else
		return (iNum > iMin && iNum < iMax);
}

function createRequest(){
	var hRequest;
	if (window.XMLHttpRequest)
	{
		hRequest = new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{
		var XMLHTTP_IDS = new Array(
			'MSXML2.XMLHTTP.5.0',
			'MSXML2.XMLHTTP.4.0',
			'MSXML2.XMLHTTP.3.0',
			'MSXML2.XMLHTTP',
			'Microsoft.XMLHTTP' );
		var success = false;
		for (var i = 0; i < XMLHTTP_IDS.length && !success; i++)	
		{
			try
			{
				hRequest = new ActiveXObject(XMLHTTP_IDS[i]);
				if (hRequest)
					success = true;
			}
			catch (e) {}
		}
		
	}
	return hRequest ? hRequest : false;
}

function getMousePosition(e) {
	if (e.pageX || e.pageY){
		var posX = e.pageX;
		var posY = e.pageY;
	}else if (e.clientX || e.clientY) 	{
		var posX = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
		var posY = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
	}
	return {x:posX, y:posY}	
}

function getElementsByClassName(node, classname){ 

	var a = [];

	var re = new RegExp('\\b' + classname + '\\b');

	var els = node.getElementsByTagName("*");

	for (var i=0, j = els.length; i<j; i++)
		if (re.test(els[i].className))
			a.push(els[i]);

	return a;
}
/*
w.onload = function(){

//	m = d.body.innerHTML.match(/(.){1}(«)/g, '<b>$1</b><span style="margin-left:-0.5em;" class="lr">$2</span>');

	d.body.innerHTML = d.body.innerHTML.replace(/«/g, '<span id="lrs"></span><span style="margin-left:-0.5em;" class="lr">«</span>');

//	m = d.body.innerHTML.match(/«/g);

	var it = getElementByClassName("lr");
alert(it.length);
	for (var i=0; 1<it.length; i++){


	}

//alert(getElementByClassName("small"));

//	var_dump(m[1]);
}
*/

function showImportantNews(nNum){

	for (var i=0; elem = $("importantNews" + i); i++){

		if (i == nNum){

			$("importantNewsLink").getElementsByTagName("A")[i].className = "selected";
			elem.style.display = "";

		} else {

			$("importantNewsLink").getElementsByTagName("A")[i].className = "";
			elem.style.display = "none";
		}
	}
}