var fancyOptions = { 
	'centerOnScroll': false,
	'zoomSpeedIn': 200, 
	'zoomSpeedOut': 200, 
	'overlayShow': false,
	'hideOnContentClick': false,
	'width': 680,
	'height': 500,
	'frameLeft': 20,
	'frameTop': 100,
	'autoDimensions': false
};

$(function() {
	var screenW = $(document).width()
	var screenH = $(document).height();


	var $hoverBubble = $("#hoverBubble");
	function showFace(face) {
		//face.find("img").css("visibility", "visible");
		var facePos = face.offset();
		var faceText = face.find("img").attr("alt");
		
		$hoverBubble.css({"left": facePos["left"] - $hoverBubble.width(), 
						 "top": facePos["top"] + face.height() - $hoverBubble.height()}) //place bubble in line with bottom of link
					.find("span").html(faceText + "!").end()
					.show();
	}
	function hideFace(face) {
		//face.find("img").css("visibility", "hidden");
		$hoverBubble.hide();
	}
	
	//load links within fancybox within itself. INCEPTION
	fancyOptions.onComplete = function () {
		$("#fancybox-content a")
			.not("a[href^=http]")
			.fancybox(fancyOptions);
	};
	
	
	$(".piecechows div").hover(function() {showFace($(this));}, function () {hideFace($(this));})
		.not("#blogChow")
		.find("a").fancybox(fancyOptions);	
	
	$("a.popup").fancybox(fancyOptions);
	
	
	//find optimal youtube size
	var youtubeSizes = [
		{width: 560, height: 315},
		{width: 640, height: 360},
		{width: 853, height: 480},
		{width: 1280, height: 720}
	];
	var youtubeSize = youtubeSizes[0];
	for (var i = 0; i < youtubeSizes.length; i++) {
		if (youtubeSizes[i].width + 100 < screenW && 
			youtubeSizes[i].height + 100 < screenH) {
			
			youtubeSize = youtubeSizes[i];
		}
	}
	
	
	$("a[href^='http://youtu']").each(function (e) {
		var vidId = this.href.replace(/.*\..*\/([^\/]+)/i, "$1");
		
		var vidContent = '<iframe width="' + youtubeSize.width + '" height="' + youtubeSize.height + '" src="http://www.youtube.com/embed/' + vidId + '" frameborder="0" allowfullscreen></iframe>';
		
		$(this).fancybox({ 
			'centerOnScroll': false,
			'zoomSpeedIn': 200, 
			'zoomSpeedOut': 200, 
			'overlayShow': false,
			'hideOnContentClick': false,
			'content': vidContent
		});
	});
	
	$("#blogChow a").click(function (e) { 
		e.preventDefault(); 
		window.open(this.href);
		
		return false;
	});
	
	$("#footerNav a").hover(function() {showFace($("#" + this.rel));}, function () {hideFace($("#" + this.rel));})
		.click(function (e) {e.preventDefault(); $("#" + this.rel).find("a").click();});
		
	
	//make sure all external links always open in new window, and others open in fancybox
	$("a[href^='http']")
		.not("a[href^='http://youtu']")
		.bind("click", function (e) { 
			e.preventDefault(); 
			window.open(this.href);
			
			return false;
		});	
	
	/////////////////////
	// theme specific! //
	/////////////////////
	/*$('#albumCover').hover(
		function() {$(this).attr("src", "images/self_titled-cover_back.jpg");}, 
		function () {$(this).attr("src", "images/self_titled-cover.jpg");}
	);*/
	
	
	/* minimize bubbles */
	$("#bottomBub").hover(function (e) {
		$("#bottomBub .desc").show('slow');
	},
	function (e) {
		$("#bottomBub .desc").hide('slow');
	});
	
	
	
	/* Parallax backgrounds */	
	const ZINDEX_MULT = 1.15;
	const MOVEMENT_MULT = 0.19;
	var maxZIndex = 0;
	
	function initParallax() {
		return $(".parallax").each(function (e, el) {
			//set the actual styles to anything from the stylesheets for faster getting
			var $this = $(this);
			this.style.zIndex = parseInt($this.css("zIndex")); 
			maxZIndex = Math.max(maxZIndex, this.style.zIndex);
			
			var pos = $this.position();
			$this.data("origLeft", pos.left);
			$this.data("origTop", pos.top);
			this.style.left = pos.left + "px";
			this.style.margin = 0; //undo any centering by margin
			this.style.top = pos.top + "px";
		});
	}

	var parallax = initParallax();
	
	
	maxZIndex = maxZIndex * ZINDEX_MULT;
	
	$("body").mousemove(function (e) {
		parallax.each(function (i, el) {
			var movement = (1 - (e.pageX / screenW)) * MOVEMENT_MULT;
			this.style.left = ($(this).data("origLeft") + ((movement - (movement / 2)) * ((parseInt(this.style.zIndex))))) + "px";
			var movementY = (1 - (e.pageY / screenH)) * MOVEMENT_MULT;
			this.style.top = ($(this).data("origTop") + ((movementY - (movementY / 2)) * ((parseInt(this.style.zIndex))))) + "px";
		});
	});
	
});


//fix spamproof emails
function fixEmails() {
	$(".emailAt").replaceWith("@");
	$(".emailLink").each(function() {
		var t = $(this);
		var addr = t.html();
		t.html("<a href='mailto:" + addr + "'>" + addr + "</a>");
	});
}

