jQuery(document).ready( function() {
	
/*
	jQuery('.post-scroll-view').each( function() {
		jQuery(this).queue( function() {
			jQuery(this).slideDown(500).delay(100).dequeue();
		});
	});
*/	
	jQuery('.post-scroll-bar').css('width', function() {
		var totWidth = 0;
		jQuery('.post-block', this).each( function() {
			totWidth += jQuery(this).width() + 20;
		});
		totWidth -= 20; // account for last block
		return totWidth;
	});
	
	jQuery('.post-scroll-bar .post-block:last-child').css('margin-right', '0');
	
	jQuery('.post-block .post-title').each( function() {
		if( jQuery(this).height() < 40 )
			jQuery(this).css('height', '40px');
	});
	
	jQuery('.post-thumbnail').hover(
		function() {
			if (jQuery('.post-title', this).height() > 45 )
			jQuery('.post-title', this).animate( { top : 143 - jQuery('.post-title', this).height() }, 4 * jQuery('.post-title', this).height(), 'swing');
		},
		function() {
			jQuery('.post-title', this).stop().dequeue().animate( { top : 103 }, 4 * jQuery('.post-title', this).height(), 'swing');
		}
	);
	
	jQuery.fn.idle = function(time){  
	    var i = jQuery(this);  
	    i.queue(function(){  
	        setTimeout(function(){  
	            i.dequeue();  
	        }, time);  
	    });  
	};  
	
	jQuery('.expand').click( function() {
		var expandTime = 700;
		if( jQuery(this).hasClass('expanded') ) {
			var newHeight = 205;
			var newWidth = 750 * parseInt(jQuery(this).siblings('.scroll-nav').children('.total-pages').html()) - 20;
			jQuery(this).parent().siblings('.post-scroll-view').animate( { height : newHeight }, expandTime, function() {
				jQuery(this).children('.post-scroll-bar').css( 'width', newWidth);
			});
			jQuery(this).html('[+]');
			jQuery(this).removeClass('expanded');
			jQuery(this).siblings('.scroll-nav').fadeIn(expandTime);
		}
		else {
			jQuery(this).parent().siblings('.post-scroll-view').children('.post-scroll-bar').css('width', '680px').css('left', '0');
			var newHeight = 225 * parseInt(jQuery(this).siblings('.scroll-nav').children('.total-pages').html());
			jQuery(this).parent().siblings('.post-scroll-view').animate( { height : newHeight }, expandTime);
			jQuery(this).html('[-]');
			jQuery(this).addClass('expanded');
			jQuery(this).siblings('.scroll-nav').fadeOut(expandTime);
			jQuery(this).siblings('.scroll-nav').children('.current-page').html('1');
			if( !jQuery(this).siblings('.scroll-nav').children('.scroll-left').hasClass('inactive') )
				jQuery(this).siblings('.scroll-nav').children('.scroll-left').addClass('inactive');
		}
	});
	
/*
	jQuery('.collapse').click( function() {
		
		jQuery(this).siblings('.post-scroll-view').animate( { height : newHeight }, 1000);
		jQuery(this).removeClass('collapse');
		jQuery(this).html('Expand');
		jQuery(this).addClass('expand');
	});
*/
	
	jQuery('a.scroll-left').hover(
		function() {
			if( jQuery(this).siblings('.current-page').html() != 1 )
				jQuery(this).addClass('highlighted');
		},
		function() {
			if( jQuery(this).siblings('.current-page').html() != 1 )
				jQuery(this).removeClass('highlighted');
		}
	);
	
	jQuery('a.scroll-right').hover(
		function() {
			if( jQuery(this).siblings('.current-page').html() != jQuery(this).siblings('.total-pages').html() )
				jQuery(this).addClass('highlighted');
		},
		function() {
			if( jQuery(this).siblings('.current-page').html() != jQuery(this).siblings('.total-pages').html() )
				jQuery(this).removeClass('highlighted');
		}
	);
	
	jQuery('a.scroll-left').click( function() {
		
		var marginOffset = jQuery(this).parent().parent().parent().find('.post-block').css('marginRight');
			marginOffset = Number(marginOffset.replace('px', ''));
		var moveSize = jQuery(this).parent().parent().parent().width() + marginOffset;
		
		if( jQuery(this).siblings('.current-page').html() > 1 ) {
			var toMove = parseInt(jQuery(this).siblings('.current-page').html() - 2) * -moveSize; // -680;
			jQuery(this).parent().parent().siblings('.post-scroll-view').children('.post-scroll-bar').stop().animate( { left : toMove } , 350 , 'swing');
			jQuery(this).siblings('.current-page').html( parseInt(jQuery(this).siblings('.current-page').html()) - 1 );
			if( jQuery(this).siblings('.current-page').html() == 1 ) {
				jQuery(this).removeClass('highlighted');
				jQuery(this).addClass('inactive');
			}
			jQuery(this).siblings('.scroll-right').removeClass('inactive');
		}
		else {
			jQuery(this).css('hover', 'default');
		}
	});
	
	jQuery('a.scroll-right').click( function() {

		var marginOffset = jQuery(this).parent().parent().parent().find('.post-block').css('marginRight');
			marginOffset = Number(marginOffset.replace('px', ''));
		var moveSize = jQuery(this).parent().parent().parent().width() + marginOffset;

		if( jQuery(this).siblings('.current-page').html() < jQuery(this).siblings('.total-pages').html() ) {
			var toMove = parseInt(jQuery(this).siblings('.current-page').html()) * -moveSize; //-680;
			jQuery(this).parent().parent().siblings('.post-scroll-view').children('.post-scroll-bar').stop().animate( { left : toMove } , 350 , 'swing');
			jQuery(this).siblings('.current-page').html( parseInt(jQuery(this).siblings('.current-page').html()) + 1 );
			if( jQuery(this).siblings('.current-page').html() == jQuery(this).siblings('.total-pages').html() ) {
				jQuery(this).removeClass('highlighted');
				jQuery(this).addClass('inactive');
			}
			jQuery(this).siblings('.scroll-left').removeClass('inactive');
		}
		else {
			jQuery(this).css('hover', 'default');
		}
	});

});

