/*
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 by ID3 for SOUND Phuket website: ajax loading, random display order and no slideshow navigation or caption
*/

(function($){

	//Init all
	supersized_init = function(){
		var options = $.extend($.fn.supersized.defaults, $.fn.supersized.options);
		/*$('#loading').hide();
		$('#supersize').fadeIn('fast');
		//$('#content').show();
		if ($('#slideshow .activeslide').length == 0) $('#supersize a: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());
    		}
			slideshow_interval = setInterval("nextslide()", options.slide_interval);
			if (options.navigation == 1){ //Skip if no navigation
				$('#navigation a').click(function(){  
						$(this).blur();  
						return false;  
					}); 	
				//Slide Navigation
			    $('#nextslide').click(function() {
			    	if($.paused) return false; if($.inAnimation) return false;
				    clearInterval(slideshow_interval);
				    nextslide();
				    slideshow_interval = setInterval(nextslide, options.slide_interval);
				    return false;
			    });
			    $('#prevslide').click(function() {
			    	if($.paused) return false; if($.inAnimation) return false;
			        clearInterval(slideshow_interval);
			        prevslide();
			        slideshow_interval = setInterval(nextslide, options.slide_interval);
			        return false;
			    });
				$('#nextslide img').hover(function() {
					if($.paused == true) return false;
				   	$(this).attr("src", "images/forward.gif");
				}, function(){
					if($.paused == true) return false;
				    $(this).attr("src", "images/forward_dull.gif");
				});
				$('#prevslide img').hover(function() {
					if($.paused == true) return false; 
				    $(this).attr("src", "images/back.gif");
				}, function(){
					if($.paused == true) return false;
				    $(this).attr("src", "images/back_dull.gif");
				});
				
			    //Play/Pause Button
			    $('#pauseplay').click(function() {
			    	if($.inAnimation) return false;
			    	var src = ($(this).find('img').attr("src") === "images/play.gif") ? "images/pause.gif" : "images/play.gif";
  					if (src == "images/pause.gif"){
  						$(this).find('img').attr("src", "images/play.gif");
  						$.paused = false;
				        slideshow_interval = setInterval(nextslide, options.slide_interval);  
			        }else{
			        	$(this).find('img').attr("src", "images/pause.gif");
			        	clearInterval(slideshow_interval);
			        	$.paused = true;
			        }
  					$(this).find('img').attr("src", src);
				    return false;
			    });
			    $('#pauseplay').mouseover(function() {
			    	var imagecheck = ($(this).find('img').attr("src") === "images/play_dull.gif");
			    	if (imagecheck){
  						$(this).find('img').attr("src", "images/play.gif"); 
			        }else{
			        	$(this).find('img').attr("src", "images/pause.gif");
			        }
			    });
			    
			    $('#pauseplay').mouseout(function() {
			    	var imagecheck = ($(this).find('img').attr("src") === "images/play.gif");
			    	if (imagecheck){
  						$(this).find('img').attr("src", "images/play_dull.gif"); 
			        }else{
			        	$(this).find('img').attr("src", "images/pause_dull.gif");
			        }
			        return false;
			    });
			}
		}
	};
	
	//Resize image on ready or resize
	$.fn.supersized = function() {
		$.inAnimation = false;
		$.paused = false;
		$.a_rnd = new Array();
		$.numLoaded = 0;
		$.a_idx = 0;
		var options = $.extend($.fn.supersized.defaults, $.fn.supersized.options);
		
		$(window).bind("load", function(){
			// show first picture
			$('#loading').hide();
			$('#supersize').fadeIn('fast');
			//$('#content').show();
			if ($('#slideshow .activeslide').length == 0) $('#supersize a:eq('+$.a_rnd[0]+')').addClass('activeslide');
			if (options.slide_captions == 1) $('#slidecaption').html($('#supersize .activeslide').find('img').attr('title'));
			if (options.navigation == 0) $('#navigation').hide();
			
			// when document is loaded load the remaining pictures and call init after
			for (var i = 1; i < $.a_rnd.length; i++){
				$('#supersize a:eq('+$.a_rnd[i]+') > img').attr('src', $('#supersize a:eq('+$.a_rnd[i]+')').attr('rel'));
			}
		});
		
		$(document).ready(function() {
			// build an array with the indexes of the elements
			$.a_rnd = new Array($('#supersize a').size());
			$.a_rnd = $.map($.a_rnd, function(n, i){ return i; });
			// randomize this array
			$.a_rnd.sort(function() {return 0.5 - Math.random()});
			
			// monitor loading of pictures
			for (var i = 0; i < $.a_rnd.length; i++){
				$('#supersize a:eq('+$.a_rnd[i]+') > img').load(function(event){
					$.numLoaded++;
					// if all pictures are loaded, call init
					if ($.numLoaded == $.a_rnd.length) {
						supersized_init();
						// no need for a new resize
					}
				});
			}
			// load first picture of array
			$('#supersize a:eq('+$.a_rnd[$.a_idx]+') > img').attr('src', $('#supersize a:eq('+$.a_rnd[$.a_idx]+')').attr('rel'));
			
			// resize all elements
			$('#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(); 
		});
		
		$('#supersize').hide();
		//$('#content').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.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,
			slideshow: 1,
			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: 0,
			slide_captions: 0,
			slide_interval: 10000
	};
	
})(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 .activeslide');
	    currentslide.removeClass('activeslide');
		
		var prevslide = $('#supersize a:eq('+$.a_rnd[$.a_idx]+')');
		$.a_idx++;
		if ($.a_idx >= $.a_rnd.length) {
			$.a_idx = 0;
			// randomize array
			$.a_rnd.sort(function() {return 0.5 - Math.random()});
		}
		var nextslide = $('#supersize a:eq('+$.a_rnd[$.a_idx]+')');
		
		
		//Display slide counter
		if (options.slide_counter == 1){
			var slidecount = $('#slidecounter .slidenumber').html();
			currentslide.next().length ? slidecount++ : slidecount = 1;
		    $('#slidecounter .slidenumber').html(slidecount);
		}
		
		$('.prevslide').removeClass('prevslide');
		prevslide.addClass('prevslide');
		
		//Captions require img in <a>
	    if (options.slide_captions == 1) $('#slidecaption').html($(nextslide).find('img').attr('title'));
		
	    nextslide.hide().addClass('activeslide')
    	if (options.transition == 0){
    		nextslide.show(); $.inAnimation = false;
    	}
    	if (options.transition == 1){
    		nextslide.fadeIn(1500, 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').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 .activeslide');
	    currentslide.removeClass('activeslide');
		
		var prevslide = $('#supersize a:eq('+$.a_rnd[$.a_idx]+')');
		$.a_idx--;
		if ($.a_idx < 0) {
			$.a_idx = $.a_rnd.length - 1;
			// randomize array
			$.a_rnd.sort(function() {return 0.5 - Math.random()});
		}
		var nextslide = $('#supersize a:eq('+$.a_rnd[$.a_idx]+')');
		var prevslide = $('#supersize a:eq('+$.a_rnd[($.a_idx == $.a_rnd.length-1 ? 0 : $.a_idx+1)]+')');
		
		//Display slide counter
		if (options.slide_counter == 1){
			var slidecount = $('#slidecounter .slidenumber').html();
			currentslide.prev().length ? slidecount-- : slidecount = $("#supersize > *").size();
		    $('#slidecounter .slidenumber').html(slidecount);
		}
		
		$('.prevslide').removeClass('prevslide');
		prevslide.addClass('prevslide');
		
		//Captions require img in <a>
	    if (options.slide_captions == 1) $('#slidecaption').html($(nextslide).find('img').attr('title'));
		
	    nextslide.hide().addClass('activeslide')
	    	if (options.transition == 0){
	    		nextslide.show(); $.inAnimation = false;
	    	}
	    	if (options.transition == 1){
	    		nextslide.fadeIn(1500, 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
	}
