$(function(){
	var classFromElt = function(elt){
		return "."+$(elt).text().toLowerCase();
	};
	var selectAllIfLast = function(){
		var retval = false;
		if($(".crewMenu .selected").length<2){
			$(".crewMenu li").addClass("selected", "normal");
			$(".roster li").slideDown();
			retval = true;
		}
		return retval;
	};
	var restoreAll = function(){
		//restore all people when dbl click on selected
		$(".crewMenu li").addClass("selected", "normal");
		$(".roster li").slideDown();
	};
	$(".menu li").click(function (event){
		event.preventDefault();
		//replace top xx elts with these elts
		var selected = $(classFromElt(this));
		console.log(classFromElt(this), selected);
		var vis = $(".roster li:not("+classFromElt(this)+"):visible");
		var count=0,max=0;
		var doMove = function(force){
			count++;
			console.log(count,max);
			if(count>=max || force){
				console.log("force?", force);
				var reset={position:"",top:"",left:"",height:""};
				//extra timeout to hopefully fix timing issues
				window.setTimeout(function(){
				$(".roster").prepend(selected).find("li").css(reset).end().css(reset);
				},100);
			}
		}
		/*
		//don't animate in IE 6 or 7, it can't end well...
		if(!(jQuery.browser.msie && jQuery.browser.version<8)){
			var height = $(".roster").parent().innerHeight();
			$(".roster").css({'position':"relative", height: height});
			$(".roster li").each(function(i,elt){
				$(elt).css($(elt).position());
			}).css({position:"absolute"});
			if(vis.length<selected.length){
				//hmmmm - not sure what to do, for now just move them with no animation
				doMove(true);
			}else{
				//swap elts
				selected.each(function(i,elt){
					max+=2;
					var target = vis.slice(i,i+1)
					var end = $(elt).position();
					var start = target.position();
					target.animate(end,{complete:doMove});
					$(elt).animate(start,{complete:doMove});
				});
			}
		}else{
			//skip animation, move immidately
			doMove(true);
		}		
		*/
		doMove();
	}).hover(function (){
		$(classFromElt(this)).addClass("active",'fast');
	},function (){
		$(classFromElt(this)).removeClass("active",'fast');
	}).dblclick(function (){
		if($(this).hasClass("selected") && $(".crewMenu li.selected").length<2){
			restoreAll();
		}else{
			$(this).addClass("selected", "normal").siblings().removeClass("selected", "normal");
			$(classFromElt(this)).slideDown();
			$(".roster li:not("+classFromElt(this)+")").slideUp();
		}
	});
	
	var loadFullImage = function (elt){
		var src = elt.parent().removeClass("lazyload").attr("full")||elt.parent().attr("href");
		console.log("full src",src, "elt", elt)
		if(window.innerWidth<=750){
			src = src.replace("-full","");
		}
		elt.replaceWith("<img src='"+src+"' alt='"+elt.attr("alt")+"'/>");
	};
	var smoothscroll = function (event){
		var hash = $(this).attr("href");
		hash = hash.substr(hash.indexOf("#"));
		if(hash=="#contact"){
			//force load of images since contact is on the bottom
			loadFullImage($(".resizable img"))
		}
		if($(hash).length){
			$('html, body').stop().animate({
				scrollTop: $(hash).offset().top
			});
			//event.preventDefault();
		}
	};
	$("nav a").click(smoothscroll);
	
	$(".more").click(function (event){
		$(this).parent().removeClass("collapsed");
	});
	
	$(window).hashchange( function(){
		$( location.hash ).removeClass("collapsed", 'normal');
		if(location.hash=="#contact"){
			//force load of images since contact is on the bottom
			loadFullImage($(".resizable img"))
		}
	});
	var sizeIt = function(){
		$(".resizable img").each(function (idx, img){
			if(window.innerWidth>750){
				if(img.src.indexOf("-full")==-1){
					img.src = img.src.replace(".gif", "-full.gif");
					img.src = img.src.replace(".png", "-full.png");
				}
			}
		});
	}
	var lazyload = function(){
		var windowBottom = $(window).scrollTop()+$(window).height();
		var offset=$(".lazyload .placeholder").offset();
		if(offset && offset.top-50<windowBottom){
			$(".lazyload .placeholder").each(function(idx, elt){
				elt = $(elt)
				if(elt.offset().top-50<windowBottom){
					loadFullImage(elt);
				}
			})
		}
	}
	
	$(window).resize( sizeIt ).bind("scroll", lazyload);
	
	$(window).hashchange();
	//make larger images load if needed
	sizeIt();
});

