Shadowbox.init({
	onFinish: function(){
		addPopupToLocation(Shadowbox.current);
	},
	onClose: function(){
		removePopupFromLocation();
	},
	overlayOpacity: 0.85,
	animate: true,
	flashParams: {scale: "noscale", bgcolor: "#000000", wmode: "opaque"}
}, initShadowBox);

function initShadowBox(){
	// var anchor = getPopupAnchor();
	// if(anchor != null){
	// 	setTimeout(function(){showPopupWithStartIndex(anchor);}, 20);
	// }
	
}

function getPopupAnchor(){
	var anchor = document.location.href.match(/^.*#popup(\d)/);
	if(anchor != null && anchor.length == 2){
		return anchor[1];
	}else{
		return null;
	}
}

function showPopupWithStartIndex(indx){
	var ob = $("a[id='popup["+indx+"]']:first");
	if(ob.length > 0){
		try{
			Shadowbox.open(ob.get(0));
		}catch(e){}
	}
}

function addPopupToLocation(indx){
	var anchor = document.location.href.match(/^(.*)#(.*)/);
	if(anchor != null){
		document.location.href = anchor[1]+"#popup"+indx;
	}else{
		document.location.href = document.location.href+"#popup"+indx;
	}
}

function removePopupFromLocation(){
	var anchor = document.location.href.match(/^(.*)#(.*)/);
	if(anchor != null){
		// Adding a hash to prevent page reload
		document.location.href = anchor[1]+"#";
	}
}

function updateLogoSize(){
	$(".logo-image").width($(window).width()-$(".logo-image").position().left);
}

function updateMenuSize(){
	var left = Math.max(Math.min(($(window).width() - $(".menu").width()),650), 60);
	$(".menu").css({left: left+"px", width: Math.max(400, $(window).width()-left-20)});
}

function navArrowRollOver(){
	$(".nav_arrow").bind("mouseover", function(e){
		var src = $(this).attr("src").replace("_over", "");
		var srcParts = src.split(".");
		$(this).attr("src",srcParts[0]+"_over."+srcParts[1]);
	});
	$(".nav_arrow").bind("mouseout", function(e){
		$(this).attr("src",$(this).attr("src").replace("_over",""));
	});
}

$(window).hashchange( function(){
	var anchor = getPopupAnchor();
	if(anchor != null){
		setTimeout(function(){
			var ob = $("a[id='popup["+anchor+"]']:first");
			if(ob.length > 0){
				try{
					if(Shadowbox.isOpen()){
						gallery = Shadowbox.gallery;
						$.each(gallery, function(i, item){
							if(item.content == ob.get(0) && Shadowbox.current != i){
								Shadowbox.change(i);
								return false;
							}
						});
					}else{
						Shadowbox.open(ob.get(0));
					}
				}catch(e){}
			}
		}, 20);
	}else{
		setTimeout(function(){
			try{
				if(Shadowbox.isOpen()){
					Shadowbox.close();
				}
			}catch(e){}
		}, 20);
	}
});

$(function(){
	updateLogoSize();
	$(window).bind("resize", updateLogoSize);
	updateMenuSize();
	$(window).bind("resize", updateMenuSize);
	navArrowRollOver();
	$(window).hashchange();
});

