
//
// gfader.js
//
// Copyright 2007 Thomas Hammel. All rights reserved.
//

//
// requires external definition of Photo[]
// 6 fields separated by spaces: "name width height number edition price"
// example: "85000104 10 12.5 1 10 400"
//
// and of PhotoDir
// example: "snow/"
//


var GalleryButton;
var HelpButton;
var GalleryNextButton;
var GalleryPreviousButton;
var GalleryLockButton;

var PreviousButton;
var NextButton;
var RandomButton;

var ForwardButton;
var SpeedElement;

var BackRef;
var NextRef;

//var GalleryLocked=0;
var GalleryDirection=1;
var ShowDirection = 1;

var ShowOn=0;
var ShowTimer = 0;
var Speed = 4000;

var SearchTextElement=0;

var MainPhoto = new Array (0,0);
var Width = new Array (0,0);
var Height = new Array (0,0);


var CaptionElement;


var Top=1;
var Bottom=0;



function gup( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}


//var PhotoCurrent=0;
//var GalleryCurrent=0;

var Opacity=0;


function OpacitySet(opacity)
{
    oopacity=100-opacity;
    if(oopacity<0)
    {
        oopacity=0;
    }
    if(oopacity>100)
    {
        oopacity=100;
    }
    var fopacity=oopacity/100;
    MainPhoto[Bottom].style.filter="alpha(opacity=" + oopacity + ")";
    MainPhoto[Bottom].style.MozOpacity=fopacity;
    MainPhoto[Bottom].style.KHTMLOpacity=fopacity;
    MainPhoto[Bottom].style.opacity=fopacity;

    if(opacity<0)
    {
        opacity=0;
    }
    if(opacity>100)
    {
        opacity=100;
    }
    var fopacity=opacity/100;

    MainPhoto[Top].style.filter="alpha(opacity=" + opacity + ")";
    MainPhoto[Top].style.MozOpacity=fopacity;
    MainPhoto[Top].style.KHTMLOpacity=fopacity;
    MainPhoto[Top].style.opacity=fopacity;
}

function FadeIn()
{
//    Width[0]=MainPhoto[0].width;
//    Height[0]=MainPhoto[0].height;
//    Width[1]=MainPhoto[1].width;
//    Height[1]=MainPhoto[1].height; 
//    SearchTextElement.value= Width[0] + ' x ' + Height[0];

    FadeTimer=0;
    //
    // If the slide show is moving quickly
    // we just flip the images.
    //
    if(ShowOn && Speed<=1000)
    {
        Opacity=100;
        OpacitySet(Opacity);
        ShowSchedule();
    }
    //
    // Otherwise slowly fade out the previous image and 
    // fade in the new one.
    //
    else
    {
        if(Opacity<100)
        {
            FadeTimer=setTimeout('FadeIn()',50);
            Opacity+=5;
            OpacitySet(Opacity);
        } 
        else if(ShowOn)
        {
            ShowSchedule();
        }
    }
}

function SpeedFaster()
{
    Speed=Math.round(Speed/2);
    if(Speed<10)
    {
        Speed=10;
    }
}

function SpeedSlower()
{
    Speed=Math.round(Speed*2);
}

// 
// Search for a particular gallery.
//
function GallerySearch(pname) 
{
    var j= -1;
    //
    //first check to see if it is an index
    //
    j=Math.floor(pname);
    if(j>=0 && j<PhotoDir.length)
    {
        return j;
    }
    //
    // then check to see if it matches a gallery name
    //
    for(j=0; j<PhotoDir.length; j++)
    {
        if(Gallery[j]==pname)
        {
            return j;
        }
    }
    //
    // didn't find it
    //
    return -1;
}

// 
// Search for a particular gallery.
//
function ImageSearch(pname) 
{
    var j= -1;
    //
    //first check to see if it is an index
    //
    j=Math.floor(pname);
    if(j>=0 && j<Photo[GalleryCurrent].length)
    {
        return j;
    }
    //
    // then check to see if it matches a gallery name
    //
    for(j=0; j<PhotoDir.length; j++)
    {
            for (i=0; i<Photo[j].length; i++) 
            {
                var field=Photo[j][i].split(" ",6);
                if(field[0]==pname)
                {
                    GalleryCurrent=j;
                    return i;
                }
            }
   }
    //
    // didn't find it
    //
    return -1;
}


// 
// Search for a particular Photo[GalleryCurrent].
//
function PhotoSearch() 
{
    var pname=SearchTextElement.value;
    //
    // first check to see if it matches a gallery name
    //
    for(j=0; j<PhotoDir.length; j++)
    {
        if(Gallery[j]==pname)
        {
            GallerySet(j);
//                    ShowStop();
            return;
        }
    }
    // 
    // then look for a specific photo number
    //
    if(GalleryLocked)
    {
        for (i=0; i<Photo[GalleryCurrent].length; i++) 
        {
            var field=Photo[GalleryCurrent][i].split(" ",6);
            if(field[0]==pname)
            {
                PhotoSet(i);
//                ShowStop();
                return;
            }
        }
    }
    else
    {
        for(j=0; j<PhotoDir.length; j++)
        {
            for (i=0; i<Photo[j].length; i++) 
            {
                var field=Photo[j][i].split(" ",6);
                if(field[0]==pname)
                {
                    GalleryCurrent=j;
                    PhotoSet(i);
//                    ShowStop();
                    return;
                }
            }
        }
    }
}

function FindAndSet(pname)
{
    var j;

        for(j=0; j<PhotoDir.length; j++)
        {
            for (i=0; i<Photo[j].length; i++) 
            {
                var field=Photo[j][i].split(" ",6);
                if(field[0]==pname)
                {
                    GalleryCurrent=j;
                    PhotoCurrent=i;
                    return;
                }
            }
        }
}


// 
// Adjust the thumbnail scroller.
//
function ThumbnailAdjust(pid) 
{
    if(Top==0)
    {
        Top=1;
        Bottom=0;
    }
    else
    {
        Top=0;
        Bottom=1;
    }
    //
    // Make sure the starting thumbnail is within range
    //
    if(pid>=Photo[GalleryCurrent].length)
    {
        pid=pid-Photo[GalleryCurrent].length;
    }
    if(pid<0)
    {
        pid=pid+Photo[GalleryCurrent].length;
    }
    // 
    // Adjust the opacity so the old photo is at 100% and
    // the photo about to be loaded is hidden.
    //
    Opacity=0;
    OpacitySet(Opacity);
    //
    // Now load the new photo.
    //
    var field=Photo[GalleryCurrent][pid].split(" ",6);
    MainPhoto[Top].src=PhotoDir[GalleryCurrent] + field[0] + '.jpg'; 
    MainPhoto[Top].alt=field[0];  
    //
    // set the back button action
    //
    button = pid-1;
    if(button>=Photo[GalleryCurrent].length) button=0;
    if(button<0) button=Photo[GalleryCurrent].length-1;
    field=Photo[GalleryCurrent][button].split(" ",6);
    BackRef.href='image.htm?image=' + field[0];
//    BackRef.href='image.htm?gallery=' + GalleryCurrent + '&image=' + button;
    //
    // set the next button action
    //
    button = Math.floor(pid) + 1;
    if(button>=Photo[GalleryCurrent].length) button=0;
    if(button<0) button=Photo[GalleryCurrent].length-1;
    field=Photo[GalleryCurrent][button].split(" ",6);
    NextRef.href='image.htm?image=' + field[0];
//    NextRef.href='image.htm?gallery=' + GalleryCurrent + '&image=' + button;

    MatrixRef.href='matrix.htm?gallery=' + Gallery[GalleryCurrent] ;
}

function GallerySet(gid)
{
    GalleryCurrent=gid;
        if(ShowDirection==1)
        {
            PhotoSet(0);
        }
        else
        {
            PhotoSet(Photo[GalleryCurrent].length-1);
        }
}

function GalleryLock()
{
    if(GalleryLocked!=0)
    {
        GalleryLocked=0;
    }
    else
    {
        GalleryLocked=1;
    }
    GalleryLockHighlight();
}

function GalleryNext() 
{
    GalleryDirection=1;

//    if(ShowTimer==0)
    {
        var gid = GalleryCurrent+1>PhotoDir.length-1? 0 : GalleryCurrent+1;
        GallerySet(gid);
    }

    if(ShowTimer!=0)
    {
        ShowStopTimer();
        ShowSchedule();
    }
}

function GalleryPrevious() 
{
    GalleryDirection= -1;

//    if(ShowTimer==0)
    {
        var gid = GalleryCurrent-1<0? PhotoDir.length-1 : GalleryCurrent-1;
        GallerySet(gid);
    }

    if(ShowTimer!=0)
    {
        ShowStopTimer();
        ShowSchedule();
    }
}

function PhotoNextDoIt()
{
            var photoNext = PhotoCurrent+1;
            if(photoNext>=Photo[GalleryCurrent].length)
            {
                if(GalleryLocked)
                {
                    photoNext=0;
                    PhotoSet(photoNext);
                }
                else
                {
                    var gid = GalleryCurrent+1>PhotoDir.length-1? 0 : GalleryCurrent+1;
                    GallerySet(gid);
                }
            }
            else
            {
                PhotoSet(photoNext);
            }
}

function PhotoPreviousDoIt()
{
            var photoNext = PhotoCurrent-1;
            if(photoNext<0)
            {
                if(GalleryLocked)
                {
                    photoNext=Photo[GalleryCurrent].length-1;
                    PhotoSet(photoNext);
                }
                else
                {
                    var gid = GalleryCurrent-1<0? PhotoDir.length-1 : GalleryCurrent-1;
                    GallerySet(gid);
                }
            }
            else
            {
                PhotoSet(photoNext);
            }
}

function PhotoRandomDoIt()
{
            if(GalleryLocked!=0)
            {
                var total=Photo[GalleryCurrent].length;
                if(total>1)
                {
                    //
                    // pick random photo
                    //
                    var photoNext=PhotoCurrent;
                    while(photoNext==PhotoCurrent || photoNext<0 || photoNext>=total)
                    {
                        photoNext = Math.floor(Math.random()*(total+1));
                    }
                    PhotoSet(photoNext);
                }
             }
            else
            {
                var total=PhotoCount();
                if(total>1)
                {
                    //
                    // pick random photo
                    //
                    var photoNext=PhotoCurrent;
                    while(photoNext==PhotoCurrent || photoNext<0 || photoNext>=total)
                    {
                        photoNext = Math.floor(Math.random()*(total+1));
                    }
                    //
                    // identify gallery and photo number
                    //
                    for(i=0; i<PhotoDir.length; i++)
                    {
                        if(photoNext<Photo[i].length)
                        {
                            GalleryCurrent=i;
                            PhotoSet(photoNext);
                            break;
                        }
                        photoNext=photoNext-Photo[i].length;
                    }
                }
            }
}

function PhotoNext() 
{
    ShowDirection=1;

//    if(ShowTimer==0)
    {
        PhotoNextDoIt();
    }

    if(ShowTimer!=0)
    {
        ShowStopTimer();
        ShowSchedule();
    }
}

function PhotoPrevious() 
{
    ShowDirection= -1;

//    if(ShowTimer==0)
    {
        PhotoPreviousDoIt();
    }

    if(ShowTimer!=0)
    {
        ShowStopTimer();
        ShowSchedule();
    }
}

function PhotoCount()
{
    var total=0;
    for(i=0; i<PhotoDir.length; i++)
    {
        total=total+Photo[i].length;
    }
    return total;
}

function PhotoRandom()
{
    ShowDirection=0;

//    if(ShowTimer==0)
    {
        PhotoRandomDoIt();
    }

    if(ShowTimer!=0)
    {
        ShowStopTimer();
        ShowSchedule();
    }
}

function PhotoSelect(thid)
{
    var a = PhotoCurrent+thid-Math.floor(ThumbMany/2);   
    PhotoSet(a);
}

function PhotoSelect0()
{
    PhotoSelect(0);
}

function PhotoSelect1()
{
    PhotoSelect(1);
}

function PhotoSelect2()
{
    PhotoSelect(2);
}

function PhotoSelect3()
{
    PhotoSelect(3);
}

function PhotoSelect4()
{
    PhotoSelect(4);
}

function PhotoSelect5()
{
    PhotoSelect(5);
}

function PhotoSelect6()
{
    PhotoSelect(6);
}

//
// Fix everything on the screen to reflect the new photo selction.
//
function PhotoSet(pid) 
{
    //
    // Make sure the starting thumbnail is within range
    //
    while(pid>=Photo[GalleryCurrent].length)
    {
        pid=pid-Photo[GalleryCurrent].length;
    }
    while(pid<0)
    {
        pid=pid+Photo[GalleryCurrent].length;
    }
    PhotoCurrent=pid;
    ThumbnailAdjust(PhotoCurrent);
    CaptionSet(PhotoCurrent);
}

//
// Load the photo info.
//   
function CaptionSet(pid)
{ 
    var field=Photo[GalleryCurrent][pid].split(" ",6);
//    CaptionElement.innerHTML='<br>'+Gallery[GalleryCurrent]+'<br>'+field[0]+'<br>'+field[1]+'"&nbsp;x&nbsp;'+field[2]+'"<br>'+field[3]+'&nbsp;of&nbsp;'+field[4]+'<br>$'+field[5];
    CaptionElement.innerHTML='<br>'+Gallery[GalleryCurrent]+'<br>'+field[0]+'<br>'+field[1]+'"&nbsp;x&nbsp;'+field[2]+'"<br>$'+field[5];
}

function ShowFlip() 
{
    if(ShowOn)
    {
        if(ShowDirection==0)
        {
            PhotoRandomDoIt();
        }
        else if(ShowDirection>0)
        {
            PhotoNextDoIt();
        }
        else
        {
            PhotoPreviousDoIt();
        }

        ShowTimer=0;

//        ShowSchedule();
    }
}

function ShowSchedule()
{
    if(ShowTimer!=0)
    {
        ShowStopTimer();
    }
    ShowTimer = setTimeout('ShowFlip()',Speed);
}

function ShowStopTimer()
{
    if(ShowTimer!=0)
    {
        clearTimeout(ShowTimer);
    }
    ShowTimer=0;
}

function ShowForwardButton()
{
    if(ShowOn)
    {
        ShowStopTimer();
        ShowOn=0;
    }
    else
    {
        ShowSchedule();
        ShowOn=1;
    }
    ShowForwardHighlight();
}

function GalleryHighlight()
{
    GalleryButton.src="button/up2w.gif";
}

function GalleryNormal()
{
    GalleryButton.src="button/up2.gif";
}
                          
function HelpHighlight()
{
    HelpButton.src="button/helpw.gif";
}

function HelpNormal()
{
    HelpButton.src="button/help.gif";
}
                          
function GalleryPreviousHighlight()
{
    GalleryPreviousButton.src="button/left2w.gif";
}

function GalleryPreviousNormal()
{
    GalleryPreviousButton.src="button/left2.gif";
}
                          
function GalleryNextHighlight()
{
    GalleryNextButton.src="button/right2w.gif";
}

function GalleryNextNormal()
{
    GalleryNextButton.src="button/right2.gif";
}
                          
function GalleryLockHighlight()
{
    if(GalleryLocked)
    {
        GalleryLockButton.src="button/lockedw.gif";
    }
    else
    {
        GalleryLockButton.src="button/unlockedw.gif";
    }
}

function GalleryLockNormal()
{
    if(GalleryLocked)
    {
        GalleryLockButton.src="button/locked.gif";
    }
    else
    {
        GalleryLockButton.src="button/unlocked.gif";
    } 
}
                          
function PhotoPreviousHighlight()
{
    PreviousButton.src="button/leftw.gif";
}

function PhotoPreviousNormal()
{
    PreviousButton.src="button/left.gif";
}
    
function PhotoMatrixHighlight()
{
    MatrixButton.src="button/upw.gif";
}

function PhotoMatrixNormal()
{
    MatrixButton.src="button/up.gif";
}
    
function PhotoNextHighlight()
{
    NextButton.src="button/rightw.gif";
}

function PhotoNextNormal()
{
    NextButton.src="button/right.gif";
}
    
function PhotoRandomHighlight()
{
    RandomButton.src="button/randomw.gif";
}

function PhotoRandomNormal()
{
    RandomButton.src="button/random.gif";
} 
 
function ShowForwardHighlight()
{
    if(ShowOn)
    {
        ForwardButton.src="button/pausew.gif";
    }
    else
    {
        ForwardButton.src="button/playw.gif";
    }
}

function ShowForwardNormal()
{
    if(ShowOn)
    {
        ForwardButton.src="button/pause.gif";
    }
    else
    {
        ForwardButton.src="button/play.gif";
    }
}  
    
function ShowFasterHighlight()
{
       FasterButton.src="button/plusw.gif";
}

function ShowFasterNormal()
{
       FasterButton.src="button/plus.gif";
}  
    
function ShowSlowerHighlight()
{
       SlowerButton.src="button/minusw.gif";
}

function ShowSlowerNormal()
{
       SlowerButton.src="button/minus.gif";
}  
    


onload = function()
{ 
    var par;

/*
    GalleryButton = document.getElementById('ButtonGallery');
    if(GalleryButton!=0)
    {
        GalleryButton.onmouseover=GalleryHighlight;
        GalleryButton.onmouseout=GalleryNormal;
        GalleryNormal();
    }

    HelpButton = document.getElementById('ButtonHelp');
    if(HelpButton!=0)
    {
        HelpButton.onmouseover=HelpHighlight;
        HelpButton.onmouseout=HelpNormal;
        HelpNormal();
    }

    GalleryPreviousButton = document.getElementById('ButtonPreviousGallery');
    GalleryPreviousButton.onclick=GalleryPrevious;
    GalleryPreviousButton.onmouseover=GalleryPreviousHighlight;
    GalleryPreviousButton.onmouseout=GalleryPreviousNormal;
    GalleryPreviousNormal();

    GalleryNextButton = document.getElementById('ButtonNextGallery');
    GalleryNextButton.onclick=GalleryNext;
    GalleryNextButton.onmouseover=GalleryNextHighlight;
    GalleryNextButton.onmouseout=GalleryNextNormal;
    GalleryNextNormal();

    GalleryLockButton = document.getElementById('ButtonLockGallery');
    GalleryLockButton.onclick=GalleryLock;
    GalleryLockButton.onmouseover=GalleryLockHighlight;
    GalleryLockButton.onmouseout=GalleryLockNormal;
    GalleryLockNormal();

    RandomButton = document.getElementById('ButtonRandom');
    RandomButton.onclick=PhotoRandom;
    RandomButton.onmouseover=PhotoRandomHighlight;
    RandomButton.onmouseout=PhotoRandomNormal;
    PhotoRandomNormal();*/
    
    MatrixRef = document.getElementById('matrix');
    MatrixButton = document.getElementById('ButtonMatrix');
//    PreviousButton.onclick=PhotoMatrix;
    MatrixButton.onmouseover=PhotoMatrixHighlight;
    MatrixButton.onmouseout=PhotoMatrixNormal;
    PhotoMatrixNormal();

    BackRef = document.getElementById('back');
    PreviousButton = document.getElementById('ButtonPrevious');
//    PreviousButton.onclick=PhotoPrevious;
    PreviousButton.onmouseover=PhotoPreviousHighlight;
    PreviousButton.onmouseout=PhotoPreviousNormal;
    PhotoPreviousNormal();
    
    NextRef = document.getElementById('next');
    NextButton = document.getElementById('ButtonNext');
//    NextButton.onclick=PhotoNext;
    NextButton.onmouseover=PhotoNextHighlight;
    NextButton.onmouseout=PhotoNextNormal;
    PhotoNextNormal();

    ForwardButton = document.getElementById('ButtonForward');
    ForwardButton.onclick=ShowForwardButton;	
    ForwardButton.onmouseover=ShowForwardHighlight;
    ForwardButton.onmouseout=ShowForwardNormal;
    ShowForwardNormal();
    
/*    FasterButton = document.getElementById('ButtonFaster');
    FasterButton.onclick=SpeedFaster;	
    FasterButton.onmouseover=ShowFasterHighlight;
    FasterButton.onmouseout=ShowFasterNormal;
    ShowFasterNormal();

    SlowerButton = document.getElementById('ButtonSlower');
    SlowerButton.onclick=SpeedSlower;	
    SlowerButton.onmouseover=ShowSlowerHighlight;
    SlowerButton.onmouseout=ShowSlowerNormal;
    ShowSlowerNormal();*/

    MainPhoto[0] = document.getElementById('MainPhoto');
    MainPhoto[0].onload=FadeIn;

    MainPhoto[1] = document.getElementById('OtherPhoto');
    MainPhoto[1].onload=FadeIn;

    CaptionElement = document.getElementById('PhotoCaption');  
	
/*    SearchTextElement=document.getElementById("SearchText");
    if(SearchTextElement!=0)
    {
        SearchTextElement.value="";
        SearchTextElement.onchange=PhotoSearch;
    }*/

//    GalleryCurrent=0;

    par=gup('gallery');
//    if(par!="") GalleryCurrent=Math.floor(par);
    if(par!="") GalleryCurrent=GallerySearch(par);
    if(GalleryCurrent<0 || GalleryCurrent>=PhotoDir.length) GalleryCurrent=0;

    par=gup('image');
//    if(par!="") PhotoCurrent=Math.floor(par);
    if(par!="") PhotoCurrent=ImageSearch(par);

    par=gup('name');
    if(par!="") FindAndSet(par);

    if(GalleryCurrent<0 || GalleryCurrent>=PhotoDir.length) GalleryCurrent=0;
    if(PhotoCurrent<0 || PhotoCurrent>=Photo[GalleryCurrent].length) PhotoCurrent=0;

    PhotoSet(PhotoCurrent); 

}




