//! from the page:
//    streamname
//    ImageList;

//
// Uses http://developer.yahoo.com/yui/
//


// useful link: mobile UA list found in http://www.zytrax.com/tech/web/mobile_ids.html
var isMobile = navigator.userAgent.match(/iP(ad|hone)|Mobile|Android|Windows CE|NokiaN/);

var current_sort_hash = 'random'; //! default is to show randomly

//! alert(document.baseURI);

function update_image_size(obj)
{
    var screen_width  = YAHOO.util.Dom.getViewportWidth();
    var screen_height = YAHOO.util.Dom.getViewportHeight();

    if (screen_width && screen_height && obj.actual_width && obj.actual_height)
    {
        var w;
        var h;

        if (obj.actual_width <= screen_width && obj.actual_height <= screen_height)
        {
            w = obj.actual_width;
            h = obj.actual_height;
        }
        else if (screen_width / screen_height  <  obj.actual_width / obj.actual_height)
        {
            w = screen_width - 4;
            h = Math.round(obj.actual_height * (screen_width / obj.actual_width));
        }
        else
        {
            h = screen_height - 4;
            w = Math.round(obj.actual_width * (screen_height / obj.actual_height));
        }

        //! document.getElementById('percent').innerHTML = Math.round((w*h) * 100 / (obj.actual_width * obj.actual_height));

        obj.style.padding = "0";
        obj.style.width  = w + "px";
        obj.style.height = h + "px";
        if (w < screen_width)
            obj.style.paddingLeft = Math.floor((screen_width - w)/2) + "px";
        if (h < screen_height)
            obj.style.paddingTop = Math.floor((screen_height - h)/2) + "px";
        obj.style.visibility = ''; //! in case it had been hidden during preloading
    }
}

function restoregrid()
{
    window.location.hash = current_sort_hash;

    document.getElementById("grid").style.display = 'block';
    document.getElementById("fs").style.visibility = 'hidden';
    var to_be_shown = document.getElementById(currently_shown_pix_id);

    //!                                     this puts the row in the center
    var scrolltop = to_be_shown.offsetTop - (YAHOO.util.Dom.getViewportHeight() - to_be_shown.clientHeight)/2;

    document.body.scrollTop = scrolltop;
    document.documentElement.scrollTop = scrolltop;

    current_image_obj = null;
}

var currently_shown_pix_id  = null;

//function set_loading_location()
//{
//    var screen_width  = YAHOO.util.Dom.getViewportWidth();
//    var screen_height = YAHOO.util.Dom.getViewportHeight();
//
//    var bottom_of_buttons = document.getElementById('button_container').clientHeight;
//    var loading = document.getElementById('loading');
//    var mid =  bottom_of_buttons + (screen_height - bottom_of_buttons) / 2 - loading.clientHeight / 2;
//    //! alert("var " + mid + " = " + bottom_of_buttons + " + (" + screen_height + " - " + bottom_of_buttons + ") / 2 - " + loading.clientHeight+" / 2");
//
//    if (mid < bottom_of_buttons)
//        mid = bottom_of_buttons;
//    loading.style.top = mid + "px";
//}

var image_blog_post_url;
var current_image_obj;
var loading_image_obj;

function SetOpacityTo1(item)
{
    var obj = YAHOO.util.Dom.get(item);
    YAHOO.util.Dom.setStyle(obj, 'opacity', 1);
    obj.style.filter = ''; //! because IE is fucked
}


function showpix(id, width, height, post_url)
{
    flush_preloading();

    if (! current_image_obj)
    {
        //! first pic after switching from grid mode
        YAHOO.util.Dom.setStyle(['fsiA', 'fsiB'], 'opacity', 0);

        document.getElementById("grid").style.display = 'none';
        document.getElementById("fs").style.visibility = ''; //! set to visible
        loading_image_obj = document.getElementById('fsiA');
        YAHOO.util.Dom.setStyle('fsiA', 'zIndex', 3);
        YAHOO.util.Dom.setStyle('fsiB', 'zIndex', 2);

        YAHOO.util.Dom.setStyle('fsiB', 'width',  '10px'); // IE7 seems to need this
        YAHOO.util.Dom.setStyle('fsiB', 'height', '10px'); // IE7 seems to need this

    }
    else if (current_image_obj.id == "fsiA")
    {
        loading_image_obj = document.getElementById('fsiB');
        YAHOO.util.Dom.setStyle('fsiB', 'opacity', 0);
        YAHOO.util.Dom.setStyle('fsiB', 'zIndex', 3);
        YAHOO.util.Dom.setStyle('fsiA', 'zIndex', 2);
    }
    else
    {
        loading_image_obj = document.getElementById('fsiA');
        YAHOO.util.Dom.setStyle('fsiA', 'opacity', 0);
        YAHOO.util.Dom.setStyle('fsiA', 'zIndex', 3);
        YAHOO.util.Dom.setStyle('fsiB', 'zIndex', 2);
    }

    loading_image_obj.actual_width  = width; //! save these so we can use in screen-fitting calculations
    loading_image_obj.actual_height = height;

    image_blog_post_url = post_url
    currently_shown_pix_id = id; //! this is here early so that quick [next] jumps in succession can actually jump past images that aren't yet loaded
    window.location.hash = id; //! this should probably be later, in load_completed

    update_image_size(loading_image_obj);

    var url = Id2Url[id]

    if (loading_image_obj.src == url)
    {
        //! alert("already loaded ["+url+"]");
        load_completed();
    }
    else
    {
        show_loading_message();
        //! alert("loading ["+url+"]");
        loading_image_obj.src = url; //! start downloading
        //! Sigh, it seems that the old image is not cleared out right away when you set the 'src' above,
        //! but a new opacity does take effect right away, so you can end up seeing the old picture for a moment
        //!   if (! current_image_obj)
        //!     YAHOO.util.Dom.setStyle(loading_image_obj, 'opacity', 0.5); //! No old one to cover, so can show the new one while it's loading
        //!                                                                 //! (this is after the assignment to 'src' so that any previous image doesn't flash up)
    }
}

var A2B_transition1;
var A2B_transition2;
var B2A_transition1;
var B2A_transition2;

function init_frame_animations()
{
    A2B_transition2 = new YAHOO.util.Anim('fsiA', { opacity: { from: 1, to: 0 } }, 0.5);
    A2B_transition1 = new YAHOO.util.Anim('fsiB', { opacity: { from: 0, to: 1 } }, 0.5);

    A2B_transition1.onComplete.subscribe(function() {
        SetOpacityTo1('fsiB');
        start_preload();
        if (slideshow_running) {
            slideshow_reset_interval();
            slideshow_interval_tick();
        }
    })

    B2A_transition1 = new YAHOO.util.Anim('fsiB', { opacity: { from: 1, to: 0 }}, 0.5);
    B2A_transition2 = new YAHOO.util.Anim('fsiA', { opacity: { from: 0, to: 1 }}, 0.5);
    B2A_transition2.onComplete.subscribe(function() {
        SetOpacityTo1('fsiA');
        start_preload();
        if (slideshow_running) {
            slideshow_reset_interval();
            slideshow_interval_tick();
        }
    })
}

function load_completed()
{
    //! alert("load_completed()");
    hide_loading_message();
    slideshow_reset_interval(); //! only matters if slideshow is on

    if (! A2B_transition1)
        init_frame_animations();

    //! just in case any of these were going...
    A2B_transition1.stop();
    A2B_transition2.stop();
    B2A_transition1.stop();
    B2A_transition2.stop();

    if (! current_image_obj) {
        //! first image after moving from grid mode, so just show the thing
        SetOpacityTo1(loading_image_obj);
    } else if (current_image_obj.id == 'fsiA') {
        A2B_transition1.duration = A2B_transition2.duration = slideshow_running ? 1.5 : 0.5;

        A2B_transition1.animate();
        A2B_transition2.animate();
    } else {
        B2A_transition1.duration = B2A_transition2.duration = slideshow_running ? 1.5 : 0.5;

        B2A_transition1.animate();
        B2A_transition2.animate();
    }
    
    current_image_obj = loading_image_obj;
    loading_image_obj = null;

    show_current_vote();


    // if we came straight here, we'll want to load the grid images in the background...
    if (! grid_images_have_been_loaded)
        jumble(false, true);
}

function fsiA_load_completed()
{
    //! alert("fsiA load_completed()");
    if (loading_image_obj && loading_image_obj.id == 'fsiA') {
        //! alert('calling load complete');
        load_completed();
    } else {
        //! alert("calling preload complete");
        preloader_load_completed();
    }
}

function fsiB_load_completed()
{
    //! alert('fisb complete');
    //! alert("fsiB load_completed()");
    if (loading_image_obj && loading_image_obj.id == 'fsiB') {
        //! alert('calling load complete');
        load_completed();
    } else {
        //! alert("calling preload complete");
        preloader_load_completed();
    }
}

var BR = document.createElement('br');
BR.clear = 'all';


var grid_images_have_been_loaded = false;

function jumble(chrono, in_background)
{
    grid_images_have_been_loaded = true;

    var images = document.getElementById("images");
    var I;

    if (chrono) {
        current_sort_hash = 'chrono';
        //! alert('setting to chrono');
        document.getElementById("shuffled").style.display = 'none';
        document.getElementById("sorted").style.display = 'inline';
    } else {
        current_sort_hash = 'random';
        //! alert('setting to random');
        document.getElementById("sorted").style.display = 'none';
        document.getElementById("shuffled").style.display = 'inline';
    }
    if (! in_background)
        window.location.hash = current_sort_hash

    if (! chrono)
    {
        I = new Array();
        for (i = 0; i < ImageList.length; i++) {
            var index = Math.floor(Math.random() * I.length);
            I.splice(index, 0, ImageList[i]);
        }
    }

    while (images.hasChildNodes())
        images.removeChild(images.childNodes[0])


    var per_row = 3
    if (window.location.pathname.match(/\/bg_v\./))
        per_row = 5;

    for (i = 0; i < ImageList.length; i++)
    {
        var obj;
        if (chrono) {
            obj = ImageList[i];
        }
        else
        {
            var index = Math.floor(Math.random() * I.length);
            var obj_list = I.splice(index, 1);
            obj = obj_list[0];
        }

        images.appendChild(obj);

        //
        // Ensure that the grid image has been loaded....
        //
        var img = document.getElementById("img_" + obj.id);
        if (! img.src)
            img.src = Id2Src[obj.id];

        if (i % per_row == (per_row-1)) {
            images.appendChild(BR.cloneNode(false));
        }
    }
}


var Expires = new Date(2020, 1, 1, 0, 0, 0);
var B = YAHOO.util.Cookie.get("B");
if (!B)
{
    B = "A" + Math.round(Math.random() * 100000000) + "x" + Math.round(Math.random() * 100000000) + "x" + Math.round(Math.random() * 100000000) + "Z";
    YAHOO.util.Cookie.set("B", B, { expires: Expires });
}

if (YAHOO.util.Cookie.get('streamranks') == null)
{
    YAHOO.util.Cookie.set('streamranks', '', { expires: Expires, path: "/photostream/" });
}

var current_vote = '-';
function show_current_vote()
{
    current_vote = YAHOO.util.Cookie.getSub('streamranks', currently_shown_pix_id);
    if (current_vote == 'U') {
        document.getElementById('check_down').style.visibility = 'hidden';
        document.getElementById('check_up'  ).style.visibility = ''; //! visible
    } else if (current_vote == 'D') {
        document.getElementById('check_down').style.visibility = ''; //! visible
        document.getElementById('check_up'  ).style.visibility = 'hidden';
    } else {
        document.getElementById('check_down').style.visibility = 'hidden';
        document.getElementById('check_up'  ).style.visibility = 'hidden';
    }
}

function save_vote(vote)
{
    current_vote = vote;
    YAHOO.util.Cookie.setSub('streamranks', currently_shown_pix_id, vote);
    YAHOO.util.Connect.asyncRequest('GET', "http://regex.info/blog/photostream/vote.cgi?B=" + B + "&ID=" + currently_shown_pix_id + "&v=" + vote + "&stream=" + streamname);
}

function thumbs_up(passive)
{
    if (current_vote == 'U') {
        document.getElementById('check_up').style.visibility = 'hidden';
        current_vote = '-';
    } else  {
        document.getElementById('check_down').style.visibility = 'hidden';
        document.getElementById('check_up').style.visibility = ''; //! visible
        current_vote = 'U';
    }
    if (! passive)
        save_vote(current_vote);
}

function thumbs_down(passive)
{
    if (current_vote == 'D') {
        document.getElementById('check_down').style.visibility = 'hidden';
        current_vote = '-';
    } else  {
        document.getElementById('check_up').style.visibility = 'hidden';
        document.getElementById('check_down').style.visibility = ''; //! visible
        current_vote = 'D';
    }
    if (! passive)
        save_vote(current_vote);
}


var loading_fade_out = new YAHOO.util.Anim('loading', { opacity: { to: 0 }}, 0.25);
var loading_fade_in  = new YAHOO.util.Anim('loading', { opacity: { to: 1 }}, 1, YAHOO.util.Easing.easeIn);

loading_fade_in.onComplete.subscribe(function() {
    SetOpacityTo1('loading');
});

function show_loading_message()
{
    if (! slideshow_running)
    {
        //set_loading_location();
        loading_fade_out.stop();
        loading_fade_in.animate()
    }
}

function hide_loading_message(e)
{
    loading_fade_in.stop();
    loading_fade_out.animate();
}

var movement_direction = 'next';
var ToBePreloaded = new Array();

function flush_preloading()
{
    ToBePreloaded.length = 0;
}

function start_preload()
{
    flush_preloading();
    var next = (slideshow_running || movement_direction == 'next') ? find_next_image(     ) : find_prev_image(     );
    var obj = next;

    var preload_target = document.getElementById(current_image_obj.id == 'fsiA' ? 'fsiB' : 'fsiA');
    preload_target.style.visibility = 'hidden';
    preload_target.src = Id2Url[next.id];

    if (! slideshow_running)
    {
        //! preload up to 5 images ('next' and 4 more)
        for (i = 1; i < 5; i++)
        {
            obj = movement_direction == 'next' ? find_next_image(obj) : find_prev_image(obj);
            ToBePreloaded.push(obj);
        }
    }
}

var preloader_img_obj = document.createElement('img');


function preloader_load_completed()
{
    var next = ToBePreloaded.shift();
    if (next) {
        preloader_img_obj.src = Id2Url[next.id];
    }
}

var buttons_fade_in  = new YAHOO.util.Anim('button_container', { opacity: { to: 1   } }, 0.3, YAHOO.util.Easing.easeIn);
var buttons_fade_out = new YAHOO.util.Anim('button_container', { opacity: { to: 0   } }, 0.6, YAHOO.util.Easing.easeOut);

buttons_fade_in.onComplete.subscribe(function() {
    SetOpacityTo1('button_container');
});

var buttons_being_shown = false;

function show_buttons()
{
    if (document.getElementById('button_container').style.display == 'none')
        return;

    if (!buttons_being_shown) 
    {
        buttons_being_shown = true;
        buttons_fade_out.stop();
        buttons_fade_in.animate();
    }
}



var slideshow_running = false;
var slideshow_interval_seconds;
var slideshow_slide_started;

function slideshow_reset_interval()
{
    slideshow_slide_started = (new Date).getTime();
}

function slideshow_interval_tick()
{
    if (slideshow_running)
    {
        var delta = ((new Date).getTime() - slideshow_slide_started)/1000;
        var diff = slideshow_interval_seconds - delta;

        if (diff <= 0)
        {
            document.getElementById('countdown_slider').style.visibility = 'hidden';
            find_next_image().onclick(); //! display it
            return;
        }

        var percent =  diff * 100 / slideshow_interval_seconds;
        var w = Math.round(document.getElementById('countdown').clientWidth * percent / 100);
        var slider = document.getElementById('countdown_slider');
        slider.style.visibility = ''; //! visible
        slider.style.width = w + "px";

        if (slideshow_interval_seconds < 10 || diff < 1)
            window.setTimeout(slideshow_interval_tick,  100);
        else if (slideshow_interval_seconds < 30 || diff < 2)
            window.setTimeout(slideshow_interval_tick,  500);
        else
            window.setTimeout(slideshow_interval_tick, 1000);
    }
}


function have_new_slider_value(val)
{
    val = val + 0;
    slideshow_interval_seconds = 4 + val + Math.floor(Math.exp(val/104 * 7.5));
    var note;

    if (slideshow_interval_seconds <= 60)
        note = slideshow_interval_seconds + " seconds";
    else if (slideshow_interval_seconds < 60 * 5) {
        var num = Math.round(10 * (slideshow_interval_seconds / 60))/10;
        if (num == "1")
            num = "1.0";
        note = num + " minutes";
    } else {
        note = Math.round(slideshow_interval_seconds / 60) + " minutes";
    }
    document.getElementById('sliderval').innerHTML = note;
}

function go_slide(e)
{
    if (! slideshow_running) {
        slideshow_running = true;
        YAHOO.util.Dom.setStyle(['S_stop', 'S_show', 'slider_bg', 'countdown_slider'], 'visibility', ''); //! visible
        slideshow_slide_started = (new Date).getTime();
        slideshow_interval_tick();
    } else {
        slider.onDrag(e);
        //alert("already running");
        return false;
    }
}

function go_grid_button() //! could also be "stop slideshow" button
{
    if (! slideshow_running)
        restoregrid();
    else
        ensure_slideshow_is_stopped();
}



function hide_buttons()
{
    if (buttons_being_shown) 
    {
        buttons_being_shown = false;
        buttons_fade_in.stop();
        buttons_fade_out.animate();
    }
}

function go_blog()
{
    ensure_slideshow_is_stopped();
    window.open (image_blog_post_url, "jeffrey_blog");
}

var N_flash_anim;
var P_flash_anim;

if (! isMobile)
{
    N_flash_anim = new YAHOO.util.Anim('N_flash', { opacity: { from: 1, to: 0 }}, 1.0, YAHOO.util.Easing.easeOut);
    N_flash_anim.onComplete.subscribe(function() {     YAHOO.util.Dom.setStyle('N_flash', 'visibility', 'hidden'); })

    P_flash_anim = new YAHOO.util.Anim('P_flash', { opacity: { from: 1, to: 0 }}, 1.0, YAHOO.util.Easing.easeOut);
    P_flash_anim.onComplete.subscribe(function() {     YAHOO.util.Dom.setStyle('P_flash', 'visibility', 'hidden'); })

    function stop_button_flashing()
    {
        N_flash_anim.stop();
        P_flash_anim.stop();
        YAHOO.util.Dom.setStyle(['P_flash', 'N_flash'], 'opacity', 0);
    }

    stop_button_flashing();
}

//!
//! Find the next image in the currently-displayed sequence. If an object is passed in,
//! the next image after that is found. Otherwise, the one after the currently-displayed image
//! is returned.
//!
//! The list cycles: the image after the last is the first.
//!
function find_next_image(obj)
{
    if (!obj)
        obj = document.getElementById(currently_shown_pix_id);

    obj = obj.nextSibling;

    while (obj) {
        if (obj.onclick) {
            return obj;
        }
        obj = obj.nextSibling
    }

    //! okay, cycle back to the first one
    obj = document.getElementById('images').firstChild;

    while (obj) {
        if (obj.onclick) {
            return obj;
        }
        obj = obj.nextSibling
    }
    alert("oops, internal error: no next image(!?)")
}

//
//! "Previous" version of find_next_image();
//
function find_prev_image(obj)
{
    if (!obj)
        obj = document.getElementById(currently_shown_pix_id)

    obj = obj.previousSibling;

    while (obj) {
        if (obj.onclick) {
            return obj;
        }
        obj = obj.previousSibling
    }

    //! okay, cycle back to the first one
    obj = document.getElementById('images').lastChild;

    while (obj) {
        if (obj.onclick) {
            return obj;
        }
        obj = obj.previousSibling
    }
    alert("oops, internal error: no previous image(!?)")
}

function go_next_image()
{
    var obj = document.getElementById(currently_shown_pix_id).nextSibling;

    movement_direction = 'next';

    if (N_flash_anim) {
        stop_button_flashing();
        YAHOO.util.Dom.setStyle('N_flash', 'visibility', ''); //! visible
        N_flash_anim.animate();
    }

    find_next_image().onclick(); //! display it
}

function go_prev_image()
{
    movement_direction = 'prev';


    if (P_flash_anim) {
        stop_button_flashing();
        YAHOO.util.Dom.setStyle('P_flash', 'visibility', ''); //! visible
        P_flash_anim.animate();
    }

    find_prev_image().onclick();
}

var slider;
var slider_start = YAHOO.util.Cookie.get('interval');
if (slider_start)
    slider_start = parseFloat(slider_start);
else
    silder_start =  21;

slider = YAHOO.widget.Slider.getHorizSlider("slider_bg", "slider_handle", 0, 104); 

slider.subscribe("change", function(newval) {
    YAHOO.util.Cookie.set('interval', newval, { expires: Expires, path: "/photostream/" });
    have_new_slider_value(newval);
});

slider.setValue(slider_start, true);
YAHOO.util.Dom.setStyle('countdown_slider', 'opacity', 0.3);


YAHOO.util.Event.addListener('control_display_trigger',  "click", function(e) {
    var clickX = YAHOO.util.Event.getPageX(e);
    var clickY = YAHOO.util.Event.getPageY(e);
    var baseX  = YAHOO.util.Dom.get('button_container').offsetLeft;
    var baseY  = YAHOO.util.Dom.get('button_container').offsetTop;

    var x = clickX - baseX;
    var y = clickY - baseY;

    // A click completely off the control pad...
    if (x < 0 || y < 0 || x > 632 || y > 191)
    {
        if (isMobile)
            return hide_buttons();
        else
            return restoregrid();
    }

    // YAHOO.util.Event.stopEvent(e);
    if (y >= 30 && y <= (30 + 52))
    {
        // top row
        if (x >= 144 && x <= (144+131))
            return thumbs_up();
        if (x >= 352 && x <= (352+131))
            return thumbs_down();
    }
    if (y >= 95 && y <= (95+71))
    {
        // second row
        if (x >=  32 && x <= ( 32+ 71))
            return go_prev_image();
        if (x >= 120 && x <= (120+142))
            return go_blog();
        if (x >= 279 && x <= (279+ 73))
            return go_grid_button();
        if (x >= 368 && x <= (368+142))
            return go_slide(e);
        if (x >= 528 && x <= (528+ 71))
            return go_next_image();
    }

    // click elsewhere on the control pad is ignored
    //! alert("nothing at " + x + " x " + y);
    if (isMobile)
        return hide_buttons();

});

//! Because IE is a festering steaming pile of shit
YAHOO.util.Dom.setStyle(['control_display_trigger'], 'opacity', '0.00001');
YAHOO.util.Dom.setStyle(['control_display_trigger'], 'backgroundColor', 'white');

YAHOO.util.Event.addListener(['fsiA', 'fsiB'], 'click', function(e) {
    var clickX = YAHOO.util.Event.getPageX(e);
    var sW = YAHOO.util.Dom.getViewportWidth();
    var percentX = clickX * 100 / sW;

    if (percentX < 40)
        go_prev_image();
    else if (percentX > 60)
        go_next_image();
});

var control = YAHOO.util.Dom.get('control_display_trigger');
control.onmouseover = show_buttons;
control.onmouseout  = hide_buttons;
function have_IE_mouse_events()
{
    control.onmouseover = null;
    control.onmouseout  = null;
    control.onmouseenter = show_buttons;
    control.onmouseleave = hide_buttons;
}
control.onmouseenter = function() { have_IE_mouse_events(); show_buttons() };
control.onmouseleave = function() { have_IE_mouse_events(); hide_buttons() };



function ensure_slideshow_is_stopped()
{
    if (slideshow_running)
    {
        slideshow_running = false;
        YAHOO.util.Dom.setStyle(['S_stop', 'S_show', 'slider_bg', 'countdown_slider'], 'visibility', 'hidden');
    }
}

YAHOO.util.Event.addListener('fsiA',       "load",  fsiA_load_completed);
YAHOO.util.Event.addListener('fsiB',       "load",  fsiB_load_completed);
YAHOO.util.Event.addListener(preloader_img_obj,  "load",  preloader_load_completed);

YAHOO.util.Event.addListener(document, "keydown", function(e)
{
    var code = YAHOO.util.Event.getCharCode(e);
    ensure_slideshow_is_stopped();

    if (code == 78 || code == 39 || code == 190) //! "N" or right arrow or unshifed ">"
    {
        YAHOO.util.Event.stopEvent(e);
        return go_next_image()
    }
    else if (code == 80 || code == 37 || code == 188) //! "P" or left arrow or unshifted "<"
    {
        YAHOO.util.Event.stopEvent(e);

        return go_prev_image();
    }
    else if (code == 13) //! <ENTER>
    {
        YAHOO.util.Event.stopEvent(e);
        return go_blog();
    }
    else if (code == 27) //! <ESC>
    {
        YAHOO.util.Event.stopEvent(e);
        return restoregrid();
    }
    //! alert(code);


    //! Must not stop the event if we get here, so that we allow the keypress to continue on
    //! to the browser. Stopping it here stops things like ^T to open a new tab...
});


function process_user_hash()
{
    var raw_hash = document.location.hash.match(/^#(.+)/)
    if (! raw_hash)
        return jumble(false); //! default is random

    raw_hash = raw_hash[1]; //! isolate the first match (the $1)
    if (raw_hash == 'chrono')
        return jumble(true);

    if (raw_hash == 'random')
        return jumble(false);

    //! Let's see whether it's an image.
    var obj = document.getElementById(raw_hash);
    if (obj && obj.onclick)
        return obj.onclick();
        
    //! Hmmm, don't know what it might be, so we'll just fall through to the default action...
    jumble(false); //! default is random
}

function on_resize()
{
    document.getElementById('black_background_for_IE6').style.height = YAHOO.util.Dom.getViewportHeight() + "px";
    if (current_image_obj)
        update_image_size(current_image_obj);
}

window.onresize = on_resize;

on_resize();
process_user_hash();

// IE7 can't handle transparency on a PNG with semitransparent pixels... they all turn opaque.
if (! window.navigator.userAgent.match(/MSIE 7/))
    YAHOO.util.Dom.setStyle(['check_down', 'check_up'], 'opacity', 0.8);

function loaded()
{
   var foo = document.getElementById('mobile_tester').style.zIndex;
//   alert("[" + foo + "]");

}
