﻿//Gallery methods
function doNull()
{
    //do nothing
}
function clearTimers()
{
   effectTimerID = clearTimer(effectTimerID);
   slideTimerID = clearTimer(slideTimerID);   
}
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()", 1000 / fps);
}
function clearTimer(timerID)
{
    if(timerID !== null)
        clearTimeout(timerID);
    
    return null;
}
function setPhoto(index)
{           
    if(typeof(slides) !== "undefined" && slides.length)
    {
        opacity = 0;
    
        var topPhoto = document.getElementById("topPhoto");
        var 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;     
            
            if(btmOverlay)
            {              
                btmOverlay.style.left = topOverlay.style.left;
                btmOverlay.style.top = topOverlay.style.top;
                btmOverlay.style.visibility = "visible";             
            }        
        }
        
        btmPhoto.src = topPhoto.src;
                
        var topPhotoDiv = document.getElementById("topPhotoDiv");
        topPhotoDiv.className = "opacity0";
        
        var topPhoto = document.getElementById("topPhoto");
        topPhoto.src = slides[index].url;       
        
        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(topOverlay)
            {
                topOverlay.style.left = (rect.x + rect.w - topOverlay.offsetWidth) + "px";
                topOverlay.style.top = (rect.y + rect.h - topOverlay.offsetHeight) + "px";
                topOverlay.style.visibility = "visible";
            }   
        }
        else  
            titleLabel.innerText = "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 = slides[currentPhotoIndex].imgRect();   
    var fs = window.open("PrintPic.aspx", "print", "menubar=1, 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;
    }
}
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, w, h)
{
    this.id = id;
    this.ref = ref;
    this.url = url;
    this.cap = cap;
    this.desc = desc;
    
    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);            
            }
            else
            {
                rect.w = clientRect.w;         
                rect.h = Math.floor(clientRect.w / aspect);                
            }
        }       
        
        rect.setX(Math.floor((clientRect.w - rect.w) / 2));
        rect.setY(Math.floor((clientRect.h - rect.h) / 2));                   
        
        return rect;
    }
}
function showThumbnails(bShow)
{
     thumbnailPanel.style.display = bShow ? "block" : "none";
     photoPanel.style.display = bShow ? "none" : "block";
}
function showPhoto(photoId, thmPanelId, photoPanelId, imageTitleId)
{    
    slideTimerID = clearTimer(slideTimerID);    
    effectTimerID = clearTimer(effectTimerID);
    
    preloadSlides();
    
    for(var i = 0; i < slides.length; i++)
    {        
        if(slides[i].id == photoId)
        {			
            thumbnailPanel = document.getElementById(thmPanelId);
            photoPanel = document.getElementById(photoPanelId);
            titleLabel = document.getElementById(imageTitleId);
         
            document.getElementById("topPhoto").src = spacerImgUrl;
            
            isPlaying = false;
            isPausing = false;                        
            
            showThumbnails(false);                                            
            setPhoto(i);                                   
                      
            return;
        }
    }      
}

var currentPhotoIndex = 0;
var photoPanel = null;
var thumbnailPanel = null;
var titleLabel = null;
var slideTimerID = null;
var effectTimerID = null;
var interval = 4000;
var isPlaying = false;
var isPausing = false;
var opacity = 0;
var fps = 20;
var spacerImgUrl = "images/spacer.gif";
var isTheatre = false;
var topOverlay = null;
var btmOverlay = null;
