/*
        @author MiRacLe <miracle@cccmos.com>
        @since 17,01.2006
        based on code LightBox.js by Lokesh Dhakar
        http://huddletogether.com/projects/lightbox/
        @changelog
                hide object & select tags before show overlay
    @since 21.02.2006
        fixed position of preloaded image    
    @since 18.05.2006
        added paging navigation
*/
var loadingImage = '/pict/sp/loading.gif';
var closeButton = '/pict/sp/close.gif';
var blankImage = '/pict/1x1.gif';
var __Top = 0;
var __Left = 0;
function createShowPic() {
        if (!document.getElementsByTagName){ return; }
        var objBody = document.getElementsByTagName("body").item(0);
        var objOverlay = document.createElement("div");
        objOverlay.setAttribute('id','overlay');
        objOverlay.onclick = function () {closeShowPic(); return false;}
        objOverlay.style.display = 'none';
        objOverlay.style.position = 'absolute';
        objOverlay.style.top = '0';
        objOverlay.style.left = '0';
        objOverlay.style.zIndex = '90';
         objOverlay.style.width = '100%';
        objBody.insertBefore(objOverlay, objBody.firstChild);
        var arrayPageSize = getPageSize();
        var arrayPageScroll = getPageScroll();
        var imgPreloader = new Image();
        imgPreloader.onload=function(){
                var objLoadingImageLink = document.createElement('a');
                objLoadingImageLink.setAttribute('href','#');
                objLoadingImageLink.onclick = function () {closeShowPic(); return false;}
                objOverlay.appendChild(objLoadingImageLink);
                var objLoadingImage = document.createElement('img');
                objLoadingImage.src = loadingImage;
                objLoadingImage.setAttribute('id','loadingImage');
                objLoadingImage.style.position = 'absolute';
                objLoadingImage.style.zIndex = '150';
                objLoadingImageLink.appendChild(objLoadingImage);
                imgPreloader.onload=function(){};        //        clear onLoad, as IE will flip out w/animated gifs
                return false;
        }
        imgPreloader.src = loadingImage;
        var objShowPic = document.createElement('div');
        objShowPic.setAttribute('id','showpic');
        objShowPic.style.display = 'none';
        objShowPic.style.position = 'absolute';
        objShowPic.style.zIndex = '100';
        objBody.insertBefore(objShowPic, objOverlay.nextSibling);
        var objLink = document.createElement('a');
        objLink.setAttribute('href','#');
        objLink.setAttribute('title',ClickToClose);
        //objLink.onclick = function () {closeShowPic(); return false;}
        objShowPic.appendChild(objLink);
        var imgPreloadCloseButton = new Image();
        imgPreloadCloseButton.onload=function() {
                var objCloseButton = document.createElement('img');
                objCloseButton.src = closeButton;
                objCloseButton.setAttribute('id','closeButton');
                objCloseButton.onclick = function () {closeShowPic(); return false;}
                objCloseButton.style.position = 'absolute';
                objCloseButton.style.zIndex = '200';
                objCloseButton.style.top = '5px';
                objCloseButton.style.right = '5px';
                objLink.appendChild(objCloseButton);
                return false;
        }
        imgPreloadCloseButton.src = closeButton;
        var objImage = document.createElement('img');
        objImage.setAttribute('id','showPicImage');
        objLink.appendChild(objImage);
        var objCaption = document.createElement('div');
        objCaption.setAttribute('id','showPicCaption');
        objCaption.style.display = 'none';
        objCaption.onselectstart = function() {return false};        
        objShowPic.appendChild(objCaption);
        var objNavigation = document.createElement('div');
        objNavigation.setAttribute('id','showPicNavigation');
        objNavigation.style.display = 'none';
        objNavigation.onselectstart = function() {return false};
        objShowPic.appendChild(objNavigation);
}
//
// 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.getElementsByTagName('body')[0].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;
        }
}

function ShowPic(objLink) {
        var objOverlay = document.getElementById('overlay');
        var objShowPic = document.getElementById('showpic');
        var objCaption = document.getElementById('showPicCaption');
        var objNavigation = document.getElementById('showPicNavigation');
        var objImage = document.getElementById('showPicImage');
        var objLoadingImage = document.getElementById('loadingImage');
// Hide some elements before show overlay
/*        var advC = document.getElementsByTagName('object');
        for (var i = 0; i < advC.length; i++) {
                var obj = advC[i];
                if (obj && obj.style) obj.style.display = 'none';
        }
*/
        var advC = document.getElementsByTagName('select');
        for (var i = 0; i < advC.length; i++) {
                var obj = advC[i];
                if (obj && obj.style) obj.style.display = 'none';
        }
        var arrayPageSize = getPageSize();
        var arrayPageScroll = getPageScroll();
        // center loadingImage if it exists
        if (objLoadingImage) {
                objLoadingImage.style.top = (arrayPageScroll[1] + ((arrayPageSize[3] - 35 - objLoadingImage.height) / 2) + 'px');
                objLoadingImage.style.left = (((arrayPageSize[0] - 20 - objLoadingImage.width) / 2) + 'px');
                objLoadingImage.style.display = 'block';
        }
        objOverlay.style.height = (arrayPageSize[1] + 'px');
        objOverlay.style.display = 'block';
        var imgPreload = new Image();
  imgPreload.onload=function(){
  objImage.onclick = function() {
    closeShowPic();
  }
//                objImage.src = this.src.replace(/t[n|m]_/i,'') + '?rn=24332dsêóåtreêóåfdsf4';

                var _Top = arrayPageScroll[1] + ((arrayPageSize[3] - 20 - this.height) / 2);
    var _Left = ((arrayPageSize[0] - 20 - this.width) / 2);
                objShowPic.style.top = (_Top < 0) ? "0px" : _Top + "px";
                objShowPic.style.left = (_Left < 0) ? "0px" : _Left + "px";

                var tooltip = objLink.getAttribute('title') || objLink.getAttribute('alt');
                if(tooltip){
                        objCaption.style.display = 'block';
                        objCaption.style.width = this.width;
                        //objCaption.style.width = objLink.width + 'px';
                        objCaption.innerHTML = tooltip;
                } else {
                        objCaption.style.display = 'none';
                }
                if (ShowPics.length > 1) {
                    var current = 0;
                    for (var i = 0; i < ShowPics.length; i++) {
                        if (ShowPics[i].src == objLink.src) {
                            current = i;
                            break;
                        }
                    }
                    var pages = '';
                    var prev = (current > 0) ? current -1 : ShowPics.length - 1;
                    var next = (current < (ShowPics.length - 1)) ? current + 1 : 0;
                    /*
                    pages += '<span class="pages" onclick="closeShowPic();ShowPic(ShowPics['+ (prev) +']);">' + PrevHTML + '</span>&nbsp;&nbsp;';
                    for (var i = 0; i < ShowPics.length; i++) {
                        var num = i + 1;
                        if (i != current) {
                            pages += '<span class="pages" onclick="closeShowPic();ShowPic(ShowPics['+ i +']);">'+ num +'</span>&nbsp;';
                        } else {
                            pages += '<span class="current">'+ num +'</span>&nbsp;';
                        }
                    }
                    pages += '&nbsp;&nbsp;<span class="pages" onclick="closeShowPic();ShowPic(ShowPics['+ (next) +']);">' + NextHTML + '</span>';
                    */
                    objNavigation.innerHTML = pages;
                    objNavigation.style.display = 'block';
                } else {
                    objNavigation.style.display = 'none';
                }
                // 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);
                }
                objLoadingImage.style.display = 'none';
                objImage.src = this.src;
                objShowPic.style.display = 'block';
                // After image is loaded, update the overlay height as the new image might have
                // increased the overall page height.
                arrayPageSize = getPageSize();
                objOverlay.style.height = (arrayPageSize[1] + 'px');
                return false;
        }
  var _src = (objLink.getAttribute('sp')) ? objLink.getAttribute('sp').replace(/t[n|m]_/i,'') : objLink.src.replace(/t[n|m]_/i,'');
  imgPreload.src = _src;
}
function closeShowPic() {
        var objOverlay = document.getElementById('overlay');
        var objShowPic = document.getElementById('showpic');
        var objImage = document.getElementById('showPicImage');
        objImage.src = blankImage;
        objOverlay.style.display = 'none';
        objShowPic.style.display = 'none';
/*        var advC = document.getElementsByTagName('object');
        for (var i = 0; i < advC.length; i++) {
                var obj = advC[i];
                if (obj && obj.style) obj.style.display = 'block';
        }
*/
        var advC = document.getElementsByTagName('select');
        for (var i = 0; i < advC.length; i++) {
                var obj = advC[i];
                if (obj && obj.style) obj.style.display = '';
        }
}
