(function($){
	$.fn.supersized = function() {
		$.inAnimation = false;
		$.paused = false;
		var options = $.extend($.fn.supersized.defaults, $.fn.supersized.options);
		$.currentSlide = options.start_slide - 1;
		/******Set up initial images -- Add class doesnt work*****/
		var imageLink = (options.slides[options.slides.length - 1].url) ? "href='" + options.slides[options.slides.length - 1].url + "'" : "";
		$("<img/>").attr("src", options.slides[options.slides.length - 1].image).appendTo("#supersized").wrap("<a " + imageLink + "></a>");
		imageLink = (options.slides[$.currentSlide].url) ? "href='" + options.slides[$.currentSlide].url + "'" : "";
		$("<img/>").attr("src", options.slides[$.currentSlide].image).appendTo("#supersized").wrap("<a class=\"activeslide\" " + imageLink + "></a>");
		imageLink = (options.slides[$.currentSlide + 1].url) ? "href='" + options.slides[$.currentSlide + 1].url + "'" : "";
		$("<img/>").attr("src", options.slides[$.currentSlide + 1].image).appendTo("#supersized").wrap("<a " + imageLink + "></a>");
		$(window).bind("load", function(){
			$('#loading').hide('fast');
			$('#supersized').fadeIn('fast');		
			$('#supersized').resizenow('fast');						
			if (options.slideshow == 1){
				if (options.slide_counter == 1){ 
					$('#slidecounter .slidenumber').html(options.start_slide);
	    			$('#slidecounter .totalslides').html(options.slides.length); 
	    		}
				slideshow_interval = setInterval(nextslide, options.slide_interval);				
			}
			/*$("#outuno_inverno").animate({ width: "55px" }, 2500, function () {
    			 $("#panel").slideUp("slide");
    		}); */
			
			$("#pre").animate({ width: "0px" }, 2500, function () {
    			 $("#panel").slideUp("slide");
    		}); 
		});		
		$(document).ready(function() {$('#supersized').resizenow(); });
		$('#supersized').hover(function() {
	   		if (options.slideshow == 1 && options.pause_hover == 1){
	   			if(!($.paused) && options.navigation == 1){
	   				$('#pauseplay').attr("src", "images/pause.png"); 
	   				clearInterval(slideshow_interval);
	   			}
	   		}
	   		if($.inAnimation) return false; 
	   	}, function() {
			if (options.slideshow == 1 && options.pause_hover == 1){
				if(!($.paused) && options.navigation == 1){
					$('#pauseplay').attr("src", "images/pause_dull.png");
					slideshow_interval = setInterval(nextslide, options.slide_interval);
				} 
			}	
	   	});
		$(window).bind("resize", function(){$('#supersized').resizenow(); });
		$('#supersized').hide();
	};
	$.fn.resizenow = function() {
		var t = $(this);
		var options = $.extend($.fn.supersized.defaults, $.fn.supersized.options);
	  	return t.each(function() {
			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;
		});
	};
	function nextslide() {
		if($.inAnimation) return false;
		else $.inAnimation = true;
	    var options = $.extend($.fn.supersized.defaults, $.fn.supersized.options);	
		var currentslide = $('#supersized .activeslide');
	    currentslide.removeClass('activeslide');
	    if ( currentslide.length == 0 ) currentslide = $('#supersized a:last'); 
	    var nextslide =  currentslide.next().length ? currentslide.next() : $('#supersized a:first');
	    var prevslide =  nextslide.prev().length ? nextslide.prev() : $('#supersized a:last');
		$('.prevslide').removeClass('prevslide');
		prevslide.addClass('prevslide');
		//Get the slide number of new slide
		$.currentSlide + 1 == options.slides.length ? $.currentSlide = 0 : $.currentSlide++;
		//Load next image
		loadSlide=false;
		$.currentSlide == options.slides.length - 1 ? loadSlide = 0 : loadSlide = $.currentSlide + 1;
		imageLink = (options.slides[loadSlide].url) ? "href='" + options.slides[loadSlide].url + "'" : "";
		$("<img/>").attr("src", options.slides[loadSlide].image).appendTo("#supersized").wrap("<a " + imageLink + "></a>");
		currentslide.prev().remove(); //Remove Old Image
		/**** End Image Loading ****/		
	    nextslide.hide().addClass('activeslide')
	    	if (options.transition == 1){nextslide.fadeIn(750, function(){$.inAnimation = false;});	}
	    $('#supersized').resizenow();
	}	
	$.fn.supersized.defaults = {startwidth: 4,startheight: 3,vertical_center: 1,slideshow: 1,navigation:0,thumbnail_navigation: 0,transition: 1,pause_hover: 0,slide_counter: 0,slide_captions: 0,slide_interval: 5000,start_slide: 1};	
})(jQuery);
