<!--
/*
	Name: 		CAirImg.js
	Ver: 		1.0a
	Author: 	Andrey Korolkov
*/
/*
	Параметры:
		imgPath - 		путь к рисункам скрипта
*/
function CAirImg (imgPath) {
// private: {
	var pImgLoading = new Image();
	var aImgLoading = {
		w: null,
		h: null
	};
	var pDiv = null;
	var pImg = null;
	var iMouseX = null;
	var iMouseY = null;
	var oldOnmousemove = null;
	var sAlign = null; // Расположение по горизонтали
	var sValign = null; // Расположение по вертикали
	var offsetX = 0;
	var offsetY = 0;
// }
// public: {
// }
// private: {
	// Отслеживаем координаты мыши
	GetMousePos = function (e) {
		var e = e || window.event; 
		
		if (document.attachEvent != null) { // Internet Explorer & Opera
			iMouseX = window.event.clientX + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
			iMouseY = window.event.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
		} else if (!document.attachEvent && document.addEventListener) { // Gecko
			if (e) {
				iMouseX = e.clientX + window.scrollX;
				iMouseY = e.clientY + window.scrollY;
			}
		} else {
			// Do nothing
		}

		// Вызываем старый обработчик
		if (oldOnmousemove != null) {
			oldOnmousemove(e);
		}
	}

	// Показывает изображение
	ShowImage = function (a) {
		var params = 	a.rel.split(':');
		var className = params[1]; // Название CSS Класса
		sAlign = 		params[2]; // Выравнивание по горизонтали
		sValign = 		params[3]; // Выравнивание по вертикали

		if (params[0] == 'airimg') {
			pDiv.className = className;
			pDiv.style.display = 'block';

			// Смещение от указателя мыши
			offsetX = pDiv.offsetWidth - aImgLoading.w + 10;
			offsetY = pDiv.offsetHeight - aImgLoading.h + 10;

			var pImage = new Image();
			pImage.onload = function () {
				pImg.width = 	pImage.width;
				pImg.height = 	pImage.height;
				pImg.src = 		pImage.src;

				// Смещение от указателя мыши
				offsetX = pDiv.offsetWidth - (pImg.width * 1) + 10;
				offsetY = pDiv.offsetHeight - (pImg.height * 1) + 10;

				MoveImage();

				pImage.onload = function () {};	// высвобождаем событие
			}

			pImage.src = a.href;
		}
	}

	// Перемещает изображение
	MoveImage = function (e) {
		// Получаем координаты мыши
		if (pDiv.style.display == 'block') {
			GetMousePos(e);

			// Смещение от указателя мыши
			offsetX = pDiv.offsetWidth - (pImg.width * 1) + 10;
			offsetY = pDiv.offsetHeight - (pImg.height * 1) + 10;
		}

		// Выравнивание по горизонтали
		// Слева
		if (sAlign == 'left') {
			pDiv.style.left = (iMouseX - pImg.width - offsetX) + 'px';
		}

		// Справа
		if (sAlign == 'right') {
			pDiv.style.left = (iMouseX + 10) + 'px';
		}

		// По центру
		if (sAlign == 'center') {
			pDiv.style.left = (iMouseX - pImg.width / 2) + 'px';
		}

		// Выравнивание по вертикали
		// Сверху
		if (sValign == 'top') {
			pDiv.style.top = (iMouseY - pImg.height - offsetY) + 'px';
		}

		// Снизу
		if (sValign == 'bottom') {
			pDiv.style.top = (iMouseY + 10) + 'px';
		}

		// По середине
		if (sValign == 'middle') {
			pDiv.style.top = (iMouseY - pImg.height / 2) + 'px';
		}
	}

	// Прячет изображение
	HideImage = function () {
		pDiv.style.display = 'none';
	}

	// Добавляет в теги события
	UpdateAnchors = function () {
		if (!document.getElementsByTagName){
			return;
		}

		var anchors = document.getElementsByTagName('a');
		var areas = document.getElementsByTagName('area');

		// Перебираем все теги ссылок "<a>...</a>"
		for (var i = 0; i < anchors.length; i++) {
			var a = anchors[i];
			var relAttr = String(a.getAttribute('rel'));

			// Выясняем налчие котовой строки
			if (a.getAttribute('href') && (relAttr.toLowerCase().match('airimg'))) {
				a.onmouseover = function () {
					ShowImage(this);
					return false;
				}
				a.onmousemove = function () {
					MoveImage();
					return false;
				}
				a.onmouseout = function () {
					HideImage();
					return false;
				}
			}
		}

		// Перебираем все теги ссылок "<area>...</area>"
		for (var i = 0; i < areas.length; i++){
			var area = areas[i];
			
			var relAttribute = String(area.getAttribute('rel'));
			
			// Выясняем налчие котовой строки
			if (area.getAttribute('href') && (relAttribute.toLowerCase().match('airimg'))){
				a.onmousemove = function () {
					alert(this.href);
					return false;
				}
				a.onmouseout = function () {
					alert(this.href);
					return false;
				}
			}
		}
	}

	// Инициализация скрипта
	// Создаём слой для рисунка
	var pBody = document.getElementsByTagName('body').item(0);
	
	pDiv = document.createElement('div');
	pDiv.style.display = 	'none';
	pDiv.style.position = 	'absolute';
	pBody.appendChild(pDiv);

	// Загружаем анимацию статуса загрузки
	pImgLoading.onload = function () {
		aImgLoading.w = pImgLoading.width;
		aImgLoading.h = pImgLoading.height;

		pImg = 			document.createElement('img');
		pImg.width = 	aImgLoading.w;
		pImg.height = 	aImgLoading.h;
		pImg.src = 		pImgLoading.src;

		pDiv.appendChild(pImg);

		pImgLoading.onload = function () {};	// высвобождаем событие
	}

	pImgLoading.src = imgPath + 'loading.gif';

	// Создаём глобальное событие
	if (document.onmousemove != 'undefined') {
		oldOnmousemove = document.onmousemove;
	}

	document.onmousemove = MoveImage;

	// Загружаем список рисунков
	UpdateAnchors();
// }
}
//-->

