/*
 * jQuery assetshow
 * Version 0.0.4
 * 3.9.2011	
 *
 * Fullscreen slideshow that loads images via ajax
 * Built on jQuery Backstretch
 *
 * Copyright (c) 2011 John Pobojewski/3st
*/

(function($) {

    $.assetshow = function(divID, options) {
    
    	// copy options for jQuery Backstretch
        var settings = {
            centeredX: true,
            centeredY: true,
            speed: 100,
            crossfade: 0        
        };
        var i = 1;
        var last;
        var assetLinks = $("ul.thumbnails li");
 		
 		// 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(){

    		var id = 1;
    		assetLinks.each(function(e){    			
    			// set id to rel
    			$(this).attr("rel", id);
    			
	    		$(this).click(function(e){
	    			e.preventDefault();
	    			if(i != $(this).attr("rel")) {
		    			i = $(this).attr("rel");	    			
		    			cue(); 
	    			}
	    		});    		
	    		
	    		id++;
    		});
    		
    		// setup next/prev nav
    		$('a.asset-nav-next, a.asset-nav-prev').click(function(e) {
    			e.preventDefault();
    			
    			if (!$(this).hasClass('not-active')){
					if($(this).attr('class') == 'asset-nav-prev') {   
					   if(i > 1) {
					        i--;
					        cue();
					   } 
					}
					if($(this).attr('class') == 'asset-nav-next') {
					   if(i < assetLinks.length) {
					        i++;
					        cue();
					   }
					}  
				}				
		    }); 
    		
    		// cue first image
			cue();
    	}
    	
    	function cue(){    	
			// remove current image + show preloader
			if (settings.crossfade == 0) $("#backstretch").remove(); else last = $("#backstretch");
			$("#asset-loader").show();
			
			var current = $(assetLinks)[i-1];
			
			if ($(current).hasClass("image")){
    			// cue image using Backstretch
    			$.backstretch( $(current).children("a").attr("href"), settings, hideLoader);	    					
			} 
			if ($(current).hasClass("video")){
				// cue video using FancyBox
				$.fancybox({
					'padding'		: 0,
					'autoScale'		: true,
					'transitionIn'	: 'fade',
					'transitionOut'	: 'none',
					'title'			: $(current).children("a").attr("title"),
					'width'			: 960,
					'height'		: 540,
					'href'			: $(current).children("a").attr("href").replace(new RegExp("watch\\?v=", "i"), 'v/'),
					'type'			: 'swf',
					'swf'			: {
						'wmode'		: 'transparent',
						'allowfullscreen'	: 'true'
					}
				});
				
				hideLoader();
			}
			
			// inject caption
			var captionStr = $(current).children("a").attr("title");
			$("#asset-caption").html(captionStr);
    		
			// set counter
			$("#asset-counter").html(i + '<span class="separator">' + $(".separator").text() + '</span>' + assetLinks.length); 
			
			// set nav
			$('.not-active').removeClass('not-active');
			if (i == 1) $("a.asset-nav-prev").addClass('not-active'); 
			if (i == assetLinks.length) $("a.asset-nav-next").addClass('not-active');	  		   	
    	}
    	
    	function hideLoader(){
    		// process crossfade
    	 	if (settings.crossfade == 1) last.fadeOut(settings.speed, function(e){ $(this).remove(); });
    		$("#asset-loader").hide();
    	}
    };
  
})(jQuery);
