$(document).ready(function() {

	// open external links in a new window. These links must be marked with the rel attribute.

	$("a[rel=external]").click(function() {
		window.open(this.href);
		return false;
	});
	
	// lazy load plugin

	/*
	$("img").not(".panel img").lazyload({ 
		placeholder : TEMPLATE_URL + "/css/img/x.gif",
		effect : "fadeIn"
	});
	*/
		 
	// replace email span by real link
	$(".email").each(function() {
		var href = $(this).text().replace(" (at) ", "@").replace(/ \(dot\) /g, ".");
		$(this).html('<a href="mailto:' + href + '">' + href + '</a>');
	});	   
	
	// distributors page
	$(".world .button").click(function(){
		$(".distpop:not("+this.id+")").fadeOut(); 
		$("."+this.id).fadeIn();
	})
	$(".distpop .close").click(function(){
		$(this).parent('.distpop').fadeOut();
	})
	 
	// Infinite Scroll
	
	$("#posts").infinitescroll({
		debug           : false,
		nextSelector    : ".subnav a:first",
		loadingImg      : TEMPLATE_URL + "/css/img/load.gif",
		loadingText     : "<em>Loading more posts...</em>",
		donetext        : "<em>No more posts to display</em>",
		navSelector     : ".subnav",
		contentSelector : "#posts",
		itemSelector    : "#posts div.post"
	});
	
	var grid = $("<div />").addClass("grid");
		$(document).bind("keyup", function(e) {
			//console.log(e.keyCode);
			if(e.keyCode==71 && e.metaKey) {
				if($(".grid").length) {
					$(".grid").remove();
				}
				else {
					$("body").prepend(grid);
				}
			}
		});

	$("#tabs a").click(function(){
		$("#scene>div").hide();
		$($(this).attr('href')).show();
		$("#tabs a").removeClass("here");
		$(this).addClass("here");
		return false;
	});
	
});
