// Created by Ihsahn and modfied by Piotr Skowronek, 2006.
// free to use


// loading indicator (gif taken from lightbox)
var preload_width=126;
var preload_height=22;
var preload_image="http://apz.org.pl/polski/images/indicator.black.gif";
var currentUrl="";

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); ' \
        > &nbsp; </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; ' \
        >\
        <img id='hideImg' src='' \
        onclick='hidePopup();' >\
        <div id='divMenu' style='padding: 5px; color: #ffffff; \
        visibility: hidden; \
        text-align: center;' > \
        <center> \
        <a href='#' onclick='javascript: loadPrev(); return false;' style='color: #ffffff; font-size: 20px;'><b>&lt;&lt;&lt;</b></a> \
        <a href='#' onclick='javascript: hidePopup(); return false;' style='color: #ffffff; font-size: 15px;'><b> \
        &nbsp;&nbsp;&nbsp;&nbsp;Zamknij&nbsp;&nbsp;&nbsp;&nbsp;</b></a> \
        <a href='#' onclick='javascript: loadNext(); return false;' style='color: #ffffff; font-size: 20px;'><b>&gt;&gt;&gt;</b></a> \
        </center> \
        </div>\
        <br> \
</div>";

// automagical constructor
var onloadSaved=window.onload;
window.onload = function() {

        doLoad();
        if (onloadSaved!=null) onloadSaved();
}


// webpage Initialization
// /piotr s
function doLoad()
{
 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');   
 var elementMen = document.getElementById('divMenu');   

 
 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";

elementImg.src = url;
// preload image
imgPreload = new Image();

imgPreload.onload=function(){

                elementImg.src = url;
                currentUrl = 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');

                elementMen.style.visibility="visible";
                return false;
        }

imgPreload.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";    

    var elementMen = document.getElementById('divMenu');        
    if (elementMen!=null) elementMen.style.visibility="hidden";    

    popup_visible=false;
};


// getKey keyboard event handler
function getKey(event)
{
    var key=event.keyCode;
// esc
    if (key==27)
    { 
        hidePopup(); return false;     
    };
    
    if ((key==' ') || (key==39))
    { 
        loadNext();
        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 oldurl;
 current_url="";
 
 for (var i=0; i<objs.length;i++)
 {
   oldurl = objs[i].getAttribute('href'); 
   oldalt=objs[i].getAttribute('title');
   // onclick
   objs[i].setAttribute('onclick',"javascript:loadFoto('"+oldurl+"'); return false;");
   // IE hack
   objs[i].onclick = new Function("loadFoto('"+oldurl+"'); return false;");
   objs[i].setAttribute('oldurl', oldurl);
 };
 
};



// returns URL of next foto
// /piotr s
function findNextFoto(url)
{

 var gnext;
 var objs=document.getElementsByName('imglink');

 gnext='';

 for (var i=0; i<objs.length;i++)
 {      
   if (objs[i].getAttribute('oldurl')==url)
   {
        if ( i + 1 < objs.length ) 
        {
            gnext=objs[i+1].getAttribute('oldurl'); 
        } else 
        {
            gnext = url;
        }
        break;
   };    
 }; 
 
  return gnext;
};


// returns URL of prev foto
// /piotr s
function findPrevFoto(url)
{

 var gnext;
 var objs=document.getElementsByName('imglink');

 gprev='';

 for (var i=0; i<objs.length;i++)
 {      
   if (objs[i].getAttribute('oldurl')==url)
   {
        if ( i - 1 >= 0) 
        {
            gprev=objs[i-1].getAttribute('oldurl'); 
        } else 
        {
            gprev = url;
        }
        break;
   };    
 }; 
 
  return gprev;
};




// loads foto
// / ihs + mod piotr s
function loadFoto(url)
{
    popupwindow(url);  
};


function loadNext()
{
        if (currentUrl!='#') 
        {
            hidePopup();
            var nextUrl=findNextFoto(currentUrl);
            if (nextUrl != '') 
            {
                hidePopup();
                loadFoto(nextUrl);              
            }
        };
}


function loadPrev()
{
        if (currentUrl!='#') 
        {
            hidePopup();
            var prevUrl=findPrevFoto(currentUrl);
            if (prevUrl != '') 
            {
                hidePopup();        
                loadFoto(prevUrl);              
            }
        };
}
