$(document).ready(function() {

	resize_gallery_buttons();

	$("#sitemap .nav li a").click(function(event) {
		if ( $(this).attr('id') == '#home')
			return true;
		else {
			event.preventDefault();
			$("#sitemap .nav li.active").removeClass('active');
			$(this).parent().addClass('active');
			$('#sitemap .tier.active').removeClass('active');
			$($(this).attr('id') + '-container').addClass('active');
		}
	});
	
	$("a.btn-sitemap").click(function(event) {
		event.preventDefault();
		$("#sitemap").css('left', (($(window).width() / 2) - ($("#sitemap").width() / 2)));
		$(".mask").fadeIn(function() {
			$("#sitemap").fadeIn();
		});
	});
	
	$(".mask, #close").click(function() {
		if ($(this).css('display') != 'none') {
			$("#sitemap").fadeOut(function() {
				$(".mask").fadeOut("slow");
			});
		}
	});
	
});

function resize_gallery_buttons() {
	// fix gallery buttons positioning	
	var el_gallery = $("div.gallery");
	if ( el_gallery.length != 0 ) {
		var gallery_left = el_gallery.position().left - 32;
		var gallery_right = el_gallery.position().left + el_gallery.width() + 20;
		$("a.link-prev").css('left', gallery_left + 'px');
		$("a.link-next").css('left', gallery_right + 'px');
	}
}

$(window).resize(function() {
	resize_gallery_buttons();
});

/** Hide sitemap if escape button is pressed and released */
$(document).keyup(function(event) {
	if (event.keyCode == 27) {
		$("#sitemap").fadeOut(function() {
			$(".mask").fadeOut("slow");
		});
	}
});

function initPage() {
	initButtons();
}
function initButtons()
{
	var buttons = document.getElementsByTagName("a");
	for(var i = 0; i < buttons.length; i++) {
		if(buttons[i].className.indexOf("button") != -1) {
			buttons[i].onmousedown = function() {
				if(this.className.indexOf("pressed") == -1) this.className += " pressed";
			}
			buttons[i].onmouseup = function() {
				this.className = this.className.replace("pressed","");
			}
			buttons[i].onmouseout = function() {
				this.className = this.className.replace("pressed","");
			} 
		}
	}
}
if (window.addEventListener)
	window.addEventListener("load", initPage, false);
else if (window.attachEvent)
	window.attachEvent("onload", initPage)
	
