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


// loading indicator (gif taken from lightbox)
var showImg_preload_width=126;
var showImg_preload_height=22;
var showImg_preload_image="http://adfinem.net/piter/polski/images/indicator.black.gif";
var showImg_ahref_id="imglinkbig";
var showImg_img_id="imgsmall";

var showImg_popup_visible=false;


// These are htmls we're going to inject into webpage
var showImg_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 showImg_imgCode = "\
<div id='hideDiv' style='margin: 0 auto 0 auto; \
        position: absolute; \
        top: 0px; left: 0px; bottom: 0px;  \
        visibility: hidden;  \
        z-index: 3; ' \
        onclick='showImg_hidePopup();'>\
        <img id='hideImg' src='' \
        onclick='showImg_hidePopup();' >\
</div>";

// automagical constructor
var showImg_onloadSaved=window.onload;

window.onload = function() {
        showImg_doLoad();
        if (showImg_onloadSaved!=null) showImg_onloadSaved();
}


// webpage Initialization
// /piotr s
function showImg_doLoad()
{
 if (!showImg_changeLinks()) return;
 document.onkeypress=showImg_getKey;

 var body = document.getElementsByTagName("body")[0];    

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

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

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


//
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
//
function showImg_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 showImg_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 showImg_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 showImg_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 = showImg_getPageSize();
 var arrayPageScroll = showImg_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=showImg_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 - showImg_preload_height) / 2);
var lightboxLeft = ((arrayPageSize[0] - 20 - showImg_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){
                        showImg_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 = showImg_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 showImg_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 showImg_getKey(event)
{
    var key=event.keyCode;
// esc
    if (key==27)
    { 
        showImg_hidePopup(); return false;     
    };

    return true;
};



// links rewriter (rewrites only gallery links, ie those named imglinkbig)
// // /ihs + mod piotr s
function showImg_changeLinks()
{
 var objs=document.getElementsByName(showImg_ahref_id);  
 var oldurl;
 
 if (objs==null || objs.length==0) return false;

 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:showImg_loadFoto('"+oldurl+"'); return false;");
   // IE hack
   objs[i].onclick = new Function("showImg_loadFoto('"+oldurl+"'); return false;");
   objs[i].setAttribute('oldurl', oldurl);
 };

 return true;
};



// returns URL of next foto
// /piotr s
function showImg_findNextFoto(url)
{

 var gnext;
 var objs=document.getElementsByName(showImg_ahref_id);
 var obj=document.getElementById(showImg_img_id);
 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
// / ihs + mod piotr s
function showImg_loadFoto(url)
{
    showImg_popupwindow(url);  
};

