﻿//Gallery methods
function doNull() {
    //do nothing
}
function clearTimers() {
    effectTimerID = clearTimer(effectTimerID);
    slideTimerID = clearTimer(slideTimerID);
}
function printPic() {      
    if (opener && opener != self) {
        var url = opener.slides[opener.currentPhotoIndex].url.toLowerCase();

        url = url.replace(/\/md\//, "/xlg/");           
        url = url.replace(/\/lg\//, "/xlg/");
        url = url.replace(/\/org\//, "/xlg/");

        document.getElementById("photo").src = url;
        print();
    }
}
function doPage(pageNum, pageFieldId, updateBtnId) {
    document.forms[0][pageFieldId].value = pageNum;
    document.forms[0][updateBtnId].click();
}
function setOpacity() {
    effectTimerID = clearTimer(effectTimerID);

    opacity += 5;
    var className = "opacity" + opacity;

    document.getElementById('topPhotoDiv').className = className;

    if (opacity !== 100)
        effectTimerID = setTimeout("setOpacity()", duration / fps);
}
function clearTimer(timerID) {
    if (timerID !== null)
        clearTimeout(timerID);

    return null;
}
function setPhoto(index, fadeIn) {
    if (typeof (slides) !== "undefined" && slides.length) {
        opacity = 0;

        if (topPhoto == null)
            topPhoto = document.getElementById("topPhoto");

        if (btmPhoto == null)
            btmPhoto = document.getElementById("btmPhoto");

        if (isTheatre) {
            btmPhoto.style.left = topPhoto.style.left;
            btmPhoto.style.top = topPhoto.style.top;
            btmPhoto.style.width = topPhoto.style.width;
            btmPhoto.style.height = topPhoto.style.height;
        }

        btmPhoto.src = topPhoto.src;

        document.getElementById("topPhotoDiv").className = "opacity0";

        topPhoto.src = slides[index].url;
        if (typeof(showText) !== "undefined" && showText) {

            var capPanel = document.getElementById("captPanel");
            if (capPanel)
                capPanel.innerHTML = slides[index].cap;

            var desPanel = document.getElementById("descPanel");
            if(desPanel)
                desPanel.innerHTML = slides[index].desc;

            var navPanel = document.getElementById("navPanel");
            if (navPanel && slides[index].navUrl) {
                if (!navigateText) 
                    navigateText = navPanel.innerHTML;
                
                if(navigateText)
                    navPanel.innerHTML = "<a href='" + slides[index].navUrl + "'>" + navigateText + "</a>";
            }
        }

        if (isTheatre) {
            var rect = slides[index].imgRect();

            topPhoto.style.left = rect.x + "px";
            topPhoto.style.top = rect.y + "px";
            topPhoto.style.width = rect.w + "px";
            topPhoto.style.height = rect.h + "px";

            if (watermark) {               
                watermark.style.left = (rect.x + rect.w - parseInt(watermark.style.width)) + "px";
                watermark.style.top = (rect.y + rect.h - parseInt(watermark.style.height)) + "px";
                watermark.style.display = "block";
            }
        }

        else if (titleLabel)
            titleLabel.innerHTML = "Ref. " + slides[index].ref;

        currentPhotoIndex = index;  
        setOpacity();                             
    }
}
function doSlide() {
    slideTimerID = clearTimer(slideTimerID);
    effectTimerID = clearTimer(effectTimerID);

    if (!isPausing) {
        setPhoto(currentPhotoIndex == slides.length - 1 ? 0 : currentPhotoIndex + 1);

        if (isPlaying)
            slideTimerID = setTimeout("doSlide()", interval);
    }

    isPausing = false;
}
function doNext() {
    doPause();
    setPhoto(currentPhotoIndex == slides.length - 1 ? 0 : currentPhotoIndex + 1);
}
function doPrev() {
    doPause();
    setPhoto(currentPhotoIndex == 0 ? slides.length - 1 : currentPhotoIndex - 1);
}
function doPlay() {
    isPlaying = true;
    isPausing = false;

    doSlide();
}
function doPause() {
    isPausing = true;
    isPlaying = false;
}
function doFirst() {
    doPause();
    setPhoto(0);
}
function doLast() {
    doPause();
    setPhoto(slides.length - 1);
}
function doPrint() {
    doPause();
    var rect = new Rectangle(0, 0, topPhoto.offsetWidth, topPhoto.offsetHeight);           
    var fs = window.open("printpic.aspx", "print", "menubar=0, location=0, status=0, scrollbars=0, resizeable=1, width=" + rect.w + ", height=" + rect.h);
}
function doTheatre() {
    doPause();
    var rect = new ScreenRect();
    var fs = window.open("theatre.aspx", "theatre", "menubar=0, location=0, status=0, scrollbars=0, resizeable=0, width=" + rect.w + ", height=" + rect.h);
    fs.moveTo(0, 0);
}
function doClose() {
    clearTimers();
    if (isTheatre)
        window.close();

    else
        showThumbnails(true);
}
function preloadSlides() {
    if (typeof (slides) !== "undefined" && slides.length)
        for (var i = 0; i < slides.length; i++)
            slides[i].preload();
}
function ScreenRect() {
    var w = screen.availWidth;
    var h = screen.availHeight;

    return new Rectangle(0, 0, w, h);
}
function ClientRect() {
    var w = document.documentElement.clientWidth;
    if (w == 0)
        w = document.body.clientWidth;

    var h = document.documentElement.clientHeight;
    if (h == 0)
        h = document.body.clientHeight;

    return new Rectangle(0, 0, w, h);
}
function Rectangle(x, y, w, h) {
    this.x = x;
    this.y = y;
    this.w = w;
    this.h = h;

    this.toString = function() {
        return this.x + "  " + this.y + "  " + this.w + "  " + this.h;
    }
    this.setX = function(x) {
        this.x = x;
    }
    this.setY = function(y) {
        this.y = y;
    }
    this.setW = function(w) {
        this.w = w;
    }
    this.setH = function(h) {
        this.h = h;
    }
    this.toString = function() {
    
        var result = "";
        
        result += "left: " + this.x + "; ";
        result += "top: " + this.y + "; ";
        result += "width: " + this.w + "; ";
        result += "height: " + this.h + ";";
        
        return result;
    }
}
function SlideShow(slidePhotos) {
    this.slidePhotos = slidePhotos;
    this.isLoaded = function() {
        return this.percentComplete == 100;
    }
    this.percentComplete = function() {
        var count = 0;
        for (var i = 0; i < this.slidePhotos.length; i++)
            if (!this.slidePhotos[i].isLoaded())
            ++count;

        return parseInt(count / this.slidePhotos.length * 100);
    }
    this.load = function() {
        if (typeof (slidePhotos) !== "undefined" && slidePhotos.length)
            for (var i = 0; i < slidePhotos.length; i++)
            slidePhotos[i].preload();
    }
}
function SlidePhoto(id, ref, url, cap, desc, navUrl, w, h) {
    this.id = id;
    this.ref = ref;
    this.url = url;
    this.cap = cap;
    this.desc = desc;
    this.navUrl = navUrl;

    this.w = w;
    this.h = h;

    this.image = new Image();
    this.preload = function() {
        this.image.src = url;
    }
    this.isLoaded = function() {
        if (typeof (this.image.readyState) != "undefined")
            return this.image.readyState == "complete";

        if (typeof (this.image.naturalWidth) != "undefined")
            return this.image.naturalWidth > 0;

        return true;
    }
    this.isPortrait = function() {
        return this.h > this.w;
    }
    this.imgRect = function() {
        var rect = new Rectangle(0, 0, this.w, this.h);
        var clientRect = new ClientRect();

        var aspect = this.w / this.h;

        if (clientRect.w < this.w || clientRect.h < this.h) {
            if (this.isPortrait()) {
                rect.h = clientRect.h;
                rect.w = Math.floor(aspect * rect.h);

                if (rect.w > clientRect.w) {
                    rect.h = Math.floor(rect.h * clientRect.w / w);
                    rect.w = clientRect.w;
                }
            }
            else {
                rect.w = clientRect.w;
                rect.h = Math.floor(clientRect.w / aspect);

                if (rect.h > clientRect.h) {
                    rect.w = Math.floor(rect.w * clientRect.h / h);
                    rect.h = clientRect.h;
                }
            }
        }

        rect.setX(Math.floor((clientRect.w - rect.w) / 2));
        rect.setY(Math.floor((clientRect.h - rect.h) / 2));

        return rect;
    }
}
function showThumbnails(bShow) {    
    if (thumbnailPanel)
        thumbnailPanel.style.display = bShow ? "block" : "none";
    if (photoPanel) {
        photoPanel.style.display = bShow ? "none" : "block";
    }
    showPagers(bShow);
}
function showPagers(bShow) {
    if(typeof(topPagerClientID) !== "undefined")
        showPager(topPagerClientID, bShow);
    if(typeof(btmPagerClientID) !== "undefined")
        showPager(btmPagerClientID, bShow);
}
function showPager(id, bShow) {
    if (id) {
        var panel = document.getElementById(id);
        panel.style.display = bShow ? "block" : "none";
    }
}
function showPhoto(photoId, thmPanelId, photoPanelId, imageTitleId) {

    if(topPhoto == null)
        topPhoto = document.getElementById("topPhoto");
        
    if(btmPhoto == null)
        btmPhoto = document.getElementById("btmPhoto");
    
    slideTimerID = clearTimer(slideTimerID);
    effectTimerID = clearTimer(effectTimerID);

    preloadSlides();

    for (var i = 0; i < slides.length; i++) {
        if (slides[i].id == photoId) {
            if (thmPanelId)
                thumbnailPanel = document.getElementById(thmPanelId);
            if (photoPanelId)
                photoPanel = document.getElementById(photoPanelId);
            if (imageTitleId)
                titleLabel = document.getElementById(imageTitleId);
                        
            isPlaying = false;
            isPausing = false;

            topPhoto.src = spacerImgUrl;

            showThumbnails(false);
            setPhoto(i);

            return;
        }
    }
}
function setConstants(slideInterval, effectDuration, spacerImageUrl) {
    interval = slideInterval;
    duration = effectDuration;
    spacerImgUrl = spacerImageUrl;
}
function slideshowStart(id) {    
    showPhoto(id, '', 'photoPanel', '');    
    slideTimerID = setTimeout('doPlay()', interval);
}

var navigateText = null;
var currentPhotoIndex = 0;
var photoPanel = null;
var thumbnailPanel = null;
var titleLabel = null;
var slideTimerID = null;
var effectTimerID = null;
var isPlaying = false;
var isPausing = false;
var opacity = 0;
var isTheatre = false;
var watermark = null;

var fps = 20;
var interval = 6000;
var duration = 1500;
var spacerImgUrl = "images/spacer.gif";

var topPhoto = null;
var btmPhoto = null;

