
(function($){

	
	$.fn.supersized = function() {
		
		var options = $.extend($.fn.supersized.defaults, $.fn.supersized.options);
		
		
		
		$().ready(function() {
			$.fn.supersized.options
			
			if( $('#supersize img').length == 1)
			{
				options.slideshow = 0;
			}
			imagensLength = 0;
			$('#supersize img').each(function(){
				imagensLength += 1;							  
				imgPreloader = new Image()
				imgPreloader.onload = function(){
					$(this).attr("width",imgPreloader.width).attr("height",imgPreloader.height)
					if( !$("#supersize img:first").is(":visible") )
					{
						alturaConteudoBanner = parseFloat($(window).height()) - parseFloat($("#topo").innerHeight()) - parseFloat($("#conteudo").innerHeight()) - parseFloat($("#rodape").innerHeight());
						$("#supersize img:first").css("display","block");
						$('#supersize').css("background","none")
						$('#supersize').resizenow();
					}
					
					if (options.slideshow == 1){
						if( imagensLength == $('#supersize img').length && $('#supersize img').length > 1 )
						{
							//setInterval("theslideshow()", options.slideinterval);
						}
					}	
					
				}
				imgPreloader.src = $(this).attr("src")								  
			})
			if( $('#supersize img').length <= 1)
			{
				options.slideshow = 0;
			}
			else
			{
				if (options.slideshow == 1){
					setInterval("theslideshow()", options.slideinterval);
				}	
			}
		});
		$(window).resize(function(){
    		$('#supersize').resizenow(); 
		});
	};
	
	//Adjust image size
	$.fn.resizenow = function() {
		
		var options = $.extend($.fn.supersized.defaults, $.fn.supersized.options);
		
	  	return this.each(function() {
	  		$(this).find("img").each(function(){
				
				widthInicial = $(this).attr("width");
				heightInicial = $(this).attr("height");
				
				alturaConteudoBanner = parseFloat($(window).height()) - parseFloat($("#topo").innerHeight()) - parseFloat($("#conteudo").innerHeight()) - parseFloat($("#rodape").innerHeight())
		
				if( parseFloat(alturaConteudoBanner) < parseFloat($("#supersize").css("min-height").replace("px","")) )
				{
					alturaConteudoBanner = $("#supersize").css("min-height").replace("px","")	
				}
				
				porcLarg = ( parseFloat($("#supersize").width())*100 ) / widthInicial
				
				alturaImg = (parseInt(heightInicial) * parseFloat(porcLarg)) / 100
				
				
				$(this).width($("#supersize").width())
				$(this).height(alturaImg)
				
				topImg = 0
				
				if( parseFloat(alturaImg) > parseFloat(alturaConteudoBanner) )
				{
					topImg = (parseFloat(alturaImg)-parseFloat(alturaConteudoBanner)) * (-1)
					topImg = parseFloat(topImg) / 2
					$(this).css("top",topImg)		
				}
				
				$("#supersize").height(parseFloat(alturaConteudoBanner))			
																															   
				
				
			});
			
			
			return false;
		});
	};
	
	$.fn.supersized.defaults = { 
			startWidth: 640,  
			startHeight: 480,			
			slideshow: 1,
			slideinterval: 5000  
	};
	
})(jQuery);

//Slideshow Add-on
function theslideshow() {
    
    var currentslide = $('#supersize img.activeslide');
    //alert(currentslide.length)	
    if ( currentslide.length == 0 ) currentslide = $('#supersize img:first');
		
    var nextslide =  currentslide.next().length ? currentslide.next() : $('#supersize img:first');
	
	$(currentslide).animate( {'opacity':'hide'},'slow');
	$(nextslide).animate( {'opacity':'show'},'slow',function(){
		nextslide.addClass('activeslide');
    	currentslide.removeClass('activeslide');														 
	});
	
    

}  	
