// 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 = 25; // milliseconds

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

var _IMG_TYPE = 0;
var _VID_TYPE = 1;

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

	if (type == _IMG_TYPE) {
	
		// Preload number and thumb images.
		var thumb_img = new Image();
		thumb_img.src = thumb_src;
		loaded = false;
		
		var loadfunc = function() {
		
			// Make the images and caption (briefly) invisible.
			$(caption_id).hide();
			
			// Set the current images to be the new images.
			var img_html = '<img id="main_image" src="' + thumb_src + '">';
			$("#slide").html(img_html);
			$(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. 
			$(caption_id).fadeIn("slow");
			
			loaded = true;
		}
		
		// 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();
		}
		
		loaded = false;
	}
	else if (type == _VID_TYPE) {
	
		// Make the images and caption (briefly) invisible.
		$(caption_id).hide();
		
		$("#slide").html( '<a href="' + thumb_src + '" style="display:block;width:700px;height:435px" id="player"></a>' );
		
		flowplayer(
			"player",
			"js/flowplayer/flowplayer-3.1.5.swf", {
				clip:  {
					autoPlay: false,
					autoBuffering: true
				},
				
				plugins: { 
					controls: { 
						url: 'flowplayer.controls-3.1.5.swf', 
				 
						// display properties 
						bottom:0, 
						height:24,  
						backgroundColor: '#ffffff', 
						backgroundGradient: 'low', 
				 
						// controlbar-specific configuration 
						fontColor: '#ffffff',
						timeFontColor: '#333333', 
						autoHide: 'never', 
				 
						// which buttons are visible and which are not? 
						play:true, 
						volume:true, 
						mute:false, 
						time:false, 
						stop:false, 
						playlist:false, 
						fullscreen:false, 
				 
						// scrubber is a well-known nickname for the timeline/playhead combination 
						scrubber: true,
						progressColor: '#555555',
						sliderColor: '#bbbbbb',
						buttonColor: '#555555',
						buttonOverColor: '#555555',
						bufferColor: '#bbbbbb',
						volumeSliderColor: '#bbbbbb',
						scrubberHeightRatio: 0.1,
						volumeBarHeightRatio: 0.1
						
				 
						// you can also use the "all" flag to disable/enable all controls 
					} 
				}
			}
		);
		
		$(num_id).attr("src", num_src);
		
		// Set the caption
		var caption_text = slide['caption'];
		$(caption_id).html( caption_text.replace( /\\/g, "" ));
			
        // Fade in the number image and caption. 
		$(caption_id).fadeIn("slow");
	}
}

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()
{
	if ( slides.length == 0 ) {
		return false;
	}

    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", "images/" + slide['thumb']);
            }
        }
    }
}
 
$(document).ready( function() {
	rotato_init();
});



                
