/*
Supersized - Fullscreen Slideshow jQuery Plugin
Version 3.0
By Sam Dunn (www.buildinternet.com // www.onemightyroar.com)
Version: supersized.3.0.js
Website: www.buildinternet.com/project/supersized
*/
var $a = jQuery.noConflict();
(function($a){

	//Resize image on ready or resize
	$a.fn.supersized = function() {
		
		
		$a.inAnimation = false;
		$a.paused = false;
		
		var options = $a.extend($a.fn.supersized.defaults, $a.fn.supersized.options);
		$a.currentSlide = options.start_slide - 1;
		
		/******Set up initial images -- Add class doesnt work*****/
		//Set previous image
		var imageLink = (options.slides[options.slides.length - 1].url) ? "href='" + options.slides[options.slides.length - 1].url + "'" : "";
		$a("<img/>").attr("src", options.slides[options.slides.length - 1].image).appendTo("#supersized").wrap("<a " + imageLink + "></a>");//Doesnt account for start slide
		
		//Set current image
		imageLink = (options.slides[$a.currentSlide].url) ? "href='" + options.slides[$a.currentSlide].url + "'" : "";
		$a("<img/>").attr("src", options.slides[$a.currentSlide].image).appendTo("#supersized").wrap("<a class=\"activeslide\" " + imageLink + "></a>");
		
		//Set next image
		imageLink = (options.slides[$a.currentSlide + 1].url) ? "href='" + options.slides[$a.currentSlide + 1].url + "'" : "";
		$a("<img/>").attr("src", options.slides[$a.currentSlide + 1].image).appendTo("#supersized").wrap("<a " + imageLink + "></a>");
		
		$a(window).bind("load", function(){
			
			$a('#loading').hide();
			$a('#supersized').fadeIn('fast');
			
			$a('#controls-wrapper').show();
			
			if (options.thumbnail_navigation == 1){
			
				/*****Set up thumbnails****/
				//Load previous thumbnail
				$a.currentSlide - 1 < 0  ? prevThumb = options.slides.length - 1 : prevThumb = $a.currentSlide - 1;
				$a('#prevthumb').show().html($a("<img/>").attr("src", options.slides[prevThumb].image));
				
				//Load next thumbnail
				$a.currentSlide == options.slides.length - 1 ? nextThumb = 0 : nextThumb = $a.currentSlide + 1;
				$a('#nextthumb').show().html($a("<img/>").attr("src", options.slides[nextThumb].image));
		
			}
			
			$a('#supersized').resizenow();
			
			if (options.slide_captions == 1) $a('#slidecaption').html(options.slides[$a.currentSlide].title);//*********Pull caption from array
			if (options.navigation == 0) $a('#navigation').hide();
			if (options.thumbnail_navigation == 0){ $a('#nextthumb').hide(); $a('#prevthumb').hide(); }
			
			//Slideshow
			if (options.slideshow == 1){
				if (options.slide_counter == 1){ //Initiate slide counter if active
					$a('#slidecounter .slidenumber').html(options.start_slide);
	    			$a('#slidecounter .totalslides').html(options.slides.length); //*******Pull total from length of array
	    		}
				slideshow_interval = setInterval(nextslide, options.slide_interval);
				
				if (options.thumbnail_navigation == 1){
					//Thumbnail Navigation
					$a('#nextthumb').click(function() {
				    	if($a.inAnimation) return false;
					    clearInterval(slideshow_interval);
					    nextslide();
					    if(!($a.paused)) slideshow_interval = setInterval(nextslide, options.slide_interval);
					    return false;
				    });
				    $a('#prevthumb').click(function() {
				    	if($a.inAnimation) return false;
				        clearInterval(slideshow_interval);
				        prevslide();
				       	if(!($a.paused)) slideshow_interval = setInterval(nextslide, options.slide_interval);
				        return false;
				    });
					}
				
				if (options.navigation == 1){ //Skip if no navigation
					$a('#navigation a').click(function(){  
   						$a(this).blur();  
   						return false;  
   					});
   					 	
					//Slide Navigation
				    $a('#nextslide').click(function() {
				    	if($a.inAnimation) return false;
					    clearInterval(slideshow_interval);
					    nextslide();
					    if(!($a.paused)) slideshow_interval = setInterval(nextslide, options.slide_interval);
					    return false;
				    });
				    $a('#prevslide').click(function() {
				    	if($a.inAnimation) return false;
				        clearInterval(slideshow_interval);
				        prevslide();
				        if(!($a.paused)) slideshow_interval = setInterval(nextslide, options.slide_interval);
				        return false;
				    });
				    $a('#nextslide').mousedown(function() {
					   	$a(this).attr("src", "images/forward.png");
					});
					$a('#nextslide').mouseup(function() {
					    $a(this).attr("src", "images/forward_dull.png");
					});
					$a('#nextslide').mouseout(function() {
					    $a(this).attr("src", "images/forward_dull.png");
					});
					
					$a('#prevslide').mousedown(function() {
					    $a(this).attr("src", "images/back.png");
					});
					$a('#prevslide').mouseup(function() {
					    $a(this).attr("src", "images/back_dull.png");
					});
					$a('#prevslide').mouseout(function() {
					    $a(this).attr("src", "images/back_dull.png");
					});
					
				    //Play/Pause Button
				    $a('#pauseplay').click(function() {
				    	if($a.inAnimation) return false;
				    	var src = ($a(this).attr("src") === "images/play.png") ? "images/pause.png" : "images/play.png";
      					if (src == "images/pause.png"){
      						$a(this).attr("src", "images/play.png");
      						$a.paused = false;
					        slideshow_interval = setInterval(nextslide, options.slide_interval);  
				        }else{
				        	$a(this).attr("src", "images/pause.png");
				        	clearInterval(slideshow_interval);
				        	$a.paused = true;
				        }
      					$a(this).attr("src", src);
					    return false;
				    });
				    $a('#pauseplay').mouseover(function() {
				    	var imagecheck = ($a(this).attr("src") === "images/play_dull.png");
				    	if (imagecheck){
      						$a(this).attr("src", "images/play.png"); 
				        }else{
				        	$a(this).attr("src", "images/pause.png");
				        }
				    });
				    
				    $a('#pauseplay').mouseout(function() {
				    	var imagecheck = ($a(this).attr("src") === "images/play.png");
				    	if (imagecheck){
      						$a(this).attr("src", "images/play_dull.png"); 
				        }else{
				        	$a(this).attr("src", "images/pause_dull.png");
				        }
				        return false;
				    });
				}
			}
		});
				
		$a(document).ready(function() {
			$a('#supersized').resizenow(); 
		});
		
		//Pause when hover on image
		$a('#supersized').hover(function() {
	   		if (options.slideshow == 1 && options.pause_hover == 1){
	   			if(!($a.paused) && options.navigation == 1){
	   				$a('#pauseplay').attr("src", "images/pause.png"); 
	   				clearInterval(slideshow_interval);
	   			}
	   		}
	   		if($a.inAnimation) return false; //*******Pull title from array
	   	}, function() {
			if (options.slideshow == 1 && options.pause_hover == 1){
				if(!($a.paused) && options.navigation == 1){
					$a('#pauseplay').attr("src", "images/pause_dull.png");
					slideshow_interval = setInterval(nextslide, options.slide_interval);
				} 
			}
				//*******Pull title from array
	   	});
		
		$a(window).bind("resize", function(){
    		$a('#supersized').resizenow(); 
		});
		
		$a('#supersized').hide();
		$a('#controls-wrapper').hide();
	};
	
	//Adjust image size
	$a.fn.resizenow = function() {
		var t = $a(this);
		var options = $a.extend($a.fn.supersized.defaults, $a.fn.supersized.options);
	  	return t.each(function() {
	  		
			//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 = $a(window).width();
			var browserheight = $a(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($a.inAnimation) return false;
		else $a.inAnimation = true;
	    var options = $a.extend($a.fn.supersized.defaults, $a.fn.supersized.options);
		
		var currentslide = $a('#supersized .activeslide');
	    currentslide.removeClass('activeslide');
		
	    if ( currentslide.length == 0 ) currentslide = $a('#supersized a:last'); //*******Check if end of array?
			
	    var nextslide =  currentslide.next().length ? currentslide.next() : $a('#supersized a:first'); //*******Array
	    var prevslide =  nextslide.prev().length ? nextslide.prev() : $a('#supersized a:last'); //*******Array
		
		$a('.prevslide').removeClass('prevslide');
		prevslide.addClass('prevslide');
		
		//Get the slide number of new slide
		$a.currentSlide + 1 == options.slides.length ? $a.currentSlide = 0 : $a.currentSlide++;
		
		/**** Image Loading ****/
		//Load next image
		loadSlide=false;
		$a.currentSlide == options.slides.length - 1 ? loadSlide = 0 : loadSlide = $a.currentSlide + 1;
		imageLink = (options.slides[loadSlide].url) ? "href='" + options.slides[loadSlide].url + "'" : "";
		$a("<img/>").attr("src", options.slides[loadSlide].image).appendTo("#supersized").wrap("<a " + imageLink + "></a>");
		
		if (options.thumbnail_navigation == 1){
		//Load previous thumbnail
		$a.currentSlide - 1 < 0  ? prevThumb = options.slides.length - 1 : prevThumb = $a.currentSlide - 1;
		$a('#prevthumb').html($a("<img/>").attr("src", options.slides[prevThumb].image));
		
		//Load next thumbnail
		nextThumb = loadSlide;
		$a('#nextthumb').html($a("<img/>").attr("src", options.slides[nextThumb].image));
		}
		
		currentslide.prev().remove(); //Remove Old Image
		
		/**** End Image Loading ****/
		
		//Display slide counter
		if (options.slide_counter == 1){
		    $a('#slidecounter .slidenumber').html($a.currentSlide + 1);//**display current slide after checking if last
		}
		
		//Captions
	    if (options.slide_captions == 1){
	    	(options.slides[$a.currentSlide].title) ? $a('#slidecaption').html(options.slides[$a.currentSlide].title) : $a('#slidecaption').html('') ; //*******Grab next slide's title from array
	    }
		
	    nextslide.hide().addClass('activeslide')
	    	if (options.transition == 0){
	    		nextslide.show(); $a.inAnimation = false;
	    	}
	    	if (options.transition == 1){
	    		nextslide.fadeIn(750, function(){$a.inAnimation = false;});
	    	}
	    	if (options.transition == 2){
	    		nextslide.show("slide", { direction: "up" }, 'slow', function(){$a.inAnimation = false;});
	    	}
	    	if (options.transition == 3){
	    		nextslide.show("slide", { direction: "right" }, 'slow', function(){$a.inAnimation = false;});
	    	}
	    	if (options.transition == 4){
	    		nextslide.show("slide", { direction: "down" }, 'slow', function(){$a.inAnimation = false;});
	    	}
	    	if (options.transition == 5){
	    		nextslide.show("slide", { direction: "left" }, 'slow', function(){$a.inAnimation = false;});
	    	}
	    $a('#supersized').resizenow();
	}
	
	//Slideshow Previous Slide
	function prevslide() {
		if($a.inAnimation) return false;
		else $a.inAnimation = true;
		var options = $a.extend($a.fn.supersized.defaults, $a.fn.supersized.options);
	    
	    var currentslide = $a('#supersized .activeslide');
	    currentslide.removeClass('activeslide');
		
	    if ( currentslide.length == 0 ) currentslide = $a('#supersized a:first');
			
	    var nextslide =  currentslide.prev().length ? currentslide.prev() : $a('#supersized a:last'); //****** If equal to total length of array
	    var prevslide =  nextslide.next().length ? nextslide.next() : $a('#supersized a:first');
				
		//Get current slide number
		$a.currentSlide == 0 ?  $a.currentSlide = options.slides.length - 1 : $a.currentSlide-- ;
		
		/**** Image Loading ****/
		//Load next image
		loadSlide=false;
		$a.currentSlide - 1 < 0  ? loadSlide = options.slides.length - 1 : loadSlide = $a.currentSlide - 1;
		imageLink = (options.slides[loadSlide].url) ? "href='" + options.slides[loadSlide].url + "'" : "";
		$a("<img/>").attr("src", options.slides[loadSlide].image).prependTo("#supersized").wrap("<a " + imageLink + "></a>");
		
		if (options.thumbnail_navigation == 1){
		//Load previous thumbnail
		prevThumb = loadSlide;
		$a('#prevthumb').html($a("<img/>").attr("src", options.slides[prevThumb].image));
		
		//Load next thumbnail
		$a.currentSlide == options.slides.length - 1 ? nextThumb = 0 : nextThumb = $a.currentSlide + 1;
		$a('#nextthumb').html($a("<img/>").attr("src", options.slides[nextThumb].image));
		}
		
		currentslide.next().remove(); //Remove Old Image
		
		/**** End Image Loading ****/
		
		//Display slide counter
		if (options.slide_counter == 1){
		    $a('#slidecounter .slidenumber').html($a.currentSlide + 1);
		}
		
		$a('.prevslide').removeClass('prevslide');
		prevslide.addClass('prevslide');
		
		//Captions
	    if (options.slide_captions == 1){
	    	(options.slides[$a.currentSlide].title) ? $a('#slidecaption').html(options.slides[$a.currentSlide].title) : $a('#slidecaption').html('') ; //*******Grab next slide's title from array
	    }
		
	    nextslide.hide().addClass('activeslide')
	    	if (options.transition == 0){
	    		nextslide.show(); $a.inAnimation = false;
	    	}
	    	if (options.transition == 1){
	    		nextslide.fadeIn(750, function(){$a.inAnimation = false;});
	    	}
	    	if (options.transition == 2){
	    		nextslide.show("slide", { direction: "down" }, 'slow', function(){$a.inAnimation = false;});
	    	}
	    	if (options.transition == 3){
	    		nextslide.show("slide", { direction: "left" }, 'slow', function(){$a.inAnimation = false;});
	    	}
	    	if (options.transition == 4){
	    		nextslide.show("slide", { direction: "up" }, 'slow', function(){$a.inAnimation = false;});
	    	}
	    	if (options.transition == 5){
	    		nextslide.show("slide", { direction: "right" }, 'slow', function(){$a.inAnimation = false;});
	    	}
	    	
	    	$a('#supersized').resizenow();//Fix for resize mid-transition
	}
	
	$a.fn.supersized.defaults = { 
			startwidth: 4,  
			startheight: 3,
			vertical_center: 1,
			slideshow: 1,
			navigation:1,
			thumbnail_navigation: 0,
			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,
			start_slide: 1
	};
	
})(jQuery);


