/*
Supersized - Fullscreen Slideshow jQuery Plugin
By Sam Dunn (www.buildinternet.com // www.onemightyroar.com)
Version: supersized.2.0.js // Relase Date: 5/7/09
Website: www.buildinternet.com/project/supersized
Thanks to Aen for preloading, fade effect, & vertical centering
*/

var images = [], cached = [], current = 0;

(function($){

	//Resize image on ready or resize
	$.fn.supersized = function() {
		$.inAnimation = false;
		$.paused = false;
		var options = $.extend($.fn.supersized.defaults, $.fn.supersized.options);
		
		if ($('#image-wrap').length && $('#image-wrap a').length) {
		    $('#image-wrap a').each(function() {
		        images.push($(this).attr('href'));
		        $(this).click(function() {
		            jumpTo($(this).parent().find('a').index($(this)));
		            $(this).addClass('active').find('img').css('opacity', 1).end()
		                .siblings('.active').removeClass('active').find('img').css('opacity', 0.5);
		            return false;
		        })
		    });
		    if ($('#image-wrap').hasClass('thumbnails')) {
		        $('#image-wrap a:not(.active) img').css('opacity', 0.5);
		    }
		}
		
		$(window).bind("load", function(){
			$('#loading').hide();
			$('#supersize').fadeIn('fast');
			if ($('#slideshow .activeslide').length == 0) $('#supersize img:first').addClass('activeslide');
			if (options.slide_captions == 1) $('#slidecaption').html($('#supersize .activeslide').find('img').attr('title'));
			if (options.navigation == 0) $('#navigation').hide();
			//Slideshow
			if (options.slideshow == 1){
				if (options.slide_counter == 1){ //Initiate slide counter if active
					$('#slidecounter .slidenumber').html(1);
	    			$('#slidecounter .totalslides').html($("#supersize > *").size());
	    		}
	    		if (options.slide_interval > 0) {
				    slideshow_interval = setInterval("nextslide()", options.slide_interval);
			    } else {
			        slideshow_interval = null;
			    }
				if (options.navigation == 1){ //Skip if no navigation
					$('#navigation a').click(function(){  
   						$(this).blur();  
   						return false;  
   					}); 	
					//Slide Navigation
				    $('#toggle-right').click(function() {
				    	if($.paused) return false; if($.inAnimation) return false;
					    clearInterval(slideshow_interval);
					    nextslide();
					    slideshow_interval = options.slide_interval ? setInterval(nextslide, options.slide_interval) : null;
					    return false;
				    });
				    $('#toggle-left').click(function() {
				    	if($.paused) return false; if($.inAnimation) return false;
				        clearInterval(slideshow_interval);
				        prevslide();
				        slideshow_interval = options.slide_interval ? setInterval(nextslide, options.slide_interval) : null;
				        return false;
				    });
					
				}
			}
		});
				
		$(document).ready(function() {
			$('#supersize').resizenow(); 
		});
		
		//Pause when hover on image
		$('#supersize > *').hover(function() {
	   		if (options.slideshow == 1 && options.pause_hover == 1){
	   			if(!($.paused) && options.navigation == 1){
	   				$('#pauseplay > img').attr("src", "images/pause.gif"); 
	   				clearInterval(slideshow_interval);
	   			}
	   		}
	   		original_title = $(this).find('img').attr("title");
	   		if($.inAnimation) return false; else $(this).find('img').attr("title","");
	   	}, function() {
			if (options.slideshow == 1 && options.pause_hover == 1){
				if(!($.paused) && options.navigation == 1){
					$('#pauseplay > img').attr("src", "images/pause_dull.gif");
					slideshow_interval = setInterval(nextslide, options.slide_interval);
				} 
			}
			$(this).find('img').attr("title", original_title);	
	   	});
		
		$(window).bind("resize", function(){
    		$('#supersize').resizenow(); 
		});
		
		$(window).bind("load", function(){
    		$('#supersize').resizenow(); 
		});
		
		$('#supersize').hide();
	};
	
	//Adjust image size
	$.fn.resizenow = function() {
		var options = $.extend($.fn.supersized.defaults, $.fn.supersized.options);
	  	return this.each(function() {
	  		
			//Define image ratio
			var ratio = options.startheight/options.startwidth;
			
			//Gather browser and current image size
			var imagewidth = $(this).width();
			var imageheight = $(this).height();
			var browserwidth = $(window).width();
			var browserheight = $(window).height();
			var offset;

			//Resize image to proper ratio
			if ((browserheight/browserwidth) > ratio){
			    $(this).height(browserheight);
			    $(this).width(browserheight / ratio);
			    $(this).children().height(browserheight);
			    $(this).children().width(browserheight / ratio);
			} else {
			    $(this).width(browserwidth);
			    $(this).height(browserwidth * ratio);
			    $(this).children().width(browserwidth);
			    $(this).children().height(browserwidth * ratio);
			}
			if (options.align_bottom == 1){
			    $(this).children().css('left', (browserwidth - $(this).width()));
				$(this).children().css('top', (browserheight - $(this).height()));
			}
			else if (options.vertical_center == 1){
				$(this).children().css('left', (browserwidth - $(this).width())/2);
				$(this).children().css('top', (browserheight - $(this).height())/2);
			}
			return false;
		});
	};
	
	$.fn.supersized.defaults = { 
			startwidth: 4,  
			startheight: 3,
			vertical_center: 1,
			align_bottom: 0,
			slideshow: 1,
			navigation:1,
			transition: 1, //0-None, 1-Fade, 2-slide top, 3-slide right, 4-slide bottom, 5-slide left
			pause_hover: 0,
			slide_counter: 1,
			slide_captions: 1,
			slide_interval: 5000
	};
	
})(jQuery);

    function fireTransition(src, nextslide, prevslide) {
        var options = $.extend($.fn.supersized.defaults, $.fn.supersized.options);
		
		//Captions require img in <a>
	    if (options.slide_captions == 1) $('#slidecaption').html($(nextslide).attr('title'));
		
		$('#supersize .activeslide').removeClass('activeslide');
	    nextslide.hide().attr('src', src).addClass('activeslide');
	    
	    $('.prevslide').removeClass('prevslide');
		prevslide.addClass('prevslide');
	    
    	if (options.transition == 0){
    		nextslide.show(); $.inAnimation = false;
    	}
    	if (options.transition == 1){
    		nextslide.fadeIn(750, function(){$.inAnimation = false;});
    	}
    	if (options.transition == 2){
    		nextslide.show("slide", { direction: "down" }, 'slow', function(){$.inAnimation = false;});
    	}
    	if (options.transition == 3){
    		nextslide.show("slide", { direction: "left" }, 'slow', function(){$.inAnimation = false;});
    	}
    	if (options.transition == 4){
    		nextslide.show("slide", { direction: "up" }, 'slow', function(){$.inAnimation = false;});
    	}
    	if (options.transition == 5){
    		nextslide.show("slide", { direction: "right" }, 'slow', function(){$.inAnimation = false;});
    	}
    	
    	$('#supersize').resizenow();//Fix for resize mid-transition
    }

    function preloadImage(nextslide, prevslide, dir) {
        var options = $.extend($.fn.supersized.defaults, $.fn.supersized.options);
        
        if (dir > 0) var next = (current + dir) < images.length ? current + dir : 0;
        else if (dir < 0) var next = (current + dir) >= 0 ? current + dir : images.length - 1;
        else next = current;
        
        if (jQuery.inArray(images[next], cached) < 0) {
            $('#loading').fadeIn(320);
            $.cacheImage(images[next], {
                load : function (e) {
                    cached.push(images[next]);
                    fireTransition(images[next], nextslide, prevslide);
                    $('#loading').fadeOut(200);
                }
            });
        } else {
            fireTransition(images[next], nextslide, prevslide);
        }
        current = next;
    }
    
    function jumpTo(index) {
        current = index;
        
        var currentslide = $('#supersize .activeslide');
         if ( currentslide.length == 0 ) currentslide = $('#supersize img:last');
        var nextslide =  currentslide.next().length ? currentslide.next() : $('#supersize img:first');
	    var prevslide =  nextslide.prev().length ? nextslide.prev() : $('#supersize img:last');
	    
        preloadImage(nextslide, prevslide, 0);
    }

	//Slideshow Next Slide
	function nextslide() {
		if($.inAnimation) return false;
		else $.inAnimation = true;
	    var options = $.extend($.fn.supersized.defaults, $.fn.supersized.options);
	    var currentslide = $('#supersize .activeslide');
		
	    if ( currentslide.length == 0 ) currentslide = $('#supersize img:last');
			
	    var nextslide =  currentslide.next().length ? currentslide.next() : $('#supersize img:first');
	    var prevslide =  nextslide.prev().length ? nextslide.prev() : $('#supersize img:last');
		
		if ($('#image-wrap').length) {
		    var num = $('#image-wrap a.active').next().length ? $('#image-wrap a.active').next() : $('#image-wrap a:first');
		    $(num).addClass('active').find('img').css('opacity', 1).end()
		        .siblings('.active').removeClass('active').find('img').css('opacity', 0.5);
		}
		
		//Display slide counter
		if (options.slide_counter == 1){
			var slidecount = $('#slidecounter .slidenumber').html();
			currentslide.next().length ? slidecount++ : slidecount = 1;
		    $('#slidecounter .slidenumber').html(slidecount);
		}
		
		preloadImage(nextslide, prevslide, 1);
	}
	
	//Slideshow Previous Slide
	function prevslide() {
		if($.inAnimation) return false;
		else $.inAnimation = true;
	    var options = $.extend($.fn.supersized.defaults, $.fn.supersized.options);
	    var currentslide = $('#supersize .activeslide');
		
	    if ( currentslide.length == 0 ) currentslide = $('#supersize img:first');
			
	    var nextslide =  currentslide.prev().length ? currentslide.prev() : $('#supersize img:last');
	    var prevslide =  nextslide.next().length ? nextslide.next() : $('#supersize img:first');
		
		if ($('#image-wrap').length) {
		    var num = $('#image-wrap a.active').prev().length ? $('#image-wrap a.active').prev() : $('#image-wrap a:last');
		    $(num).addClass('active').find('img').css('opacity', 1).end()
		        .siblings('.active').removeClass('active').find('img').css('opacity', 0.5);
		}
		
		//Display slide counter
		if (options.slide_counter == 1){
			var slidecount = $('#slidecounter .slidenumber').html();
			currentslide.prev().length ? slidecount-- : slidecount = $("#supersize > *").size();
		    $('#slidecounter .slidenumber').html(slidecount);
		}
		
		preloadImage(nextslide, prevslide, -1);
	}
