var iZoomed=0;

var sSZ_ImgWaitURL = "http://www.neorealty.ru/scripts.js/preload.gif";

var sSZ_ImgURL = ""; // Zoomed image URL

var sSZ_LastLoadedImgURL = "none"; // Last zoomed image loaded

var vSZ_BkgrDiv, // Background layer to shade the whole page
    vSZ_ZoomDiv, // The layer to hold zoomed picture
    vSZ_ZoomDivImg, // Zoomed picture itself
    vSZ_ZoomDivControls, // Control pane layer: 'prev', 'next', 'play', 'close'
    vSZ_ImgURLNode, // Zoomed picture URL
    vSZ_ZoomDivText, // Zoomed picture title
    vSZ_ZoomDivNewA, vSZ_ZoomDivNewAText, // To open zoomed picture in a new window
    vSZ_ZoomDivCount, // [iPicture/nPictures] 
    vSZ_ZoomDivPrev, vSZ_ZoomDivPrevText, // To switch to the previous image
    vSZ_ZoomDivNext, vSZ_ZoomDivNextText, // To switch to the next image
    vSZ_ZoomDivPlay, vSZ_ZoomDivPlayText, // To start and stop play mode 
    vSZ_ZoomDivClose, vSZ_ZoomDivCloseText, // To close zoom mode
    vSZ_WaitDiv, vSZ_WaitDivImg; // To display 'wait' icon

var aSZ_HRefs = new Array(); // Zoomable images array (navigation)

iSZ_ZoomedDivMaxHorMargin=100; // Horizontal margin between zoom layer and client area border
iSZ_ZoomedDivMaxVerMargin=50; // Vertical margim between zoom layer and client area border

// Interval for syncronization...
var iSZ_Interval=-1;

// To unzoom when scrolled
var iSZ_OriginalScrollTop;

// Currently displayed image
var iSZ_ZoomDivImg = -1; 

// Play switching between images
var iSZ_Play = 0;
var iSZ_PlayCounter = -1;
var aSZ_Play = new Array();
aSZ_Play[0] = " ..." + String.fromCharCode(187) + "... ";
aSZ_Play[1] = " ...." + String.fromCharCode(187) + ".. ";
aSZ_Play[2] = " ....." + String.fromCharCode(187) + ". ";
aSZ_Play[3] = " ......" + String.fromCharCode(187) + " ";
aSZ_Play[4] = " " + String.fromCharCode(187) + "...... ";
aSZ_Play[5] = " ." + String.fromCharCode(187) + "..... ";
aSZ_Play[6] = " .." + String.fromCharCode(187) + ".... ";

var vSZ_WaitDivImgLoader = new Image;
vSZ_WaitDivImgLoader.src = sSZ_ImgWaitURL;
iSZ_WaitDivImgWidth = 200;
iSZ_WaitDivImgHeight = 200;
iSZ_WaitDivWidth = iSZ_WaitDivImgWidth;
iSZ_WaitDivHeight = iSZ_WaitDivImgHeight;

var vSZ_ZoomDivImgLoader = new Image;
vSZ_ZoomDivImgLoader.onload = function()
{
vSZ_ZoomDivImg.src = vSZ_ZoomDivImgLoader.src;
}

var iStylized=0;
// window.onload = fnSZ_Initialize;

function fnSZ_Initialize()
{
if( !document.getElementById )
  { return; }

vSZ_BkgrDiv = document.createElement("div");
vSZ_BkgrDiv.className="ZoomedBkgr";
vSZ_BkgrDiv.style.width=document.body.clientWidth;
vSZ_BkgrDiv.style.height=document.body.clientHeight;
vSZ_BkgrDiv.style.display='none';
vSZ_BkgrDiv.onclick=fnSZ_Unzoom;
document.body.appendChild(vSZ_BkgrDiv);

// ****************
// 'Zoom' layer
vSZ_ZoomDiv = document.createElement("div");
vSZ_ZoomDiv.className = 'Zoomed';
vSZ_ZoomDiv.style.top = document.body.scrollTop+50;
vSZ_ZoomDiv.style.left = 100;
vSZ_ZoomDiv.style.display='none';
document.body.appendChild(vSZ_ZoomDiv);

vSZ_ZoomDivImg = document.createElement("img"); //document.createElement("img");
vSZ_ZoomDivImg.style.display='none';
vSZ_ZoomDivImg.onclick = fnSZ_Unzoom;
vSZ_ZoomDivImg.onload = fnSZ_ImgOnLoad;
vSZ_ZoomDiv.appendChild(vSZ_ZoomDivImg);

vSZ_ZoomDivControls = document.createElement("div");
vSZ_ZoomDivControls.className = 'ZoomedControlPane';
vSZ_ZoomDivControls.style.display='block';
vSZ_ZoomDiv.appendChild(vSZ_ZoomDivControls);

vSZ_ZoomDivText = document.createTextNode( "Untitled" );
vSZ_ZoomDivControls.appendChild(vSZ_ZoomDivText);

// *********************************************
// Assigning 'onclick' handlers

aObjs = document.getElementsByTagName("a");
for( i = 0 ; i < aObjs.length ; i++ )
  {
   vObj = aObjs[i];
   if( vObj.className != 'StylizeZoom' )
     { continue; }

   vObj.onclick = fnSZ_Zoom;

   vObj.iNumber = aSZ_HRefs.length;

   aSZ_HRefs[aSZ_HRefs.length] = vObj;
  }


// Controls ************************************
// 'Open in a new window'

vSZ_ZoomDivNewA = document.createElement( "a" );
vSZ_ZoomDivNewA.className = 'StylizeZoomGoto';
vSZ_ZoomDivNewA.setAttribute( "target", "_blank" );

vSZ_ZoomDivNewAText = document.createTextNode( String.fromCharCode(160) + "+" + String.fromCharCode(160) );
vSZ_ZoomDivNewA.appendChild(vSZ_ZoomDivNewAText);                            
vSZ_ZoomDivControls.appendChild(vSZ_ZoomDivNewA);

// Controls ************************************
// 'Prev', 'Next', 'Play' and 'Close'

//if( aSZ_HRefs.length > 1 )
//  {
   vSZ_ZoomDivPrev = document.createElement( "a" );           
   vSZ_ZoomDivPrev.className = 'StylizeZoomGoto';
   vSZ_ZoomDivPrevText = document.createTextNode( String.fromCharCode(8592) );
   vSZ_ZoomDivPrev.appendChild(vSZ_ZoomDivPrevText);
   vSZ_ZoomDivControls.appendChild(vSZ_ZoomDivPrev);

   vSZ_ZoomDivCount = document.createTextNode( "?/?" );
   vSZ_ZoomDivControls.appendChild(vSZ_ZoomDivCount);

   vSZ_ZoomDivNext = document.createElement( "a" );
   vSZ_ZoomDivNext.className = 'StylizeZoomGoto';
   vSZ_ZoomDivNextText = document.createTextNode( String.fromCharCode(8594) );
   vSZ_ZoomDivNext.appendChild(vSZ_ZoomDivNextText);
   vSZ_ZoomDivControls.appendChild(vSZ_ZoomDivNext);

   vSZ_ZoomDivPlay = document.createElement( "a" );
   vSZ_ZoomDivPlay.setAttribute( "href", "javascript:fnSZ_PlaySwitch();" );
   vSZ_ZoomDivPlay.className = 'StylizeZoomGoto';
   vSZ_ZoomDivPlayText = document.createTextNode( aSZ_Play[0] ); // String.fromCharCode(187)
   vSZ_ZoomDivPlay.appendChild(vSZ_ZoomDivPlayText);
   vSZ_ZoomDivControls.appendChild(vSZ_ZoomDivPlay);
//  }

vSZ_ZoomDivClose = document.createElement( "a" );
vSZ_ZoomDivClose.setAttribute( "href", "javascript:fnSZ_Unzoom();" );
vSZ_ZoomDivClose.className = 'StylizeZoomClose';
vSZ_ZoomDivCloseText = document.createTextNode( " X " );
vSZ_ZoomDivClose.appendChild(vSZ_ZoomDivCloseText);
vSZ_ZoomDivControls.appendChild(vSZ_ZoomDivClose);

// **********************************************
// 'Wait' layer

vSZ_WaitDiv = document.createElement("div");
vSZ_WaitDiv.className = 'ZoomedWait';
vSZ_WaitDiv.onclick = fnSZ_Unzoom;
vSZ_WaitDiv.style.display = 'none';
vSZ_WaitDiv.style.width = iSZ_WaitDivWidth;
vSZ_WaitDiv.style.height = iSZ_WaitDivHeight;
document.body.appendChild(vSZ_WaitDiv);

vSZ_WaitDivImg = document.createElement("img"); //document.createElement("img");
vSZ_WaitDivImg.src = sSZ_ImgWaitURL;
vSZ_WaitDivImg.width = iSZ_WaitDivImgWidth;
vSZ_WaitDivImg.height = iSZ_WaitDivImgHeight;
vSZ_WaitDiv.appendChild(vSZ_WaitDivImg);

iStylized=1;
}

// *********************************
// 'Wait' image 'onload' handler

function fnSZ_WaitOn()
{
if( iZoomed == 0 )
  { return; }

vSZ_WaitDiv.style.top = document.body.scrollTop + document.body.clientHeight/2 - iSZ_WaitDivHeight/2;
vSZ_WaitDiv.style.left = document.body.clientWidth/2 - iSZ_WaitDivWidth/2;

vSZ_WaitDiv.style.display='block';
vSZ_WaitDivImg.style.display='block';
}

function fnSZ_WaitOff()
{
vSZ_WaitDiv.style.display='none';
vSZ_WaitDivImg.style.display='none';
}

// *******************************
// Zoom image 'onload' handler

function fnSZ_ImgOnLoad()
{
if( iZoomed == 0 )
  { return; }

fnSZ_WaitOff();

sSZ_LastLoadedImgURL = sSZ_ImgURL;

vSZ_ZoomDiv.style.display='block';
vSZ_ZoomDivImg.style.display='block';

fnSZ_PositionZoomedDiv();

iSZ_PlayCounter = 0;
}

// ****************
// Zoom handler

function fnSZ_Zoom()
{
fnSZ_ZoomParam( this, 0, 0 );
}

function fnSZ_ZoomParam( vThis, iSwitching, iPlaying )
{
if( iStylized == 0 || (iZoomed != 0 && iSwitching != 1) ) // Not stylized or already zoomed and
  { return; }                                             // not switching - exiting the function
iZoomed=1;

vSZ_ImgURLNode = vThis.getAttributeNode("href"); // Getting the URL of the image to zoom
sSZ_ImgURL = vSZ_ImgURLNode.value;
vSZ_ImgURLNode.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);
vSZ_ZoomDivText.nodeValue = sTitle;
vSZ_ZoomDivNewA.setAttribute( "href", sSZ_ImgURL );
vSZ_ZoomDivPrev.setAttribute( "href", "javascript:fnSZ_Switch(" + vThis.iNumber + ",-1,0);" ); 
vSZ_ZoomDivNext.setAttribute( "href", "javascript:fnSZ_Switch(" + vThis.iNumber + ",1,0);" );  

vSZ_ZoomDivCount.nodeValue = "[ " + (vThis.iNumber + 1) + "/" + aSZ_HRefs.length + " ]";  

vSZ_BkgrDiv.style.left=0;
vSZ_BkgrDiv.style.top=document.body.scrollTop;
vSZ_BkgrDiv.style.display='block';

// if( !(iPlaying == 1) )
//   { fnSZ_WaitOn(); }
fnSZ_WaitOn();

vSZ_ZoomDivImgLoader.src = sSZ_ImgURL; // Loading image into loader, to copy into DOM element vSZ_ZoomDivImg

if( sSZ_ImgURL == sSZ_LastLoadedImgURL )
  { fnSZ_ImgOnLoad(); }

// To unzoom when scrolled...
fnSZ_SetInterval();

// Currently displayed image
iSZ_ZoomDivImg = vThis.iNumber;
}

// **************************
// Unzoom image procedure

function fnSZ_Unzoom()
{
fnSZ_WaitOff();

vSZ_ZoomDiv.style.display='none';

vSZ_BkgrDiv.style.display='none';

vSZ_ImgURLNode.value = sSZ_ImgURL;

fnSZ_ClearInterval();

fnSZ_PlaySet(0);

iZoomed=0;
}

// **************************
// Switch image procedure

function fnSZ_Switch( iNow, iStep, iPlaying )
{
var iNext;

if( aSZ_HRefs.length == 1 )
  { return; }

vSZ_ImgURLNode.value = sSZ_ImgURL;

iNext = iNow + iStep;
if( iNext > aSZ_HRefs.length - 1 )
  { iNext = 0; }
else
  {
   if( iNext < 0 )
     { iNext = aSZ_HRefs.length - 1; }
  }
fnSZ_ZoomParam( aSZ_HRefs[iNext], 1, iPlaying );
}


// **************************
// Positioning zoom layer

function fnSZ_PositionZoomedDiv()
{
iCWidth = document.body.clientWidth;
iCHeight = document.body.clientHeight;
iIWidth = vSZ_ZoomDivImgLoader.width;
iIHeight = vSZ_ZoomDivImgLoader.height;

if( iCWidth - iSZ_ZoomedDivMaxHorMargin*2 < iIWidth || iCHeight - iSZ_ZoomedDivMaxVerMargin*2 < iIHeight )
  {
   fCRatio = iCWidth/iCHeight;
   fIRatio = iIWidth/iIHeight;
   if( fIRatio > fCRatio ) // The image is less fittable in width
     { iIWidth = iCWidth - iSZ_ZoomedDivMaxHorMargin*2;
       iIHeight = iIWidth / fIRatio; }
   else
     { iIHeight = iCHeight - iSZ_ZoomedDivMaxVerMargin*2;
       iIWidth = iIHeight * fIRatio; }
  }
vSZ_ZoomDivImg.width = iIWidth;
vSZ_ZoomDivImg.height = iIHeight;

vSZ_ZoomDiv.style.left = (iCWidth - iIWidth)/2;
vSZ_ZoomDiv.style.top = document.body.scrollTop + (iCHeight - iIHeight)/2;

//vSZ_ZoomDiv.style.width = iIWidth;
//vSZ_ZoomDiv.style.height = iIHeight+24;
}


// **************************************
// To unzoom if scrolling client area

function fnSZ_SetInterval()
{
fnSZ_ClearInterval();
iSZ_Interval = setInterval( fnSZ_Interval, 100 );

iSZ_OriginalScrollTop = document.body.scrollTop;
}

function fnSZ_ClearInterval()
{
if( iSZ_Interval != -1 )
  { clearInterval(iSZ_Interval);
    iSZ_Interval = -1; }
}


function fnSZ_Interval()
{
if( iSZ_OriginalScrollTop != document.body.scrollTop )
  { fnSZ_Unzoom(); }

if( iSZ_Play == 1 )
  { 
   if( iSZ_PlayCounter == 40 )
     { fnSZ_Switch( iSZ_ZoomDivImg, 1, 1 ); }

   var iIndex = Math.floor( iSZ_PlayCounter/2 % aSZ_Play.length );
   vSZ_ZoomDivPlayText.nodeValue = aSZ_Play[ iIndex ];

   iSZ_PlayCounter += 1;
  }

}

// **************************************
// Play switching between images

function fnSZ_PlaySwitch()
{
fnSZ_PlaySet(-1);
}

function fnSZ_PlaySet( iSet )
{
if( iSet == -1 )
  { iSZ_Play = (iSZ_Play == 1 ) ? 0 : 1; }
else
  { iSZ_Play = iSet; }

//vSZ_ZoomDivPlay.style.textDecoration = (iSZ_Play == 1) ? 'blink' : 'none';
}

