var selector = {speed:500,region:'',animating:''};
var mouse = {offsetX:0,offsetY:0};
var popupTimer = false;
var lighter = {pause:100,evtTimer:'',index:0,fire:["North_America","Central_and_South_America","Iceland_Greenland","Africa","Europe","Middle_East","Russia","Asia","Japan","Southeast_Asia","Australia"]};

mapImages = new Array();
for(var i in regionList) {
	if(regionList[i].mapSource.length > 1){
		var mapImage = new Image();
		mapImage.src = 'design/images/' + regionList[i].mapSource + '.png';
		mapImages.push(mapImage);
	}
}

$(document).ready(function(){
	lighter.evtTimer = setTimeout("nextLightIn();",7500);
});


function nextLightIn() {
	setRegion(lighter.fire[lighter.index]);
	lighter.evtTimer = setTimeout("nextLightOut();",lighter.pause);
}


function nextLightOut() {
	unsetRegion(lighter.fire[lighter.index++]);
	if(lighter.index <= lighter.fire.length) {
		lighter.evtTimer = setTimeout("nextLightIn();",lighter.pause);
	}
}

function setRegion(region) {
/*
	if(selector.animating == 'out' && selector.region == region) {
		setTimeout("setRegion('"+region+"')",selector.speed);
	}
	if($("#popupMenu").css("display") != "none") {
		removePopup();
	}
*/
	if(!document.getElementById(region)) {
		if(region.length > 1){
			newMap = new Image();
			newMap.src = "/design/images/" + region + ".png";
			newMap.alt = "";
			newMap.id = region;
			newMap.style.display = "none";
			$("#Backdrop").append(newMap);
			selector.animating = 'in';
			$(newMap).fadeIn(selector.speed,function(){
				selector.animating = '';
			});
			selector.region = region;
		}
	}
}

function unsetRegion(region) {
	selector.region = region;
	if(document.getElementById(region)) {
		selector.animating = 'out';
		$("#"+region).fadeOut(selector.speed,function(){
			$("#"+region).remove();
			selector.animating = '';
		});
		selector.region = '';
	}
}

function selectRegion() {
	var items = new Array();
	for(var i=0;i<=arguments.length-1;i++) {
		for(var j in regionList) {
			if(arguments[i] == regionList[j].id) {
				items.push('<a href="'+regionList[j].homePage+'" target="'+regionList[j].target+'">'+regionList[j].pagetitle+'</a>');
				break;
			}
		}
	}
	$("#popupMenu").empty();
	$("#popupMenu").append(items.join("\n"));
	var popupWidth = $("#popupMenu").width();
	$("#popupMenu").css({left:(mouse.offsetX-(popupWidth-10)),top:(mouse.offsetY-10),display:"block"});
	$("#popupMenu").mouseleave(function(){
		if(!popupTimer) popupTimer = setTimeout("removePopup()",500);
	});
	$("#popupMenu").mouseenter(function(){
		clearTimeout(popupTimer);
		popupTimer = false;
	});
}

function removePopup() {
	popupTimer = false;
	$("#popupMenu").css("display","none").empty();
}

$(document).mousemove(function(evt){
	mouse.offsetX = evt.pageX;
	mouse.offsetY = evt.pageY;
});

/*$(document).ready(function(evt){
	$("#Stage area").each(function(){
		//alert(this.getAttribute("coords"));
	});
});
*/
