// ========= Photo Rotator
jQuery.fn.photoRotator = function(totalNum) {
	var container = $(this);
	$('#text',container).before('<img src="" alt="" class="homephoto" />');
	var randomNum = Math.ceil(Math.random()*totalNum);
	var nextNum = randomNum;
	var nextPhoto = function() {
		if (nextNum < totalNum && nextNum > 0) {
			nextNum++;
		} else {
			nextNum = 1;	
		}
		
		$('.homephoto').fadeOut(1100,function(){ 
			$(this).attr('src','/images/home-photos/'+nextNum+'.jpg');
		});
		$('.homephoto').load(function() {    
			$('.homephoto').fadeIn(1100);
		}); 
	};
	
	$('.homephoto').attr('src','/images/home-photos/'+randomNum+'.jpg');
	setInterval(nextPhoto, 8000);
};

// ========= Jquery Onload Events
$(document).ready(function(){
						   
	$('#home #main .content').photoRotator(4);
	
	$('#gallery #thumbnails a img').css('opacity','0.6');
	$('#gallery #thumbnails a:first').addClass('current').children('img').css('opacity','1');
	$('#gallery #thumbnails a').hover(function(){
		$(this).children('img').css('opacity','1');
	}, 
	function () {
		$(this).not('.current').children('img').css('opacity','0.6');
	});

	$('#gallery #thumbnails a').not('.jTscrollerNextButton, .jTscrollerPrevButton').click(function(event){
		event.preventDefault();
		
		$('#gallery #thumbnails .current').removeClass('current').children('img').css('opacity','0.6');
		$(this).addClass('current').children('img').css('opacity','1');
		
		var photoheight = $('#gallery #photos').css('height');
		$('#gallery #photos').css('height',photoheight);
  		var entry = $(this).attr("href");
		
		$('#photocontent').css('background-image','url(/images/loading.png)');
    	$('#gallery #photos').animate({
			opacity: 0,
			height: photoheight
		},500, function() {
			
			$.ajax({
				url: entry,
				dataType: "html",
				error: function(){alert(error);},
				success: function(data, textStatus) {
					$('#gallery #photos .before').empty().append( $(data).find('.before').contents() );
					$('#gallery #photos .after').empty().append( $(data).find('.after').contents() );
					
					photoheight = 0;
					$('#gallery #photos div').each(function () {
						$container = $(this);
						if ( $container.outerHeight() > photoheight ) {
							photoheight = $container.outerHeight();
						}
					});
					
					$('#gallery #photos').animate({
						height: photoheight
					},500, function() {
						$('#gallery #photos').animate({
							opacity: 1
						},500);
						$('#photocontent').css('background-image','none');
					});
					$('#gallery #category').empty().append( $(data).find('#category'));
				}
			});
				
/*			
			$('#gallery #photocontent').load(entry+' #photos', function() {
				photoheight = $('#gallery #photos').css('height');
				$('#gallery #photocontent').animate({
					height: photoheight
				},500, function() {
					$('#gallery #photos').animate({
						opacity: 1
					},500);
					$('#photocontent').css('background-image','none');
				});
			});
*/		
		});
		$('#gallery #category').load(entry+' #category');
	});
	
	if($("#gallery #thumbnails .jTscroller").width() >= 590) {
		$("#gallery #thumbnails").thumbnailScroller({ 
			scrollerType:"clickButtons", 
			scrollerOrientation:"horizontal", 
			scrollSpeed:2, 
			scrollEasing:"easeOutCirc", 
			scrollEasingAmount:600, 
			acceleration:4, 
			scrollSpeed:800, 
			noScrollCenterSpace:10, 
			autoScrolling:0, 
			autoScrollingSpeed:2000, 
			autoScrollingEasing:"easeInOutQuad", 
			autoScrollingDelay:500 
		});
	};
});
