/*
* supersized2 - Fullscreen slideshow2 jQuery Plugin
* Version 3.0
* By Sam Dunn (www.buildinternet.com // www.onemightyroar.com)
* Version: supersized2.3.0.js
* Website: www.buildinternet.com/project/supersized2
* Adaptation - Eduardo Carvalho (www.eduardo.fot.br)
*/

(function($){

	//Resize image on ready or resize
	$.fn.supersized2 = function() {
		
		$.inAnimation2 = false;
		$.paused2 = false;
	
		var options = $.extend($.fn.supersized2.defaults2, $.fn.supersized2.options);
		$.currentSlide2 = options.start_slide2 - 1;
		
		/******Set up initial images -- Add class doesnt work*****/
		//Set previous image
		var imageLink2 = (options.slides2[options.slides2.length - 1].url) ? "href='" + options.slides2[options.slides2.length - 1].url + "'" : "";
		$("<img/>").attr("src", options.slides2[options.slides2.length - 1].image).appendTo("#supersized2").wrap("<a " + imageLink2 + "></a>");//Doesnt account for start slide
		
		//Set current image
		imageLink2 = (options.slides2[$.currentSlide2].url) ? "href='" + options.slides2[$.currentSlide2].url + "'" : "";
		$("<img/>").attr("src", options.slides2[$.currentSlide2].image).appendTo("#supersized2").wrap("<a class=\"activeslide2\" " + imageLink2 + "></a>");
		
		//Set next image
		imageLink2 = (options.slides2[$.currentSlide2 + 1].url) ? "href='" + options.slides2[$.currentSlide2 + 1].url + "'" : "";
		$("<img/>").attr("src", options.slides2[$.currentSlide2 + 1].image).appendTo("#supersized2").wrap("<a " + imageLink2 + "></a>");
		
		$(window).bind("load", function(){
			
			$('#loading').hide();
			$('#supersized2').fadeIn('fast');
			$('#supersized2').resizenow();
			
			if (options.slide_captions2 == 1) $('#slidecaption').html(options.slides2[$.currentSlide2].title);//*********Pull caption from array
			if (options.navigation2 == 0) $('#navigation2').hide();
			if (options.thumbnail_navigation2 == 0){ $('#nextthumb').hide(); $('#prevthumb').hide(); }
			
			//slideshow2
			if (options.slideshow2 == 1){
				if (options.slide_counter2 == 1){ //Initiate slide counter if active
					$('#slidecounter .slidenumber').html(options.start_slide2);
	    			$('#slidecounter .totalslides').html(options.slides2.length); //*******Pull total from length of array
	    		}
				slideshow_interval2 = setInterval(nextslide2, options.slide_interval2);
				
				if (options.thumbnail_navigation2 == 1){
					//Thumbnail navigation2
					$('#nextthumb').click(function() {
				    	if($.inAnimation2) return false;
					    clearInterval(slideshow_interval2);
					    nextslide2();
					    if(!($.paused2)) slideshow_interval2 = setInterval(nextslide2, options.slide_interval2);
					    return false;
				    });
				    $('#prevthumb').click(function() {
				    	if($.inAnimation2) return false;
				        clearInterval(slideshow_interval2);
				        prevslide2();
				       	if(!($.paused2)) slideshow_interval2 = setInterval(nextslide2, options.slide_interval2);
				        return false;
				    });
					}
				
				if (options.navigation2 == 1){ //Skip if no navigation2
					$('#navigation2 a').click(function(){  
   						$(this).blur();  
   						return false;  
   					});
   					 	
					//Slide navigation2
				    $('#nextslide2').click(function() {
				    	if($.inAnimation2) return false;
					    clearInterval(slideshow_interval2);
					    nextslide2();
					    if(!($.paused2)) slideshow_interval2 = setInterval(nextslide2, options.slide_interval2);
					    return false;
				    });
				    $('#prevslide2').click(function() {
				    	if($.inAnimation2) return false;
				        clearInterval(slideshow_interval2);
				        prevslide2();
				        if(!($.paused2)) slideshow_interval2 = setInterval(nextslide2, options.slide_interval2);
				        return false;
				    });
				    $('#nextslide2').mousedown(function() {
					   	$(this).attr("src", "images/forward.png");
					});
					$('#nextslide2').mouseup(function() {
					    $(this).attr("src", "images/forward_dull.png");
					});
					$('#nextslide2').mouseout(function() {
					    $(this).attr("src", "images/forward_dull.png");
					});
					
					$('#prevslide2').mousedown(function() {
					    $(this).attr("src", "images/back.png");
					});
					$('#prevslide2').mouseup(function() {
					    $(this).attr("src", "images/back_dull.png");
					});
					$('#prevslide2').mouseout(function() {
					    $(this).attr("src", "images/back_dull.png");
					});
					
				    //Play/Pause Button
				    $('#pauseplay').click(function() {
				    	if($.inAnimation2) return false;
				    	var src = ($(this).attr("src") === "images/play.png") ? "images/pause.png" : "images/play.png";
      					if (src == "images/pause.png"){
      						$(this).attr("src", "images/play.png");
      						$.paused2 = false;
					        slideshow_interval2 = setInterval(nextslide2, options.slide_interval2);  
				        }else{
				        	$(this).attr("src", "images/pause.png");
				        	clearInterval(slideshow_interval2);
				        	$.paused2 = true;
				        }
      					$(this).attr("src", src);
					    return false;
				    });
				    $('#pauseplay').mouseover(function() {
				    	var imagecheck = ($(this).attr("src") === "images/play_dull.png");
				    	if (imagecheck){
      						$(this).attr("src", "images/play.png"); 
				        }else{
				        	$(this).attr("src", "images/pause.png");
				        }
				    });
				    
				    $('#pauseplay').mouseout(function() {
				    	var imagecheck = ($(this).attr("src") === "images/play.png");
				    	if (imagecheck){
      						$(this).attr("src", "images/play_dull.png"); 
				        }else{
				        	$(this).attr("src", "images/pause_dull.png");
				        }
				        return false;
				    });
				}
			}
		});
		
		$(window).bind("resize", function(){
    		$('#supersized2').resizenow(); 
		});
		
		$('#supersized2').hide();
		$('#controls-wrapper').hide();
	};
	
	//Adjust image size
	$.fn.resizenow = function() {
		var t = $(this);
		var options = $.extend($.fn.supersized2.defaults2, $.fn.supersized2.options);
	  	return t.each(function() {
	  		
			//Define image ratio
			var ratio = options.startheight2/options.startwidth2;
			
			//Gather browser and current image size
			var imagewidth = t.width();
			var imageheight = t.height();
			var browserwidth = $(window).width() - $(".nav").width();
			var browserwidthreal = browserwidth - $(".logos").width();
			var browserreal = $(window).width();
			var browserheight = $(window).height();
			var offset;
								
			//Resize image to proper ratio
			if ((browserheight/browserwidthreal) > ratio){
			    t.height(browserheight);
			    t.width(browserheight / ratio);
			    t.children().height(browserheight);
			    t.children().width(browserheight / ratio);
			} else {
			    t.width(browserwidthreal);
			    t.height(browserwidthreal * ratio);
			    t.children().width(browserwidthreal);
			    t.children().height(browserwidthreal * ratio);
			}
			
			// Se for menor que 1024 o slider chega para esquerda
			if (browserreal <= 1024){
				//$(".slider").css("margin-left", "-200px");
				//$("#loading").css("margin-left", "200px");
			} else {
				$(".slider").css("margin-left", "0px");	
				$("#loading").css("margin-left", "0px");
			}
			
			// Se for menor que 600, barra de navegação com valores fixos
			if (browserheight <= 600){
				$(".nav").css('overflow','auto');
	 			$(".nav h1").addClass("just_h1");
				$(".nav_txt").addClass("just_nav_txt");
	 			$(".nav_menu").addClass("just_nav_menu");
			} else {
				$(".nav").css('overflow','hidden');
	 			$(".nav h1").removeClass("just_h1");
				$(".nav_txt").removeClass("just_nav_txt");
	 			$(".nav_menu").removeClass("just_nav_menu");	
			}
			
			if (options.vertical_center2 == 1){
				t.children().css('left', (browserwidthreal - t.width())/2);
				t.children().css('top', (browserheight - t.height())/2);
			}
			return false;
		});
	};
	
		//slideshow2 Next Slide
	function nextslide2() {
		if($.inAnimation2) return false;
		else $.inAnimation2 = true;
	    var options = $.extend($.fn.supersized2.defaults2, $.fn.supersized2.options);
		
		var currentSlide2 = $('#supersized2 .activeslide2');
	    currentSlide2.removeClass('activeslide2');
		
	    if ( currentSlide2.length == 0 ) currentSlide2 = $('#supersized2 a:last'); //*******Check if end of array?
			
	    var nextslide2 =  currentSlide2.next().length ? currentSlide2.next() : $('#supersized2 a:first'); //*******Array
	    var prevslide2 =  nextslide2.prev().length ? nextslide2.prev() : $('#supersized2 a:last'); //*******Array
		
		$('.prevslide2').removeClass('prevslide2');
		prevslide2.addClass('prevslide2');
		
		//Get the slide number of new slide
		$.currentSlide2 + 1 == options.slides2.length ? $.currentSlide2 = 0 : $.currentSlide2++;
		
		/**** Image Loading ****/
		//Load next image
		loadSlide2=false;
		$.currentSlide2 == options.slides2.length - 1 ? loadSlide2 = 0 : loadSlide2 = $.currentSlide2 + 1;
		imageLink2 = (options.slides2[loadSlide2].url) ? "href='" + options.slides2[loadSlide2].url + "'" : "";
		$("<img/>").attr("src", options.slides2[loadSlide2].image).appendTo("#supersized2").wrap("<a " + imageLink2 + "></a>");
		
		if (options.thumbnail_navigation2 == 1){
		//Load previous thumbnail
		$.currentSlide2 - 1 < 0  ? prevThumb = options.slides2.length - 1 : prevThumb = $.currentSlide2 - 1;
		$('#prevthumb').html($("<img/>").attr("src", options.slides2[prevThumb].image));
		
		//Load next thumbnail
		nextThumb = loadSlide2;
		$('#nextthumb').html($("<img/>").attr("src", options.slides2[nextThumb].image));
		}
		
		currentSlide2.prev().remove(); //Remove Old Image
		
		/**** End Image Loading ****/
		
		//Display slide counter
		if (options.slide_counter2 == 1){
		    $('#slidecounter .slidenumber').html($.currentSlide2 + 1);//**display current slide after checking if last
		}
		
		//Captions
	    if (options.slide_captions2 == 1){
	    	(options.slides2[$.currentSlide2].title) ? $('#slidecaption').html(options.slides2[$.currentSlide2].title) : $('#slidecaption').html('') ; //*******Grab next slide's title from array
	    }
		//Efeitos de transição
	    nextslide2.hide().addClass('activeslide2')
	    	if (options.transition2 == 0){
	    		nextslide2.show(); $.inAnimation2 = false;
	    	}
	    	if (options.transition2 == 1){
	    		nextslide2.fadeIn(750, function(){$.inAnimation2 = false;});
	    	}
	    	if (options.transition2 == 2){
	    		nextslide2.show("slide", { direction: "up" }, 'slow', function(){$.inAnimation2 = false;});
	    	}
	    	if (options.transition2 == 3){
	    		nextslide2.show("slide", { direction: "right" }, 'slow', function(){$.inAnimation2 = false;});
	    	}
	    	if (options.transition2 == 4){
	    		nextslide2.show("slide", { direction: "down" }, 'slow', function(){$.inAnimation2 = false;});
	    	}
	    	if (options.transition2 == 5){
	    		nextslide2.show("slide", { direction: "left" }, 'slow', function(){$.inAnimation2 = false;});
	    	}
	    $('#supersized2').resizenow();
	}
	
	//slideshow2 Previous Slide
	function prevslide2() {
		if($.inAnimation2) return false;
		else $.inAnimation2 = true;
		var options = $.extend($.fn.supersized2.defaults2, $.fn.supersized2.options);
	    
	    var currentSlide2 = $('#supersized2 .activeslide2');
	    currentSlide2.removeClass('activeslide2');
		
	    if ( currentSlide2.length == 0 ) currentSlide2 = $('#supersized2 a:first');
			
	    var nextslide2 =  currentSlide2.prev().length ? currentSlide2.prev() : $('#supersized2 a:last'); //****** If equal to total length of array
	    var prevslide2 =  nextslide2.next().length ? nextslide2.next() : $('#supersized2 a:first');
				
		//Get current slide number
		$.currentSlide2 == 0 ?  $.currentSlide2 = options.slides2.length - 1 : $.currentSlide2-- ;
		
		/**** Image Loading ****/
		//Load next image
		loadSlide2=false;
		$.currentSlide2 - 1 < 0  ? loadSlide2 = options.slides2.length - 1 : loadSlide2 = $.currentSlide2 - 1;
		imageLink2 = (options.slides2[loadSlide2].url) ? "href='" + options.slides2[loadSlide2].url + "'" : "";
		$("<img/>").attr("src", options.slides2[loadSlide2].image).prependTo("#supersized2").wrap("<a " + imageLink2 + "></a>");
		
		if (options.thumbnail_navigation2 == 1){
		//Load previous thumbnail
		prevThumb = loadSlide2;
		$('#prevthumb').html($("<img/>").attr("src", options.slides2[prevThumb].image));
		
		//Load next thumbnail
		$.currentSlide2 == options.slides2.length - 1 ? nextThumb = 0 : nextThumb = $.currentSlide2 + 1;
		$('#nextthumb').html($("<img/>").attr("src", options.slides2[nextThumb].image));
		}
		
		currentSlide2.next().remove(); //Remove Old Image
		
		/**** End Image Loading ****/
		
		//Display slide counter
		if (options.slide_counter2 == 1){
		    $('#slidecounter .slidenumber').html($.currentSlide2 + 1);
		}
		
		$('.prevslide2').removeClass('prevslide2');
		prevslide2.addClass('prevslide2');
		
		//Captions
	    if (options.slide_captions2 == 1){
	    	(options.slides2[$.currentSlide2].title) ? $('#slidecaption').html(options.slides2[$.currentSlide2].title) : $('#slidecaption').html('') ; //*******Grab next slide's title from array
	    }
		//Efeitos de Transição
	    nextslide2.hide().addClass('activeslide2')
	    	if (options.transition2 == 0){
	    		nextslide2.show(); $.inAnimation2 = false;
	    	}
	    	if (options.transition2 == 1){
	    		nextslide2.fadeIn(750, function(){$.inAnimation2 = false;});
	    	}
	    	if (options.transition2 == 2){
	    		nextslide2.show("slide", { direction: "down" }, 'slow', function(){$.inAnimation2 = false;});
	    	}
	    	if (options.transition2 == 3){
	    		nextslide2.show("slide", { direction: "left" }, 'slow', function(){$.inAnimation2 = false;});
	    	}
	    	if (options.transition2 == 4){
	    		nextslide2.show("slide", { direction: "up" }, 'slow', function(){$.inAnimation2 = false;});
	    	}
	    	if (options.transition2 == 5){
	    		nextslide2.show("slide", { direction: "right" }, 'slow', function(){$.inAnimation2 = false;});
	    	}
	    	
	    	$('#supersized2').resizenow();//Fix for resize mid-transition2			
	}
	
	$.fn.supersized2.defaults2 = { 
			startwidth2: 4,  
			startheight2: 3,
			vertical_center2: 1,
			slideshow2: 1,
			navigation2:1,
			thumbnail_navigation2: 0,
			transition2: 1, //0-None, 1-Fade, 2-slide top, 3-slide right, 4-slide bottom, 5-slide left
			pause_hover2: 0,
			slide_counter2: 1,
			slide_captions2: 1,
			slide_interval2: 3000,
			start_slide2: 1
	};
})(jQuery);
