/*
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

MODIFIED FOR GINNY ANDERSON ARCHITECTURE BY SCOTT ANDERSON / ROOM 34 CREATIVE SERVICES
*/

(function($) {

	//Resize image on ready or resize
	$.fn.supersized = function() {
		$.inAnimation = false;
		$.paused = false;
		var options = $.extend($.fn.supersized.defaults, $.fn.supersized.options);

		$(window).bind("load", function() {
			$('#supersize').fadeIn('fast');
			if ($('#supersize>a.activeslide').length == 0) $('#supersize>a:first').addClass('activeslide');
			//Slideshow
			if (options.slideshow == 1) {
				if (options.slide_counter == 1) { //Initiate slide counter if active
					$('#slidecounter>span.slidenumber').html(1);
					$('#slidecounter>span.totalslides').html($("#supersize>a").size());
				}
				slideshow_interval = setInterval("nextslide()", options.slide_interval);
				if (options.navigation == 1) { //Skip if no navigation
					//Slide Navigation
					$('#nextslide').click(function() {
						if ($.inAnimation) return false;
						clearInterval(slideshow_interval);
						nextslide();
						slideshow_interval = setInterval(nextslide, options.slide_interval);
						return false;
					});
					$('#prevslide').click(function() {
						if ($.inAnimation) return false;
						clearInterval(slideshow_interval);
						prevslide();
						slideshow_interval = setInterval(nextslide, options.slide_interval);
						return false;
					});
					$('#nextslide>img').hover(function() {
					   	$(this).attr("src", "img/supersized/forward.gif");
					}, function() {
						$(this).attr("src", "img/supersized/forward_dull.gif");
					});
					$('#prevslide>img').hover(function() {
						$(this).attr("src", "img/supersized/back.gif");
					}, function() {
						$(this).attr("src", "img/supersized/back_dull.gif");
					});

					//Play/Pause Button
					$('#pauseplay').click(function() {
						if ($.inAnimation) return false;
						var src = ($(this).children('img').attr("src") === "img/supersized/play.gif") ? "img/supersized/pause.gif" : "img/supersized/play.gif";
  						if (src == "img/supersized/pause.gif") {
  							$(this).children('img').attr("src", "img/supersized/play.gif");
  							$.paused = false;
							slideshow_interval = setInterval(nextslide, options.slide_interval);
						}else{
							$(this).children('img').attr("src", "img/supersized/pause.gif");
							clearInterval(slideshow_interval);
							$.paused = true;
						}
  						$(this).children('img').attr("src", src);
						return false;
					});
					$('#pauseplay').mouseover(function() {
						var imagecheck = ($(this).children('img').attr("src") === "img/supersized/play_dull.gif");
						if (imagecheck) {
  							$(this).children('img').attr("src", "img/supersized/play.gif");
						}else{
							$(this).children('img').attr("src", "img/supersized/pause.gif");
						}
					});

					$('#pauseplay').mouseout(function() {
						var imagecheck = ($(this).children('img').attr("src") === "img/supersized/play.gif");
						if (imagecheck) {
  							$(this).children('img').attr("src", "img/supersized/play_dull.gif");
						}else{
							$(this).children('img').attr("src", "img/supersized/pause_dull.gif");
						}
						return false;
					});
				}
			}
		});

		$(document).ready(function() {
			$('#supersize').resizenow();
		});

		$(window).bind("resize", 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.horizontal_center == 1) {
				$(this).children().css('left', (browserwidth - $(this).width())/2);
			}
			if (options.vertical_center == 1) {
				$(this).children().css('top', (browserheight - $(this).height())/2);
			}
			
			return false;
		});
	};

	$.fn.supersized.defaults = {
			startwidth: 4,
			startheight: 3,
			vertical_center: 1,
			horizontal_center: 1,
			slideshow: 1,
			navigation: 1,
			transition: 1, //0-None, 1-Fade, 2-slide top, 3-slide right, 4-slide bottom, 5-slide left
			slide_counter: 1,
			slide_interval: 5000
	};

})(jQuery);

	//Slideshow Next Slide
	function nextslide() {
		if ($.inAnimation) return false;
		else $.inAnimation = true;
		var options = $.extend($.fn.supersized.defaults, $.fn.supersized.options);
		var currentslide = $('#supersize>a.activeslide');
		currentslide.removeClass('activeslide');

		if ( currentslide.length == 0 ) currentslide = $('#supersize>a:last');

		var nextslide =  currentslide.next().length ? currentslide.next() : $('#supersize>a:first');
		var prevslide =  nextslide.prev().length ? nextslide.prev() : $('#supersize>a:last');


		//Display slide counter
		if (options.slide_counter == 1) {
			var slidecount = $('#slidecounter span.slidenumber').html();
			currentslide.next().length ? slidecount++ : slidecount = 1;
			$('#slidecounter span.slidenumber').html(slidecount);
		}

		$('#supersize>a.prevslide').removeClass('prevslide');
		prevslide.addClass('prevslide');

		nextslide.hide().addClass('activeslide')
			if (options.transition == 0) {
				nextslide.show(); $.inAnimation = false;
			}
			if (options.transition == 1) {
				nextslide.fadeIn(1200, 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;});
			}

		$('#supersize>a.activeslide').resizenow(); //Fix for resize mid-transition

	}

	//Slideshow Previous Slide
	function prevslide() {
		if ($.inAnimation) return false;
		else $.inAnimation = true;
		var options = $.extend($.fn.supersized.defaults, $.fn.supersized.options);
		var currentslide = $('#supersize>a.activeslide');
		currentslide.removeClass('activeslide');

		if ( currentslide.length == 0 ) currentslide = $('#supersize>a:first');

		var nextslide = currentslide.prev().length ? currentslide.prev() : $('#supersize>a:last');
		var prevslide = nextslide.next().length ? nextslide.next() : $('#supersize>a:first');

		//Display slide counter
		if (options.slide_counter == 1) {
			var slidecount = $('#slidecounter>span.slidenumber').html();
			currentslide.prev().length ? slidecount-- : slidecount = $("#supersize>*").size();
			$('#slidecounter>span.slidenumber').html(slidecount);
		}

		$('#supersize>a.prevslide').removeClass('prevslide');
		prevslide.addClass('prevslide');

		nextslide.hide().addClass('activeslide')
			if (options.transition == 0) {
				nextslide.show(); $.inAnimation = false;
			}
			if (options.transition == 1) {
				nextslide.fadeIn(1200, 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>a.activeslide').resizenow(); //Fix for resize mid-transition
	}