// rotato.js
// author: Jason Frinchaboy
// version < 0.1 alpha

// G L O B A L S

var _i = -1;
var _hash_interval_obj = null;
var _hash_interval = 2; // milliseconds

var idir = "images";
    
var num_id = "#num";
var thumb_id = "#main_image";
var caption_id = "#caption";

function show_slide( i )
{
    // Make sure the slide index is valid.
    if ( (i > slides.length - 1) || (slide < 0) )
        return false;
    
    // Get the slide at the index, and verify its properties.
    var slide = slides[i];     
    if (!slide || !slide['num'] || !slide['thumb'] || !slide['caption']) 
        return false;
    
    // Check for document objects.
    if ( !$(num_id).length || !$(thumb_id).length || !$(caption_id).length )
        return false;
       
    // Set the URL.
    location.hash = "#" + (i+1);
    
    // Create paths to new images.
    var num_src = "images/" + slide['num'];
    var thumb_src = "images/" + slide['thumb'];
    
    // Preload number and thumb images.
    var num_img = new Image();
    var thumb_img = new Image();
    num_img.src = num_src;
    thumb_img.src = thumb_src;

    loaded = false;
    
    var loadfunc = function() {

        // Make the images and caption (briefly) invisible.
        // $(num_id).hide();
        $(caption_id).hide();
        // $(thumb_id).hide();
        
        // Set the current images to be the new images.
        $(thumb_id).attr("src", thumb_src);
        $(num_id).attr("src", num_src);
        
        // Set the caption
        var caption_text = slide['caption'];
        $(caption_id).html(caption_text);
        
        // Fade in the number image and caption. 
        // $(num_id).fadeIn("slow");
        $(caption_id).fadeIn("slow");
        // $(thumb_id).fadeIn("slow");
        
        // This prevents animated gifs in IE to continue firing.  Stupid.
        $(thumb_img).unbind("load");
    }
    
    // Make sure the image is really loaded...
    // ...these two test will handle cached and non-cached image.  I hope.
    var loaded = false; 
    
    $(thumb_img).load( function() {
        loaded = true;
        loadfunc();
    });
    
    if (!loaded && $(thumb_img)[0].complete) {
        loadfunc();
    }
}

function fix_i(old_i)
{
    var retval = old_i;
    if ( typeof parseInt(old_i) == "number" ) {
        var last_i = slides.length - 1;
        if (old_i < 0) retval = last_i;
        if (old_i > last_i) retval = 0;
    }
    return retval;
}

function rotato_go(go_num)
{
    clear_hash_interval();
    _i = fix_i(_i + go_num);
    show_slide(_i);
    set_hash_interval();
}

function update_from_hash()
{
    var h = location.hash;
    var h_loc = h.lastIndexOf("#");
    var new_i = 0;

    if (h_loc > -1) { // Did we find the hash symbol?
        var hash_i = h_loc + 1; // should be 1
        var hash_val = h.substring(hash_i);
        if (typeof parseInt(hash_val) == "number") {
            new_i = hash_val - 1; // to index into the slides.
        }
    }

    if (new_i != _i) {   
        if (new_i >= 0 && new_i < slides.length) {
            _i = new_i;
        }

        rotato_go(0);
    }
}

function set_hash_interval()
{
    _hash_interval_obj = setInterval( update_from_hash, _hash_interval );
}

function clear_hash_interval()
{
    clearInterval(_hash_interval);
}

function rotato_init()
{
    rotato_preload();
    update_from_hash();
}

function rotato_preload()
{
    for (i = 0; i < slides.length; i++) {
        if (slides[i]) {
            var slide = slides[i];
            if (slide['thumb'] && slide['thumb'].length) {
                $("<img>").attr("src", slide['thumb']);
            }
        }
    }
}





                