var docRoot = "http://matthewhittinger.com/";

var c, w, h, scale;
var bg, imgData;
var navItems, showpanel, bodyClasses;
var isZoomed = false;
var clickOut = false;
var isPhotoPage = false;

var subpages = {
	books: new Array(),
	other_works: new Array(),
	media: new Array()
}

function setBlips() {
	var blip = arguments[0];
	
	if(!isZoomed) {	
		if(jQuery.inArray(blip.page_id, bodyClasses) != -1) {
			$("#mh_showpanel").css({
				'top': blip.showpanel.top,
				'left': blip.showpanel.left,
				'width': blip.showpanel.width ? blip.showpanel.width : "75%"
			});
		}
		
		$(blip.root).css({
			"top": blip.alignment.top,
			"left": blip.alignment.left
		});
		$(blip.root).append(
			$(document.createElement('p'))
				.html(blip.alias)
		);
		$(blip.root).click(function() {
			if(blip.init) {
				blip.init.call();
			}
			
		});
		$(blip.root).mouseenter(function() {
			$(blip.root).css('opacity',1);
		});
		$(blip.root).mouseleave(function() {
			$(blip.root).css('opacity',0.4);
		});
		
	} else {
		$(blip.root).css({
			"display": "block"
		});
		
		if(blip.subpages) {
			var sublist = document.createElement('ul');
			$(sublist)
				.attr({
					"class": "m_rollover",
					"id": "m_sublist"
				})
				.css('opacity',0);
			
			$.each(blip.subpages, function() {
				var s = this;
				$(sublist).append(
					$(document.createElement('li'))
						.html(s.alias)
						.click(function() {
							window.location = docRoot + s.link;
						})
						.mouseenter(function() {
							clickOut = true;
						})
						.mouseleave(function() {
							clickOut = false;
						})
				);
			});
			
			$(blip.root).append(sublist);
		}
	}
}

function doZoom() {
	// takes target object
	var blip = arguments[0];
	if(!isZoomed) {
		isZoomed = true;
		$.each(navItems, function() {
			var blip = this;
			$(blip.root).css({
				"display": "none"
			});
		});
		
		setBlips(blip);
		
		var tSize = $($(blip.root).children('p')[0]).css('font-size');
		var ts = parseInt(tSize.substring(0, tSize.length - 2));
		var gs = 36;
		
		var tze = setInterval(function() {
			if(ts < gs) {
				$($(blip.root).children('p')[0]).css({
					'padding': '15px 20px 15px 15px',
					'font-size': ts++,
					'line-height': '30px'
				});
			} else {
				clearInterval(tze);
				var opa = parseInt($("#m_sublist").css('opacity'));
				var inc = 0.1;
				tze = setInterval(function() {
					if(opa < 1.0)
						$("#m_sublist").css('opacity', opa += inc);
					else
						clearInterval(tze);
				}, 20);
				
			}
		}, 10);
	}
}

function unZoom() {
	if(isZoomed) {
		if($("#m_sublist")) {
			var opa = parseInt($("#m_sublist").css('opacity'));
			var inc = 0.1;
			var tze = setInterval(function() {
				if(opa > 0)
					$("#m_sublist").css('opacity', opa -= inc);
				else {
					clearInterval(tze);
					tze = setInterval(function() {
						var tSize = $("#m_sublist").prev().css('font-size');
						var ts = parseInt(tSize.substring(0, tSize.length - 2));
												
						if(ts > 13) {
							$("#m_sublist").prev().css({
								'font-size': ts - 1,
								'line-height': "15px",
								'padding': '3px 14px 3px 7px'
							});
						} else {
							clearInterval(tze);
							$("#m_sublist").remove();
						}
						
					}, 10);
				}
			}, 20);
			
		}
		
		$.each(navItems, function() {
			var blip = this;
			$(blip.root).css({
				"display": "block"
			});
		});
		
		isZoomed = false;
	}
}

function displayShowpanel() {
	showpanel.css({
		"display": "block",
		"top": arguments[0].top,
		"left": arguments[0].left
	});
	$("#mh_navigation").css('display','none');
}

function closeShowpanel() {
	showpanel.css('display','none');
	$("#mh_navigation").css('display','block');
}

function setElements() {
	navItems = {
		bio: {
			root: $("#bio"),
			alignment: {
				top: h * 0.415,
				left: w * 0.735
			},
			alias: "Bio",
			page_id: "page-id-7",
			showpanel: {
				top: h * 0.2,
				left: w * 0.2
			},
			init: function() {
				window.location = docRoot + "bio";
			}
		},
		books: {
			root: $("#books"),
			alignment: {
				top: h * 0.18,
				left: w * 0.18
			},
			alias: "Books",
			showpanel: {
				top: h * 0.2,
				left: w * 0.2
			},
			subpages: subpages.books,
			init: function() {
				doZoom(navItems.books);
			}
			
		},
		other_works: {
			root: $("#other_works"),
			alignment: {
				top: h * 0.75,
				left: w * 0.29
			},
			alias: "Other Works",
			showpanel: {
				top: h * 0.2,
				left: w * 0.2
			},
			subpages: subpages.other_works,
			init: function() {
				doZoom(navItems.other_works);
			}
		},
		media: {
			root: $("#media"),
			alignment: {
				top: h * 0.6,
				left: w * 0.54
			},
			alias: "Media",
			showpanel: {
				top: h * 0.2,
				left: w * 0.2
			},
			subpages: subpages.media,
			init: function() {
				doZoom(navItems.media);
			}
		},
		awards_and_fellowships: {
			root: $("#awards_and_fellowships"),
			alignment: {
				top: h * 0.16,
				left: w * 0.77
			},
			alias: "Awards & Fellowships",
			page_id: "page-id-5",
			showpanel: {
				top: h * 0.25,
				left: w * 0.23
			},
			init: function() {
				window.location = docRoot + "awards-fellowships";
			}
		},
		blog: {
			root: $("#blog"),
			alignment: {
				top: h * 0.41,
				left: w * 0.22
			},
			alias: "Blog",
			page_id: "blog",
			showpanel: {
				top: h * 0.2,
				left: w * 0.01,
				width: w * 0.98
			},
			init: function() {
				window.location = docRoot + "blog";
			}
		},
		links: {
			root: $("#links"),
			alignment: {
				top: h * 0.075,
				left: w * 0.47
			},
			alias: "Links",
			page_id: "page-id-12",
			showpanel: {
				top: h * 0.2,
				left: w * 0.013
			},
			init: function() {
				window.location = docRoot + "links";
			}
		},
		contact_info: {
			root: $("#contact_info"),
			alignment: {
				top: h * 0.72,
				left: w * 0.88
			},
			alias: "Contact Info",
			page_id: "page-id-9",
			showpanel: {
				top: h * 0.2,
				left: w * 0.2
			},
			init: function() {
				window.location = docRoot + "contact-info";
			}
		}
		
	};
	
	bodyClasses = $(document.getElementsByTagName('body')[0]).attr('class').split(" ");
	
	if(jQuery.inArray("home", bodyClasses) != -1)
		$("#mh_showpanel").css('display','none');
	else {
		$("#mh_navigation").css('display','none');
	}
	
	
	$.each(navItems,function() {
		setBlips(this);
		
	});
	
	if(jQuery.inArray("single-post", bodyClasses) != -1) {
		$("#mh_showpanel").css({
			'top': navItems.blog.showpanel.top,
			'left': navItems.blog.showpanel.left,
			'width': navItems.blog.showpanel.width
		});
		
		$("#primary").css({
			'margin-left': w * 0.683,
			'width': w * 0.3
		});
			

	} else if(jQuery.inArray("blog", bodyClasses) != -1) {
		$("#primary").css({
			'margin-left': "100%",
			'width': w * 0.3
		});
	} else if(jQuery.inArray("page-child", bodyClasses) != -1) {
		$("#mh_showpanel").css({
			'top': navItems.awards_and_fellowships.showpanel.top,
			'left': navItems.awards_and_fellowships.showpanel.left
		});
		
		if(jQuery.inArray("page-id-1237", bodyClasses) != -1) {
			// i.e. this is the gallery page
			$("#mh_gallery_thumbs").css('height', h * 0.4);
			console.log('gallery!');
		}
		
	} else if(jQuery.inArray("archive", bodyClasses) != -1) {
		$("#mh_showpanel").css({
			'top': navItems.blog.showpanel.top,
			'left': navItems.blog.showpanel.left,
			'width': navItems.blog.showpanel.width
		});
		
		$("#primary").css({
			'margin-left': w * 0.683,
			'width': w * 0.3
		});
	}
	
	if($("#container"))
		$("#container").css('width', w * 0.98);
}

function showGalleryImage() {
	$("#mh_gallery_show").empty();
	$("#mh_gallery_show").append(
		$(document.createElement('div'))
			.css({
				'width': w * 0.45,
				'height': h * 0.45,
				'background-image' : "url('" + $(arguments[0]).attr('src') + "')"
			})
	);
}

$(document).ready(function() {
	$(document.getElementsByTagName('body')[0]).css('width', $(window).width());
	showpanel = $("#mh_showpanel");
	
	w = $(window).width();
	h = $(window).height();
	
	c = $("#mh_background")[0].getContext("2d");
	
	
	
	bg = new Image();
	bg.src = docRoot + "wp-content/themes/matthew_2point0/images/mh_paint.png";
	bg.onload = function() {	
	
		c.drawImage(bg, 0, 0, w, h);
		$("#mh_loading").css('display','none');
	}
	
	$("#wrapper").css({
		"width": w,
		"height": h
	});
	
	$("#mh_background")
		.attr({
			"width": $("#wrapper").width(),
			"height": $("#wrapper").height()
		})
		.click(function() {
			if(isZoomed && !clickOut) {
				unZoom();
			}
		});
	
	showpanel.css({
		"height": h * 0.7
	});
	
	$(showpanel.children()[0]).css({
		"height": h * 0.6
	});
	
	$("#mh_logo").click(function() {
		if(isZoomed) {
			unZoom();
		} else {
			window.location = docRoot;
		}
	});

	scale = 1;
	setElements();

	
});
