
//------------------------------------------------------------------------------------
// Н А Ч А Л О   Ф У Н К Ц И Й   Д Л Я   М О Д Н О Г О   П О К А З А   П Р О Д У К Т А 

// определение координаты элемента по горизонтали
function pageX(elem)
{
	return elem.offsetParent ?
		elem.offsetLeft + pageX( elem.offsetParent ) :
		elem.offsetLeft;
}
// определение координаты элемента по вертикали
function pageY(elem)
{
	return elem.offsetParent ?
		elem.offsetTop + pageY( elem.offsetParent ) :
		elem.offsetTop;
//	return elem.offsetTop;
}

// определение скроллинга страницы
function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  var tt = new Array();
  tt['x'] = scrOfX;
  tt['y'] = scrOfY;
//  document.getElementById("zzz").innerHTML='scrOfX: ' + scrOfX + '<br>scrOfY: ' + scrOfY;
  return tt;
}
var ex=0, ey=0, picX=0, picY=0;
var picWidth=0, picHeight=0, imgWidth=0, imgHeight=0, lensWidth=0, lensHeight=0, factorX=1, factorY=1, cursorPicX=0, cursorPicY=0, cursorImgX=0, cursorImgY=0;
var lensIcoWidth=88, lensIcoHeight=69;
function photoLensMove(mEvent)
{
	var pageScrollXY = getScrollXY();
  ex = mEvent.clientX + pageScrollXY['x'];
	ey = mEvent.clientY + pageScrollXY['y']; // по экрану + скроллинг = по странице
	picX = pageX(document.getElementById("pic1"));
	picY = pageY(document.getElementById("pic1")); // по странице
	picWidth = document.getElementById("pic1").width;
	picHeight = document.getElementById("pic1").height;
	imgWidth = document.getElementById("img1").width;
	imgHeight = document.getElementById("img1").height;
	var lensWidthPX = document.getElementById("lens").style.width;
	lensWidth = lensWidthPX.replace('px', '')
	var lensHeightPX = document.getElementById("lens").style.height;
	lensHeight = lensHeightPX.replace('px', '')
	factorX = imgWidth / picWidth;
	cursorPicX = ex - picX;
	cursorImgX = cursorPicX * factorX;
	factorY = imgHeight / picHeight;
	cursorPicY = ey - picY;
	cursorImgY = cursorPicY * factorY;
	//alert(picX);
	lens.style.display = "block";
	lens.style.width = (lensIcoWidth-2) * factorX + "px";
	lens.style.height = (lensIcoHeight-2) * factorY + "px";
	lens.style.left = -(lensIcoWidth) * factorY - 20 + "px";
	lens.style.top = -picHeight - 60 + "px";
	lens1.style.display = "block";
	var picDivX = pageX(document.getElementById("picDiv"));
	cursorLens.style.width = picWidth;
	cursorLens.style.height = picHeight;
	cursorLens.style.right = picX-picDivX;
	 // "88" и "69" - половина размеров картинки id=cursorLens  в пикселях
//	cursorLens.style.display = "block";

// определение граничных положений линзы
  var positionX = 0-cursorImgX + (lensWidth / 2);
  var stopLeft = lensWidth / 2;
  var stopRight = imgWidth - (lensWidth / 2);
	if(cursorImgX < stopLeft) positionX = 0;
	if(cursorImgX > stopRight) positionX = 0-(imgWidth-lensWidth);
	
  var positionY = 0-cursorImgY + (lensHeight / 2);
  var stopTop = lensHeight / 2;
  var stopBottom = imgHeight - (lensHeight / 2);
  if(cursorImgY < stopTop) positionY = 0;
  if(cursorImgY > stopBottom) positionY = 0-(imgHeight-lensHeight);
	
	lens.style.backgroundPosition = positionX + "px " + positionY + "px";
	
  var positionLensPicX = cursorPicX-588;
  var stopLensLeft = lensIcoWidth / 2;
  var stopLensRight = picWidth - (lensIcoWidth / 2);
  if(cursorPicX < stopLensLeft) positionLensPicX = -544;
  if(cursorPicX > stopLensRight) positionLensPicX = stopLensRight - 588;
	
  var positionLensPicY = cursorPicY-569;
  var stopLensTop = lensIcoHeight / 2;
  var stopLensBottom = picHeight - (lensIcoHeight / 2);
  if(cursorPicY < stopLensTop) positionLensPicY = -535;
  if(cursorPicY > stopLensBottom) positionLensPicY = stopLensBottom - 570;
	
	cursorLens.style.backgroundPosition = positionLensPicX + "px " + positionLensPicY + "px";

//	document.getElementById("vvv").innerHTML="ex="+ex+" || picX="+picX+" || cursorPicX="+cursorPicX;
}
function lensPopUp()
{

	cursorLens.style.width = document.getElementById("pic1").width;
	cursorLens.style.height = document.getElementById("pic1").height;
}
function photoLensOut()
{
//	document.getElementById("vvv").innerHTML="";
	lens.style.display = "none";
	lens1.style.display = "none";
	//cursorLens.style.display = "none";
}



/* кроссбраузерная прозрачность

пример: setElementOpacity(document.body, 0.5); 
*/
function setElementOpacity(oElem, nOpacity)
{
	var p = getOpacityProperty();
	(setElementOpacity = p=="filter"?new Function('oElem', 'nOpacity', 'nOpacity *= 100;	var oAlpha = oElem.filters["DXImageTransform.Microsoft.alpha"] || oElem.filters.alpha;	if (oAlpha) oAlpha.opacity = nOpacity; else oElem.style.filter += "progid:DXImageTransform.Microsoft.Alpha(opacity="+nOpacity+")";'):p?new Function('oElem', 'nOpacity', 'oElem.style.'+p+' = nOpacity;'):new Function)(oElem, nOpacity);
}

// функция getOpacityProperty() возвращает свойство, которое пользуется для сменыпрозрачности или undefined, и может использоваться для изменения прозрачности
function getOpacityProperty()
{
	var p;
	if (typeof document.body.style.opacity == 'string') p = 'opacity';
	else if (typeof document.body.style.MozOpacity == 'string') p =  'MozOpacity';
	else if (typeof document.body.style.KhtmlOpacity == 'string') p =  'KhtmlOpacity';
	else if (document.body.filters && navigator.appVersion.match(/MSIE ([\d.]+);/)[1]>=5.5) p =  'filter';
	
	return (getOpacityProperty = new Function("return '"+p+"';"))();
}

/* для плавного изменения прозрачности:

1) fadeOpacity.addRule('opacityRule1', 1, 0.5, 30); //вначале создаем правило, задаем имя правила, начальную прозрачность и конечную, необязательный параметр задержки, влияющий на скорость смены прозрачности
2) fadeOpacity('elemID', 'opacityRule1'); // выполнить плавную смену прозрачности элемента с id равным elemID, по правилу opacityRule1
3) fadeOpacity.back('elemID'); //вернуться в исходное состояние прозрачности
*/
function fadeOpacity(sElemId, sRuleName, bBackward)
{
	var elem = document.getElementById(sElemId);
	if (!elem || !getOpacityProperty() || !fadeOpacity.aRules[sRuleName]) return;
	
	var rule = fadeOpacity.aRules[sRuleName];
	var nOpacity = rule.nStartOpacity;
	
	if (fadeOpacity.aProc[sElemId]) {clearInterval(fadeOpacity.aProc[sElemId].tId); nOpacity = fadeOpacity.aProc[sElemId].nOpacity;}
	if ((nOpacity==rule.nStartOpacity && bBackward) || (nOpacity==rule.nFinishOpacity && !bBackward)) return;

	fadeOpacity.aProc[sElemId] = {'nOpacity':nOpacity, 'tId':setInterval('fadeOpacity.run("'+sElemId+'")', fadeOpacity.aRules[sRuleName].nDalay), 'sRuleName':sRuleName, 'bBackward':Boolean(bBackward)};
}

fadeOpacity.addRule = function(sRuleName, nStartOpacity, nFinishOpacity, nDalay){fadeOpacity.aRules[sRuleName]={'nStartOpacity':nStartOpacity, 'nFinishOpacity':nFinishOpacity, 'nDalay':(nDalay || 30),'nDSign':(nFinishOpacity-nStartOpacity > 0?1:-1)};};

fadeOpacity.back = function(sElemId){fadeOpacity(sElemId,fadeOpacity.aProc[sElemId].sRuleName,true);};

fadeOpacity.run = function(sElemId)
{
	var proc = fadeOpacity.aProc[sElemId];
	var rule = fadeOpacity.aRules[proc.sRuleName];
	
	proc.nOpacity = Math.round(( proc.nOpacity + .1*rule.nDSign*(proc.bBackward?-1:1) )*10)/10;
	setElementOpacity(document.getElementById(sElemId), proc.nOpacity);
	
	if (proc.nOpacity==rule.nStartOpacity || proc.nOpacity==rule.nFinishOpacity) clearInterval(fadeOpacity.aProc[sElemId].tId);
}
fadeOpacity.aProc = {};
fadeOpacity.aRules = {};


// Создаем правило изменения прозрачности: задаем имя правила, начальную и конечную прозрачность, а также необязательный параметр задержки влияющий на скорость смены прозрачности
  fadeOpacity.addRule('oR1', 0, .5, 30);
// К О Н Е Ц   Ф У Н К Ц И Й   Д Л Я   М О Д Н О Г О   П О К А З А   П Р О Д У К Т А 
//----------------------------------------------------------------------------------

