// Gallery code by Tyrel Haveman for Amberfly Studio
// Copyright (c) 2008 Amberfly Studio. All rights reserved.

var first;
var second;
var third;
var fourth;
var popup;
var fading = false;
var visible = false;
var fade1;
var fade2;
var fade3;
var fade4;
var fade5;

function set_new_image_prev(width, height, src)
{
    var popwidth = width;
    var popheight = height + 20;
    var popright = 266 - width / 2;
    set_new_image_root(popwidth, popheight, popright, 'images/previous/' + src + '.jpg');
}

function set_new_image(width, height, src)
{
    var popwidth = width;
    var popheight = height + 20;
    var popright = 266 - width / 2;
    set_new_image_root(popwidth, popheight, popright, 'images/transitions/' + src + '.jpg');
}

function set_new_image_root(popwidth, popheight, popright, src)
{
    if (visible == true && fading == true)
    {
        // Fading out, so we will wait for that to finish first
        setTimeout('set_new_image_root(' + popwidth + ',' + popheight + ',' + popright + ',\'' + src + '\')', 100);
        return;
    }
    if (visible == true && fading == false)
    {
        // Visible
        // Let's fade it out first
        close_popup();
        setTimeout('set_new_image_root(' + popwidth + ',' + popheight + ',' + popright + ',\'' + src + '\')', 100);
        return;
    }
    if (visible == false && fading == true)
    {
        // Fading in already... wait and fade it back out
        setTimeout('set_new_image_root(' + popwidth + ',' + popheight + ',' + popright + ',\'' + src + '\')', 100);
        return;
    }
    
    // If we get here, it's not visible and not fading

    var img = popup.lastChild;

    img.style.display = 'none';
    popup.style.display = 'none';
    fourth.style.display = 'none';
    third.style.display = 'none';
    second.style.display = 'none';
    first.style.display = 'none';
    
    first.style.width = popwidth + 28 + 'px';
    first.style.height = popheight + 23 + 'px';
    first.style.right = popright - 4 + 'px';

    second.style.width = popwidth + 26 + 'px';
    second.style.height = popheight + 21 + 'px';
    second.style.right = popright - 3 + 'px';

    third.style.width = popwidth + 24 + 'px';
    third.style.height = popheight + 19 + 'px';
    third.style.right = popright - 2 + 'px';

    fourth.style.width = popwidth + 22 + 'px';
    fourth.style.height = popheight + 17 + 'px';
    fourth.style.right = popright - 1 + 'px';

    popup.style.width = popwidth + 'px';
    popup.style.height = popheight + 'px';
    popup.style.right = popright + 'px';
    
    img.width = popwidth;
    img.height = popheight - 20;
    img.src = 'images/blank.gif';
    
    fade_it_in();
    
    setTimeout('pop_it_in(' + popwidth + ',' + (popheight - 20) + ',\'' + src + '\')', 100);
}

function fade_it_in()
{
    fade1 = new Spry.Effect.Fade('first', {duration: 800, from: 0, to: 20});
    fade2 = new Spry.Effect.Fade('second', {duration: 800, from: 0, to: 40});
    fade3 = new Spry.Effect.Fade('third', {duration: 800, from: 0, to: 60});
    fade4 = new Spry.Effect.Fade('fourth', {duration: 800, from: 0, to: 80});
    fade5 = new Spry.Effect.Fade('popup', {duration: 800, from: 0, to: 100});
    
    fading = true;
    
    fade1.start();
    fade2.start();
    fade3.start();
    fade4.start();
    fade5.start();
    
    setTimeout('visible = true', 900);
    setTimeout('fading = false', 900);
}

function final_fade_in()
{
    var fade6 = new Spry.Effect.Fade('preview', {duration: 400, from: 0, to: 100})
    fade6.start();
}

function pop_it_in(width, height, file)
{
    var img = popup.lastChild;
    img.onload = final_fade_in;
    img.src = file;
}

function load_vars()
{
    first = document.getElementById('first');
    second = document.getElementById('second');
    third = document.getElementById('third');
    fourth = document.getElementById('fourth');
    popup = document.getElementById('popup');
}

function close_popup()
{
    fade1 = new Spry.Effect.Fade('first', {duration: 500, from: 20, to: 0});
    fade2 = new Spry.Effect.Fade('second', {duration: 500, from: 40, to: 0});
    fade3 = new Spry.Effect.Fade('third', {duration: 500, from: 60, to: 0});
    fade4 = new Spry.Effect.Fade('fourth', {duration: 500, from: 80, to: 0});
    fade5 = new Spry.Effect.Fade('popup', {duration: 250, from: 100, to: 0});
    
    fading = true;
    
    fade1.start();
    fade2.start();
    fade3.start();
    fade4.start();
    fade5.start();
  
    setTimeout('first.style.display=\'none\'', 500);
    setTimeout('second.style.display=\'none\'', 500);
    setTimeout('third.style.display=\'none\'', 500);
    setTimeout('fourth.style.display=\'none\'', 500);
    setTimeout('popup.style.display=\'none\'', 500);
    setTimeout('visible = false', 600);
    setTimeout('fading = false', 600);
}

