/** 
 * Main jS/jQuery Scripts
 *
 * @author Eduardo Morais
 * @version 2011.04.29
 */

/* Refresh embeds */
	$('div.post_media embed').each(function(){
		$(this).attr('wmode', 'opaque');
	});
	$('div.post_media object').each(function(){
		$(this).append('<param name="wmode" value="opaque"></param>');
	});

	$('#main article div.post_media object').css('display', 'inherit');

/* Anchor fix */
	$("a[href^='\#']").click(function(e){
	  e.preventDefault();
	  document.location.hash=this.href.substr(this.href.indexOf('#')+1);
	})

	
	
$(window).load(function() {
	
/* Scroll to post, if a # exists */

	if (window.location.hash.length) {
		var hash = window.location.hash.substr(1);
		scrollToPost("#post_"+hash);
	}
	
/* Keyboard */
	// Get article scroll offsets
	postOffsets = postOffsets();
	
	$(document.documentElement).keydown(function (e) {
	
		if ($('input:focus, textarea:focus, select:focus').length < 1) {

			if ((e.keyCode == 39) || (e.keyCode == 65) || (e.keyCode == 74) || (e.keyCode == 32)) { // Right or A/J or space
				e.preventDefault();
				scrollY = $(document).scrollTop();
				
				if (scrollY < $(document).height()-$(window).height()) {
					i = 0;
					while (scrollY >= postOffsets[i] && i <= postOffsets.length-1) {
						i = i + 1;
					}
					offsetY = postOffsets[i];
					
					// advance but not to the next post if key is space:
					if (e.keyCode == 32) {
						if ((offsetY - scrollY) > ($(window).height() - 100)) {
							offsetY = scrollY + $(window).height() - 100;
						}
					}
					$('html,body').animate({scrollTop: offsetY}, 500);
									
				}
			}
			
			if ((e.keyCode == 37) || (e.keyCode == 81) || (e.keyCode == 75)) { // Left or Q/K
				e.preventDefault();
				scrollY = $(document).scrollTop();
				if (scrollY > 0) {
					i = postOffsets.length-1;
					while (scrollY <= postOffsets[i] && i > 0) {
						i = i - 1;
					}
					$('html,body').animate({scrollTop: postOffsets[i]}, 800);
				}
			}
		}
	});
	
/* Click on fixed navigation */
	$('#hd_fixed').click(function(ev) {
		$('html,body').animate({scrollTop: 0}, 800);
	});
	$('#hd_fixed a').click(function(ev) {
		ev.stopPropagation();
	});

	
/* Initialize Lightboxes */
	
	$('.post_photoset').each(function(){
		$('a.lightbox', this).lightBox();
	});
	$('.post_media').each(function(){
		$('a.lightbox', this).lightBox();
	});
	$('#sidebar').each(function(){
		$('a.lightbox', this).lightBox();
	});
	$('a.imagebox').lightBox();
	
}); // END jQ execution loop


/*
 * Get post Offsets (for keyboard navigation)
 */
function postOffsets() {
	var postOffsets = new Array();
	var i = 1;
	var y = 0;
	postOffsets[0] = 0;	

	$('article').each(function(){
		y = $(this).offset().top - 90;

		if ((y - postOffsets[i-1]) > 100) { 
			postOffsets[i] = "" + Math.floor(y);
			i = i + 1;
		}
	});
	if (postOffsets[i-1] < ($(document).height()-$(window).height())) {
		postOffsets[i] = $(document).height()-$(window).height();
	}

	return postOffsets;
}


/*
 * Scroll to ID
 */
function scrollToPost(id){
	if ($(id).offset().top > 300) {
		$('html,body').delay(1000).animate({scrollTop: $(id).offset().top - 30}, 1000);
	}
}


/*
 * Load the new comment 
 */
function LoadNewComment(commid, set) { 
	$.getJSON("actions/comment_latest.json.php?set=" + set + "&commid=" + commid,
	  function(data) {
		if (data[0].comment) {
			newComment = '<div class="the_comment"><p>' + data[0].author + '&nbsp; ' + data[0].comment + '</p><p class="comment_date">' + data[0].date + '</p></div>';
			
			$(newComment).clone().hide().appendTo('#post_comments_' + commid).slideDown();
			$('#comment_postlink_' + commid).html('Write a new comment');
			$('#comment_count_' + commid).html('<p>New comments!</p>');
		}
	  }
	);
}

