$ = jQuery.noConflict();
var resizeTimer = 0;

$(document).ready(function() {
	window.setInterval(function() {
		$("#social-box ul").animate({top: "-=45"}, 500, function() {
			$(this).find("li").first().appendTo("#social-box ul");
			$(this).css("top", 0);
		});
	}, "5000");
	
	if($(".panel").length > 0) {
		celtic.init();
		dynamicSize();
		$(window).resize(function() {
			if(resizeTimer) {
				window.clearTimeout(resizeTimer);
			}
			resizeTimer = window.setTimeout(dynamicSize, 333);
		});
	}
});

var celtic = {
	panelWidth: 0,
	numPanels: 0,
	curPanel: 0,
	isAnimating: false,
	curHash: "",
	onPortfolio: false,
	curPortfolioPanel: 0,
	
	init: function() {
		celtic.panelWidth = $(".panel").first().width();
		celtic.panelHeight = $(".panel").first().height();
		celtic.numPanels = $("#panel-container > .panel").length;
		
		$("#panel-container").width(celtic.numPanels * celtic.panelWidth);
		
		$("#panel-container > .panel").each(function(i) {
			$(this).css("top", (i * celtic.panelHeight));
		});
		
		$(".portfolio").each(function(i) {
			$(this).find(".panel").each(function(j) {
				$(this).css("left", (j * celtic.panelWidth));
			});
		});
		
		celtic.bindArrows();
		
		window.setTimeout(celtic.checkHashChanged, 200);
	},
	
	bindArrows: function() {
		$(document).keydown(function(e) {
			if(e.keyCode == 39) {
				celtic.slideRight();
			} else if(e.keyCode == 38) {
				celtic.slideUp();
			} else if(e.keyCode == 37) {
				celtic.slideLeft();
			} else if(e.keyCode == 40) {
				celtic.slideDown();
			}
		});
		
		$(".arrow-right").css("cursor", "pointer").click(celtic.slideRight);
		$(".arrow-left").css("cursor", "pointer").click(celtic.slideLeft);
	},
	
	slideRight: function() {
		if(celtic.onPortfolio) {
			var curPortfolioLength = $("#panel-container > .panel").eq(celtic.curPanel).find(".panel").length;
			if(celtic.curPortfolioPanel < (curPortfolioLength - 1)) {
				celtic.slidePortfolioToNum( celtic.curPortfolioPanel + 1 );
			}
		}
	},
	
	slideLeft: function() {
		if(celtic.onPortfolio) {
			if(celtic.curPortfolioPanel != 0) {
				celtic.slidePortfolioToNum( celtic.curPortfolioPanel - 1 );
			}
		}
	},
	
	slideDown: function() {
		if(celtic.curPanel < (celtic.numPanels - 1)) {
			celtic.slideToNum( celtic.curPanel + 1 );
		}
	},
	
	slideUp: function() {
		if(celtic.curPanel != 0) {
			celtic.slideToNum( celtic.curPanel - 1 );
		}
	},
	
	slideToNum: function(num) {
		if(celtic.isAnimating) {
			return false;
		}
		
		var resetPortfolio = false;
		
		if(celtic.checkIsPortfolio(celtic.curPanel)) {
			celtic.curPortfolioPanel = 0;
			resetPortfolio = true;
		}
		
		var newPosition = "-" + num * celtic.panelHeight;
		celtic.isAnimating = true;
		celtic.addHistory(num);
		celtic.checkIsPortfolio(num);
		$("#panel-container").animate({top: newPosition}, 1000, function() {
			if(resetPortfolio) {
				$("#panel-container > .panel:eq(" + celtic.curPanel + ") .portfolio-container").css("left", 0);
			}
			celtic.isAnimating = false;
			celtic.curPanel = num;
		});
		
		return true;
	},
	
	slideToName: function(name) {
		celtic.slideToNum( $("#" + name).index() );
	},
	
	addHistory: function(num) {
		var panelName = $("#panel-container > .panel").eq(num).attr("id");
		panelName = "/" + panelName;
		var top = $(window).scrollTop();
		
		window.location.hash = panelName;
		celtic.curHash = panelName;
		
		$(window).scrollTop(top);
	},
	
	slidePortfolioToNum: function(num) {
		if(celtic.isAnimating) {
			return false;
		}
		
		var $masterPanel = $("#panel-container > .panel").eq(celtic.curPanel);
		celtic.isAnimating = true;
		var newPosition = "-" + num * celtic.panelWidth;
		$masterPanel.find(".portfolio-container").animate({left: newPosition}, 1000, function() {
			celtic.isAnimating = false;
			celtic.curPortfolioPanel = num;
		});
		
		$masterPanel.find(".panel").eq(celtic.curPortfolioPanel + 2).find("img[src='images/blank.gif']").each(function() {
			loadImage(this);
		})
		
		return true;
	},
	
	checkIsPortfolio: function(num) {
		var $panel = $("#panel-container > .panel").eq(num);
		if($panel.hasClass("portfolio")) {
			celtic.onPortfolio = true;
			return true;
		} else {
			celtic.onPortfolio = false;
			return false;
		}
	},
	
	checkHashChanged: function() {
		var newHash = window.location.hash.replace("#", "");
		if(newHash == "") {
			newHash = "/home";
		}
		if(newHash != celtic.curHash) {
			celtic.curHash = newHash;
			var divID = newHash.replace("/", "");
			celtic.slideToName(divID);
		}
		
		window.setTimeout(celtic.checkHashChanged, 200);
	}
}

function dynamicSize() {
	var contentMargin = 0;
	
	contentMargin = $(window).height() - 180;
	contentMargin = (contentMargin - $("#content").height() ) / 2;
	
	if(contentMargin < 15) {
		$("#wrapper").height(660)();
		return false;
	}
	
	$("#wrapper").height( $(window).height() -20 );
	$("#content").css("marginTop", contentMargin);
}

function loadImage(img) {
	$(img).attr("src", $(img).data("src")).data("src", "");
}
