$(function(){
	var num_images = $("a.pop").size();
	var cur_index = "";
	$("#media_viewer").append("<div id=\"share_pop\"><div id=\"fbook\"><div id=\"fb-root\"></div><script src=\"http://connect.facebook.net/en_US/all.js#appId=133705896711357&amp;xfbml=1\"></script><fb:like href=\""+window.location.href+"\" send=\"true\" layout=\"button_count\" width=\"105\" show_faces=\"false\" font=\"arial\"></fb:like></div><div id=\"twtr\"><a href=\"http://twitter.com/share\" class=\"twitter-share-button\" data-url=\""+window.location.href+"\" data-count=\"none\">Tweet</a><script type=\"text/javascript\" src=\"http://platform.twitter.com/widgets.js\"></script></div><div id=\"gplus\"><g:plusone size=\"medium\" annotation=\"none\"></g:plusone></div></div><div id=\"mod\"></div><div id=\"ctrls\"><a href=\"#\" class=\"prev\" alt=\"Previous\" title=\"Previous\"><span>Prev</span></a><a href=\"#\" class=\"next\"alt=\"Next\" title=\"Next\"><span>Next</span></a></div><div id=\"dat\"></div><a href=\"#\" id=\"close\" alt=\"Close\" title=\"Close\"><span>Close</span></a>");
	center_viewer();

	$("a.pop").click(function(e){
		e.preventDefault();
		$("#media_viewer").before("<div id=\"mask\"></div>").fadeIn(function(){
			$("#close").css({
				left: parseFloat($("#media_viewer").css("left"))+$("#media_viewer").width()+"px",
				top: $("#media_viewer").css("top")
			});
		});
		cur_index = $("a.pop").index(this);
		init_viewer(e,cur_index,$(this).attr("href"));
	});

	$("#ctrls a.next").live('click',function(e){
		cur_index = (cur_index < num_images-1) ? cur_index+1 : 0;
		init_viewer(e,cur_index,$(this).attr("href"));
	});

	$("#ctrls a.prev").live('click',function(e){
		cur_index = (cur_index > 0) ? cur_index-1 : num_images-1;
		init_viewer(e,cur_index,$(this).attr("href"));
	});

	$("#ctrls a").live("mouseenter",function()
	{
		$(this).css("opacity",0.5);
	}).live("mouseleave",function()
	{
		$(this).css("opacity",0.2);
	});

	$("#mask").live('click',function(){
		close_viewer();
	});

	$("#close").live('click',function(e){
		e.preventDefault();
		close_viewer();
	});

	$(window).resize(function(){
		center_viewer();
	});

	$(window).scroll(function(){
		center_viewer();
	});

	function close_viewer()
	{
		$("#dat").fadeOut("fast");
		$("#close").fadeOut("fast");
		$("#share_pop").fadeOut("fast");
		$("#media_viewer").fadeOut(function(){
			$("#mask").fadeOut("fast").remove();
		});
		$("#media_viewer #mod").html("");
	}

	function center_viewer()
	{
		$("#media_viewer").css("left",($(window).width()-$("#media_viewer").width())/2+$(window).scrollLeft()+"px");
		$("#media_viewer").css("top",($(window).height()-$("#media_viewer").height())/2+$(window).scrollTop()+"px");
	}

	function init_viewer(e,index,src)
	{
		e.preventDefault();
		$("#mod").fadeOut("fast");
		$("#dat").fadeOut("fast");
		$("#close").fadeOut("fast");

		// Get the next and previous images
		var next = (index < (num_images-1)) ? (index+1) : 0;
		var prev = (index > 0) ? (index-1) : (num_images-1);
		$("#media_viewer a.next").attr("href",$("a.pop:eq("+next+")").attr("href"));
		$("#media_viewer a.prev").attr("href",$("a.pop:eq("+prev+")").attr("href"));
		$("#media_viewer #mod").fadeOut("fast").empty();

		var a_temp = document.createElement('a');
		a_temp.href = src;

		$("#media_viewer").prepend("<img src=\"/wp-content/themes/ybs/images/preloader.gif\" id=\"pldr\">");
		$("#pldr").css("left",Math.floor(($("#media_viewer").width()-$("#pldr").width())/2)+"px");
		$("#pldr").css("top",Math.floor(($("#media_viewer").height()-$("#pldr").height())/2)+"px");
		$("#pldr").fadeIn("fast");

		// Load image
		var img = new Image();
		$(img).hide();
		$(img).load(function(){
			$("#pldr").remove();
			$("#media_viewer #mod").html(this);
			$("#media_viewer").animate({
				width: this.width+"px",
				height: this.height+40+"px",
				left: ($(window).width()-this.width)/2+$(window).scrollLeft()+"px",
				top: ($(window).height()-this.height)/2+$(window).scrollTop()+"px"
			}, "fast", function(){
				$(img).fadeIn("fast");
				$("#share_pop").fadeIn("fast");
				$("#mod").fadeIn("fast");
				$("#dat").fadeIn("fast");
				$("#close").fadeIn("fast");
			});
			$("#ctrls .prev").css({width:this.width/2,height:this.height});
			$("#ctrls .next").css({left:this.width/2,width:this.width/2,height:this.height,backgroundPosition:(this.width/2-70) + "px center"});
		});
		$(img).attr('src',src);

		// Load info
		$("#media_viewer #dat").html($(".pop:eq("+index+") .info b").html());
//		$("#media_viewer #dat #counter").html(pad_zeros((index+1),2)+" out of "+num_images);
	}
});

function pad_zeros(num,digits)
{
   var res = num.toString();
   while(res.length < digits) res = "0"+res;
   return res;
}

function get_file_ext(src)
{
   return (/[.]/.exec(src)) ? /[^.]+$/.exec(src) : "";
}

function is_video(src)
{
   return ((get_file_ext(src) == "flv") || (get_file_ext(src) == "f4v"));
}
