$(document).ready(function(){
	$(".popup-state").css( 'display', 'none' );
	$("area").hover(
		function () {
			if(popupStatus==0){
				$("#map-list li[id='" + $(this).attr('id').replace('state_', '') + "']").toggleClass('show');
			}
		},
		function () {
			if(popupStatus==0){
				$("#map-list li[id='" + $(this).attr('id').replace('state_', '') + "']").toggleClass('show');
			}
		}
	);
	$("area").click(
		function () {
			var stateID = $(this).attr('id').replace('state_', '');
			if(popupStatus==0){
				$("#map-list li[id='" + stateID + "']").toggleClass('show');
			}
			centerPopup(stateID);
			loadPopup(stateID);
		}
	);

	$(".popup-close").click(function(){
		disablePopup();
	});

	$("#popup-background").click(function(){
		disablePopup();
	});

	$(document).keypress(
		function(e){
			if(e.keyCode==27 && popupStatus==1){
				disablePopup();
			}
		}
	);
	
	$(".popup-close").hover(
		function(){
			$(".popup-close img").attr("src", "assets/images/global/close-button-hover.png");
		},
		function(){
			$(".popup-close img").attr("src", "assets/images/global/close-button.png");
		}
	).click(function(event){event.preventDefault()});
	
});

var popupStatus = 0;

function loadPopup(stateID){
	if(popupStatus==0){
		$("#popup-background").css({
			"opacity": "0.7"
		});
		$("#popup-background").fadeIn("slow");
		$("#popup_" + stateID).fadeIn("slow");
		popupStatus = 1;
	}
}

function disablePopup(){
	if(popupStatus==1){
		$("#popup-background").fadeOut("slow");
		$(".popup-state").fadeOut("slow");
		popupStatus = 0;
	}
}

function centerPopup(stateID) {
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popup_" + stateID).height();
	var popupWidth = $("#popup_" + stateID).width();
	
	$("#popup_" + stateID).css({
		"position": "absolute",
		"top": windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2
	});
	
	$("#popup-background").css({
		"height": windowHeight
	});
}
