/*
jquery.imgscaler - by Dave
based on - imgscaler - Fullscreen Slideshow jQuery Plugin By Sam Dunn Website: www.buildinternet.com/project/imgscaler
*/

(function($){

	//Resize image on ready or resize
	$.fn.imgscaler = function() {
		var options = $.extend($.fn.imgscaler.defaults, $.fn.imgscaler.options);
		
		$(window).bind("load", function(){
			$('#scale').fadeIn('slow');
			$('#content').show();
			
			//Gallery
			if(options.gallery == 1){
				$('.photo a').click(function(){
					var sel = $(this).attr('rel');
					if(sel == $('#scale img').attr('src')){return false;}
					$('.photo a').removeClass('selected');
					$(this).addClass('selected')
					$('#scale img').fadeOut('slow', function(){
						$(this).load(function(){
							$(this).fadeIn('slow');
						}).attr('src', sel);
					});
					//$('#scale img').load(function(){
					//	$(this).fadeIn('slow');
					//}).attr('src', $(this).attr('rel'));
					checkPag();
					return false;
				});
				
				checkPag();
				
				// prev
				$('a.prev').click(function(){
					var prev = $('.selected').parent().prev().children('a');
					
					if(prev.length == 0){return false;}
					$('.selected').removeClass('selected');
					prev.addClass('selected');
					$('#scale img').fadeOut('slow', function(){
						$(this).load(function(){
							$(this).fadeIn('slow');
						}).attr('src', prev.attr('rel'));
					});
					checkPag();
					return false;
				});
				
				// next
				$('a.next').click(function(){
					var next = $('.selected').parent().next().children('a');
					
					if(next.length == 0){return false;}
					$('.selected').removeClass('selected');
					next.addClass('selected');
					$('#scale img').fadeOut('slow', function(){
						$(this).load(function(){
							$(this).fadeIn('slow');
						}).attr('src', next.attr('rel'));
					});
					checkPag();
					return false;
				});
			}//
			
			//Slideshow
			if (options.slideshow == 1){
				slideshow_interval = setInterval("slides()", options.slide_interval);
			}//
			
		});
				
		$(document).ready(function() {
			$('#scale').resizenow(); 
		});
		
		$(window).bind("resize", function(){
    		$('#scale').resizenow();
		});
		
		$('#scale').hide();
		$('#content').hide();
	};
	
	// ie fix for scrollbars - not part of image scaler just lazy
	$.fn.scrollfix = function() {
		if ($.browser.msie) {
			var iScrollWidth = photos.scrollWidth+parseInt($(this).parent().parent().css('margin-left'));
			var iOffsetWidth = document.body.clientWidth;
			var iDifference = iScrollWidth - iOffsetWidth;
			if(iDifference >= 20){
				$(this).css({'padding-bottom' : '37px'});
			}else{
				$(this).css({'padding-bottom' : '20px'});
			}
		}
	}
	//
	
	//Adjust image size
	$.fn.resizenow = function() {
		var options = $.extend($.fn.imgscaler.defaults, $.fn.imgscaler.options);
	  	return this.each(function() {
	  		
			//Define image ratio
			var ratio = options.startheight/options.startwidth;
			
			//Gather browser and current image size
			var imagewidth = $(this).width();
			var imageheight = $(this).height();
			var browserwidth = $(window).width();
			var browserheight = $(window).height();
			var offset;

			//Resize image to proper ratio
			if ((browserheight/browserwidth) > ratio){
			    $(this).height(browserheight);
			    $(this).width(browserheight / ratio);
			    $(this).children().height(browserheight);
			    $(this).children().width(browserheight / ratio);
			} else {
			    $(this).width(browserwidth);
			    $(this).height(browserwidth * ratio);
			    $(this).children().width(browserwidth);
			    $(this).children().height(browserwidth * 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.imgscaler.defaults = { 
			startwidth: 16,  
			startheight: 10.5,
			vertical_center: 1,
			gallery: 0, 
			slideshow: 0, 
			slide_interval: 5000, 
			transition: 0
	};
	
})(jQuery);
	
	function slides() {
		var currentslide = $('#scale a.activeslide');
		var nextslide =  $('#scale a.activeslide').next().length ? $('#scale a.activeslide').next() : $('#scale a:first');
		currentslide.removeClass('activeslide');
		nextslide.addClass('activeslide');
		$('#scale img').fadeOut('slow', function(){
			$(this).load(function(){
				$(this).fadeIn('slow');
			}).attr('src', nextslide.attr('href'));
		});
		$('#scale').resizenow();//Fix for resize mid-transition
	}
	
	function checkPag(){
		var $current = $('.photo').find('.selected').parent();
		if($current.prev().attr('class') == null){
			$('a.prev').hide();
		}else{
			$('a.prev').show();
		}
		
		if($current.next().attr('class') == null){
			$('a.next').hide();
		}else{
			$('a.next').show();
		}
	}