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

(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(){
			
			/* ajweb  - add navigation bar to parent of gallery holder (#supersize) */
			
			$('<div id="navigation"><a href="#" id="prevslide"><img src="/wp-content/themes/bosquevinas/supersize/back_dull.gif"/></a><a href="#" id="pauseplay"><img src="/wp-content/themes/bosquevinas/supersize/pause_dull.gif"/></a><a href="#" id="nextslide"><img src="/wp-content/themes/bosquevinas/supersize/forward_dull.gif"/></a></div>').insertBefore('#supersize');
			$('#loading').hide();
			$('#supersize').fadeIn('fast');
			$('#content').show(); // ? what is this ?
			
			// sets the first image element as active slide if there is no active slide
			if ($('#slideshow .activeslide').length == 0) $('#supersize a:first').addClass('activeslide');
			// if captions, sets the title of activeslide image as caption
			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());
	    		}
	
				// set the timing for the slides
				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", "/supersizeImages/forward.gif");
					}, function(){
						if($.paused == true) return false;
					    $(this).attr("src", "/supersizeImages/forward_dull.gif");
					});
					$('#prevslide img').hover(function() {
						if($.paused == true) return false; 
					    $(this).attr("src", "/supersizeImages/back.gif");
					}, function(){
						if($.paused == true) return false;
					    $(this).attr("src", "/supersizeImages/back_dull.gif");
					});
					
				    //Play/Pause Button
				    $('#pauseplay').click(function() {
				    	if($.inAnimation) return false;
				    	var src = ($(this).find('img').attr("src") === "/supersizeImages/play.gif") ? "/supersizeImages/pause.gif" : "/supersizeImages/play.gif";
      					if (src == "/supersizeImages/pause.gif"){
      						$(this).find('img').attr("src", "/supersizeImages/play.gif");
      						$.paused = false;
					        slideshow_interval = setInterval(nextslide, options.slide_interval);  
				        }else{
				        	$(this).find('img').attr("src", "/supersizeImages/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") === "/supersizeImages/play_dull.gif");
				    	if (imagecheck){
      						$(this).find('img').attr("src", "/supersizeImages/play.gif"); 
				        }else{
				        	$(this).find('img').attr("src", "/supersizeImages/pause.gif");
				        }
				    });
				    
				    $('#pauseplay').mouseout(function() {
				    	var imagecheck = ($(this).find('img').attr("src") === "/supersizeImages/play.gif");
				    	if (imagecheck){
      						$(this).find('img').attr("src", "/supersizeImages/play_dull.gif"); 
				        }else{
				        	$(this).find('img').attr("src", "/supersizeImages/pause_dull.gif");
				        }
				        return false;
				    });
				}
			}
		});
				
		$(document).ready(function() {
			$('#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", "/supersizeImages/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", "/supersizeImages/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);
		
		if( !options.rescale_images ) return;
		
		// this function is applied as a method to the #supersize div, 
		// so this.each applies the bellow function to every child of #supersize (imageElements)
	  	return this.each(function() {
	  		
			// if width and height are set the browser window means nothing
			if(!options.width) {
				var browserwidth = $(window).width();
			} else {
				var browserwidth = options.width; 
			}
			if(!options.height) {
				var browserheight = $(window).height();
			} else {
				var browserheight = options.height;
			}
			
			$(this).width(browserwidth);
			$(this).height(browserheight);
			
			
			// todo esto AJWEB
	 		$(this).children().each(function(){
				// set the width and height of subwrappers (imageElement)
				// $(this).width = $(this).children('img').attr('naturalWidth');
				// 				$(this).height = $(this).children('img').attr('naturalHeight');
				
				// ratio of img
				var imgRatio = $(this).children('img').attr('naturalHeight')/$(this).children('img').attr('naturalWidth');
				
				// ratio of wrapper
				var wrapperRatio = browserheight/browserwidth;

				// make image proportional to wrapper ratio 
				if(imgRatio>wrapperRatio) {
					if( !$(this).children('img').data('supersized') ) {
						// alert($(this).children('img').width())
						var finalRatio = browserwidth/$(this).children('img').width();
						$(this).children('img').height($(this).children('img').height() * finalRatio);
						$(this).children('img').width( $(this).children('img').width() * finalRatio );
						// alert($(this).children('img').width())
						
					}
					// set a flag so we don't scale it again
					$(this).children('img').data('supersized', 'true');
				}
				if(imgRatio<wrapperRatio){
					if( !$(this).children('img').data('supersized') ) {
						// alert('imgratio<wrapperratio')
						var finalRatio = browserheight/$(this).children('img').height();
						$(this).children('img').width( $(this).children('img').width() * finalRatio );
						$(this).children('img').height( $(this).children('img').height() * finalRatio );
					}
					$(this).children('img').data('supersized', 'true');
				}
				

			});
			

			
			//Define image ratio
			var ratio = options.startheight/options.startwidth;
			
			// ajweb - comment previous line and get ratios from image size
			// var ratio = imgHeight/imgWidth;
			
			
			//Gather browser and current image size
			// it is not the image width/height but the container's
			var imagewidth = $(this).width();
			var imageheight = $(this).height();
			// alert(imagewidth)
			
			
			
			var offset; // ? it is not being used
			

			//Resize image to proper ratio
			// if ((browserheight/browserwidth) > ratio){
			// 			// the proportions of wrapper and images are not equal
			// 			// and height is 
			// 		    $(this).height(browserheight);
			// 		    $(this).width(browserheight / ratio);
			// 		
			// 		    // $(this).children().height(browserheight);
			// 		    // 				    $(this).children().width(browserheight / ratio);
			// 		
			// 		// alert('ratio wrapper mayor que image ratio')
			// 
			// 		} else {
			// 		    $(this).width(browserwidth);
			// 		    $(this).height(browserwidth * ratio);
			// 		
			// 		    // $(this).children().width(browserwidth);
			// 		    // 			   	$(this).children().height(browserwidth * ratio);
			// 		// alert('ratio wrapper mennor que image 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:1,
			transition: 1, //0-None, 1-Fade, 2-slide top, 3-slide right, 4-slide bottom, 5-slide left
			pause_hover: 0,
			slide_counter: 1,
			slide_captions: 1,
			slide_interval: 5000,
			// ajweb
			width: 0,
			height: 0
	};
	
})(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');
		
	    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 .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(750, 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');
		
	    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 .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(750, 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
	}
