var imageMap = {};
imageMap.interval_delay = 7500;
imageMap.currentImageMapDOM = null;
imageMap.keyboardlock = false;

imageMap.balloons = {};
imageMap.balloons.activeBalloon = new Array();

$(document).ready(function() {
	
	if ($("#CONTENT_IMAGE").attr("delay")!=null) {
		imageMap.interval_delay = $("#CONTENT_IMAGE").attr("delay") * 1;
	}

	/*
	 * Image map balloon popups
	 */
	$(".BALLOON").each(function(index, elm) {
		$(elm).html("<div class='BALLOON_BODY'>" + $(elm).html() + "</div>");
		$(elm).click(function() {
			location.href=$(this).attr("href");
		});
	});
	$(".BALLOON DIV").addClass("BALLOON_ELM");
	$(".BALLOON").prepend("<div class='BALLOON_BACKGROUND'>&nbsp;</div>");
	$(".BALLOON H3").addClass("BALLOON_H3");
	$(".BALLOON P").addClass("BALLOON_P");

	$("area").mouseover(function() {
		clearInterval(imageMap.hideInterval);

		imageMap.showBalloon(this);
		return false; 
	});

	$("area").live("focus", function() {
		imageMap.keyboardlock = true;
		
		$(".CONTENT_IMAGE_NAVIGATION").show();
		imageMap.showBalloon(this);
		imageMap.pause();

		return false; 
	});

	$("area").live("blur", function() {
		imageMap.keyboardlock = false;
		$(".CONTENT_IMAGE_NAVIGATION").hide();
		$(".BALLOON").hide();
		imageMap.run();

		return false; 
	});
 	$(".BALLOON").bind("mouseover", function(e) {
			clearInterval(imageMap.hideInterval);
	});
 	$("area").bind("mouseout", function(e) {
			clearInterval(imageMap.hideInterval);
			imageMap.hideInterval = setInterval(function() {
				clearInterval(imageMap.hideInterval);
				$(balloon).hide();
				delete imageMap.balloons.activeBalloon[balloon];
			}, 100);
	});
 	$(".BALLOON").bind("mouseout", function(e) {
			clearInterval(imageMap.hideInterval);
			imageMap.hideInterval = setInterval(function() {
				clearInterval(imageMap.hideInterval);
				$(balloon).hide();
				delete imageMap.balloons.activeBalloon[balloon];
			}, 100);
		for (balloon in imageMap.balloons.activeBalloon) {
			if(e.target.className.indexOf('BALLOON_')==-1&&e.target.tagName!='AREA') {
				$(balloon).hide();
				delete imageMap.balloons.activeBalloon[balloon];
			}
		}
	});

	$(".IMAGE_MAP").bind("mousemove", function () {
		$(".CONTENT_IMAGE_NAVIGATION").fadeIn("fast");
		imageMap.pause();
	});

	$(".IMAGE_MAP").mousedown(function () {
		return false;
	});

	$(".IMAGE_MAP").bind("selectstart", function () {
		return false;
	});

	$(document).bind("mousemove", function (e) {
		if (imageMap.keyboardlock) return;
		if ($(e.target).closest("#CONTENT_IMAGE").length==0) {
			if(e.target.className.indexOf('BALLOON_')==-1&&e.target.tagName!='AREA') {
				$(".CONTENT_IMAGE_NAVIGATION").fadeOut("fast");
				for (balloon in imageMap.balloons.activeBalloon) {
					$(balloon).hide();
					delete imageMap.balloons.activeBalloon[balloon];
				}
			}

			imageMap.run(e);
		}
	});

	imageMap.imageMaps = $(".IMAGE_MAP").length;
	//$("#IMAGE_MAP_INDEX")

	for (var i=0;i<imageMap.imageMaps;i++) {
		$("#IMAGE_MAP_INDEX").append("<span class='MAP_INDEX_BUTTON MAP_INDEX_BUTTON_" + (i+1) + "' mapId='"+(i+1)+"' style=''>&nbsp;</span>");
		$(".MAP_INDEX_BUTTON_" + (i+1)).click(function () {
			imageMap.setMap($(this).attr("mapId"));
		});
		$(".MAP_INDEX_BUTTON_" + (i+1)).keypress(function (e) {
			if (e.keyCode==13) {
				window.scrollTo(0, 0);
				imageMap.setMap($(this).attr("mapId"));
			}
		});
		$(".MAP_INDEX_BUTTON_" + (i+1)).mouseover(function () {
			$(this).attr("bg", $(this).css("background-color"));
			$(this).css("background-color", "#777");
		});
		$(".MAP_INDEX_BUTTON_" + (i+1)).mouseout(function () {
			$(this).css("background-color", $(this).attr("bg"));
		});
	}

	$("#CONTENT_IMAGE").prepend("<div class='CONTENT_IMAGE_NAVIGATION_LEFT CONTENT_IMAGE_NAVIGATION' onselectstart='return false;' onmousedown='return false;'><div class='CONTENT_IMAGE_NAVIGATION_ARROW'>&#9668;</div></div>");
	$("#CONTENT_IMAGE").prepend("<div class='CONTENT_IMAGE_NAVIGATION_RIGHT CONTENT_IMAGE_NAVIGATION' onselectstart='return false;' onmousedown='return false;'><div class='CONTENT_IMAGE_NAVIGATION_ARROW'>&#9658;</div></div>");

	$(".CONTENT_IMAGE_NAVIGATION_LEFT").click(function () {
		imageMap.setMap(imageMap.currentImageMap-1);
	});
	$(".CONTENT_IMAGE_NAVIGATION_RIGHT").click(function () {
		imageMap.setMap(imageMap.currentImageMap+1);
	});
	$(".CONTENT_IMAGE_NAVIGATION_LEFT").mouseover(function () {
		$(this).css("background-color", "black");
	});
	$(".CONTENT_IMAGE_NAVIGATION_LEFT").mouseout(function () {
		$(this).css("background-color", "");
	});
	$(".CONTENT_IMAGE_NAVIGATION_RIGHT").mouseover(function () {
		$(this).css("background-color", "black");
	});
	$(".CONTENT_IMAGE_NAVIGATION_RIGHT").mouseout(function () {
		$(this).css("background-color", "");
	});
	var isSet = false;
	$(".IMAGE_MAP").each(function(index, elm) {
		if ($(elm).attr("default")=="true"&&!isSet) {
			imageMap.setMap(index + 1, true);
			isSet = true;
		}
	});
	if ($(".IMAGE_MAP").length>0&&!isSet) {
		imageMap.setMap(1, true);
	}
});

imageMap.showBalloon = function (balloonArea) {
//	var mapId = $(balloonArea).closest("map").attr("name");

	var imageMapDOM = $(balloonArea).closest(".IMAGE_MAP");
	var imageDOM = $($(imageMapDOM).find(".IMAGE"));
	var balloonDOM = imageMapDOM.find($(balloonArea).attr("use"));
	if (balloonDOM.is(":visible")) return;
	
	if (this.balloons.activeBalloon[$(balloonArea).attr("use")]!=null) {
		return false;
	}
	$(".BALLOON").hide();
	for (balloon in this.balloons.activeBalloon) {
//		$(balloon).hide();
		delete this.balloons.activeBalloon[balloon];
	}
	
	//var url = $(balloonArea).attr('href');
	var leftOffset = 0; //($("#HOME_IMAGE_" + mapId).offset().left);
	var topOffset = 0; //($("#HOME_IMAGE_" + mapId).offset().top);

	leftOffset = imageDOM.offset().left;
	topOffset = imageDOM.offset().top;
	
	var coords = $(balloonArea).attr('coords').split(','); 
	balloonDOM.attr("href", balloonArea.href);

//	$(balloonArea).css("background-color", "red");
	leftOffset += (1*coords[0]);
//alert(balloonDOM.width());
	if ((leftOffset +  balloonDOM.width()) >= imageDOM.offset().left + imageDOM.width()) {
		leftOffset = imageDOM.offset().left + imageDOM.width() - (balloonDOM.width() + 40);
	}

	if ((leftOffset) <= imageDOM.offset().left + 20) {
		leftOffset = imageDOM.offset().left + 20;
	}

	if (leftOffset>$(".CONTENT_IMAGE_NAVIGATION_RIGHT").offset().left) {
//alert($(".CONTENT_IMAGE_NAVIGATION_RIGHT").offset().left); 
//		leftOffset = ($(".CONTENT_IMAGE_NAVIGATION_RIGHT").offset().left - (balloonDOM.width() + $(".CONTENT_IMAGE_NAVIGATION_RIGHT").width()));
	}

	balloonDOM.fadeIn("fast");

	balloonDOM.css("left", (leftOffset));
	balloonDOM.css("top", (1*coords[1]) + topOffset);
	this.balloons.activeBalloon[$(balloonArea).attr("use")] = balloonDOM;

};

imageMap.resetDisplay = function() {
	setTimeout(function() {
		imageMap.setDisplay();
	}, 10);

	clearInterval(imageMap.displayInterval);
	imageMap.displayInterval = setInterval(function() {imageMap.setDisplay();}, 200);
	clearInterval(imageMap.displayIntervalClear);
	imageMap.displayIntervalClear = setInterval(function() {clearInterval(imageMap.displayInterval);clearInterval(imageMap.displayIntervalClear);}, 2000);
	$(window).resize(function() {
		setTimeout(function() {
			imageMap.setDisplay();
		}, 10);
	});
};

imageMap.setDisplay = function() {
		var imageDom = $($(".IMAGE_MAP .IMAGE")[this.currentImageMap-1]);
		if (imageDom.offset().left==0) return;

		$(".CONTENT_IMAGE_NAVIGATION").css("top", $("#CONTENT_IMAGE").offset().top);
		$(".CONTENT_IMAGE_NAVIGATION").css("height", imageDom.height());
		$(".CONTENT_IMAGE_NAVIGATION_LEFT").css("left", imageDom.offset().left);
		$(".CONTENT_IMAGE_NAVIGATION_RIGHT").css("left", imageDom.offset().left + imageDom.width() - $(".CONTENT_IMAGE_NAVIGATION_RIGHT").width());
		$(".CONTENT_IMAGE_NAVIGATION_ARROW").css("padding-top", (imageDom.height() / 2) - 20);

};
imageMap.lastIndex = -2;
imageMap.lastHeight = 0;
imageMap.lastWidth = 0;

imageMap.setMap = function(idx, cancelEffect, e) {
	
	clearInterval(this.interval);
	this.running = false;

	if (idx==null) {
		idx = this.currentImageMap;
	}
	if (idx<=0) {
		idx = this.imageMaps;
	}

	$(".BALLOON").hide();
	if (e!=null) {
		for (balloon in this.balloons.activeBalloon) {
			if(e.target.className.indexOf('BALLOON_')==-1&&e.target.tagName!='AREA') {
//				$(balloon).hide();
				
				delete this.balloons.activeBalloon[balloon]; //TODO: added this back in, research if probs: 9/1
			}
		}
	}
	
	if (idx>this.imageMaps) {
		idx = 1;
	}
	
	$(".MAP_INDEX_BUTTON").attr("tabindex", 0);
	$(".MAP_INDEX_BUTTON").css("background-color", "#999");
	$(".MAP_INDEX_BUTTON_" + idx).css("background-color", "#ccc");
	$(".MAP_INDEX_BUTTON_" + idx).attr("bg", $(".MAP_INDEX_BUTTON_" + idx).css("background-color"));

	this.currentImageMap = idx;

	$(".IMAGE_MAP").hide();
	$(".IMAGE_MAP").css("position", "static");
//	if ($(".CONTENT_IMAGE_CONTAINER").offset()!=null) {
		$(".IMAGE_MAP").css("left", "0");
//	}
	$(".IMAGE_MAP").css("top", "0");
//	$($(".IMAGE_MAP")[idx-1]).css("position", "relative");
	$($(".IMAGE_MAP")[idx-1]).css("z-index", "99");

	if (cancelEffect&&cancelEffect!=null) {
		$($(".IMAGE_MAP")[idx-1]).toggle();
	} else {
		$($(".IMAGE_MAP")[idx-1]).show();
		if ($(".IMAGE_MAP").height()!=0) {
			imageMap.lastHeight = $(".IMAGE_MAP").height();
		}
		if ($(".IMAGE_MAP").width()!=0) {
			imageMap.lastWidth = $(".IMAGE_MAP").width();
		}
		$($(".IMAGE_MAP")[idx-1]).closest(".CONTENT_IMAGE_CONTAINER").css("width", imageMap.lastWidth);
		$($(".IMAGE_MAP")[idx-1]).closest(".CONTENT_IMAGE_CONTAINER").css("height", imageMap.lastHeight);
		$($(".IMAGE_MAP IMG")[idx-1]).hide();
		$($(".IMAGE_MAP IMG")[idx-1]).fadeIn(1000);
		if (imageMap.lastIndex!=-2) {
			$($(".IMAGE_MAP")[imageMap.lastIndex ]).css("position", "absolute");
			$($(".IMAGE_MAP")[imageMap.lastIndex ]).css("z-index", "999");
			$($(".IMAGE_MAP")[imageMap.lastIndex ]).show();

			$($(".IMAGE_MAP")[imageMap.lastIndex ]).css("top", $("#CONTENT_IMAGE").offset().top);
//			if ($(".CONTENT_IMAGE_CONTAINER").offset()!=null) {
				$($(".IMAGE_MAP")[imageMap.lastIndex ]).css("left", $("#CONTENT_IMAGE").offset().left + 20);
//			}
//			$($(".IMAGE_MAP")[imageMap.lastIndex ]).css("left", $($(".IMAGE_MAP IMG")[imageMap.lastIndex]).offset().left);
//			$($(".IMAGE_MAP IMG")[imageMap.lastIndex ]).hide();
			$($(".IMAGE_MAP IMG")[imageMap.lastIndex ]).fadeOut(1000);

//			$($(".IMAGE_MAP")[imageMap.lastIndex ]).closest(".CONTENT_IMAGE_CONTAINER").css("width", $($(".IMAGE_MAP")[imageMap.lastIndex ]).width());
//			$($(".IMAGE_MAP")[imageMap.lastIndex ]).closest(".CONTENT_IMAGE_CONTAINER").css("height", $($(".IMAGE_MAP")[imageMap.lastIndex ]).height());
//			$($(".IMAGE_MAP")[imageMap.lastIndex ]).hide();

		}

	}
	$(".CONTENT_IMAGE_NAVIGATION").attr("tabindex", 0);
	imageMap.lastIndex = idx -1;
	this.run();
	this.resetDisplay();
};

imageMap.run = function (e) {
	if (this.running) return;
	this.running = true;
	clearInterval(this.interval);
	this.interval = setInterval(function() {
		imageMap.setMap(imageMap.currentImageMap + 1, null, e);

	}, this.interval_delay );
};

imageMap.pause = function () {
	this.running = false;
	clearInterval(this.interval);
};


