function preloadImg(imgsArr, time, callback, obj) {	
	var hide = document.createElement("div"), s = hide.style;
	s.position = "absolute";
	s.top = s.left = 0;
	s.height = '1px';
	s.width = '1px';
	s.visibility = s.overflow = "hidden";
	document.body.appendChild(hide);	
	for(var i = 0; i < imgsArr.length; i++) hide.innerHTML += '<img src="'+imgsArr[i]+'" />';	
	var imgs = hide.getElementsByTagName('img');
	var count = 0;
	var start = new Date().getTime();
	(function() {
		if(count < imgs.length) {
			if(imgs[count].complete == true) {
				if(/MSIE 6/.test(navigator.userAgent)) {
					var src = imgs[count].src;
					imgs[count].src  = 'templates/site/images/blank.gif';
					imgs[count].style.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src='+src+',sizingMethod=image)';		
				}
				count++;
				arguments.callee();	
			}
			else {				
				var nov = new Date().getTime();
				if(nov - start < time) setTimeout(arguments.callee, 100);
				else {
					// Закончилось время
				}
			}
		}
		else {
			callback.call(obj, imgs);
		}
	})();	
}

function prevElem(elem) {
	do {elem = elem.previousSibling;}
	while(elem && elem.nodeType != 1);
	return elem;
}

function nextElem(elem) {
	do {elem = elem.nextSibling;}
	while(elem && elem.nodeType != 1);
	return elem;
}

function windowSize() {
	var 
		e = document.documentElement, 
		d = document.body,
		cw = e.clientWidth,
		ch = e.clientHeight,
		sw = e.scrollWidth,
		sh = e.scrollHeight,
		st = e.scrollTop > d.scrollTop ? e.scrollTop : d.scrollTop,
		sl = e.scrollLeft,
		vw = cw > sw ? cw : sw,
		vh = ch > sh ? ch : sh;
	return {'vw': vw, 'vh': vh, 'cw': cw, 'ch': ch, 'sw': sw, 'sh': sh, 'st': st, 'sl': sl}
}


function addEvent(elem, type, handler) {
  if(element.addEventListener) elem.addEventListener(type, handler, false);
  else if(document.attachEvent) elem.attachEvent('on'+type, handler);
  else elem['on'+type] = handler;
}



function centerAbsolute(elem) {
	var size = windowSize();
	elem.style.top = parseInt(size.st+size.ch/2-elem.offsetHeight/2)+'px';
	elem.style.left = parseInt(size.sl+size.cw/2-elem.offsetWidth/2)+'px';
}



function fixPNG(element) {
		if (/MSIE (5\.5|6).+Win/.test(navigator.userAgent)) {
			var src;
        	if (element.tagName=='IMG') {
				if (/\.png$/.test(element.src)) {
        			src = element.src;
        			element.src = dir+'images/blank.gif';
				}
    		}
			else {
      			src = element.currentStyle.backgroundImage.match(/url\("(.+\.png)"\)/i);
      			if (src) {
        			src = src[1];
        			element.runtimeStyle.backgroundImage="none";
      			}
    		}    		
    		if (src) element.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "',sizingMethod='scale')";
  		}
	}
	
	function getScreenSize() {
		return {width: window.screen.width, height: window.screen.height};
	}
	
function screenSize() {
      var w, h; 
      w = (window.innerWidth ? window.innerWidth : (document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body.offsetWidth));
      h = (window.innerHeight ? window.innerHeight : (document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.offsetHeight));
      return {w:w, h:h};
} 

function getPageSize() {

	     var xScroll, yScroll;

		if (window.innerHeight && window.scrollMaxY) {
			xScroll = window.innerWidth + window.scrollMaxX;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}

		var windowWidth, windowHeight;

		if (self.innerHeight) {	// all except Explorer
			if(document.documentElement.clientWidth){
				windowWidth = document.documentElement.clientWidth;
			} else {
				windowWidth = self.innerWidth;
			}
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}

		// for small pages with total height less then height of the viewport
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else {
			pageHeight = yScroll;
		}

		// for small pages with total width less then width of the viewport
		if(xScroll < windowWidth){
			pageWidth = xScroll;
		} else {
			pageWidth = windowWidth;
		}
		return [pageWidth,pageHeight, xScroll, yScroll];
	}

function declensionNum(num, arr) {
	var num = num.toString(), l = num.substr(num.length - 1, 1), tl;
	if(num.length > 1) tl = num.substr(num.length - 2, 2);							
	if(l == '1') {
		if(tl && tl == '11') return arr[3];
		else return arr[0];
	}
	else if(l == '2' || l == '3' || l == '4') {
		if(tl && (tl == '12' || tl == '13' || tl == '14')) return arr[2];
		else return arr[1];
	}
	else return arr[2];
}

function encode_utf8( s )
{
  return unescape( encodeURIComponent( s ) );
}


function decode_utf8( s )
{
  return decodeURIComponent( escape( s ) );
}
