var myFile = document.location.toString();
var toahAnchor2 = "";
var timePeriodCurrent = "06";
var zoomLevel = 0;
var whatsVisible = "Main";
var scriptDelay = "ok";
var fadeInDelay;
var pageLoaded = "no";
var current_hash = window.location.hash;
// Make sure page is correct (back button)
hashCheck = setInterval( "check_hash()", 200 );

function check_hash() {
    if ( window.location.hash != current_hash ) {
        current_hash = window.location.hash;
		resetTimePlace();
		if (toahAnchor2 == "") {
			toahAnchor2 = "World-Map";
		}
		
		$(".timebar"+timePeriodCurrent).addClass("activeTime");
		$(".timeList"+timePeriodCurrent).addClass("activeTime");
		runTimePlace(timePeriodCurrent,toahAnchor2);
    }
}

// North America CLICKED
function runNorthAmerica() {
	window.location = "#/" + timePeriodCurrent + "/North-America";
	current_hash = window.location.hash;
	redrawTitles();
	$('#blankMap').attr({src : "/toah/art/map/northamericamap.png"});
	$(".containerGenTL").fadeOut(700);
	$(".containerGen").fadeOut(700);
	$("#worldMap").animate({ 
		width: "200%",
		marginLeft:"-80px",
		marginTop:"-70px"
	}, 400 );
	$("#worldMap").fadeOut(300);
	$("#subRegionText").load("/toah/content/ht/maps/map_"+timePeriodCurrent+"_na.htm");
	$(".subLevel").animate({
		marginRight:"0px"
		},1000 );
	zoomLevel = 1;
	$("#wmIconsMain").attr("innerHTML","");
	$("#wmIconsMain").hide();
}

// Central North Asia CLICKED
function runNorthAsia() {
	window.location = "#/" + timePeriodCurrent + "/Central-North-Asia";
	current_hash = window.location.hash;
	redrawTitles();
	$('#blankMap').attr({src : "/toah/art/map/centralnorthasiamap.png"});
	$(".containerGenTL").fadeOut(700);
	$(".containerGen").fadeOut(700);
	$("#worldMap").animate({ 
		width: "280%",
		marginLeft:"-1570px",
		marginTop:"-135px"
	}, 400 );
	$("#worldMap").fadeOut(300);
	$("#subRegionText").load("/toah/content/ht/maps/map_"+timePeriodCurrent+"_nc.htm");
	$(".subLevel").animate({
		marginRight:"0px"
		},1000 );
	zoomLevel = 1;
	$("#wmIconsMain").attr("innerHTML","");
	$("#wmIconsMain").hide();
}

// Oceania CLICKED
function runOceania() {
	window.location = "#/" + timePeriodCurrent + "/Oceania";
	current_hash = window.location.hash;
	redrawTitles();
	$('#blankMap').attr({src : "/toah/art/map/oceaniamap.png"});
	$(".containerGenTL").fadeOut(300);
	$(".containerGen").fadeOut(300);
	$("#worldMap").animate({ 
		width: "290%",
		marginLeft:"-2070px",
		marginTop:"-820px"
	}, 400 );
	$("#worldMap").fadeOut(300);
	$("#subRegionText").load("/toah/content/ht/maps/map_"+timePeriodCurrent+"_oc.htm");
	$(".subLevel").animate({
		marginRight:"0px"
		},1000 );
	zoomLevel = 1;
	$("#wmIconsMain").attr("innerHTML","");
	$("#wmIconsMain").hide();
}

// Europe CLICKED
function runEurope() {
	window.location = "#/" + timePeriodCurrent + "/Europe";
	current_hash = window.location.hash;
	redrawTitles();
	$('#blankMap').attr({src : "/toah/art/map/europemap.png"});
	$("#subRegionText").load("/toah/content/ht/maps/map_"+timePeriodCurrent+"_eu.htm");
	$(".containerGenTL").fadeOut(300);
	$(".containerGen").fadeOut(300);
	$("#worldMap").animate({ 
		width: "485%",
		marginLeft:"-2011px",
		marginTop:"-600px"
	}, 500 );
	$("#worldMap").fadeOut(300);
	$(".subLevel").animate({
		marginRight:"0px"
		},1000 );
	zoomLevel = 1;
	$("#wmIconsMain").attr("innerHTML","");
	$("#wmIconsMain").hide();
}

// Africa CLICKED
function runAfrica() {
	window.location = "#/" + timePeriodCurrent + "/Africa";
	current_hash = window.location.hash;
	redrawTitles();
	$('#blankMap').attr({src : "/toah/art/map/africamap.png"});
	$(".containerGenTL").fadeOut(700);
	$(".containerGen").fadeOut(700);
	$("#worldMap").animate({ 
		width: "244%",
		marginLeft:"-955px",
		marginTop:"-495px"
	}, 400 );
	$("#worldMap").fadeOut(300);
	$("#subRegionText").load("/toah/content/ht/maps/map_"+timePeriodCurrent+"_af.htm");
	$(".subLevel").animate({
		marginRight:"0px"
		},1000 );
	zoomLevel = 1;
	$("#wmIconsMain").attr("innerHTML","");
	$("#wmIconsMain").hide();
}

// South America CLICKED
function runSouthAmerica() {
	window.location = "#/" + timePeriodCurrent + "/South-America";
	current_hash = window.location.hash;
	redrawTitles();
	$('#blankMap').attr({src : "/toah/art/map/southamericamap.png"});
	$(".containerGenTL").fadeOut(700);
	$(".containerGen").fadeOut(700);
	$("#worldMap").animate({ 
		width: "244%",
		marginLeft:"-470px",
		marginTop:"-655px"
	}, 400 );
	$("#worldMap").fadeOut(300);
	$("#subRegionText").load("/toah/content/ht/maps/map_"+timePeriodCurrent+"_sa.htm");
	$(".subLevel").animate({
		marginRight:"0px"
		},1000 );
	zoomLevel = 1;
	$("#wmIconsMain").attr("innerHTML","");
	$("#wmIconsMain").hide();
}

// Central America CLICKED
function runCentralAmerica() {
	window.location = "#/" + timePeriodCurrent + "/Central-America";
	current_hash = window.location.hash;
	redrawTitles();
	$('#blankMap').attr({src : "/toah/art/map/centralamericamap.png"});
	$(".containerGenTL").fadeOut(700);
	$(".containerGen").fadeOut(700);
	$("#worldMap").animate({ 
		width: "484%",
		marginLeft:"-728px",
		marginTop:"-1033px"
	}, 500 );
	$("#worldMap").fadeOut(300);
	$("#subRegionText").load("/toah/content/ht/maps/map_"+timePeriodCurrent+"_ca.htm");
	$(".subLevel").animate({
		marginRight:"0px"
		},1000 );
	zoomLevel = 1;
	$("#wmIconsMain").attr("innerHTML","");
	$("#wmIconsMain").hide();
}

// West Asia CLICKED
function runWestAsia() {
	window.location = "#/" + timePeriodCurrent + "/West-Asia";
	current_hash = window.location.hash;
	redrawTitles();
	$('#blankMap').attr({src : "/toah/art/map/westasiamap.png"});
	$(".containerGenTL").fadeOut(700);
	$(".containerGen").fadeOut(700);
	$("#worldMap").animate({ 
		width: "484%",
		marginLeft:"-2458px",
		marginTop:"-908px"
	}, 500 );
	$("#worldMap").fadeOut(300);
	$("#subRegionText").load("/toah/content/ht/maps/map_"+timePeriodCurrent+"_wa.htm");
	$(".subLevel").animate({
		marginRight:"0px"
		},1000 );
	zoomLevel = 1;
	$("#wmIconsMain").attr("innerHTML","");
	$("#wmIconsMain").hide();
}

// South and South East Asia CLICKED
function runSouthAsia() {
	window.location = "#/" + timePeriodCurrent + "/South-Southeast-Asia";
	current_hash = window.location.hash;
	redrawTitles();
	$('#blankMap').attr({src : "/toah/art/map/southeastasiamap.png"});
	$(".containerGenTL").fadeOut(300);
	$(".containerGen").fadeOut(300);
	$("#worldMap").animate({ 
		width: "395%",
		marginLeft:"-2400px",
		marginTop:"-825px"
	}, 500 );
	$("#worldMap").fadeOut(300);
	$("#subRegionText").load("/toah/content/ht/maps/map_"+timePeriodCurrent+"_ss.htm");
	$(".subLevel").animate({
		marginRight:"0px"
		},1000 );
	zoomLevel = 1;
	$("#wmIconsMain").attr("innerHTML","");
	$("#wmIconsMain").hide();
}

// East Asia CLICKED
function runEastAsia() {
	window.location = "#/" + timePeriodCurrent + "/East-Asia";
	current_hash = window.location.hash;
	redrawTitles();
	$('#blankMap').attr({src : "/toah/art/map/eastasiamap.png"});
	$(".containerGenTL").fadeOut(300);
	$(".containerGen").fadeOut(300);
	$("#worldMap").animate({ 
		width: "395%",
		marginLeft:"-2640px",
		marginTop:"-673px"
	}, 500 );
	$("#worldMap").fadeOut(300);
	$("#subRegionText").load("/toah/content/ht/maps/map_"+timePeriodCurrent+"_ea.htm");
	$(".subLevel").animate({
		marginRight:"0px"
		},1000 );
	zoomLevel = 1;
	$("#wmIconsMain").attr("innerHTML","");
	$("#wmIconsMain").hide();
}

// World Map
function runWorldMap() {
	window.location = "#/" + timePeriodCurrent + "/World-Map";
	current_hash = window.location.hash;
	redrawTitles();
	$(".containerGenRegion").fadeOut(300);
	$(".subLevel").animate({
		marginRight:"-350px"
		},300 , 
		function() {
			$("#worldMap").animate({ 
				width: "100%",
				left:"0px",
				top:"0px",
				marginLeft:"0px",
				marginTop:"0px"
			}, 500, function() {
				//$("#wmIconsMain").show();
			})
		})
	setTimeout('$("#wmIconsMain").load("/toah/content/ht/maps/map_"+timePeriodCurrent+".htm",$("#wmIconsMain").show());',500);
	zoomLevel = 0;
}


$(document).ready(function(){
	$(".na-container").live("click",function() { runNorthAmerica(); });
	$(".nc-container").live("click",function() { runNorthAsia(); });
	$(".oc-container").live("click",function() { runOceania(); });
	$(".eu-container").live("click",function() { runEurope(); });
	$(".nf-container").live("click",function() { runAfrica(); });
	$(".af-container").live("click",function() { runAfrica(); });
	$(".sf-container").live("click",function() { runAfrica(); });
	$(".sa-container").live("click",function() { runSouthAmerica(); });
	$(".ca-container").live("click",function() { runCentralAmerica(); });
	$(".wa-container").live("click",function() { runWestAsia(); });
	$(".ss-container").live("click",function() { runSouthAsia(); });
	$(".ea-container").live("click",function() { runEastAsia(); });
	$(".regionInfo h5").live("click",function() { runWorldMap(); });
	$(".regionInfo .mapSidebar").live("click",function() { runWorldMap(); });
	$(".regionInfo h7").live("click",function() { runWorldMap(); });



	// ####### TIME BAR SPECIFIC ###### //
		$(".timeBarItem").click(function() {
			resetTimePlace();
			if ($(this).attr("rel") != "") {
				timePeriodCurrent = $(this).attr("rel");
				$(".timeBarItem").removeClass("activeTime");
				$(".timeBarItem2").removeClass("activeTime");
				$(".timebar"+timePeriodCurrent).addClass("activeTime");
				$(".timeList"+timePeriodCurrent).addClass("activeTime");
			}
			//runTimePlace(timePeriodCurrent,toahAnchor2);
			window.location = "#/" + timePeriodCurrent + "/"+ toahAnchor2;
		});
	
		$(".timeBarItem").hover(
			function () {
				$(this).addClass("hoverTime");
			},
			function () {
				$(this).removeClass("hoverTime");
			}
		)
	// ####### END OF TIME BAR ###### //
		
		
	




	// Regional Map CLICKED
	$(".containerGenRegion").live("click",function() {
		var destURL = $(this).attr("rel");
		window.location.href = destURL;
		return false;
	});
	
	resetTimePlace();
	if (toahAnchor2 == "") {
		toahAnchor2 = "World-Map";
	}
	
	$(".timebar"+timePeriodCurrent).addClass("activeTime");
	$(".timeList"+timePeriodCurrent).addClass("activeTime");
	
	firstRun();
});


function firstRun() {
	if (pageLoaded != "yes") {
		pageLoaded = "yes";
		runTimePlace(timePeriodCurrent,toahAnchor2);
	}
}


// ### RESET ### //
function resetTimePlace() {
	myFile = document.location.toString();
	toahAnchor = "";
	timePeriodCurrent = "06";

	if (myFile.match('#')) { // the URL contains an anchor
	  var toahAnchor = '#' + myFile.split('#')[1];
		if (myFile.match('/')) { // the URL contains an anchor
			toahAnchor1 = toahAnchor.split('/')[1];
			toahAnchor2 = toahAnchor.split('/')[2];
			//alert(toahAnchor2);
			//alert(toahAnchor1 + ', ' + toahAnchor2);
			if (toahAnchor1 == "02" || toahAnchor1 == "03" || toahAnchor1 == "04" || toahAnchor1 == "05" || toahAnchor1 == "06" || toahAnchor1 == "07" || toahAnchor1 == "08" || toahAnchor1 == "09" || toahAnchor1 == "10" || toahAnchor1 == "11") {
				timePeriodCurrent = toahAnchor1;			
			} else {
				timePeriodCurrent = "06";
			}
		}
	}
}

var currentRegion = "";

function runTimePlace(timePeriodCurrent,toahAnchor2) {
	clearInterval(hashCheck);
	hashCheck = setInterval( "check_hash()", 200 );
	$(".timeBarItem").removeClass("activeTime");
	$(".timeBarItem2").removeClass("activeTime");
	$(".timebar"+timePeriodCurrent).addClass("activeTime");
	$(".timeList"+timePeriodCurrent).addClass("activeTime");

	redrawTitles();
	if (toahAnchor2 != "") {
		if (toahAnchor2 == "Europe") { runEurope(); }
		if (toahAnchor2 == "Africa") { runAfrica(); }
		if (toahAnchor2 == "South-America") { runSouthAmerica(); }
		if (toahAnchor2 == "North-America") { runNorthAmerica(); }
		if (toahAnchor2 == "Central-North-Asia") { runNorthAsia(); }
		if (toahAnchor2 == "Oceania") { runOceania(); }
		if (toahAnchor2 == "Central-America") { runCentralAmerica(); }
		if (toahAnchor2 == "West-Asia") { runWestAsia(); }
		if (toahAnchor2 == "South-Southeast-Asia") { runSouthAsia(); }
		if (toahAnchor2 == "East-Asia") { runEastAsia(); }
		if (toahAnchor2 == "World-Map") { runWorldMap(); }
	}
	current_hash = window.location.hash;
}


function getTimeText(timePeriodCurrent,location) {
	var tempVar = ""
	if (timePeriodCurrent == "02") { tempVar = "8000-2000 B.C."; }
	if (timePeriodCurrent == "03") { tempVar = "2000-1000 B.C."; }
	if (timePeriodCurrent == "04") { tempVar = "1000 B.C.-1 A.D."; }
	if (timePeriodCurrent == "05") { tempVar = "1-500 A.D."; }
	if (timePeriodCurrent == "06") { tempVar = "500-1000 A.D."; }
	if (timePeriodCurrent == "07") { tempVar = "1000-1400 A.D."; }
	if (timePeriodCurrent == "08") { tempVar = "1400-1600 A.D."; }
	if (timePeriodCurrent == "09") { tempVar = "1600-1800 A.D."; }
	if (timePeriodCurrent == "10") { tempVar = "1800-1900 A.D."; }
	if (timePeriodCurrent == "11") { tempVar = "1900 A.D.-Present"; }
	if (location != "pageTitle") {
		tempVar = tempVar.replace("-","&ndash;");
		tempVar = tempVar.replace(" A.D."," <span class=\"smallCap\">a.d.</span>");
		tempVar = tempVar.replace(" B.C."," <span class=\"smallCap\">b.c.</span>");
	}
	return tempVar;
}

function redrawTitles() {
	resetTimePlace();
	currentRegion = setFullRegion(toahAnchor2);
	$(".timelineTitle").attr("innerHTML",currentRegion+", "+getTimeText(timePeriodCurrent));
	setPageTitle(currentRegion+", "+getTimeText(timePeriodCurrent,"pageTitle") + " | Heilbrunn Timeline of Art History | The Metropolitan Museum of Art");
}
function setFullRegion(urlText) {
	var regionFull = urlText.replace("-"," ");
	regionFull = regionFull.replace("-"," ");
	regionFull = regionFull.replace("-"," ");
	regionFull = regionFull.replace("South Southeast","South and Southeast");
	regionFull = regionFull.replace("North Asia","Central and North Asia");
	regionFull = regionFull.replace("Central Central","Central");
	regionFull = regionFull.replace("World Map","World");
	return regionFull
}

function setPageTitle(newTitle) {
	document.title = newTitle;
}

// Setup regional maps
var bindBehaviors = function() {
	$(function() {
		// Regional Map HOVERED
		$(".containerGenRegion").hover(
		  function () {
			$(this).css('background-image', 'url(/toah/art/trans80white.png)');
			var theTimeCode = $(this).find("img").attr('id');
			if (theTimeCode != "") {
				$("."+theTimeCode).addClass("activeRegion");
			}
			var container = $('<div class="activeMapTL"><a class="mapLink">&nbsp;<\/a><\/div>');
			$(this).append(container);
			if ($(this).hasClass("small")) {
				var howWide = $(this).width()+24;
			} else {
				var howWide = $(this).width()+8;
			}
			$(".activeMapTL").width(howWide);
		  }, 
		  function () {
			$(this).css('background-image', 'url(/toah/art/trans-white.png)');
			var theTimeCode = $(this).find("img").attr('id');
			if (theTimeCode != "") {
				$("."+theTimeCode).removeClass("activeRegion");
			}
			$(this).removeClass("activeMapTL");
			$('div.activeMapTL').remove();
		  }
		);
		
		$("#subRegionText h5").hover(
		  function () {
		  	$(this).addClass("hovered");
		  }, function() {
		  	$(this).removeClass("hovered");
		  }
		);
	})
}


