$(document).ready(function () {
	var t1, t2, t3, t4;
	var in1, in2, in3, in4;
	var fadeInSpeed = 400;
	var fadeOutSpeed = 300;


	// Navigation Checker
	$("#worldRegionsNav li a").click(function () {
		var newValue = $(this).attr("rel");
		$("#subnav1 div.last ul li").each(function () {
			var tempCode = $(this).children("a").attr("rel");
			var tempURL = "/toah/world-regions/#/" + newValue + "/" + tempCode;
			$(this).children("a").attr("href", tempURL)
		})
	});

	$("#subnav1 ul li").click(function () {
		$(this).parent().children("li").removeClass("active");
		$(this).addClass("active");
	})

	//$(".topLevelWM").removeAttr("href");


	var cleanURL = function (oldURL) {
		finalURL = oldURL;
		finalURL = finalURL.replace("-", "_").replace("-", "_").replace("-", "_").replace("-", "_").replace("-", "_").replace("-", "_").replace("-", "_").replace("-", "_");
		finalURL = finalURL.replace(" ", "-").replace(" ", "-").replace(" ", "-").replace(" ", "-").replace(" ", "-").replace(" ", "-").replace(" ", "-").replace(" ", "-");
		return finalURL;
	}

	// Show the subnav
	$(".globalNav").hoverIntent({
		interval: 200,
		over: globalNavTriggerOn,
		timeout: 500,
		out: globalNavTriggerOff
	});

	function globalNavTriggerOn() {
		$(this).find(".navExtra").fadeIn(100);
		$(this).addClass("StayHovered");
	}

	function globalNavTriggerOff() {
		$(this).find(".navExtra").fadeOut(200);
		$(this).removeClass("StayHovered");
	}

	// Keep the nav active on hover
	$(".globalNav").hover(function () {
		$(this).addClass("Hovered");
		$(this).siblings().each(function () {
			$(this).children(".navExtra").hide();
		})
		if ($(this).hasClass("toahNav3")) {
			$(this).find(".first").css({ "opacity": "1" });
		}
		if ($(this).hasClass("toahNav4")) {
			$(this).find(".first").css({ "opacity": "1" });
		}
	}, function () {
		$(this).removeClass("Hovered");
	});



	$(".floatLeft").hover(function () {
		$(this).css({ "opacity": "1" });
	}, function () {
		$(this).css({ "opacity": ".6" });
	})

	$("#woaNavSubmit").click(function () {
		runForm();
	})

	mainFormClicked = function () {
		$("#theBox").removeClass("empty");
		$("#theBox").addClass("filled");
		$("#theBox2").attr("src", "/toah/art/search-go-on.png");
	}

	mainFormBlurred = function () {
		if ($("#theBox").val()) {

		} else {
			$("#theBox").removeClass("filled");
			$("#theBox").addClass("empty");
			$("#theBox2").attr("src", "/toah/art/search-go.png");
		}
	}


	// World Map Specific
	var lockRegion = "No";
	var lockTime = "No";
	var currentActive = "World-Map";
	var newRegion = "World-Map";
	$(".timeChoose li a").each(function () {
		$(this).removeAttr("href");
	})
	$(".worldmapNavigation li").click(function () {
		if ($(this).children("a").hasClass("stayHovered")) {
			$(this).children("a").removeClass("stayHovered");
			$(".globalNav ul.timeChoose").slideUp(300);
			$("#regionPicker").attr("innerHTML", "Choose a region");
			lockRegion = "No";
		} else {
			$(".stayHovered").removeClass("stayHovered");
			$(this).children("a").addClass("stayHovered");
			var theClasses = $(this).attr("class");
			newRegion = theClasses.replace("wr-MapArea wr-", "");
			newRegion = newRegion.replace(" zPull", "");
			/*$(".timeChoose li").each(function(){
			var newURL = $(this).children("a").attr("href");
				
			if(theClasses.indexOf("wr-North-America") != -1){
			newURL = newURL.replace(currentActive,"North-America");
			newActive = "North-America";
			} else if(theClasses.indexOf("wr-Europe") != -1){
			newURL = newURL.replace(currentActive,"Europe");
			newActive = "Europe";
			} else if(theClasses.indexOf("wr-Central-North-Asia") != -1){
			newURL = newURL.replace(currentActive,"Central-North-Asia");
			newActive = "Central-North-Asia";
			} else if(theClasses.indexOf("wr-East-Asia") != -1){
			newURL = newURL.replace(currentActive,"East-Asia");
			newActive = "East-Asia";
			} else if(theClasses.indexOf("wr-Central-America") != -1){
			newURL = newURL.replace(currentActive,"Central-America");
			newActive = "Central-America";
			} else if(theClasses.indexOf("wr-South-America") != -1){
			newURL = newURL.replace(currentActive,"South-America");
			newActive = "South-America";
			} else if(theClasses.indexOf("wr-Africa") != -1){
			newURL = newURL.replace(currentActive,"Africa");
			newActive = "Africa";
			} else if(theClasses.indexOf("wr-West-Asia") != -1){
			newURL = newURL.replace(currentActive,"West-Asia");
			newActive = "West-Asia";
			} else if(theClasses.indexOf("wr-Oceania") != -1){
			newURL = newURL.replace(currentActive,"Oceania");
			newActive = "Oceania";
			} else if(theClasses.indexOf("wr-South-Southeast-Asia") != -1){
			newURL = newURL.replace(currentActive,"South-Southeast-Asia");
			newActive = "South-Southeast-Asia";
			} else {
			newURL = newURL.replace(currentActive,"World-Map");
			newActive = "World-Map";
			}
				
			//alert(newURL);
			$(this).children("a").attr("href",newURL);
			})*/
			//currentActive = newActive;
			$(".globalNav ul.timeChoose").slideDown(300);
			var newText = $(this).children("a").attr("innerHTML");
			$("#regionPicker").attr("innerHTML", newText);
			lockRegion = "Yes";
		}
	})

	$(".timeChoose a").click(function () {
		var newTimePeriod = $(this).attr("id");
		newTimePeriod = newTimePeriod.replace("timePeriodPick", "");
		window.location = "/toah/world-regions/#/" + newTimePeriod + "/" + newRegion;
	})

	$(".worldmapNavigation li").hover(function () {
		$(this).children("a").addClass("hoveredMap");
	}, function () {
		$(this).children("a").removeClass("hoveredMap");
	})

	$(".worldmapNavigation li").each(function () {
		$(this).children("a").removeAttr("href");
	})

	$(".closeDates").click(function () {
		$(".stayHovered").removeClass("stayHovered");
		$(".globalNav ul.timeChoose").slideUp(300);
		$("#regionPicker").attr("innerHTML", "Choose a region");
	})
	$(".worldmapNavigation li").hover(function () {
		if (lockRegion != "Yes") {
			var newText = $(this).children("a").attr("innerHTML");
			$("#regionPicker").attr("innerHTML", newText);
			if ($(this).hasClass("zPull")) {
				$(this).css({ 'z-index': 500 });
			}
		}
	}, function () {
		if (lockRegion != "Yes") {
			$("#regionPicker").attr("innerHTML", "Select a region");
			if ($(this).hasClass("zPull")) {
				$(this).css({ 'z-index': 5 });
			}
		}
	})
	$(".timeChoose li").hover(function () {
		if (lockTime != "Yes") {
			var newText = $(this).children("a").attr("innerHTML");
			$("#timePicker").attr("innerHTML", newText);
		}
	}, function () {
		if (lockTime != "Yes") {
			$("#timePicker").attr("innerHTML", "Select a time period");
		}
	})

	$(".allRegionToggle").click(function () {
		newRegion = "World-Map";
		if ($(this).children("a").hasClass("activeToggle")) {
			$(".wr-MapArea").each(function () {
				$(this).children("a").removeClass("stayHovered");
			})
			$(".allRegionsShown").hide();
			$(".globalNav ul.timeChoose").slideUp(300);
			$("#regionPicker").attr("innerHTML", "Select a region");
			$(this).children("a").removeClass("activeToggle");
			lockRegion = "No";
		} else {
			$(".wr-MapArea").each(function () {
				$(this).children("a").addClass("stayHovered");
			})
			$(".allRegionsShown").show();
			$(".globalNav ul.timeChoose").slideDown(300);
			$("#regionPicker").attr("innerHTML", "All World Regions");
			$(this).children("a").addClass("activeToggle");
			lockRegion = "Yes";
			$(".timeChoose li").each(function () {
				var newURL = $(this).children("a").attr("href");
				newURL = newURL.replace(newActive, "World-Map");
				$(this).children("a").attr("href", newURL);
			})
			newActive = "World-Map";
			newRegion = "World-Map";
		}
	})

	$(".timeChoose").click(function () {
		$(".worldmapNavigation").children("li").children("a").removeClass("stayHovered");
		$(".globalNav ul.timeChoose").slideUp(300);
		$("#regionPicker").attr("innerHTML", "Choose a region");
		$(".globalNav ul.timeChoose").slideUp(300);
		currentActive = "World-Map";
		newRegion = "World-Map";
	})

	$("#GlobalCategoryTECat").change(function () {
		if ($(this).val() != "") {
			window.location = $(this).val();
		}
	})
	$("#GlobalCategoryDept").change(function () {
		if ($(this).val() != "") {
			window.location = $(this).val();
		}
	})

	$('.toahNav4 input').bind('keypress', function (e) {
		var code = (e.keyCode ? e.keyCode : e.which);
		if (code == 13) { //Enter keycode
			runForm();
		}
	});

	$("#ULallthematicessaysArrow").trigger("click");

	$(".thumbnailHover").each(function () {
		$(this).parent().parent().prepend("<div class=\"previewDiv\"></div>");
	})

	$(".thumbnailHover").hoverIntent(function () {
		var accNo = $(this).attr("rel");
		$(this).parent().parent().find(".previewDiv").load("/toah/content/ho/preview/ho_" + accNo + ".htm", function () {
			$(this).show();
		})
	}, function () {
		$(".previewDiv").hide();
	})

	var extraClassOnView = $("#onViewText").attr("class");
	if ($("#onViewText").attr("innerHTML") != undefined) {
		if ($('.tombstoneSmall').length != 0) {
			$(".tombstoneSmall").append("<div class=\"onViewSmall " + extraClassOnView + "\"><span><small>" + $("#onViewText").attr("innerHTML") + "</small> &nbsp; Last Updated "+$("#onViewText").attr("rel")+"</span></div>");
		} else {
			$(".objAccessionNumber").append("<div class=\"onViewSmall " + extraClassOnView + "\"><span><small>" + $("#onViewText").attr("innerHTML") + "</small> &nbsp; Last Updated "+$("#onViewText").attr("rel")+"</span></div>");
		}
	}
	$("#onViewText").hide();
});

function runForm() {
		var finalString = "";
		var multipleQuery = "";
		if($("#navtitleInput").attr("value")!=""){
			if (multipleQuery == "Yes"){
				finalString = finalString+"&Title="+$("#navtitleInput").attr("value");
			} else {
				finalString = finalString+"?Title="+$("#navtitleInput").attr("value");
			}
			multipleQuery = "Yes";
		}
		if($("#navartistInput").attr("value")!=""){
			if (multipleQuery == "Yes"){
				finalString = finalString+"&Artist="+$("#navartistInput").attr("value");
			} else {
				finalString = finalString+"?Artist="+$("#navartistInput").attr("value");
			}
			multipleQuery = "Yes";
		}
		if($("#navmaterialTechnique").attr("value")!=""){
			if (multipleQuery == "Yes"){
				finalString = finalString+"&Material="+$("#navmaterialTechnique").attr("value");
			} else {
				finalString = finalString+"?Material="+$("#navmaterialTechnique").attr("value");
			}
			multipleQuery = "Yes";
		}
		if($("#navcreditLine").attr("value")!=""){
			if (multipleQuery == "Yes"){
				finalString = finalString+"&CreditLine="+$("#navcreditLine").attr("value");
			} else {
				finalString = finalString+"?CreditLine="+$("#navcreditLine").attr("value");
			}
			multipleQuery = "Yes";
		}
		if($("#navaccNoInput").attr("value")!=""){
			if (multipleQuery == "Yes"){
				finalString = finalString+"&AccNo="+$("#navaccNoInput").attr("value");
			} else {
				finalString = finalString+"?AccNo="+$("#navaccNoInput").attr("value");
			}
			multipleQuery = "Yes";
		}
		//alert("Your permanent URL is: "+finalString);
		window.location = "/toah/works-of-art/"+cleanURL(finalString);
}


function resetCategoriesHeader(whichClicked) {
	if (whichClicked == 'timeSpace') { 
		document.getElementById('GlobalCategoryGeo').value="all";
		document.getElementById('GlobalCategoryTime').value="all";
	} else { 
		document.getElementById('GlobalCategoryDept').value="all";
	}
	
}

function cleanURL(oldURL){
	finalURL = oldURL;
	finalURL = finalURL.replace("-","_").replace("-","_").replace("-","_").replace("-","_").replace("-","_").replace("-","_").replace("-","_").replace("-","_");
	finalURL = finalURL.replace(" ","-").replace(" ","-").replace(" ","-").replace(" ","-").replace(" ","-").replace(" ","-").replace(" ","-").replace(" ","-");
	return finalURL;
}



/**
* hoverIntent r5 // 2007.03.27 // jQuery 1.1.2+
* <http://cherne.net/brian/resources/jquery.hoverIntent.html>
* 
* @param  f  onMouseOver function || An object with configuration options
* @param  g  onMouseOut function  || Nothing (use configuration options object)
* @author    Brian Cherne <brian@cherne.net>
*/
(function($){$.fn.hoverIntent=function(f,g){var cfg={sensitivity:7,interval:100,timeout:0};cfg=$.extend(cfg,g?{over:f,out:g}:f);var cX,cY,pX,pY;var track=function(ev){cX=ev.pageX;cY=ev.pageY;};var compare=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);if((Math.abs(pX-cX)+Math.abs(pY-cY))<cfg.sensitivity){$(ob).unbind("mousemove",track);ob.hoverIntent_s=1;return cfg.over.apply(ob,[ev]);}else{pX=cX;pY=cY;ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}};var delay=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);ob.hoverIntent_s=0;return cfg.out.apply(ob,[ev]);};var handleHover=function(e){var p=(e.type=="mouseover"?e.fromElement:e.toElement)||e.relatedTarget;while(p&&p!=this){try{p=p.parentNode;}catch(e){p=this;}}if(p==this){return false;}var ev=jQuery.extend({},e);var ob=this;if(ob.hoverIntent_t){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);}if(e.type=="mouseover"){pX=ev.pageX;pY=ev.pageY;$(ob).bind("mousemove",track);if(ob.hoverIntent_s!=1){ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}}else{$(ob).unbind("mousemove",track);if(ob.hoverIntent_s==1){ob.hoverIntent_t=setTimeout(function(){delay(ev,ob);},cfg.timeout);}}};return this.mouseover(handleHover).mouseout(handleHover);};})(jQuery);

// JQuery RegEx Extender
jQuery.expr[':'].regex = function(elem, index, match) {
	var matchParams = match[3].split(','),
		validLabels = /^(data|css):/,
		attr = {
			method: matchParams[0].match(validLabels) ? 
						matchParams[0].split(':')[0] : 'attr',
			property: matchParams.shift().replace(validLabels,'')
		},
		regexFlags = 'ig',
		regex = new RegExp(matchParams.join('').replace(/^\s+|\s+$/g,''), regexFlags);
	return regex.test(jQuery(elem)[attr.method](attr.property));
}


// Custom function for Expansion of divs :: Dan Shields //
jQuery.fn.contentExpand = function(passedInfo) {
	return this.each(function() {
		$(this).click(function(){
			if ($(this).find("div.moreInfoDiv")) {
				$(this).find(".essayExpand a img").attr("src","/toah/art/open.png");
				if ($(this).hasClass("expanded")) {
					$(this).find(".sepBibItems2").slideUp(100,function(){
						$(this).parent().parent().find("div.moreInfoDiv").slideUp(function(){
							$(this).remove();
						});
					});
					$(this).removeClass("expanded");
				} else {
					$(this).addClass("expanded");
					$(this).find(".essayExpand a img").attr("src","/toah/art/close.png");
					var dataURL = $(this).attr("rel");
					if (dataURL != null) {
						var moreInfoContainerLoad = $("<div class=\"moreInfoDivLoad\">loading...</div>").hide();
						var moreInfoContainer = $("<div class=\"moreInfoDiv\"></div>").hide();
						$(this).append(moreInfoContainer);
						$(this).find("div.moreInfoDiv").load(dataURL,function(){
							$(this).parent().find("div.moreInfoDivLoad").slideUp(100);
							$(this).find(".sepBibItems2").hide();
							$(this).slideDown(500,function(){
								$(this).find(".sepBibItems2").slideDown(100);
							});
						});
	
					}
				}
			}
		})
	});	
};



// Custom function for Object Previews :: Dan Shields //
jQuery.fn.objPreview = function() {

	var container = $('<div id="ContentPopupContainer">'
		+ '<table width="" border="0" cellspacing="0" cellpadding="0" align="center" class="ContentPopupPopup">'
		+ '<tr>'
		+ '   <td class="corner topLeft"></td>'
		+ '   <td class="top"></td>'
		+ '   <td class="corner topRight"></td>'
		+ '</tr>'
		+ '<tr>'
		+ '   <td class="left">&nbsp;</td>'
		+ '   <td class="whiteBG"><div id="ContentPopupContent"></div></td>'
		+ '   <td class="right">&nbsp;</td>'
		+ '</tr>'
		+ '<tr>'
		+ '   <td class="corner bottomLeft">&nbsp;</td>'
		+ '   <td class="bottom">&nbsp;</td>'
		+ '   <td class="corner bottomRight"></td>'
		+ '</tr>'
		+ '</table>'
		+ '</div>');
	
	$('body').append(container);


	return this.each(function() {
	
	});	
};

