// Created by Ihsahn and Piotr Skowronek, 2006.
// free to use


// loading indicator (gif taken from lightbox)
var preload_width=126;
var preload_height=22;
var preload_image="http://adfinem.net/piter/polski/images/indicator.black.gif";

var popup_visible=false;


// These are htmls we're going to inject into webpage
var fadeCode = "\
<div id='fadeDiv' style='margin: 0 auto 0 auto; \
        top: 0px; left: 0px; bottom: 0px; height: 100%; width: 100%; \
        position: absolute; \
        visibility: hidden;  \
        z-index: 2;  \
        background: black; \
        opacity: .6;  \
        filter: alpha(opacity=50); ' \
        ></div> \
        ";
var imgCode = "\
<div id='hideDiv' style='margin: 0 auto 0 auto; \
        position: absolute; \
        top: 0px; left: 0px; bottom: 0px;  \
        visibility: hidden;  \
        z-index: 3; ' \
        onclick='hidePopup();'>\
        <img id='hideImg' src='' \
        onclick='hidePopup();' >\
</div>";

// automagical constructor
var onloadSaved=window.onload;
window.onload = function() {
        doLoad();
        if (onloadSaved!=null) onloadSaved();
}


// webpage Initialization
// /piotr s
function doLoad()
{
 if (changeLinks()) { 
   document.onkeypress=getKey;
   var body = document.getElementsByTagName("body")[0];    

   var fadeLayer = document.createElement('div');
   fadeLayer.setAttribute('id', '');
   fadeLayer.innerHTML = fadeCode;
   body.insertBefore(fadeLayer, body.firstChild);

   var imgLayer = document.createElement('div');
   imgLayer.setAttribute('id', '');
   imgLayer.innerHTML = imgCode;
   body.insertBefore(imgLayer, body.firstChild);

   // preload loading indicator image 
   var elementImg = document.getElementById('hideImg'); 
   
   if (elementImg!=null) elementImg.src=preload_image;
 };
};


//
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
//
function getPageScroll(){

        var yScroll;

        if (self.pageYOffset) {
                yScroll = self.pageYOffset;
        } else if (document.documentElement && document.documentElement.scrollTop){      // Explorer 6 Strict
                yScroll = document.documentElement.scrollTop;
        } else if (document.body) {// all other Explorers
                yScroll = document.body.scrollTop;
        }

        arrayPageScroll = new Array('',yScroll) 
        return arrayPageScroll;
}



//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize(){
        
        var xScroll, yScroll;
        
        if (window.innerHeight && window.scrollMaxY) {  
                xScroll = document.body.scrollWidth;
                yScroll = window.innerHeight + window.scrollMaxY;
        } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
                xScroll = document.body.scrollWidth;
                yScroll = document.body.scrollHeight;
        } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
                xScroll = document.body.offsetWidth;
                yScroll = document.body.offsetHeight;
        }
        
        var windowWidth, windowHeight;
        if (self.innerHeight) { // all except Explorer
                windowWidth = self.innerWidth;
                windowHeight = self.innerHeight;
        } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
                windowWidth = document.documentElement.clientWidth;
                windowHeight = document.documentElement.clientHeight;
        } else if (document.body) { // other Explorers
                windowWidth = document.body.clientWidth;
                windowHeight = document.body.clientHeight;
        }       
        
        // for small pages with total height less then height of the viewport
        if(yScroll < windowHeight){
                pageHeight = windowHeight;
        } else { 
                pageHeight = yScroll;
        }

        // for small pages with total width less then width of the viewport
        if(xScroll < windowWidth){      
                pageWidth = windowWidth;
        } else {
                pageWidth = xScroll;
        }


        arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
        return arrayPageSize;
}



//
// pause(numberMillis)
// Pauses code execution for specified time. Uses busy code, not good.
// Code from http://www.faqts.com/knowledge_base/view.phtml/aid/1602
//
function pause(numberMillis) {
        var now = new Date();
        var exitTime = now.getTime() + numberMillis;
        while (true) {
                now = new Date();
                if (now.getTime() > exitTime)
                        return;
        }
}


// Popup image from given URL
// my code /piotr s.
function popupwindow(url)
{

 var elementDiv = document.getElementById('hideDiv');   
 var elementFad = document.getElementById('fadeDiv');   
 var elementImg = document.getElementById('hideImg');   
 if (elementDiv==null) return;
 if (elementImg==null) return;
 if (elementFad==null) return;

 popup_visible=true;
// taken from Lightbox - preloading and centering/resizing layers(divs)

 var arrayPageSize = getPageSize();
 var arrayPageScroll = getPageScroll();

// set height of Overlay to take up whole page and show
elementFad.style.height = (arrayPageSize[1] + 'px');


// show preload (loading/hourglass animated gif)  image
elementImg.src=preload_image;

// center lightbox and make sure that the top and left values are not negative
// and the image placed outside the viewport
var lightboxTop = arrayPageScroll[1] + ((arrayPageSize[3] - 35 - preload_height) / 2);
var lightboxLeft = ((arrayPageSize[0] - 20 - preload_width) / 2);


// center 
elementDiv.style.top = (lightboxTop < 0) ? "0px" : lightboxTop + "px";
elementDiv.style.left = (lightboxLeft < 0) ? "0px" : lightboxLeft + "px";

//var tmp=(arrayPageScroll[1] + ((arrayPageSize[3] - 35 - elementDiv.height) / 2) + 'px');
//if (tmp>0) elementDiv.style.top = tmp;

//tmp=(((arrayPageSize[0] - 20 - elementDiv.width) / 2) + 'px');        
//if (tmp>0)  elementDiv.style.left = tmp;

//elementDiv.style.display = 'block';
elementDiv.style.visibility="visible";

// preload image
        imgPreload = new Image();

        imgPreload.onload=function(){
                elementImg.src = url;

                // center lightbox and make sure that the top and left values are not negative
                // and the image placed outside the viewport
                var lightboxTop = arrayPageScroll[1] + ((arrayPageSize[3] - 35 - imgPreload.height) / 2);
                var lightboxLeft = ((arrayPageSize[0] - 20 - imgPreload.width) / 2);
                
                elementDiv.style.top = (lightboxTop < 0) ? "0px" : lightboxTop + "px";
                elementDiv.style.left = (lightboxLeft < 0) ? "0px" : lightboxLeft + "px";


                // A small pause between the image loading and displaying is required with IE,
                // this prevents the previous image displaying for a short burst causing flicker.
                if (navigator.appVersion.indexOf("MSIE")!=-1){
                        pause(250);
                } 
        
//              elementDiv.style.display = 'block';
//               elementDiv.style.visibility="visible";

                // After image is loaded, update the overlay height as the new image might have
                // increased the overall page height.
                arrayPageSize = getPageSize();
                elementFad.style.height = (arrayPageSize[1]+20 + 'px');
                
                return false;
        }

        imgPreload.src = url;


// elementImg.src=url;
 elementFad.style.visibility="visible";
     
};


// hides popup
// /piotr s
function hidePopup()
{
    var elementImg = document.getElementById('hideImg');        
    if (elementImg!=null) elementImg.src='#';

    var element = document.getElementById('hideDiv');   
    if (element!=null) element.style.visibility="hidden";
    
    var elementFad = document.getElementById('fadeDiv');        
    if (elementFad!=null) elementFad.style.visibility="hidden";    

     popup_visible=false;
};


// getKey keyboard event handler
function getKey(event)
{
    var key=event.keyCode;
    var obj=document.getElementById('mrbig');
    var oldsrc=obj.src.substring(0,obj.src.lastIndexOf('/')+1);
    
//alert(key);
//next
    if ((key==' ') || (key==39))
    { 
        var href=document.getElementById('mrnext').getAttribute('href');
        href=href.replace("javascript:loadFoto('",'');
        href=href.replace("');",'');

        if (href!='#') 
        {
//              if (!popup_visible) 
                        loadFoto(href);
//                      else
//              {
//                      var url=findNextFoto(href);             
                        popupwindow(oldsrc+"big_"+href);
//              };
        };
        return false;
    };



// prev
    if (key==37)
    { 
        var href=document.getElementById('mrprev').getAttribute('href');
        href=href.replace("javascript:loadFoto('",'');
        href=href.replace("');",'');
        if (href!='#') 
        {
//              if (!popup_visible) { 
                        loadFoto(href);
//                      else
//              {
//                      var url=findNextFoto(href);
//                      if (url!='') 
                        popupwindow(oldsrc+"big_"+href);
//              };
        };
        return false;
    };

// esc
    if (key==27)
    { 
        hidePopup(); return false;     
    };

    return true;
};



// links rewriter (rewrites only gallery links, ie those named imglink)
// // /ihs + mod piotr s
function changeLinks()
{
 var objs=document.getElementsByName('imglink');
 var obj=document.getElementById('mrbig');
 
 if (obj==null) return false
 if (objs==null || objs.length==0) return false;
 var oldsrc=obj.src.substring(0,obj.src.lastIndexOf('/')+1);
 
 var oldurl;
 var gprev;
 var gnext;

 for (var i=0; i<objs.length;i++)
 {
   oldurl=objs[i].getAttribute('href').substring(objs[i].getAttribute('href').lastIndexOf(':')+1,objs[i].getAttribute('href').length);
   oldalt=objs[i].getAttribute('title');

//   var temp = objs[i].getAttribute('href');
   // onclick
//   objs[i].setAttribute('onclick',"javascript:loadFoto('"+oldurl+"'); return false;");
   // IE hack
//   objs[i].onclick = new function () { loadFoto(oldurl); return false; };   
   // w/o js or openning in tabs
   objs[i].setAttribute('href', "javascript:loadFoto('"+oldurl+"')");
   objs[i].setAttribute('oldurl', oldurl);
 };

 for (var i=0; i<objs.length;i++)
 {
   gprev='';
   gnext='';
   if (i>0) gprev=objs[i-1].getAttribute('oldurl');
   if (i+1<objs.length) gnext=objs[i+1].getAttribute('oldurl');
   
   objs[i].setAttribute('gnext', gnext);
   objs[i].setAttribute('gprev', gprev);
   
   if (oldsrc+objs[i].getAttribute('oldurl')==document.getElementById('mrbig').src)
   {
        if (gnext=='')
        document.getElementById('mrnext').setAttribute('href', "#")
                else
        document.getElementById('mrnext').setAttribute('href', "javascript:loadFoto('"+gnext+"');");
  

        if (gprev=='')
        document.getElementById('mrprev').setAttribute('href', "#")
                else  
        document.getElementById('mrprev').setAttribute('href', "javascript:loadFoto('"+gprev+"');");
   };    
 };
 
 document.getElementById('mrbighref').setAttribute('onclick', "javascript: popupwindow('"+document.getElementById('mrbighref').getAttribute('href')+"')"); 
 // IE dirty workaround
 document.getElementById('mrbighref').onclick=function () { popupwindow(document.getElementById('mrbighref').getAttribute('href')); return false; };
 document.getElementById('mrbighref').setAttribute('href', document.getElementById('mrbighref').getAttribute('href'));

 return true;
};



// returns URL of next foto
// /piotr s
function findNextFoto(url)
{
 var gnext;
 var objs=document.getElementsByName('imglink');
 var obj=document.getElementById('mrbig');
 var oldsrc=obj.src.substring(0,obj.src.lastIndexOf('/')+1);
  

 gnext='';
 galt='';

 for (var i=0; i<objs.length;i++)
 {      
   if (objs[i].getAttribute('oldurl')==url)
   {
        gnext=objs[i].getAttribute('gnext');    
        break;
   };    
 }; 
 
  return gnext;
};




// loads foto from preview into page (not a popup)
// / ihs + mod piotr s
function loadFoto(url)
{
 var gprev;
 var gnext;
 var galt;
 var objs=document.getElementsByName('imglink');
 var obj=document.getElementById('mrbig');
 var oldsrc=obj.src.substring(0,obj.src.lastIndexOf('/')+1);
  

 gprev='';
 gnext='';
 galt='';

 for (var i=0; i<objs.length;i++)
 {      
   if (objs[i].getAttribute('oldurl')==url)
   {
        gprev=objs[i].getAttribute('gprev');
        gnext=objs[i].getAttribute('gnext');    
        galt=objs[i].getAttribute('title');    
        break;
   };    
 }; 
 
  if (galt==null) galt='';
  obj.src=oldsrc+url;
  obj.alt=galt;
  document.getElementById('mropis').innerHTML=galt;


// on click
  document.getElementById('mrbighref').setAttribute('onclick', "javascript: popupwindow('"+oldsrc+"big_"+url+"'); return false;");
 // IE dirty workaround
  document.getElementById('mrbighref').onclick=function () { popupwindow(oldsrc+"big_"+url); return false; };

// popup  (non-js browsers/users or launched into tabs)
  document.getElementById('mrbighref').setAttribute('href', oldsrc+"big_"+url);
   
  
  if (gnext=='')
  document.getElementById('mrnext').setAttribute('href', "#")
    else
  document.getElementById('mrnext').setAttribute('href', "javascript:loadFoto('"+gnext+"');");  

  if (gprev=='')
  document.getElementById('mrprev').setAttribute('href', "#")
    else  
  document.getElementById('mrprev').setAttribute('href', "javascript:loadFoto('"+gprev+"');");
  

};

