// объект popup картинки
mimg			= null;
// отступы popup картинки от мыши
mimgOffset		= {x:20,y:20};
// ID картинки, на которую наведена мышь
active_movie_id	= null;
// направление вывода картинки {0|-1,0|-1}
direction		= null;

// превью "Картинка загружается"
imgLoadProcess				= new Image;
imgLoadProcess.id			= "_load";
imgLoadProcess.src			= "/files/images/_load.gif";
imgLoadProcess.overImgId	= null;

// превью "Ошибка загрузки картинки"
imgLoadError			= new Image;
imgLoadError.id			= "_error";
imgLoadError.src		= "/files/images/_error.gif";
imgLoadError.overImgId	= null;

function initImages() {
	if (!document.getElementsByTagName) return ;
	imagesList = document.getElementsByTagName('img');
	if (!imagesList) return ;

	if (!document.getElementById) return ;
	document.write('<img id="mimg" style="top:-3000px;left:0;position:absolute;display:none;" />');
	mimg = document.getElementById('mimg');
	if (!mimg) return ;

	for(i = 0; i < imagesList.length; i++) {
		if (0 == imagesList[i].id.indexOf('movie_')) {
			imagesList[i].onmousemove	= imgOnMouseMove;
			imagesList[i].onmouseover	= imgOnMouseOver;
			imagesList[i].onmouseout	= imgOnMouseOut;
			imagesList[i].popupImg		= new Image;
		}
	}
}

function imgOnMouseMove(e) {
	if (!mimg) return ;
	if (!e) e = window.event;

	coords_page = getPageEventCoords(e);

	if (!direction) {
		direction = getImgDirection(coords_page);
	}

	if (mimg.src && mimg.width && mimg.height) {
		mimg.style.top  = coords_page.py + direction.y * mimg.height + direction.oY * mimgOffset.y + "px";
		mimg.style.left = coords_page.px + direction.x * mimg.width  + direction.oX * mimgOffset.x + "px";
	}
}

function imgOnMouseOver() {
	if (!mimg) return ;
	if (!this.popupImg) return ;

	if (this.popupImg.src && this.popupImg.complete) {
		swapImg(this.popupImg);
	} else {
		swapImg(imgLoadProcess);

		var searchStr	= '.jpg';
		var replaceStr	= '_set.jpg';
		var re = new RegExp(searchStr, "g");

		imgLoadProcess.overImgId	= this.id;
		imgLoadError.overImgId		= this.id;

		this.popupImg.id		= this.id + "_set";
		this.popupImg.onload	= imgOnLoad;
		this.popupImg.onerror	= imgOnError;
		this.popupImg.src		= this.src.replace(re, replaceStr);
	}

	active_movie_id = this.id;
}

function imgOnMouseOut() {
	active_movie_id = null;
	hideImg();
}

function imgOnLoad() {
	if (active_movie_id + "_set" == this.id) {
		swapImg(this);
	}
}

function imgOnError() {
	if (active_movie_id + "_set" == this.id) {
		swapImg(imgLoadError);
	}
}

function swapImg(imgObj) {
	if (!mimg) return ;

	hideImg();

	imgLoadDelay	= (('_load' == imgObj.id) || ('_error' == imgObj.id)) ? 100 : 300;
	direction		= null;

	setTimeout(
		function () {
			if (
				(active_movie_id + "_set" == imgObj.id)
				||
				(
					(
						('_load' == imgObj.id)
						||
						('_error' == imgObj.id)
					)
					&&
					(active_movie_id == imgObj.overImgId)
				)
			) {
				mimg.src	= imgObj.src;
				mimg.width	= imgObj.width;
				mimg.height	= imgObj.height;
				mimg.style.display = "block";
			}
		},
		imgLoadDelay
	);
}

function hideImg() {
	if (!mimg) return ;
	mimg.style.display = "none";
}

function getImgDirection(pageCoords) {
	var win_size	= getWindowSize();
	var direction	= {x:0,y:0,oX:1,oY:1};

	if (win_size && win_size.X && win_size.Y && pageCoords && pageCoords.wx && pageCoords.wy) {
		if (pageCoords.wx > win_size.X - pageCoords.wx) {
			direction.x		= -1;
			direction.oX	= -1;
		}
		if (pageCoords.wy > win_size.Y - pageCoords.wy) {
			direction.y		= -1;
			direction.oY	= -1;
		}
	}

	return direction;
}

// возвращает координаты мыши относительно всей страницы
function getPageEventCoords(evt) {
	var coords = {px:0, py:0, wx:0, wy:0};

	scrolls		= getScrollSize();
	if (evt.pageX) {
		// opera, nn
		coords.px = evt.pageX;
		coords.py = evt.pageY;
		coords.wx = evt.pageX - scrolls.X;
		coords.wy = evt.pageY - scrolls.Y;
	} else {
		// ie
		coords.px = evt.clientX + scrolls.X;
		coords.py = evt.clientY + scrolls.Y;
		coords.wx = evt.clientX;
		coords.wy = evt.clientY;
	}

	return coords;
}



// возвращает значения скроллеров на просматриваемой страницы
function getScrollSize() {
	var scroll = {X:0, Y:0};

   if ( document.body && ( document.body.scrollTop || document.body.scrollLeft ) && !( window.debug || navigator.vendor == 'KDE' ) ) {
      scroll.X = document.body.scrollLeft; 
		scroll.Y = document.body.scrollTop;
   } else if ( document.documentElement && ( document.documentElement.scrollTop || document.documentElement.scrollLeft ) && !( window.debug || navigator.vendor == 'KDE' ) ) {
      scroll.X = document.documentElement.scrollLeft; 
		scroll.Y = document.documentElement.scrollTop;
   }
	return scroll;
}


// возвращает размеры видимой части окна
function getWindowSize() {
	var win_size = {X:0, Y:0};

   if (window.innerWidth && window.innerHeight) {
      win_size.X = window.innerWidth; 
		win_size.Y = window.innerHeight;
   } else if (document.documentElement && document.documentElement.clientWidth && document.documentElement.clientHeight) {
      win_size.X = document.documentElement.clientWidth; 
		win_size.Y = document.documentElement.clientHeight;
   } else if (document.body && document.body.clientWidth && document.body.clientHeight) {
      win_size.X = document.body.clientWidth; 
		win_size.Y = document.body.clientHeight;
   } else {
   	return false;	
	}
	return win_size;
}



