$(function() {
	/**
	 * Reviews on home page
	 */
	$('#revColumn a').click(function(e) {
		e.preventDefault();

		var curRating = $(this).attr('data-rating');
		$(this).parent().parent().find('li.active').removeClass('active');
		$(this).parent().addClass('active');

		$('#revCurrent a').attr('href', $(this).attr('href').replace('#',''));
		$('#revCurrent a img').attr('src', $(this).find('img').attr('src'));
		$('#revCurrent a .title').html($(this).attr('data-album'));
		$('#revCurrent a .subtitle').html($(this).attr('data-artist'));
		$('#revCurrent #ratings li.current').removeClass('current');
		$('#revCurrent #ratings li.r' + curRating).addClass('current');
	});

	// Initialize scrolling
	initScrolling();
	
	
	// Set featured article heights
	initFeatureHeights();
	
	// Set anchor heights for clipped images in each article
	//window.setTimeout('initArticleImageHeights', 500);
	initArticleImageHeights();	
	
	
	/**
	 * Setup the the tool tips for footer icons that need dynamicly added
	 * span tags
	 */
	var first = true;
	$('article footer nav li a').each(function(key, item) {
		var href = $(this).attr('href');
		if ($(this).attr('href').match(/disqus_thread/)) {
			$(this).after('<span>Leave a Comment</span>');
		}
		
		$(this).parent().find('span').css('left', (($(this).width() / 2) - 61));
	});
	
	
	/**
	 * Initizliaze the comment html from 'View Comments' / '0 Comments' to the
	 * # of comments only.
	 */
	var ctr = 0;
	$('article footer nav li a[href*="#disqus_thread"]').each(function(key, item) {
		initCommentIcon(item, key);
	});
});

/**
 *
 */
function initFeatureHeights() 
{
	var $featured = $('#featured');
	if ($featured.length > 0) {
		var liHeight = 0, buffer = 35;
		$featured.find('li article').each(function(key, item) {
			if (($(this).height() + buffer) > liHeight) {
				liHeight = $(this).height() + buffer;
			}
		});
		
		$featured.find('li').each(function(key, item) {
			$(this).css('height', liHeight);
		});
	}
	
	// Default single article inline elements to 460px width
	var $single = $('article.single');
	if ($single.length > 0) {
		if ($single.find('section.content iframe').length > 0) {
			$single.find('section.content iframe').width('460px');
		}
		
		if ($single.find('section.content object').length > 0) {
			$single.find('section.content object').width('460px');
		}
	}
}

function initArticleImageHeights() 
{
	window.setTimeout(function() {
		var $articles = $('section[role="main"] article');
		if ($articles.length > 1) {
			$articles.each(function(key, item) {
				var height = $(this).find('header a img').height();
				console.log(height);
				if (height > 0) {
					$(this).find('header a').css('height', height);
				}
			});
		}
	}, 500);
}

/**
 * Page scroll for 'fixed' positioning on single page
 */
function initScrolling() 
{
	var $single = $('section[role="main"] article.single');
	if ($single.length === 0 || ($.browser.msie && parseInt($.browser.version, 10) < 7)) {
			return;
	}

	var $window = $(window);
	var $column = $single.find('footer'),
		screenTopPadding = 20,
		disqusMarginTop = 25,
		commentsOffsetTop = $('#disqus_thread').offset().top,
		columnHeight = $single.find('footer').outerHeight(true),		
		h1Height = $single.find('section h1').outerHeight(true);

	// Set min height of the content for shorter lengthed posts
	$single.find('section.content').css('min-height', columnHeight + 'px');
	
	var relativeTopY = (h1Height - 16);
	var scrollTopClipY = parseInt($column.offset().top + relativeTopY + 8);
	
	var relativeBottomY = parseInt(commentsOffsetTop - columnHeight - $single.offset().top - disqusMarginTop);
	var scrollBottomClipY = parseInt(commentsOffsetTop - columnHeight - disqusMarginTop);
	
   	//$('body').append('<div id="temporary" style="z-index: 100; position: absolute; height: 20px; color: white; width: 500px; background: red;"></div>');
   	//$ruler = $('#temporary');
   	
   	window.setTimeout(function() {
   		columnHeight = $single.find('footer').outerHeight(true);
   		relativeBottomY = parseInt(commentsOffsetTop - columnHeight - $single.offset().top - disqusMarginTop);
   		scrollBottomClipY = parseInt(commentsOffsetTop - columnHeight - disqusMarginTop);	
   		//console.log(columnHeight, relativeTopY, scrollTopClipY, relativeBottomY, scrollBottomClipY, commentsOffsetTop);
   	}, 1000);
   	
	function positionColumnScroll() 
	{
		var scrollY = $window.scrollTop();
		//var fixed = $column.css('position') == 'fixed';				
	
		//console.log(scrollY, columnHeight, scrollTopClipY, scrollBottomClipY, commentsOffsetTop);
		//console.log(scrollY, scrollBottomClipY, relativeBottomY);
		
		//$ruler.css('top', scrollY + 'px').html(scrollY + ' Bottom: ' + (scrollY + columnHeight));
		if ((columnHeight + scrollTopClipY + 50) >= $column.prev().height()) {
			$column.css({ 
        		position: 'relative', 
        		top: relativeTopY + 'px'
        	});
			return;
		}
		
		if (scrollY > scrollBottomClipY) {
			$column.css({
				position: 'relative',
				top: relativeBottomY + 'px'
			});
		}
		else if (scrollY > (scrollTopClipY - screenTopPadding)) {
			$column.css({
	          	position: 'fixed',
	          	top: screenTopPadding + 'px'
	        });
		} 
		else if (scrollY < scrollTopClipY) {
        	$column.css({ 
        		position: 'relative', 
        		top: relativeTopY + 'px'
        	});
		}
	}
	
	$window.bind('scroll.positionColumn', function() {
		positionColumnScroll();
	});
	positionColumnScroll();
}



function initCommentIcon(item, key) 
{
	window.setTimeout(function() {
		var disqus = $(item);
						
		if (!disqus.length)
			return;
		
		var pieces = disqus.html().split(' ');			
		if (disqus.html().match(/View Comments/) || pieces.length < 2) {
			initCommentIcon(item, key);
		} else {
			disqus.html(pieces[0]).css('text-indent', '0');
		}
			
		//console.log('Finished #' + key + ' @ ' + disqus.html() + '\n\n');
		
		return;
	}, 200);
}
