// 
// photo album handling
//
// TODO: it can be slow, loading the
// thumbs, and the user is not certain
// that it is happening. could we make
// this process asynchronous using the
// HTTPrequest protocol? It should be
// possible to make the request and
// get the response, but can we load 
// an image and do something with it?
//
// if this all works, we could cache the "next"
// image while the user is viewing the current
// one and then just invoke it. This is a little
// like Google maps and how they load the
// image parts of the map while you view other
// parts
//

var total_photos = 56; // 57;

var numbers = Array(
    346,
    347,
    348,
    349,
    350,
    351,
    352,
    353,
    354,
    355,
    356,
    357,
    358,
    359,
    360,
    361,
    362,
    363,
    330,
    331,
    332,
    317,
    318,
    319,
    320,
    321,
    322,
    333,
    334,
    335,
    336,
    337,
    338,
    339,
    340,
    341,
    342,
    343,
    344,
    345,
    300,
    301,
    302,
    303,
    304,
    // 305,
    306,
    307,
    308,
    309,
    310,
    311,
    312,
    313,
    314,
    315,
    316);

var titles = Array(
    // 346
    "Sunrise September 6, 2005                   ",
    // 347
    "Sunrise September 6, 2005                   ",
    // 348
    "Sunrise September 5, 2005                   ",
    // 349
    "Sunrise September 5, 2005                   ",
    // 350
    "Sunrise September 5, 2005                   ",
    // 351
    "Sunrise September 1, 2005                   ",
    // 352
    "Sunrise September 1, 2005                   ",
    // 353
    "Sunrise August 31, 2005                     ",
    // 354
    "Sunrise August 31, 2005                     ",
    // 355
    "Sunrise August 31, 2005                     ",
    // 356
    "Sunrise August 31, 2005                     ",
    // 357
    "Sunrise August 31, 2005                     ",
    // 358
    "Sunrise August 31, 2005                     ",
    // 359
    "Sunrise August 31, 2005                     ",
    // 360
    "Sunrise August 30, 2005                     ",
    // 361
    "Sunrise August 30, 2005                     ",
    // 362
    "Sunrise August 29, 2005                     ",
    // 363
    "Sunrise August 26, 2005                     ",
    // 330
    "Sunrise August 22, 2005                     ",
    // 331
    "Sunrise August 22, 2005                     ",
    // 332
    "Sunrise August 22, 2005                     ",
    // 317
    "Sunrise August 15, 2005                     ",
    // 318
    "Sunrise August 11, 2005                     ",
    // 319
    "Sunrise August 11, 2005                     ",
    // 320
    "Sunrise March 23, 2005                      ",
    // 321
    "Sunrise March 14, 2005                      ",
    // 322
    "Sunrise March 14, 2005                      ",
    // 333
    "Sunrise March 4, 2005                       ",
    // 334
    "Sunrise March 4, 2005                       ",
    // 335
    "Sunrise February 25, 2005                   ",
    // 336
    "Sunrise February 25, 2005                   ",
    // 337
    "Sunrise February 25, 2005                   ",
    // 338
    "Sunrise February 25, 2005                   ",
    // 339
    "Sunrise February 24, 2005                   ",
    // 340
    "Sunrise February 24, 2005                   ",
    // 341
    "Sunrise February 24, 2005                   ",
    // 342
    "Sunrise January 31, 2005                    ",
    // 343
    "Sunrise January 30, 2005                    ",
    // 344
    "Sunrise January 20, 2005                    ",
    // 345
    "Sunrise January 19, 2005                    ",
    // 300
    "Sunrise October 9, 2003                     ",
    // 301
    "Sunrise October 9, 2003 (earlier)           ",
    // 302
    "Sunrise October 7, 2003 (with tug and barge)",
    // 303
    "Sunrise October 6, 2003                     ",
    // 304
    "Sunrise October 6, 2003 (earlier)           ",
    // 305
    //"Sunrise October 3, 2003                     ",
    // 306
    "Sunrise October 3, 2003 (more)              ",
    // 307
    "Sunrise October 3, 2003 (earlier)           ",
    // 308
    "Sunrise October 2, 2003                     ",
    // 309
    "Sunrise October 1, 2003 (1 of 3)            ",
    // 310
    "Sunrise October 1, 2003 (2 of 3)            ",
    // 311
    "Sunrise October 1, 2003 (3 of 3)            ",
    // 312
    "Sunrise September 30, 2003 (1 of 3)         ",
    // 313
    "Sunrise September 30, 2003 (2 of 3)         ",
    // 314
    "Sunrise September 30, 2003 (3 of 3)         ",
    // 315
    "Sunrise September 26, 2003                  ",
    // 316
    "Sunrise September 26, 2003 (more)           ");

var ksizes = Array(
    // 346
    117,
    // 347
    112,
    // 348
    113,
    // 349
    112,
    // 350
    108,
    // 351
    105,
    // 352
    136,
    // 353
    230,
    // 354
    171,
    // 355
    146,
    // 356
    151,
    // 357
    172,
    // 358
    146,
    // 359
    147,
    // 360
    155,
    // 361
    121,
    // 362
    126,
    // 363
    188,
    // 330
    137,
    // 331
    101,
    // 332
    157,
    // 317
    110,
    // 318
    138,
    // 319
    114,
    // 320
    89,
    // 321
    96,
    // 322
    101,
    // 333
    98,
    // 334
    108,
    // 335
    117,
    // 336
    93,
    // 337
    164,
    // 338
    138,
    // 339
    101,
    // 340
    118,
    // 341
    149,
    // 342
    110,
    // 343
    115,
    // 344
    135,
    // 345
    150,
    // 300
    191,
    // 301
    125,
    // 302
    182,
    // 303
    140,
    // 304
    137,
    // 305
    //158,
    // 306
    163,
    // 307
    113,
    // 308
    180,
    // 309
    171,
    // 310
    158,
    // 311
    113,
    // 312
    142,
    // 313
    116,
    // 314
    81,
    // 315
    128,
    // 316
    113);

var narratives = Array(
    // 346
    "Massive rays blasting from behind the cloud. [Kodak DX6490]",
    // 347
    "Colorful sunrise. [Kodak DX6490]",
    // 348
    "Sun just appearing over the land. [Kodak DX6490]",
    // 349
    "Lighting up the clouds from behind. [Kodak DX6490]",
    // 350
    "Coming out of a bank of clouds. [Kodak DX6490]",
    // 351
    "Beautiful Maui sunrise, rising just over the point of land. [Kodak DX6490]",
    // 352
    "A fiery one. [Kodak DX6490]",
    // 353
    "The moon hanging over a tropical sunrise. [Kodak DX6490]",
    // 354
    "Coloring the sky, clouds and ocean. [Kodak DX6490]",
    // 355
    "The cloud in the foreground looks like Kermit the Frog. [Kodak DX6490]",
    // 356
    "Powerful rays. [Kodak DX6490]",
    // 357
    "The upper cloud bank is backlit. [Kodak DX6490]",
    // 358
    "Sun breaking through the clouds. [Kodak DX6490]",
    // 359
    "Fiery tropical sunrise. [Kodak DX6490]",
    // 360
    "The sky is painted in spectacular colors. [Kodak DX6490]",
    // 361
    "Sea and sky painted. [Kodak DX6490]",
    // 362
    "Great reflection off the water. [Kodak DX6490]",
    // 363
    "Sky lit up with amazing color. [Kodak DX6490]",
    // 330
    "Nice reflection off of the ocean. [Kodak DX6490]",
    // 331
    "More reflections, nice sun rays coming through the clouds. [Kodak DX6490]",
    // 332
    "Sun rays and reflections off of the water. [Kodak DX6490]",
    // 317
    "Clouds lit up from below, lots of sun beams shooting off in all directions. [Kodak DX6490]",
    // 318
    "Caught the sun higher up today, behind the clouds; again lots of sunbeams escaping outward. [Kodak DX6490]",
    // 319
    "About to break out of the clouds. [Kodak DX6490]",
    // 320
    "Sun rising over the mountain and caught in the clouds. [Kodak DX6490]",
    // 321
    "A pretty spectacular sunrise. [Kodak DX6490]",
    // 322
    "Just breaking out of the clouds, but there are several layers of clouds this morning. [Kodak DX6490]",
    // 333
    "Pride of Aloha arriving on a smooth bay. [Kodak DX6490]",
    // 334
    "Closeup of Pride of Aloha. [Kodak DX6490]",
    // 335
    "Part of a series today, showing beautiful clouds over Haleakala. [Kodak DX6490]",
    // 336
    "Closeup of sun behind the clouds. [Kodak DX6490]",
    // 337
    "Clouds over the ocean, note red light on buoy. [Kodak DX6490]",
    // 338
    "The whole lovely scene. [Kodak DX6490]",
    // 339
    "The sun, blocked by the clouds, still lights up the mist near Paia. [Kodak DX6490]",
    // 340
    "Tug and barge enter Kahului Harbor under sunrise. [Kodak DX6490]",
    // 341
    "Haleakala Highway, morning rush hour. [Kodak DX6490]",
    // 342
    "A bit of haze, but lots of color this morning. Hazy for several days. [Kodak DX6490]",
    // 343
    "Beautiful reflection on the water. [Kodak DX6490]",
    // 344
    "Nearly monochrome morning with Haleakala Highway rush hour. [Kodak DX6490]",
    // 345
    "Incredible colors this morning helped along by the haze. [Kodak DX6490]",
    // 300
    "Nice sunrise this morning, with plenty of higher clouds to catch the sunrise colors. [Minolta S414]",
    // 301
    "Earlier, before the photo above. The colors in the darkened sky were just incredible. [Minolta S414]",
    // 302
    "Tug and barge heading for Kahului Harbor, overshadowed by Haleakala and a lovely sunrise. [Minolta S414]",
    // 303
    "Another beautiful sunrise as the sun tries to break through the clouds along Haleakala. [Minolta S414]",
    // 304
    "Earlier than the previous shot, the sun is just starting to appear through the clouds along Haleakala. [Minolta S414]",
    // 305
    //"The sun was in haze during this morning's show. Managed to get just the right timing as the tug and barge went through the sun's reflection. The water was like glass, not at all common. [Minolta S414]",
    // 306
    "This was a general shot of the sunrise. The sun was in haze during this morning's show. The water was like glass, not at all common. [Minolta S414]",
    // 307
    "Sun was just peeking over Haleakala. [Minolta S414]",
    // 308
    "Fantastic clouds this morning, catching the great colors. [Minolta S414]",
    // 309
    "Very nice sunrise this morning. This is one of a series of 3 photos. This shows just how fast the sunrise changes in color and form. These 3 are all from the same period of a few minutes. [Minolta S414]",
    // 310
    "Earlier than the image above. These 3 are all from the same period of a few minutes. [Minolta S414]",
    // 311
    "Earliest image of the set. These 3 are all from the same period of a few minutes. [Minolta S414]",
    // 312
    "Another series of 3 photos, taken only a few minutes from each other. [Minolta S414]",
    // 313
    "Earlier, the sky was a different color. Again, part of the series of 3 photos, taken only a few minutes from each other. [Minolta S414]",
    // 314
    "First of the series of 3, with the sun just peeking over Haleakala. [Minolta S414]",
    // 315
    "The sun was sending rays in all directions from behind the clouds. [Minolta S414]",
    // 316
    "Earlier, the clouds had a bright border formed by sun. [Minolta S414]");

var current = 0;

var first_time = 1;

function first_pass()
{
    if (first_time === 1)
    {
      //  alert("first pass");
        load_picture(0); // load picture zero
        first_time = 0; // prevent re-invocation
    }
}

function build_selector()
{
    var selector = document.getElementById("picture_selector"); 
    
    for (i = 0 ; i < total_photos ; i++)
    {
        newtext = document.createTextNode(i + " - " + titles[i]); // text content
        newoption = document.createElement("option");
        newoption.setAttribute("value",i); // value is pic number only
        newoption.appendChild(newtext); // add text
        selector.appendChild(newoption);
    }
}

function create_selector()
{
    for (i = 0 ; i < total_photos ; i++)
    {
        document.write('<option value="' + i + '">' + 
                       i + " - " + titles[i] + '</option>');
    }
}

function get_thumb_url (position) {

    url = "http://www219.pair.com/swansonr/thumbs/maui_small" + numbers[position] + ".jpg";
    return url;
}

function get_picture_url (position) {

    url = "http://www219.pair.com/swansonr/pics/maui_large" + numbers[position] + ".jpg";
    return url;
}

//
// advance and load next if there is one
//
function load_next() {
    //alert(current);
    current = current + 1;
    if (current < total_photos)
    {
        load_picture(current);
    }
    else
    {
        current = current - 1;
        load_picture(total_photos - 1);
    }
}
//
// pop up window with current big photo
//
function load_current() {
    var the_picture_url = get_picture_url(current);
    window.open(the_picture_url,'sunrise_photo','resizable=yes,scrollbars=yes');
}

function load_selected()
{
    var selector = document.getElementById("picture_selector"); 
    var cur = selector.options[selector.selectedIndex].value;
    //alert("load: " + cur);
    current = parseInt(cur);
    load_picture(current);
}

//
// decrement and load previous if there is one
//
function load_previous() {
    current = current - 1;
    if (current >= 0)
    {
        load_picture(current);
    }
    else
    {
        current = 0;
        load_picture(0);
    }
}

function load_picture(at)
{
    var ximage = document.getElementById("thumb"); // the IMG tag
    var the_thumb_url = get_thumb_url(at);
    ximage.setAttribute("src",the_thumb_url);
    var the_picture_url = get_picture_url(at);
    set_text(the_picture_url);
    set_head_narrative(at);
}

function removeAllChildren(xxx)
{
    while (xxx.hasChildNodes()) 
    {
        xxx.removeChild(xxx.firstChild);
    }
}

//
// sets the prompt just below the photo with
// an anchor that loads the full-size photo
//
function set_text(text)
{
    var prompt = document.getElementById("prompt");
    removeAllChildren(prompt); // remove all content
    //
    // add back in
    //
    newtext = document.createTextNode("View Larger Photo"); // text for anchor
    newpara = document.createElement("p"); // new wrapper for anchor
    newpara.setAttribute("class","timestamp"); // make it smaller 
    anchor = document.createElement("a"); // new anchor
    anchor.setAttribute("href",text);
    anchor.appendChild(newtext); // add text to anchor
    newpara.appendChild(anchor); // put anchor inside paragraph
    // DO NOT DO THIS FOR NOW  prompt.appendChild(newpara); // put paragraph into div
}


function set_head_narrative(position)
{
    //
    // first the heading
    //
    var subh = document.getElementById("subhead");
    removeAllChildren(subh); // remove all content
    //
    // add back in
    //
    newtext = document.createTextNode(titles[position]); // text for title
    newhead = document.createElement("h2"); 
    newhead.setAttribute("class","heading2");
    newhead.appendChild(newtext); // add text to heading
    subh.appendChild(newhead); // put heading into div
    //
    // now the narrative below
    //
    var narr = document.getElementById("narrative");
    removeAllChildren(narr); // remove all content
    //
    // add back in
    //
    newtext = document.createTextNode("[Image: " + position + "] " + narratives[position]); // text for narrative
    newpara = document.createElement("p"); // new wrapper for narrative
    newpara.setAttribute("class","body"); // make it normal
    newpara.appendChild(newtext); // add text to paragraph
    narr.appendChild(newpara); // put paragraph into div
}

