/*
superstatic - Fullscreen Slideshow jQuery Plugin
Version 3.0
By Sam Dunn (www.buildinternet.com // www.onemightyroar.com)
Version: superstatic.3.0.js
Website: www.buildinternet.com/project/superstatic
*/

(function($) {
	//Resize image slideshow on ready or resize
	$.fn.superstatic = function(target) {
		
		$.inAnimation = false;
		$.paused = false;		
		var options = $.extend($.fn.superstatic.defaults, $.fn.superstatic.options);
		$.currentSlide = options.start_slide - 1;
		
		//Set current image
		var img = new Image();
	    $(img).load(function() {
	    	if ($(target + ' #topslide').hasClass('active') == false)
	    	{
		    	// remove old image and load new image
		    	$(target + ' #topslide img').remove();
				$(target + ' #topslide').append(this);
				$(target + ' #topslide').resizenow();
				
				if (navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPad/i)) {
					$('#iPhoneSlides img').css({ '-webkit-animation-play-state': 'paused' });
					$('#iPhoneSlides img').css({ '-webkit-animation': 'wordmarkFadeOut 1s' }).queue(function() {
			        	$('#iPhoneSlides').remove();
					});
					$(target + ' #topslide img').css({ opacity: 1 });
					$(target + ' #topslide').addClass('active');
				} else {
					$(target + ' #topslide img').css({ opacity: 1 });
					$(target + ' #topslide').css({ display:'block' });
					$(target + ' #topslide').addClass('active');
					$(target + ' #topslide').animate({ opacity: 1 });
				}
			} else {
				
				// remove old image and load new image
		    	$(target + ' #bottomslide img').remove();
				$(target + ' #bottomslide').append(this);
				$(target + ' #bottomslide').resizenow();
				
				if (navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPad/i)) {
					$(target + ' #topslide img').css({ opacity: 0 });
					$(target + ' #topslide').removeClass('active');
				} else {
					$(target + ' #topslide').animate({ opacity: 0 });
					$(target + ' #topslide').removeClass('active');
				}
			}			        
	    }).error(function () {
	        // notify the user that the image could not be loaded
	        console.log('image failed to load')
	    }).attr('src', options.slides[$.currentSlide].image);
	    
		$(window).bind("load", function(){
			
			$('#loading').hide();
			//$(target + ' #topslide').fadeIn(1000);
			//$(target + ' #bottomslide').fadeIn(1000);
    		$(target + ' #topslide').resizenow();
    		$(target + ' #bottomslide').resizenow();
			
			//Slideshow
			if (options.slideshow == 1){
				slideshow_interval = setInterval(nextslide, options.slide_interval);
			}
		});
				
		$(document).ready(function() {
    		$(target + ' #topslide').resizenow();
    		$(target + ' #bottomslide').resizenow();
		});
		
		$(window).bind("resize", function(){
    		$(target + ' #topslide').resizenow();
    		$(target + ' #bottomslide').resizenow();
		});
	};
	
	//Adjust image size
	$.fn.resizenow = function() {
		var t = $(this);
		var options = $.extend($.fn.superstatic.defaults, $.fn.superstatic.options);
		
	  	return t.each(function() {
	  	
			/*if (navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPad/i)) {
				var browserwidth = $(window).width();
				var browserheight = $(window).height();				

				//Resize image to proper ratio
				t.height(browserheight);
				t.width(browserwidth);
				t.children().height(browserheight);
				t.children().width(browserwidth);
				t.children().css('left', (browserwidth - t.width())/2);
				t.children().css('top', (browserheight - t.height())/2);
				return false;
					
		    } else {*/
	  		
				//Define image ratio
				var ratio = options.startheight/options.startwidth;
				
				//Gather browser and current image size
				var imagewidth = t.width();
				var imageheight = t.height();

				var browserwidth = $(window).width();
				var browserheight = $(window).height();
				var offset;

				//Resize image to proper ratio
				if ((browserheight/browserwidth) > ratio){
				    t.height(browserheight);
				    t.width(browserheight / ratio);
				    t.children().height(browserheight);
				    t.children().width(browserheight / ratio);
				} else {
				    t.width(browserwidth);
				    t.height(browserwidth * ratio);
				    t.children().width(browserwidth);
				    t.children().height(browserwidth * ratio);
				}
				if (options.vertical_center == 1){
					t.children().css('left', (browserwidth - t.width())/2);
					t.children().css('top', (browserheight - t.height())/2);
				}
				return false;
			//}
		});
	};
	
	//Slideshow Next Slide
	function nextslide() {
		if($.inAnimation) return false;
		else $.inAnimation = true;
	    var options = $.extend($.fn.superstatic.defaults, $.fn.superstatic.options);
		
		var currentslide = $('#superstatic .activeslide-static');
	    currentslide.removeClass('activeslide-static');
		
	    if ( currentslide.length == 0 ) currentslide = $('#superstatic span.last'); //*******Check if end of array?
			
	    var nextslide =  currentslide.next().length ? currentslide.next() : $('#superstatic span.first'); //*******Array
	    var prevslide =  nextslide.prev().length ? nextslide.prev() : $('#superstatic span.last'); //*******Array
		
		$('.prevslide-static').removeClass('prevslide-static');
		prevslide.addClass('prevslide-static');
		
		//Get the slide number of new slide
		$.currentSlide + 1 == options.slides.length ? $.currentSlide = 0 : $.currentSlide++;
		
		/**** Image Loading ****/
		//Load next image
		loadSlide=false;
		$.currentSlide == options.slides.length - 1 ? loadSlide = 0 : loadSlide = $.currentSlide + 1;
		$("<img/>").attr("src", options.slides[loadSlide].image).appendTo("#superstatic").wrap("<span></span>");
		
		currentslide.prev().remove(); //Remove Old Image
		
		/**** End Image Loading ****/
		
	    nextslide.hide().addClass('activeslide-static')
	    	if (options.transition == 0){
	    		nextslide.show(); $.inAnimation = false;
	    	}
	    	if (options.transition == 1){
	    		nextslide.fadeIn(1000, function(){$.inAnimation = false;});
	    	}
	    	if (options.transition == 2){
	    		nextslide.show("slide", { direction: "up" }, 'slow', function(){$.inAnimation = false;});
	    	}
	    	if (options.transition == 3){
	    		nextslide.show("slide", { direction: "right" }, 'slow', function(){$.inAnimation = false;});
	    	}
	    	if (options.transition == 4){
	    		nextslide.show("slide", { direction: "down" }, 'slow', function(){$.inAnimation = false;});
	    	}
	    	if (options.transition == 5){
	    		nextslide.show("slide", { direction: "left" }, 'slow', function(){$.inAnimation = false;});
	    	}
	    $('#superstatic').resizenow();
	}
	
	//Slideshow Previous Slide
	function prevslide() {
		if($.inAnimation) return false;
		else $.inAnimation = true;
		var options = $.extend($.fn.superstatic.defaults, $.fn.superstatic.options);
	    
	    var currentslide = $('#superstatic .activeslide-static');
	    currentslide.removeClass('activeslide-static');
		
	    if ( currentslide.length == 0 ) currentslide = $('#superstatic span.first');
			
	    var nextslide =  currentslide.prev().length ? currentslide.prev() : $('#superstatic span.last'); //****** If equal to total length of array
	    var prevslide =  nextslide.next().length ? nextslide.next() : $('#superstatic span.first');
				
		//Get current slide number
		$.currentSlide == 0 ?  $.currentSlide = options.slides.length - 1 : $.currentSlide-- ;
		
		/**** Image Loading ****/
		//Load next image
		loadSlide=false;
		$.currentSlide - 1 < 0  ? loadSlide = options.slides.length - 1 : loadSlide = $.currentSlide - 1;
		$("<img/>").attr("src", options.slides[loadSlide].image).prependTo("#superstatic").wrap("<span></span>");
		
		currentslide.next().remove(); //Remove Old Image
		
		/**** End Image Loading ****/
		
		$('.prevslide-static').removeClass('prevslide-static');
		prevslide.addClass('prevslide-static');
		
	    nextslide.hide().addClass('activeslide-static')
	    	if (options.transition == 0){
	    		nextslide.show(); $.inAnimation = false;
	    	}
	    	if (options.transition == 1){
	    		nextslide.fadeIn(1000, 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;});
	    	}
	    	
	    	$('#superstatic').resizenow();//Fix for resize mid-transition
	}
	
	$.fn.superstatic.defaults = { 
			startwidth: 4,  
			startheight: 3,
			vertical_center: 1,
			slideshow: 1,
			transition: 1, //0-None, 1-Fade, 2-slide top, 3-slide right, 4-slide bottom, 5-slide left
			slide_interval: 5000,
			start_slide: 1
	};
	
	
})(jQuery);


