$("document").ready(function () {
	cleanupLinks();
	enhanceProjects();
	enableEmailAddressViewing();
});

function cleanupLinks () {
	$("a").click(function () {$(this).blur();});
}

function enhanceProjects() {
	/* 	Walk through the different projects
			and hide all those that aren't the first one.
			
			Set up toggling functionality to hide/show
			the different project when the user clicks the title.
	*/
			
	$("li .stage").not(":first").css("display","none");
	
	$("#recentWork li:first").addClass("opened");
	$("#recentWork li:first a:first").addClass("active");
	
	$("#recentWork li a.trigger").mousedown(function() {
		var lid = $($(this).parents()[0]).attr('id');
		if (!$($(this).parents()[0]).hasClass("opened")) {
			$(this).addClass("active");
			$("#"+lid+" .stage").animate({ height: "show", opacity: 1}, 500 );
			$("#"+lid+" .stage").fadeIn(500);
			$($(this).parents()[0]).addClass("opened");
		} else {
			$(this).removeClass("active");
			$($(this).parents()[0]).removeClass("opened");
			//$("#"+lid+" .stage").hide(500);
			$("#"+lid+" .stage").animate({ height: "hide", opacity: 0.0}, 500 );
			$("#"+lid+" .stage").fadeOut(500);
		}

	return false;
	});
	$("#recentWork li a.trigger").click(function () { return false; });
	/*		
	Setup the pages within each project:
		1) 	Hide all pages but the first in each stage
		2) 	Set up the pagination controls to al	low
			switching between the pages.
	*/
	var pages = $(".page");
	for (var i = 0; i < pages.length; i++) {
		if ($(pages[i]).prev().attr("class") && $(pages[i]).prev().attr("class") == "page") {
			$(pages[i]).css("display", "none");
		}
	}
	
	var scs = $(".stageControls a.arrow");
	for (var i = 0; i<scs.length; i+=2) {
		$(scs[i]).mousedown(function () {
			$($(this).children()[0]).attr("src", $($(this).children()[0]).attr("src").replace(/(.*)(Up|Over|Down)\.gif/, "$1Down.gif"));
			var lid = $(this).parents()[2].id;
			$("#"+lid+" .page").each(function () {
				if ($(this).css("display") != "none") {
					if ($(this).prev().attr("class") == "page") {
						$(this).css("display", "none");
						$(this).prev().css("display", "block");
						$(this).prev().fadeIn(1000);
						
						decrementPagination(lid);
						return false;
					}
				}
			});
			return false;
		});
		
		
		$(scs[i]).hover(	
				function () {
					$($(this).children()[0]).attr("src", $($(this).children()[0]).attr("src").replace(/(.*)(Up|Over|Down)\.gif/, "$1Over.gif"));},
				function () {
					$($(this).children()[0]).attr("src", $($(this).children()[0]).attr("src").replace(/(.*)(Up|Over|Down)\.gif/, "$1Up.gif"));}
		);
		
		$(scs[i+1]).hover(	
				function () {
					$($(this).children()[0]).attr("src", $($(this).children()[0]).attr("src").replace(/(.*)(Up|Over|Down)\.gif/, "$1Over.gif"));},
				function () {
					$($(this).children()[0]).attr("src", $($(this).children()[0]).attr("src").replace(/(.*)(Up|Over|Down)\.gif/, "$1Up.gif"));}
		);
		
		$(scs[i]).mouseup(function () {
			$($(this).children()[0]).attr("src", $($(this).children()[0]).attr("src").replace(/(.*)(Up|Over|Down)\.gif/, "$1Up.gif"));
			return false;
			}
		);
		$(scs[i+1]).mouseup(function () {
			$($(this).children()[0]).attr("src", $($(this).children()[0]).attr("src").replace(/(.*)(Up|Over|Down)\.gif/, "$1Up.gif"));
			return false;
			}
		);
		
		$(scs[i+1]).mousedown(function () {
			$($(this).children()[0]).attr("src", $($(this).children()[0]).attr("src").replace(/(.*)(Up|Over|Down)\.gif/, "$1Down.gif"));
			var lid = $(this).parents()[2].id;
			$("#"+lid+" .page").each(function () {
				if ($(this).css("display") != "none") {
					if ($(this).next().attr("class") == "page") {
						$(this).css("display", "none");
						$(this).next().css("display", "block");
						incrementPagination(lid);
						return false;
					}
				}
			});
			return false;
		});
		
		$(scs[i]).click(function () { return false; });
		$(scs[i+1]).click(function () { return false; });
		
		var lid = $(scs[i]).parents()[2].id;
		var pagination = $("#"+lid+" .stageControls a").not(".arrow");
		for (var x=0; x<pagination.length; x++) {
			$(pagination[x]).attr("id", "pagin"+lid+x);
			$(pagination[x]).mousedown(function () {
				var lid = $(this).parents().get(2).id;
				$("#"+lid+" .page").css("display", "none");
				var num = parseInt($(this).attr("id").match(/[0-9]{1,2}/));
				//num = parseInt(num.replace(/.*([0-9]{2}).*/, "$1"));
				$($("#"+lid+" .page")[num]).css("display", "block");
				changePagination(lid, $(this).attr("id"));
				return false; 
			});
			$(pagination[x]).hover(	
				function () {
					if (!$(this).hasClass("upPage"))
						$($(this).children()[0]).attr("src", $($(this).children()[0]).attr("src").replace(/(.*)([0-9]{2}).*\.gif/, "$1$2Over.gif"));},
				function () {
					if (!$(this).hasClass("upPage")) 
						$($(this).children()[0]).attr("src", $($(this).children()[0]).attr("src").replace(/(.*)([0-9]{2}).*\.gif/, "$1$2Up.gif"));}
			);
			$(pagination[x]).click(function () { return false; });
		}
		changePagination(lid, $(pagination[0]).attr("id"));
		
	}
	
}

function changePagination( lid, upPage ) {
	$("#"+lid+" .stageControls a").not(".arrow").each(function () {
		if ($(this).attr("id") != upPage) {
			$(this).removeClass("upPage");
			$($(this).children()[0]).attr("src", $($(this).children()[0]).attr("src").replace(/(.*)([0-9]{2}).*\.gif/, "$1$2Up.gif"));
		} else {
			$(this).addClass("upPage");
			$($(this).children()[0]).attr("src", $($(this).children()[0]).attr("src").replace(/(.*)([0-9]{2}).*\.gif/, "$1$2Active.gif"));
		}
	
	});
}
function incrementPagination (lid) {
	changePagination(lid, $("#"+lid+" .stageControls a.upPage").next().attr("id"));
}
function decrementPagination (lid) {
	changePagination(lid, $("#"+lid+" .stageControls a.upPage").prev().attr("id"));
}
function enableEmailAddressViewing() {
	$("#suckItSpammers").click(function () {
		if ($(this).attr("href").indexOf("@") < 0) {
			$(this).fadeOut(150, function () {
									$(this).html("jacksongabbard"+"@"+"gmail"+"."+"com");
									$(this).attr("href", "mail"+"to"+":"+$(this).html());
									$(this).fadeIn(150);
								  });
			return false;
		} else {
			return true;
		}
	});
}