//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var mapPopupStatus = 0;

//loading popup with jQuery magic!
function loadMap(){
	//loads popup only if it is disabled
	if(mapPopupStatus==0){
		$("#backgroundPopup").css({ "opacity": "0.5"});
		$("#backgroundPopup").fadeIn("slow");
		$("#mapPopup").fadeIn("slow");
		mapPopupStatus = 1;
	}
}

function disableMapPopup(){
	if(mapPopupStatus==1) {
		$("#backgroundPopup").fadeOut("slow");
		$("#mapPopup").fadeOut("slow");
		mapPopupStatus = 0;
	}
}


//centering popup
function centerMap(){
	//request data for centering
	/*
	var windowWidth = $(window).width();
	var windowHeight = $(window).height();
	var popupHeight = $("#mapPopup").height();
	var popupWidth = $("#mapPopup").width();
	//centering
	$("#mapPopup").css({
		"position": "absolute",
		"top": (windowHeight/2-popupHeight/2) + $(window).scrollTop(),
		"left": windowWidth/2-popupWidth/2
	});
	*/
	
	//Instead of centering it, I'm placing it X number of pixels below the top of the page
	var popupWidth = $("#mapPopup").width();
	var windowWidth = $(window).width();
	var pixelsFromTop = -500;
	$("#mapPopup").css({
		"position": "absolute",
		"top": pixelsFromTop
		//"left": windowWidth/2-popupWidth/2
	});
	
	//only need force for IE6
	var windowHeight = $(window).height();
	$("#backgroundPopup").css({
		"height": windowHeight
	});
	
}

function popupMap(url) {
	loadMap();	
	centerMap();
}

jQuery(document).ready(function() {
    var map = new FusionMaps("/ext/infosoft/fusionmaps/Maps/FCMap_USA.swf", "map", 800, 550, "0", "0");
    map.setDataURL("/candidates/map_data.xml");
    map.render("FooterMapContainer");
});

//CONTROLLING EVENTS IN jQuery
$(document).ready(function(){
					
	//CLOSING POPUP
	//Click the x event!
	$("#closePopup").click(function(){
		disableMapPopup();
	});
	//Click out event!
	$("#backgroundPopup").click(function(){
		disableMapPopup();
	});
	
	//Press Escape event!
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disableMapPopup();
		}
	});

});
