	var largeImg;
	var largeImgTooltip;
	
	/* Thumbnails */
	function preloadThumbs()
  {
  	for( i=1; i<=all; i++ )
  	{
  		thumbs[i] = new Image();
  		thumbs[i].src = "images/TN_" + fotos[i] + ".JPG";
  	}
  }

	/* Element positions */
	function getElementTop(eElement)
	{
    if (!eElement && this)
    {
        eElement = this;
    }

    var nTopPos = eElement.offsetTop;
    var eParElement = eElement.offsetParent;
    while (eParElement != null)
    {
        nTopPos += eParElement.offsetTop;
        eParElement = eParElement.offsetParent;
    }
    return nTopPos;
	}

	function getElementLeft(eElement)
	{
    if (!eElement && this)                       // if argument is invalid
    {                                            // (not specified, is null or is 0)
        eElement = this;                         // and function is a method
    }                                            // identify the element as the method owner

    var nLeftPos = eElement.offsetLeft;          // initialize var to store calculations
    var eParElement = eElement.offsetParent;     // identify first offset parent element
    while (eParElement != null)
    {                                            // move up through element hierarchy
        nLeftPos += eParElement.offsetLeft;      // appending left offset of each parent
        eParElement = eParElement.offsetParent;  // until no more offset parents exist
    }
    return nLeftPos;                             // return the number calculated
	}

	function getElementRight(eElement)
	{
    if (!eElement && this)
    {
        eElement = this;
    }

		return getElementLeft(eElement) + eElement.offsetWidth;
	}

	
	function setIViewPosition()
	{
		var el = document.getElementById( "i_view" );
		el.style.position = "absolute";
		el.style.top = getElementTop( document.getElementById("picture") ) + 10;
		el.style.left = getElementLeft( document.getElementById("picture") ) + 10;
		el.style.zindex = 10;	
	} 
	
  /* image preview */

  function showPreview(num, el)
  {
  	//el.style.borderColor="#FF4500";

  	p = document.getElementById("preview");
  	p.src = thumbs[num].src;
  	p.style.top = getElementTop(el) - thumbs[num].height - 5;
  	p.style.left = getElementLeft(el) - thumbs[num].width / 2 + 10;
  	p.zindex = 10;
  	p.style.visibility = "visible";

  }

  function showPreview2(fileName, el)
  {
  	//el.style.borderColor="#FF4500";

  	p = document.getElementById("preview");
  	img = new Image();
  	img.src = fileName;
  	
  	p.src = fileName;
  	p.style.top = getElementTop(el) - img.height - 5;
  	p.style.left = getElementLeft(el) - img.width / 2 + 60;
  	p.zindex = 10;
  	p.style.visibility = "visible";

  }

  function hidePreview(el)
  {
  	//el.style.borderColor="black";
  	document.getElementById("preview").style.visibility = "hidden";
  }

	 /* Page navigation */
	 function gotoPage( num )
	 {
	 	 document.location = "page" + num + ".html";
	 }

  /* Show picture */
  function showPicture(num, imgLoading)
  {
		// same image
		if( (n > 1) && (num == n) )
		  return;
		
		
		// reset memo
		var memo_row = document.getElementById("memo_row");
		var memo = document.getElementById("memo");
		
		if( memo_row != null )
		 	memo_row.style.visibility = "hidden";
		if( memo != null )
		 	memo.innerHTML = ""; 	
		
		// lame workaround
		if( (num == 1) && (n == 1) ) // show first image on page load 
			n = 0;
	
	 // move between pages
	 //  next page
	 if( (num > all) && (currPage < numOfPages) )
	 {
	 		document.location = "page" + ( currPage + 1 ) + ".html";
			return;
	 }
	 // previous
	 else if( (num == 0) && (currPage > 1 ) )
	 {
	 		document.location = "page" + ( currPage - 1 ) + ".html";
			return;
	 }

	 
	 // move between images
	 
	 if ( (num <= all) && (num > 0) )
   {
   	 var img = new Image();
   	 var picture = document.getElementById("picture");
   	 
		 
   	 picture.src = imgLoading;
   	 picture.src = "images/" + fotos[num] + ".jpg";

		 largeImg = "images/" + fotos[num] + "_L.jpg";
		 // to display width/height
		 img.src = "images/" + fotos[num] + "_L.jpg";  
		 
   	 
   	 // memo
   	 if( (memo != null) && (memos[num] != "") )
   	 {
   	 	memo.innerHTML = memos[num];
   	 	if( memo_row != null )
   	 		memo_row.style.visibility = "visible";
   	 }
  
		 b = document.getElementById("b"+num); 
		 b.className = "num_button_active";
     b.blur();

		 // deactivate previous button
     if( n > 0 )
      document.getElementById("b"+n).className = "num_button";

		 // back & forward buttons
		 b_forward = document.getElementById("forward_button");
		 b_back = document.getElementById("back_button");
		 
		 if( (num == all) && (currPage == numOfPages) )
		 {
		 	b_forward.style.visibility = "hidden";
		 }	
		 else
		 {
		 	b_forward.style.visibility = "visible";			 		
			b_forward.blur();
		 }
		 
		 if( (num == 1) && (currPage == 1) )
		 {
		 	b_back.style.visibility = "hidden";
		 }	
		 else
		 {
		 	b_back.style.visibility = "visible";			 		
			b_back.blur();
		 }

		 // large image tooltip
		 largeImgTooltip = "Посмотреть на весь экран: " + img.width + "x" + img.height;
		 imgLink = document.getElementById("img_link");
		 if( imgLink != undefined )
		 	imgLink.title = largeImgTooltip;	
			
     n = num;
     self.scrollTo(0,0);
     picture.blur();
   }
  }


	/* Show picture with optional memo */
  function showPicture2(num, imgLoading, showMemo)
  {
		//alert("memo: " + showMemo);
		//return;
		
		// same image
		if( (n > 1) && (num == n) )
		  return;
		
		
		// reset memo
		var memo_row = document.getElementById("memo_row");
		var memo = document.getElementById("memo");
		
		if( memo_row != null )
		 	memo_row.style.visibility = "hidden";
		if( memo != null )
		 	memo.innerHTML = ""; 	
		
		// lame workaround
		if( (num == 1) && (n == 1) ) // show first image on page load 
			n = 0;
	
	 // move between pages
	 //  next page
	 if( (num > all) && (currPage < numOfPages) )
	 {
	 		document.location = "page" + ( currPage + 1 ) + ".html";
			return;
	 }
	 // previous
	 else if( (num == 0) && (currPage > 1 ) )
	 {
	 		document.location = "page" + ( currPage - 1 ) + ".html";
			return;
	 }

	 
	 // move between images
	 
	 if ( (num <= all) && (num > 0) )
   {
   	 var img = new Image();
   	 var picture = document.getElementById("picture");
   	 
		 
   	 picture.src = imgLoading;
   	 picture.src = "images/" + fotos[num] + ".jpg";

		 largeImg = "images/" + fotos[num] + "_L.jpg";
		 // to display width/height
		 img.src = "images/" + fotos[num] + "_L.jpg";  
		 
   	 
   	 // memo
   	 if( showMemo && (memo != null) && (memos[num] != "") )
   	 {
   	 	memo.innerHTML = memos[num];
   	 	if( memo_row != null )
   	 		memo_row.style.visibility = "visible";
   	 }
  
		 b = document.getElementById("b"+num); 
		 b.className = "num_button_active";
     b.blur();

		 // deactivate previous button
     if( n > 0 )
      document.getElementById("b"+n).className = "num_button";

		 // back & forward buttons
		 b_forward = document.getElementById("forward_button");
		 b_back = document.getElementById("back_button");
		 
		 if( (num == all) && (currPage == numOfPages) )
		 {
		 	b_forward.style.visibility = "hidden";
		 }	
		 else
		 {
		 	b_forward.style.visibility = "visible";			 		
			b_forward.blur();
		 }
		 
		 if( (num == 1) && (currPage == 1) )
		 {
		 	b_back.style.visibility = "hidden";
		 }	
		 else
		 {
		 	b_back.style.visibility = "visible";			 		
			b_back.blur();
		 }

		 // large image tooltip
		 largeImgTooltip = "Посмотреть на весь экран: " + img.width + "x" + img.height;
		 imgLink = document.getElementById("img_link");
		 if( imgLink != undefined )
		 	imgLink.title = largeImgTooltip;	
			
     n = num;
     self.scrollTo(0,0);
   }
  }

	/* misc */
	function setClass(o, c)
  {
   o.className = c;
  }
	
	function changeColor(num, c)
	{
		document.getElementById("b"+num).style.borderColor = c;
	}

	function hideMsg()
  {
    msg = document.getElementById("info");
    msg.style.visibility = "hidden";
  }
  
  function showLargeImage( name )
  {
		var winwidth = screen.availWidth;
	  var winheight = screen.availHeight;
    
		w = window.open( "", "LargeImage", 'left=0,top=0,width=' + winwidth + ',height=' + winheight + ',scrollbars=auto,fullscreen=yes' );
    var d = w.document;
		d.write( "<html><head><script language='JavaScript' type='text/javascript'>" );
    d.write( "function SetBG(color){document.bgColor = color;}</script></head>" );
    d.write( '<body bgcolor="#999999">' );
		d.write( '<table width="100%" height="100%" border=0 cellpadding=0 cellspacing=0>' );
		d.write( '<tr><td valign=middle align=center><table border=0 cellpadding=0 cellspacing=0>' );
		d.write( '<tr><td align=center><a href="#" onClick="window.close()"><img border=0 src="' + name + '"></a></td></tr>' );
		d.write( '<tr><td>&nbsp;</td</tr>' );
		d.write( '<tr><td>' );
		d.write( '<table align=center cellpadding=0 cellspacing=2 border=0>' );
		d.write( '<tr>' );
		d.write( '<td bgcolor="#FFFFFF" onmouseover="SetBG(\'#FFFFFF\')"><img src="../../images/space.gif" width=18 height=18 border=0></td>' );
		d.write( '<td bgcolor="#E5E5E5" onmouseover="SetBG(\'#E5E5E5\')"><img src="../../images/space.gif" width=18 height=18 border=0></td>' );
		d.write( '<td bgcolor="#CCCCCC" onmouseover="SetBG(\'#CCCCCC\')"><img src="../../images/space.gif" width=18 height=18 border=0></td>' );
		d.write( '<td bgcolor="#B3B3B3" onmouseover="SetBG(\'#B3B3B3\')"><img src="../../images/space.gif" width=18 height=18 border=0></td>' );
		d.write( '<td bgcolor="#999999" onmouseover="SetBG(\'#999999\')"><img src="../../images/space.gif" width=18 height=18 border=0></td>' );
		d.write( '<td bgcolor="#808080" onmouseover="SetBG(\'#808080\')"><img src="../../images/space.gif" width=18 height=18 border=0></td>' );
		d.write( '<td bgcolor="#666666" onmouseover="SetBG(\'#666666\')"><img src="../../images/space.gif" width=18 height=18 border=0></td>' );
		d.write( '<td bgcolor="#4D4D4D" onmouseover="SetBG(\'#4D4D4D\')"><img src="../../images/space.gif" width=18 height=18 border=0></td>' );
		d.write( '<td bgcolor="#333333" onmouseover="SetBG(\'#333333\')"><img src="../../images/space.gif" width=18 height=18 border=0></td>' );
		d.write( '<td bgcolor="#1A1A1A" onmouseover="SetBG(\'#1A1A1A\')"><img src="../../images/space.gif" width=18 height=18 border=0></td>' );
		d.write( '<td bgcolor="#000000" onmouseover="SetBG(\'#000000\')"><img src="../../images/space.gif" width=18 height=18 border=0></td>' );
		d.write( '<td><img src="../../images/space.gif" width=18 height=18 border=0></td>' );
		d.write( '<td><a href="#" onClick=window.close()><img src="../../images/b_close.gif" width=18 height=18 border=0></a></td>' );
		d.write( '</tr></table>' );
		d.write( '</td></tr></table></td></tr>' );
		d.write( '<tr><td style="font-family: Verdana;font-size:10;color:black">Идея украдена с <a style="font-family:Verdana;font-size:10;color:black" href="http://www.photosight.ru" target="_new">photosight.ru</a></td></tr>' );
		d.write( '</table></body></html>' ); 
		
		d.close();
  }
 
 /*******************************
    Cookies
 ********************************/ 
  
function Get_Cookie( name ) {
	
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) )
	{
		return null;
	}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ";", len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}

/*
only the first 2 parameters are required, the cookie name, the cookie
value. Cookie time is in milliseconds, so the below expires will make the 
number you pass in the Set_Cookie function call the number of days the cookie
lasts, if you want it to be hours or minutes, just get rid of 24 and 60.

Generally you don't need to worry about domain, path or secure for most applications
so unless you need that, leave those parameters blank in the function call.
*/
function Set_Cookie( name, value, expires, path, domain, secure ) {
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );
	// if the expires variable is set, make the correct expires time, the
	// current script below will set it for x number of days, to make it
	// for hours, delete * 24, for minutes, delete * 60 * 24
	if ( expires )
	{
		expires = expires * 1000 * 60 * 60 * 24;
	}
	//alert( 'today ' + today.toGMTString() );// this is for testing purpose only
	var expires_date = new Date( today.getTime() + (expires) );
	//alert('expires ' + expires_date.toGMTString());// this is for testing purposes only

	document.cookie = name + "=" +escape( value ) +
		( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + //expires.toGMTString()
		( ( path ) ? ";path=" + path : "" ) + 
		( ( domain ) ? ";domain=" + domain : "" ) +
		( ( secure ) ? ";secure" : "" );
}

function Delete_Cookie( name, path, domain ) {
	if ( Get_Cookie( name ) ) document.cookie = name + "=" +
			( ( path ) ? ";path=" + path : "") +
			( ( domain ) ? ";domain=" + domain : "" ) +
			";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}


/**
 Find if need to display comments along with images
*/
function getMemoFlag(album)
{
	memoCookie = Get_Cookie(album);
	
	if (memoCookie == null)
		return true; // show comments by default
	else if(memoCookie == "false")
	  return false;
	else
	  return true;  	
} 