var iZoomed=0;

var sImgWaitURL = "http://www.neorealty.ru/scripts.js/preload.gif";

var sImgURL=""; // Zoomed image URL

var sLastLoadedImgURL="none"; // Last zoomed image loaded

var vBkgrDiv, // Background layer to shade the whole page
    vZmDiv, // The layer to hold zoomed picture
    vZmDivImg, // Zoomed picture itself
    vZmDivControls, // Control pane layer: 'prev', 'next', 'play', 'close'
    vImgURLNode, // Zoomed picture URL
    vZmDivText, // Zoomed picture title
    vZmDivNewA, vZmDivNewAText, // To open zoomed picture in a new window
    vZmDivCount, // [iPicture/nPictures] 
    vZmDivPrev, vZmDivPrevText, // To switch to the previous image
    vZmDivNext, vZmDivNextText, // To switch to the next image
    vZmDivPlay, vZmDivPlayText, // To start and stop play mode 
    vZmDivClose, vZmDivCloseText, // To close zoom mode
    vWaitDiv, vWaitDivImg; // To display 'wait' icon

var aHRefs = new Array(); // Zoomable images array (navigation)

iZoomedDivMaxHorMargin=100; // Horizontal margin between zoom layer and client area border
iZoomedDivMaxVerMargin=50; // Vertical margim between zoom layer and client area border

// Interval for syncronization...
var iInterval=-1;

// To unzoom when scrolled
var iOriginalScrollTop;

// Currently displayed image
var iZmDivImg = -1; 

// Play switching between images
var iPlay = 0;
var iPlayCounter = -1;
var aPlay = new Array();
aPlay[0] = " ..." + String.fromCharCode(187) + "... ";
aPlay[1] = " ...." + String.fromCharCode(187) + ".. ";
aPlay[2] = " ....." + String.fromCharCode(187) + ". ";
aPlay[3] = " ......" + String.fromCharCode(187) + " ";
aPlay[4] = " " + String.fromCharCode(187) + "...... ";
aPlay[5] = " ." + String.fromCharCode(187) + "..... ";
aPlay[6] = " .." + String.fromCharCode(187) + ".... ";

var vWaitDivImgLoader = new Image;
vWaitDivImgLoader.src = sImgWaitURL;
iWaitDivImgWidth = 200;
iWaitDivImgHeight = 200;
iWaitDivWidth = iWaitDivImgWidth;
iWaitDivHeight = iWaitDivImgHeight;

var vZmDivImgLoader = new Image;
vZmDivImgLoader.onload = function()
{
vZmDivImg.src = vZmDivImgLoader.src;
}

var iStylized=0;
window.onload = fnStylizeZoom;

function fnStylizeZoom()
{
if( !document.getElementById )
  { return; }

vBkgrDiv = document.createElement("div");
vBkgrDiv.className="ZoomedBkgr";
vBkgrDiv.style.width=document.body.clientWidth;
vBkgrDiv.style.height=document.body.clientHeight;
vBkgrDiv.style.display='none';
vBkgrDiv.onclick=fnUnzoom;
document.body.appendChild(vBkgrDiv);

// ****************
// 'Zoom' layer
vZmDiv = document.createElement("div");
vZmDiv.className = 'Zoomed';
vZmDiv.style.top = document.body.scrollTop+50;
vZmDiv.style.left = 100;
vZmDiv.style.display='none';
document.body.appendChild(vZmDiv);

vZmDivImg = document.createElement("img"); //document.createElement("img");
vZmDivImg.style.display='none';
vZmDivImg.onclick = fnUnzoom;
vZmDivImg.onload = fnImgOnLoad;
vZmDiv.appendChild(vZmDivImg);

vZmDivControls = document.createElement("div");
vZmDivControls.className = 'ZoomedControlPane';
vZmDivControls.style.display='block';
vZmDiv.appendChild(vZmDivControls);

vZmDivText = document.createTextNode( "Untitled" );
vZmDivControls.appendChild(vZmDivText);

// *********************************************
// Assigning 'onclick' handlers

aObjs = document.getElementsByTagName("a");
for( i = 0 ; i < aObjs.length ; i++ )
  {
   vObj = aObjs[i];
   if( vObj.className != 'StylizeZoom' )
     { continue; }

   vObj.onclick = fnZoom;

   vObj.iNumber = aHRefs.length;

   aHRefs[aHRefs.length] = vObj;
  }


// Controls ************************************
// 'Open in a new window'

vZmDivNewA = document.createElement( "a" );
vZmDivNewA.className = 'StylizeZoomGoto';
vZmDivNewA.setAttribute( "target", "_blank" );

vZmDivNewAText = document.createTextNode( String.fromCharCode(160) + "+" + String.fromCharCode(160) );
vZmDivNewA.appendChild(vZmDivNewAText);                            
vZmDivControls.appendChild(vZmDivNewA);

// Controls ************************************
// 'Prev', 'Next', 'Play' and 'Close'

//if( aHRefs.length > 1 )
//  {
   vZmDivPrev = document.createElement( "a" );           
   vZmDivPrev.className = 'StylizeZoomGoto';
   vZmDivPrevText = document.createTextNode( String.fromCharCode(8592) );
   vZmDivPrev.appendChild(vZmDivPrevText);
   vZmDivControls.appendChild(vZmDivPrev);

   vZmDivCount = document.createTextNode( "?/?" );
   vZmDivControls.appendChild(vZmDivCount);

   vZmDivNext = document.createElement( "a" );
   vZmDivNext.className = 'StylizeZoomGoto';
   vZmDivNextText = document.createTextNode( String.fromCharCode(8594) );
   vZmDivNext.appendChild(vZmDivNextText);
   vZmDivControls.appendChild(vZmDivNext);

   vZmDivPlay = document.createElement( "a" );
   vZmDivPlay.setAttribute( "href", "javascript:fnPlaySwitch();" );
   vZmDivPlay.className = 'StylizeZoomGoto';
   vZmDivPlayText = document.createTextNode( aPlay[0] ); // String.fromCharCode(187)
   vZmDivPlay.appendChild(vZmDivPlayText);
   vZmDivControls.appendChild(vZmDivPlay);
//  }

vZmDivClose = document.createElement( "a" );
vZmDivClose.setAttribute( "href", "javascript:fnUnzoom();" );
vZmDivClose.className = 'StylizeZoomClose';
vZmDivCloseText = document.createTextNode( " X " );
vZmDivClose.appendChild(vZmDivCloseText);
vZmDivControls.appendChild(vZmDivClose);

// **********************************************
// 'Wait' layer

vWaitDiv = document.createElement("div");
vWaitDiv.className = 'ZoomedWait';
vWaitDiv.onclick = fnUnzoom;
vWaitDiv.style.display = 'none';
vWaitDiv.style.width = iWaitDivWidth;
vWaitDiv.style.height = iWaitDivHeight;
document.body.appendChild(vWaitDiv);

vWaitDivImg = document.createElement("img"); //document.createElement("img");
vWaitDivImg.src = sImgWaitURL;
vWaitDivImg.width = iWaitDivImgWidth;
vWaitDivImg.height = iWaitDivImgHeight;
vWaitDiv.appendChild(vWaitDivImg);

iStylized=1;
}

// *********************************
// 'Wait' image 'onload' handler

function fnWaitOn()
{
if( iZoomed == 0 )
  { return; }

vWaitDiv.style.top = document.body.scrollTop + document.body.clientHeight/2 - iWaitDivHeight/2;
vWaitDiv.style.left = document.body.clientWidth/2 - iWaitDivWidth/2;

vWaitDiv.style.display='block';
vWaitDivImg.style.display='block';
}

function fnWaitOff()
{
vWaitDiv.style.display='none';
vWaitDivImg.style.display='none';
}

// *******************************
// Zoom image 'onload' handler

function fnImgOnLoad()
{
if( iZoomed == 0 )
  { return; }

fnWaitOff();

sLastLoadedImgURL = sImgURL;

vZmDiv.style.display='block';
vZmDivImg.style.display='block';

fnPositionZoomedDiv();

iPlayCounter = 0;
}

// ****************
// Zoom handler

function fnZoom()
{
fnZoomParam( this, 0, 0 );
}

function fnZoomParam( vThis, iSwitching, iPlaying )
{
if( iStylized == 0 || (iZoomed != 0 && iSwitching != 1) ) // Not stylized or already zoomed and
  { return; }                                             // not switching - exiting the function
iZoomed=1;

vImgURLNode = vThis.getAttributeNode("href"); // Getting the URL of the image to zoom
sImgURL = vImgURLNode.value;
vImgURLNode.value = 'javascript:void(0);'; // Changing the target to prevent opening it's URL
sTitle = new String("");
sTitle = vThis.title;
if( !(sTitle.length > 0) )
  {
   if( vNode = vThis.firstChild.getAttributeNode("alt") )
     {  sTitle = vNode.value; }
  }
sTitle += String.fromCharCode(160)+String.fromCharCode(160)+String.fromCharCode(160)+String.fromCharCode(160);
vZmDivText.nodeValue = sTitle;
vZmDivNewA.setAttribute( "href", sImgURL );
vZmDivPrev.setAttribute( "href", "javascript:fnSwitch(" + vThis.iNumber + ",-1,0);" ); 
vZmDivNext.setAttribute( "href", "javascript:fnSwitch(" + vThis.iNumber + ",1,0);" );  

vZmDivCount.nodeValue = "[ " + (vThis.iNumber + 1) + "/" + aHRefs.length + " ]";  

//vZmDivClose.focus();

vBkgrDiv.style.left=0;
vBkgrDiv.style.top=document.body.scrollTop;
vBkgrDiv.style.display='block';

// if( !(iPlaying == 1) )
//   { fnWaitOn(); }
fnWaitOn();

vZmDivImgLoader.src = sImgURL; // Loading image into loader, to copy into DOM element vZmDivImg

if( sImgURL == sLastLoadedImgURL )
  { fnImgOnLoad(); }

// To unzoom when scrolled...
fnSetInterval();

// Currently displayed image
iZmDivImg = vThis.iNumber;
}

// **************************
// Unzoom image procedure

function fnUnzoom()
{
fnWaitOff();

vZmDiv.style.display='none';

vBkgrDiv.style.display='none';

vImgURLNode.value = sImgURL;

fnClearInterval();

fnPlaySet(0);

iZoomed=0;
}

// **************************
// Switch image procedure

function fnSwitch( iNow, iStep, iPlaying )
{
var iNext;

if( aHRefs.length == 1 )
  { return; }

vImgURLNode.value = sImgURL;

iNext = iNow + iStep;
if( iNext > aHRefs.length - 1 )
  { iNext = 0; }
else
  {
   if( iNext < 0 )
     { iNext = aHRefs.length - 1; }
  }
fnZoomParam( aHRefs[iNext], 1, iPlaying );
}


// **************************
// Positioning zoom layer

function fnPositionZoomedDiv()
{
iCWidth = document.body.clientWidth;
iCHeight = document.body.clientHeight;
iIWidth = vZmDivImgLoader.width;
iIHeight = vZmDivImgLoader.height;

if( iCWidth - iZoomedDivMaxHorMargin*2 < iIWidth || iCHeight - iZoomedDivMaxVerMargin*2 < iIHeight )
  {
   fCRatio = iCWidth/iCHeight;
   fIRatio = iIWidth/iIHeight;
   if( fIRatio > fCRatio ) // The image is less fittable in width
     { iIWidth = iCWidth - iZoomedDivMaxHorMargin*2;
       iIHeight = iIWidth / fIRatio; }
   else
     { iIHeight = iCHeight - iZoomedDivMaxVerMargin*2;
       iIWidth = iIHeight * fIRatio; }
  }
vZmDivImg.width = iIWidth;
vZmDivImg.height = iIHeight;

vZmDiv.style.left = (iCWidth - iIWidth)/2;
vZmDiv.style.top = document.body.scrollTop + (iCHeight - iIHeight)/2;

//vZmDiv.style.width = iIWidth;
//vZmDiv.style.height = iIHeight+24;
}


// **************************************
// To unzoom if scrolling client area

function fnSetInterval()
{
fnClearInterval();
iInterval = setInterval( fnInterval, 100 );

iOriginalScrollTop = document.body.scrollTop;
}

function fnClearInterval()
{
if( iInterval != -1 )
  { clearInterval(iInterval);
    iInterval = -1; }
}


function fnInterval()
{
if( iOriginalScrollTop != document.body.scrollTop )
  { fnUnzoom(); }

if( iPlay == 1 )
  { 
   if( iPlayCounter == 40 )
     { fnSwitch( iZmDivImg, 1, 1 ); }

   var iIndex = Math.floor( iPlayCounter/2 % aPlay.length );
   vZmDivPlayText.nodeValue = aPlay[ iIndex ];

   iPlayCounter += 1;
  }

}

// **************************************
// Play switching between images

function fnPlaySwitch()
{
fnPlaySet(-1);
}

function fnPlaySet( iSet )
{
if( iSet == -1 )
  { iPlay = (iPlay == 1 ) ? 0 : 1; }
else
  { iPlay = iSet; }

//vZmDivPlay.style.textDecoration = (iPlay == 1) ? 'blink' : 'none';
}
