function resizenow() {
	
	return element.each(function() {
		var ratio = ($(this).height()/$(this).width()).toFixed(2);	//Define image ratio
		thisSlide = $(this);
		
		//Gather browser size
		var browserwidth = $(window).width();
		var browserheight = $(window).height();
		var offset;
		
		/**Resize image to proper ratio**/
		//If window smaller than minimum width and height
		if ((browserheight <= options.min_height) && (browserwidth <= options.min_width)) {
			if ((browserheight/browserwidth) > ratio) {
				options.fit_landscape && ratio <= 1 ? resizeWidth(true) : resizeHeight(true);	//If landscapes are set to fit
			} else {
				options.fit_portrait && ratio > 1 ? resizeHeight(true) : resizeWidth(true);		//If portraits are set to fit
			}
			
		//If window only smaller than minimum width
		} else if (browserwidth <= options.min_width) {
			if ((browserheight/browserwidth) > ratio) {
				options.fit_landscape && ratio <= 1 ? resizeWidth(true) : resizeHeight();		//If landscapes are set to fit
			} else {
				options.fit_portrait && ratio > 1 ? resizeHeight() : resizeWidth(true);			//If portraits are set to fit
			}
			
		//If window only smaller than minimum height
		} else if (browserheight <= options.min_height) {
			if ((browserheight/browserwidth) > ratio) {
				options.fit_landscape && ratio <= 1 ? resizeWidth() : resizeHeight(true);		//If landscapes are set to fit
			} else {
				options.fit_portrait && ratio > 1 ? resizeHeight(true) : resizeWidth();			//If portraits are set to fit
			}
			
		//If larger than minimums
		} else {
			if ((browserheight/browserwidth) > ratio) {
				options.fit_landscape && ratio <= 1 ? resizeWidth() : resizeHeight();			//If landscapes are set to fit
			} else {
				options.fit_portrait && ratio > 1 ? resizeHeight() : resizeWidth();				//If portraits are set to fit
			}
		}
		/**End Image Resize**/
		
		/**Resize Functions**/
		function resizeWidth(minimum) {
			//If minimum height needs to be considered
			if (minimum) {
				if (thisSlide.width() < browserwidth || thisSlide.width() < options.min_width ) {
					if (thisSlide.width() * ratio >= options.min_height) {
						thisSlide.width(options.min_width);
						thisSlide.height(thisSlide.width() * ratio);
					} else {
						resizeHeight();
					}
				}
			} else {
				//If minimum height needs to be considered
				if (options.min_height >= browserheight && !options.fit_landscape) {
					//If resizing would push below minimum height or image is a landscape
					if (browserwidth * ratio >= options.min_height || (browserwidth * ratio >= options.min_height && ratio <= 1)) {
						thisSlide.width(browserwidth);
						thisSlide.height(browserwidth * ratio);
					//Else the image is portrait
					} else if (ratio > 1) {
						thisSlide.height(options.min_height);
						thisSlide.width(thisSlide.height() / ratio);
					} else if (thisSlide.width() < browserwidth) {
						thisSlide.width(browserwidth);
						thisSlide.height(thisSlide.width() * ratio);
					}
				//Otherwise, resize as normal
				} else {
					thisSlide.width(browserwidth);
					thisSlide.height(browserwidth * ratio);
				}
			}
		};
		
		function resizeHeight(minimum) {
			//If minimum height needs to be considered
			if (minimum) {
				if(thisSlide.height() < browserheight) {
					if (thisSlide.height() / ratio >= options.min_width) {
						thisSlide.height(options.min_height);
						thisSlide.width(thisSlide.height() / ratio);
					}else{
						resizeWidth(true);
					}
				}
			//Otherwise, resized as normal
			} else {
				//If minimum width needs to be considered
				if (options.min_width >= browserwidth){
					//If resizing would push below minimum width or image is a portrait
					if (browserheight / ratio >= options.min_width || ratio > 1) {
						thisSlide.height(browserheight);
						thisSlide.width(browserheight / ratio);
					//Else the image is landscape
					} else if (ratio <= 1) {
						thisSlide.width(options.min_width);
						thisSlide.height(thisSlide.width() * ratio);
					}
				//Otherwise, resize as normal
				} else {
					thisSlide.height(browserheight);
					thisSlide.width(browserheight / ratio);
				}
			}
		};
		/**End Resize Functions**/
		
		// Horizontally Center
		if (options.horizontal_center) {
			$(this).css('margin-left', ((element.width() - $(this).width())/2)+'px');
		}
		// Vertically Center
		if (options.vertical_center) {
			$(this).css('margin-top', ((element.height() - $(this).height())/2)+'px');
		}
	});
}

function show_first() {
	$('#supersized-loader').remove();
	if( !$('.supersized img:first-child').hasClass('current') ) {
		$('.supersized img:first-child').addClass('current').fadeIn('fast');
		resizenow();
	}
}

$('document').ready(function() {
	var element_count = $('.supersized img').length;
	var element_loaded = 0;
	var count = 1;
	
	element.each(function(index) {
		count++;
		if( !$.browser.msie ) {	
			$(this).load( function() {
				element_loaded++;
				if(index == 0) {
					resizenow();
					$(this).addClass('current').fadeIn('fast');
					$('#supersized-loader').remove();
				}
			});
		} else {
				element_loaded++;
				if(index == 0) {
					resizenow();
					$(this).addClass('current').fadeIn('fast');
					$('#supersized-loader').remove();
				}
		}
		
		if(count == element_count && options.cycle == true) {
			$('#supersized-loader').remove();
			if( !$('.supersized img:first-child').hasClass('current') ) {
				$('.supersized img:first-child').addClass('current').fadeIn('fast');
				resizenow();
			}
			$('.HOMESLIDES').each(function(index) {
				$(this).cycle({
					fx: 'fade', // scrollHorz
					timeout: 10000, 
					speed:   500,
					prev: '.activeSlide',
					next: '.slideshow-next',
					pager: '.SLIDECAPTION-toggles',
					pagerAnchorBuilder: function(i) {
						if (index == 0) { // for first slideshow, return a new anchor
							return '<a href="javascript:void(0);">&nbsp;<\/a>';
						} else { // for 2nd slideshow, select the anchor created previously
						//	return '#SLIDESHOW-pager a:eq('+i+')';
						}
					},
					before: function(curr, next, opts, fwd) {
						var slide = $(next).attr('id');
						$(curr).width( $(this).parent().width().toFixed(2) );
						$(next).width( $(this).parent().width().toFixed(2) );
						$(".SLIDECAPTION-inner h1.active").removeClass('active').fadeOut('fast', function() {
							$("#"+slide+"-caption").fadeIn('fast', function() { }).addClass('active');
						});
					},
					after: function(curr, next, opts, fwd) {
						$(curr).width( $(this).parent().width().toFixed(2) );
					},
					cleartype: true,	// IE fills the slide containers with a white background, and this will knock that out
					cleartypeNoBg: true
				}).show();
			});
			$('.SLIDECAPTION-toggles a').click(function(event) {
				element.cycle('stop');
			});
		}
	});
	
	setTimeout("show_first();", 1000);
	
});
$(window).resize(function() {
	resizenow();
});
