function $(id){
	return document.getElementById(id);
}
function addHandler(object, event, handler, useCapture) { 
	if (object.addEventListener) object.addEventListener(event, handler, useCapture ? useCapture : false); 
	else if (object.attachEvent) object.attachEvent('on' + event, handler); 
}
function defPosition(event) {
	var x = y = 0;
	if (document.attachEvent != null) {
		x = window.event.clientX + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
		y = window.event.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
	} else if (!document.attachEvent && document.addEventListener) {
		x = event.clientX + window.scrollX;
		y = event.clientY + window.scrollY;
	}
	return {x:x, y:y};
}
function fixClassName(node, addclass, delclass) {
	var re = new RegExp("\\b" + delclass + "\\b", "g");
	if (delclass) node.className = node.className.replace(re, "");
	if (addclass) if (node.className.indexOf(addclass) == -1) node.className += " " + addclass;
}
var fadeTimer = null;
function fadeIn(id, value, step, interval) { // 100% -> 0%
	value -= step;
	value = value <= 0 ? 0 : value;
	setElementOpacity(id, value);
	clearTimeout(fadeTimer);
	fadeTimer = null;
	if (value) fadeTimer = setTimeout("fadeIn('"+id+"', "+value+", "+step+", "+interval+")", interval);
	else if (id == 'map-fade') {
		clickArea('ru');
		$('map-fade').style.display='none';
	}
	else if (id == 'map-fade2') $('map-fade2').style.display='none';
	else if (id == 'lenta-fade') $('lenta-fade').style.display='none';
}
function fadeOut(id, value, step, interval) { // 0% -> 100%
	value += step;
	value = value >= 100 ? 100 : value;
	setElementOpacity(id, value);
	clearTimeout(fadeTimer);
	fadeTimer = null;
	if (value != 100) fadeTimer = setTimeout("fadeOut('"+id+"', "+value+", "+step+", "+interval+")", interval);
	else if (id == 'lenta-fade') {
		$('lenta-all').style.display = 'none';
		$('menuId').className = "menu1";
		$('lenta-fade').style.display = 'none';
		$('mapId').style.display = 'block';
		$('map-fade').style.display = 'block';
		$('map-fade2').style.display = 'none';
		$('map-fade3').style.display = 'none';
		fadeIn('map-fade', 100, step, interval);
	} else if (id == 'map-fade') {
		fixClassName($('switchMapId'), 0, "world");
		fixClassName($('switchMapId'), "ea", 0);
		mouseOverArea();
		drawRegList(evrazia,1);
		drawPrevRegList(evraziaPreview,1);
		// Russian init
		mouseOverArea('ru',0,1);
		scrollBeforeCurrent('pl');
		fadeIn('map-fade', 100, step, interval);
	} else if (id == 'map-fade2') {
		fixClassName($('switchMapId'), 0, "ea");
		fixClassName($('switchMapId'), "world", 0);
		mouseOverArea();
		drawRegList(materiki);
		drawPrevRegList(materikiPreview);
		fadeIn('map-fade2', 100, step, interval);
	} else if (id == 'map-fade3') {
		$('lenta-all').style.display = 'block';
		$('menuId').className = "menu";
		$('mapId').style.display = 'none';
		$('lenta-fade').style.display = 'block';
		lenta.wasHide = false;
		fadeIn('lenta-fade', 100, step, interval);
	}
}
function setElementOpacity(sElemId, nOpacity) {
	var opacityProp = getOpacityProperty();
	var elem = $(sElemId);
	if (!elem || !opacityProp) return;
	if (opacityProp=="filter") {
		var oAlpha = elem.filters['DXImageTransform.Microsoft.alpha'] || elem.filters.alpha;
		if (oAlpha) oAlpha.opacity = nOpacity;
		else elem.style.filter += "progid:DXImageTransform.Microsoft.Alpha(opacity="+nOpacity+")";
	} else elem.style[opacityProp] = parseFloat(nOpacity / 100);
}
function getOpacityProperty() {
	if (typeof document.body.style.opacity == 'string') return 'opacity';
	else if (typeof document.body.style.MozOpacity == 'string') return 'MozOpacity';
	else if (typeof document.body.style.KhtmlOpacity == 'string') return 'KhtmlOpacity';
	else if (document.body.filters && navigator.appVersion.match(/MSIE ([\d.]+);/)[1]>=5.5) return 'filter';
	return false;
}

// AJAX-object
var net = new Object();
net.READY_STATE_UNINITIALIZED = 0;
net.READY_STATE_LOADING = 1;
net.READY_STATE_LOADED = 2;
net.READY_STATE_INTERACTIVE = 3;
net.READY_STATE_COMPLETE = 4;

net.ContentLoader = function(key, method, url, params, onload, onerror, contentType, headers) {
	this.hashKey = key; 
	this.unrequestBrowser = false;
	this.req = null;
	this.onload = onload;
	this.onerror = (onerror) ? onerror : this.defaultError;
	this.loadXMLDoc(method, url, params, contentType, headers);
}

net.ContentLoader.prototype = {
	loadXMLDoc : function(method, url, params, contentType, headers) {
		if (!method) method="GET";
		if (!contentType && method=="POST") contentType='application/x-www-form-urlencoded';
		if (window.XMLHttpRequest) {
			this.req=new XMLHttpRequest(); 
		} else if (window.ActiveXObject) {
			this.req=new ActiveXObject("Microsoft.XMLHTTP");
		} else {
			this.unrequestBrowser = true;
			return;
		}
		if (this.req) {
			try {
				this.req.open(method,url,true);
				if (contentType){
					this.req.setRequestHeader('Content-Type', contentType);
				}
				if (headers) {
					for (var h in headers) {
						this.req.setRequestHeader(h,headers[h]);
					}
				}
				var loader=this;
				this.req.onreadystatechange=function() {
				loader.onReadyState.call(loader);
			}
			this.req.send(params);
			} catch (err){
				this.onerror.call(this);
			}
		}
	},
	onReadyState : function() {
		var req=this.req;
		var ready=req.readyState;
		if (ready==net.READY_STATE_COMPLETE) {
			var httpStatus=req.status;
			if (httpStatus==200 || httpStatus==0) {
				this.onload.call(this);
			} else {
				this.onerror.call(this);
			}
		}
	},
	defaultError : function() {
		alert("error fetching data!"+"\n\nreadyState:"+this.req.readyState +"\nstatus: "+this.req.status+"\nheaders: "+this.req.getAllResponseHeaders());
	}
}

var requestsHash = [];
function setAjaxRequest(method, url, params, onload, onerror, contentType, headers) {
	if (!url) {
		alert("Necessary parameters are not specified");
		return;
	}
	requestsHash.push(new net.ContentLoader(requestsHash.length, method, url, params, onload, onerror, contentType, headers));
	return requestsHash[requestsHash.length - 1].unrequestBrowser;
}
