/*
 * jQuery assetshow
 * Version 0.0.1
 *
 * Basic fullscreen slideshow that loads image via ajax
 * Currently requires jQuery Backstretch as external lib 
 *
 * Copyright (c) 2011 John Pobojewski/3st
 * Heavily influenced by Backstretch
*/

(function($) {

    $.assetshow = function(files, options) {
    
    	// copy options for jQuery Backstretch
        var settings = {
            centeredX: true,
            centeredY: true,
            speed: 100,
            crossfade: 0        
        };
        var i = 1;
        var last;
 		
 		// Extend the settings with those the user has provided
        if(options && typeof options == "object") $.extend(settings, options);        
    
	    // init
	    $(document).ready(_init);
	
	    // include for chaining
	    return this;
    
    	function _init(){
    		$('a.asset-nav-next, a.asset-nav-prev').click(function(e) {
    			e.preventDefault();
    			
    			if (!$(this).hasClass('not-active')){
					$('.not-active').removeClass('not-active');
					
					if($(this).attr('class') == 'asset-nav-prev') {   
					   if(i > 1) {
					        i--;
					        cue();
					        
					        if (i == 1) $(this).addClass('not-active');
					   } 
					}
					if($(this).attr('class') == 'asset-nav-next') {
					   if(i < files.length) {
					        i++;
					        cue();
					        if (i == files.length) $(this).addClass('not-active');
					   }
					}  
				}				

		    }); 
    		
    		// cue first image
			cue();
    	}
    	
    	function cue(){
			// remove current image + show preloader
			if (settings.crossfade == 0) $("#backstretch").remove(); else last = $("#backstretch");

			$("#asset-loader").show();

    		// cue image
    		$.backstretch(files[i-1], settings, hideLoader);
    		
			// set counter
			$("#asset-counter").html(i + '<span class="separator">' + $(".separator").text() + '</span>' + files.length);    		   	
    	}
    	
    	function hideLoader(){
    		// process crossfade
    	 	if (settings.crossfade == 1) last.fadeOut(settings.speed, function(e){ $(this).remove(); });
    		$("#asset-loader").hide();
    	}
    };
  
})(jQuery);
