// JQuery additions for Thematic Essays 

$(document).ready(function(){
  var hideDelay = 10;  
  var currentID;
  var hideTimer = null;

  var container = $('<div id="essayPopupContainer">'
      + '<table width="" border="0" cellspacing="0" cellpadding="0" align="center" class="essayPopupPopup">'
      + '<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="essayPopupContent"></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);


	$(".externalLink").after(" <img src=\"/toah/art/leavesite_red.gif\" border=\"0\">");
	$(".externalLink").addClass('external').attr('target', '_blank');

	$('p a:regex(href,ho_)').addClass("toahTipObj");
	$('p a:regex(href,hd_)').addClass("toahTipTE");
	$(".toahTipObj").hoverIntent(showTooltipOBJ,hideTooltipOBJ);
	function showTooltipOBJ() {
		
		var objAccNo;
		var re = new RegExp('.*ho_(.*)\.htm','ig');
		objAccNo = $(this).attr("href");
		objAccNo = objAccNo.replace(re,"$1")

		var offset = $(this).offset();
		if (hideTimer)
			clearTimeout(hideTimer);


			var tipHeight = jQuery('#essayPopupContainer').height();
			var winHeight = jQuery(window).height();
			var scrollTop = $(window).scrollTop();

			var totalDivHeight = tipHeight + 180;
			var orientDiv = offset.top-scrollTop;

			$.ajax({
				url: "/toah/content/ho/preview/ho_"+objAccNo+".htm",
				success: function(msg){
					//alert( "Data Saved: " + msg );
					$("#essayPopupContent").attr("innerHTML",msg);
					tipHeight = jQuery('#essayPopupContainer').height();
					winHeight = jQuery(window).height();
					scrollTop = $(window).scrollTop();
					
					totalDivHeight = tipHeight + 180;
					orientDiv = offset.top-scrollTop;
	
					$("#essayPopupContainer").css('left',offset.left);
					$("#essayPopupContent").css('width',250);
					
					$("div.adSpace").remove();
					$("div.readMore").remove();
					
					var checkLength = $("#essayPopupContent").attr("innerHTML");
					if (checkLength.length > 100) {
						var divLoaded = $(container).height();
						//alert(divLoaded);
						if (orientDiv > (winHeight/2)) {
							$("#essayPopupContainer").css('top',offset.top-divLoaded);
						} else {
							$("#essayPopupContainer").css('top',offset.top+20);
						}
						
						container.fadeIn(100);
					}
				}
			});
		
	}
	function hideTooltipOBJ() {
		if (hideTimer)
		  clearTimeout(hideTimer);
		hideTimer = setTimeout(function()
		{
		  //container.css('display', 'none');
			container.fadeOut(100, function() {
				$("#essayPopupContent").attr("innerHTML","");
			});
		}, hideDelay);
	}

	$(".toahTipTE").hoverIntent(showTooltipTE,hideTooltipTE);
	function showTooltipTE() {
		
		var objAccNo;
		var re = new RegExp('.*hd_(.*)\.htm','ig');
		objAccNo = $(this).attr("href");
		objAccNo = objAccNo.replace(re,"$1")

		var offset = $(this).offset();

		if (hideTimer)
			clearTimeout(hideTimer);


			var tipHeight = jQuery('#essayPopupContainer').height();
			var winHeight = jQuery(window).height();
			var scrollTop = $(window).scrollTop();

			var totalDivHeight = tipHeight + 180;
			var orientDiv = offset.top-scrollTop;

			$.ajax({
				url: "/toah/content/te_ads/hd_"+objAccNo+".html",
				success: function(msg){
					//alert( "Data Saved: " + msg );
					$("#essayPopupContent").attr("innerHTML",msg);
					tipHeight = jQuery('#essayPopupContainer').height();
					winHeight = jQuery(window).height();
					scrollTop = $(window).scrollTop();
					
					totalDivHeight = tipHeight + 230;
					orientDiv = offset.top-scrollTop;
	
					$("#essayPopupContainer").css('left',offset.left);
					$("#essayPopupContent").css('width',295);
					
					//$("div.adSpace").remove();
					//$("div.readMore").remove();
					
					var checkLength = $("#essayPopupContent").attr("innerHTML");
					//alert(checkLength);
					if (checkLength.length > 100) {
						var divLoaded = $(container).height();
						if (orientDiv > (winHeight/2)) {
							$("#essayPopupContainer").css('top',offset.top-divLoaded);
						} else {
							$("#essayPopupContainer").css('top',offset.top+20);
						}
						
						container.fadeIn(100);
					}
				}
			});

	}
	function hideTooltipTE() {
		if (hideTimer)
		  clearTimeout(hideTimer);
		hideTimer = setTimeout(function()
		{
		  //container.css('display', 'none');
			container.fadeOut(100, function() {
				$("#essayPopupContent").attr("innerHTML","");
			});
		}, hideDelay);
	}

	$(".objThumbLI").hoverIntent(showPreviewer,hidePreviewer);
	function showPreviewer() {
		var divID = $(this).attr("id");
		var accessNo = $(this).attr("rel");
		$(this).prepend("<div id=\""+divID+"Hover\" class=\"objThumb\"></div>");
		ajaxpage('/toah/content/ho/preview/ho_'+accessNo+'.htm', divID+'Hover');
		$("#"+divID+"Hover").fadeIn(100);
	}
	function hidePreviewer() {
		var divID = $(this).attr("id");
		$("#"+divID+"Hover").fadeOut(100);
	}

    $(".showPDF").click(function(){
    	$(this).parent().children(".pdfList").toggle();
    })
});








function swapRelated(theID) {
	var el = document.getElementById(theID);
	var elArrow = document.getElementById(theID+'Arrow');
		if (el.style.display == 'none' || el.style.display == '') {
			$('#'+theID).slideDown("fast");
			elArrow.src = '/toah/art/thematic_arrow_down.gif';
		} else {
			$('#'+theID).slideUp("fast");
			elArrow.src = '/toah/art/thematic_arrow_right.gif';
		}
	//alert(el.style.display);
}

function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

function previewObj(accessNo,whichID) {
	ajaxpage('/toah/content/ho/preview/ho_'+accessNo+'.htm', 'objThumbEnlarge'+whichID);
	document.getElementById('objThumbEnlarge'+whichID).style.display = "block";
	//divName = 'objThumbEnlarge'+whichID;
}
function hideObj(accessNo,whichID) {
	document.getElementById('objThumbEnlarge'+whichID).style.display = "none";
}

var stallVariable = 0;
var c=0
var t

function unStallSwap() {
	stallVariable = 0;
}

function stallSwap() {
	stallVariable = 1;
	clearTimeout(t);
	t=setTimeout("unStallSwap()",1000);
}
function swapCategory(theID) {
	if (stallVariable == 0) {
		var el = document.getElementById(theID);
		var elArrow = document.getElementById(theID+'Arrow');
			if (el.style.display == 'none' || el.style.display == '') {
				$('#'+theID).slideDown("fast");
				elArrow.src = '/toah/art/thematic_arrow_down.gif';
			} else {
				$('#'+theID).slideUp("fast");
				elArrow.src = '/toah/art/thematic_arrow_right.gif';
			}
	}
	//alert(el.style.display);
}

function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

function resetCategories(whichToReset) {
	if (whichToReset == 'timeSpace') { 
		document.getElementById('categoryGeo').value="all";
		document.getElementById('categoryTime').value="all";
	} else { 
		document.getElementById('categoryDept').value="all";
	}
}



// ##### HANDLES NON MET IMAGES ON THEMATIC ESSAY PAGES ##### //
function makeObjectsActive(whichItem) {
	if (totalAV > 0) {
		// First make all invisible //
		for (i=1;i<(totalAV+1);i++) 
		{
			document.getElementById('tombTextAlt'+i).style.display = "none";
		}	
			// Now make the appropriate text space visible if it's not null //
		document.getElementById('tombTextAlt0').style.display = "block";
		document.getElementById('tombTextAlt'+whichItem).style.display = "block";
				
		for (x in replaceArray)
		{
			if (replaceArray[x] == whichItem) {
				document.getElementById('tombTextAlt0').style.display = "none";
			}
		}				
	}
}

function toggleEnlargeThematic(xxx,whichItem) {
	var el = document.getElementById('altViewsEnlarge');
		document.getElementById('activeBig').src='../../images/h2/h2_'+xxx+'.jpg';

		  var scrOfX = 0, scrOfY = 0;
		  if( typeof( window.pageYOffset ) == 'number' ) {
			//Netscape compliant
			scrOfY = window.pageYOffset;
			scrOfX = window.pageXOffset;
		  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
			//DOM compliant
			scrOfY = document.body.scrollTop;
			scrOfX = document.body.scrollLeft;
		  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
			//IE6 standards compliant mode
			scrOfY = document.documentElement.scrollTop;
			scrOfX = document.documentElement.scrollLeft;
		  }
		
		document.getElementById("altViewsEnlarge").style.top = (scrOfY+50)+"px";
		document.getElementById("altViewsEnlarge").style.left = "75px";

	if (totalAV.length > 0) {
		// First make all invisible //
		for (i=1;i<(totalAV.length+1);i++) 
		{
			document.getElementById('tombTextAlt'+totalAV[i-1]).style.display = "none";
		}	
			// Now make the appropriate text space visible if it's not null //
		document.getElementById('tombTextAlt'+xxx).style.display = "block";
	}

		el.style.display = 'block';

   var newdiv = document.createElement('div');
   newdiv.id = "wholePageClose";
   newdiv.style.width = "100%";
   newdiv.style.height = "100%";
   newdiv.style.position = "absolute";
   newdiv.style.left = 0;
   newdiv.style.top = scrOfY+"px";
   
   newdiv.style.zIndex = 1;  
   document.body.appendChild(newdiv);
   document.getElementById("wholePageClose").onclick=new Function("hideEnlargement()");

}

function toggleEnlargeThematicMap(xxx,whichItem) {
	var el = document.getElementById('altViewsEnlargeMap');
		document.getElementById('activeBigMap').src='../../hg/'+xxx;
		  var scrOfX = 0, scrOfY = 0;
		  if( typeof( window.pageYOffset ) == 'number' ) {
			//Netscape compliant
			scrOfY = window.pageYOffset;
			scrOfX = window.pageXOffset;
		  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
			//DOM compliant
			scrOfY = document.body.scrollTop;
			scrOfX = document.body.scrollLeft;
		  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
			//IE6 standards compliant mode
			scrOfY = document.documentElement.scrollTop;
			scrOfX = document.documentElement.scrollLeft;
		  }
		
		document.getElementById("altViewsEnlargeMap").style.top = (scrOfY+50)+"px";
		document.getElementById("altViewsEnlargeMap").style.left = "75px";

	if (totalMaps > 0) {
		// First make all invisible //
		for (i=1;i<(totalMaps+1);i++) 
		{
			document.getElementById('mapTextAlt'+i).style.display = "none";
		}	
			// Now make the appropriate text space visible if it's not null //
		document.getElementById('mapTextAlt'+whichItem).style.display = "block";
	}

		el.style.display = 'block';

   var newdiv = document.createElement('div');
   newdiv.id = "wholePageClose";
   newdiv.style.width = "100%";
   newdiv.style.height = "100%";
   newdiv.style.position = "absolute";
   newdiv.style.left = 0;
   newdiv.style.top = scrOfY+"px";
   
   newdiv.style.zIndex = 1;  
   document.body.appendChild(newdiv);
   document.getElementById("wholePageClose").onclick=new Function("hideEnlargementMap()");


}

function doWin(){
	var orig_div_cont=document.getElementById("EnlargedView").innerHTML;
	var myWin=window.open("","myWin","menubar,scrollbars,left=30px,top=40px,height=400px,width=680px");
	myWin.document.write('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd"><html><head><title>The Heilbrunn Timeline of Art History | The Metropolitan Museum of Art</title><script type="text/javascript">function printFunction(){var t=setTimeout("window.print()",700);}</script><style>body { background:none; margin:0px; padding:0px; font-family:Arial,sans-serif; font-size:14px; } #footer { font-size:11px; margin-top:3px; } img { margin-bottom:20px; display:block; }</style></head><body onload="printFunction();"><div id="dest_div"></div><div id="footer">Copyright &copy; 2000&ndash;2008 The Metropolitan Museum of Art. All rights reserved.</body></html>');
	myWin.document.close();
	myWin.document.getElementById("dest_div").innerHTML+=orig_div_cont;
}

function hideEnlargement() {
	var el = document.getElementById('altViewsEnlarge');
	document.getElementById('activeBig').src = "/toah/images/spacer.gif";
	el.style.display = "none";
	var el2 = document.getElementById('wholePageClose');
   document.body.removeChild(el2);	
}

function doWinMap(){
	var orig_div_cont=document.getElementById("EnlargedViewMap").innerHTML;
	var myWin=window.open("","myWin","menubar,scrollbars,left=30px,top=40px,height=400px,width=680px");
	myWin.document.write('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd"><html><head><title>The Heilbrunn Timeline of Art History | The Metropolitan Museum of Art</title><script type="text/javascript">function printFunction(){var t=setTimeout("window.print()",700);}</script><style>body { background:none; margin:0px; padding:0px; font-family:Arial,sans-serif; font-size:14px; } #footer { font-size:11px; margin-top:3px; } img { margin-bottom:20px; display:block; }</style></head><body onload="printFunction();"><div id="dest_div"></div><div id="footer">Copyright &copy; 2000&ndash;2008 The Metropolitan Museum of Art. All rights reserved.</body></html>');
	myWin.document.close();
	myWin.document.getElementById("dest_div").innerHTML+=orig_div_cont;
}

function hideEnlargementMap() {
	var el = document.getElementById('altViewsEnlargeMap');
	document.getElementById('activeBigMap').src = "/toah/images/spacer.gif";
	el.style.display = "none";
	var el2 = document.getElementById('wholePageClose');
   document.body.removeChild(el2);	
}


// ###### FUNCTIONS FOR THE THEMATIC ESSAY INDEX ###### //

function thematicIndexReveal(theID,content,whichOne) {
	var el = document.getElementById('thematicIndex'+theID+whichOne);
	var el2 = document.getElementById('thematicIndex'+theID+'Holder'+whichOne);
	//var elspacer = document.getElementById('sp'+content);
	var elArrow = document.getElementById('thematicArrow'+theID+whichOne);
	if (stallVariable == 0) {
		if (el.style.display == 'none' || el.style.display == '') {
			el.style.display='none';
			ajaxpageWhite('/toah/content/te_ads/hd_'+content+'.html', 'thematicIndex'+theID+whichOne);
			//$("#thematicIndex"+theID+whichOne).load("/toah/content/te_ads/hd_"+content+".html");
			elArrow.src = '/toah/art/close.png';
			$('#thematicIndex'+theID+whichOne).slideDown("fast");
			el2.className='stayHighlight';
			document.getElementById('thematicSlideIndex'+theID).style.display = "block";
		} else {
			$('#thematicIndex'+theID+whichOne).slideUp("fast");
			el2.className='indexHighlight';
			elArrow.src = '/toah/art/open.png';
			document.getElementById('thematicSlideIndex'+theID).style.display = "none";
		}
	}
}

function highlightIndex(theNum,whichOne) {
	var el = document.getElementById('thematicIndex'+theNum+'Holder'+whichOne);
	if (el.className != 'stayHighlight') {
		el.className='indexHighlight';
	}
}

function unhighlightIndex(theNum,whichOne) {
	var el = document.getElementById('thematicIndex'+theNum+'Holder'+whichOne);
	if (el.className != 'stayHighlight') {
		el.className='thematicIndexInfo';
	}
}

function unhighlightIndex2(theNum,whichOne) {
	var el = document.getElementById('thematicIndex'+theNum+'Holder'+whichOne);
	if (el.className != 'stayHighlight') {
		el.className='indexAlt thematicIndexInfo';
	}
}

function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

function expand(obj) {
	if (obj != 'recent') {
		var el = document.getElementById('UL' + obj);
		var changeIMG = 'UL' + obj + 'Arrow';
		if ( el.style.display != 'block' ) {
			el.style.display = 'block';
			document.getElementById(changeIMG).src='/toah/art/thematic_arrow_down.gif';
		}
		else {
			el.style.display = 'none';
			document.getElementById(changeIMG).src='/toah/art/thematic_arrow_right.gif';
		}
	}
}



/**************************************************
 * dom-drag.js
 * 09.25.2001
 * www.youngpup.net
 * Script featured on Dynamic Drive (http://www.dynamicdrive.com) 12.08.2005
 **************************************************
 * 10.28.2001 - fixed minor bug where events
 * sometimes fired off the handle, not the root.
 **************************************************/

var Drag = {

	obj : null,

	init : function(o, oRoot, minX, maxX, minY, maxY, bSwapHorzRef, bSwapVertRef, fXMapper, fYMapper)
	{
	minY = 0;
		o.onmousedown	= Drag.start;

		o.hmode			= bSwapHorzRef ? false : true ;
		o.vmode			= bSwapVertRef ? false : true ;

		o.root = oRoot && oRoot != null ? oRoot : o ;

		if (o.hmode  && isNaN(parseInt(o.root.style.left  ))) o.root.style.left   = "0px";
		if (o.vmode  && isNaN(parseInt(o.root.style.top   ))) o.root.style.top    = "0px";
		if (!o.hmode && isNaN(parseInt(o.root.style.right ))) o.root.style.right  = "0px";
		if (!o.vmode && isNaN(parseInt(o.root.style.bottom))) o.root.style.bottom = "0px";

		o.minX	= typeof minX != 'undefined' ? minX : null;
		o.minY	= typeof minY != 'undefined' ? minY : null;
		o.maxX	= typeof maxX != 'undefined' ? maxX : null;
		o.maxY	= typeof maxY != 'undefined' ? maxY : null;

		o.xMapper = fXMapper ? fXMapper : null;
		o.yMapper = fYMapper ? fYMapper : null;

		o.root.onDragStart	= new Function();
		o.root.onDragEnd	= new Function();
		o.root.onDrag		= new Function();
	},

	start : function(e)
	{
		var o = Drag.obj = this;
		e = Drag.fixE(e);
		var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
		var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
		o.root.onDragStart(x, y);

		o.lastMouseX	= e.clientX;
		o.lastMouseY	= e.clientY;

		if (o.hmode) {
			if (o.minX != null)	o.minMouseX	= e.clientX - x + o.minX;
			if (o.maxX != null)	o.maxMouseX	= o.minMouseX + o.maxX - o.minX;
		} else {
			if (o.minX != null) o.maxMouseX = -o.minX + e.clientX + x;
			if (o.maxX != null) o.minMouseX = -o.maxX + e.clientX + x;
		}

		if (o.vmode) {
			if (o.minY != null)	o.minMouseY	= e.clientY - y + o.minY;
			if (o.maxY != null)	o.maxMouseY	= o.minMouseY + o.maxY - o.minY;
		} else {
			if (o.minY != null) o.maxMouseY = -o.minY + e.clientY + y;
			if (o.maxY != null) o.minMouseY = -o.maxY + e.clientY + y;
		}

		document.onmousemove	= Drag.drag;
		document.onmouseup		= Drag.end;

		return false;
	},

	drag : function(e)
	{
		e = Drag.fixE(e);
		var o = Drag.obj;

		var ey	= e.clientY;
		var ex	= e.clientX;
		var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
		var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
		var nx, ny;

		if (o.minX != null) ex = o.hmode ? Math.max(ex, o.minMouseX) : Math.min(ex, o.maxMouseX);
		if (o.maxX != null) ex = o.hmode ? Math.min(ex, o.maxMouseX) : Math.max(ex, o.minMouseX);
		if (o.minY != null) ey = o.vmode ? Math.max(ey, o.minMouseY) : Math.min(ey, o.maxMouseY);
		if (o.maxY != null) ey = o.vmode ? Math.min(ey, o.maxMouseY) : Math.max(ey, o.minMouseY);

		nx = x + ((ex - o.lastMouseX) * (o.hmode ? 1 : -1));
		ny = y + ((ey - o.lastMouseY) * (o.vmode ? 1 : -1));

		if (o.xMapper)		nx = o.xMapper(y)
		else if (o.yMapper)	ny = o.yMapper(x)

		Drag.obj.root.style[o.hmode ? "left" : "right"] = nx + "px";
		Drag.obj.root.style[o.vmode ? "top" : "bottom"] = ny + "px";
		Drag.obj.lastMouseX	= ex;
		Drag.obj.lastMouseY	= ey;

		Drag.obj.root.onDrag(nx, ny);
		return false;
	},

	end : function()
	{
		document.onmousemove = null;
		document.onmouseup   = null;
		Drag.obj.root.onDragEnd(	parseInt(Drag.obj.root.style[Drag.obj.hmode ? "left" : "right"]), 
									parseInt(Drag.obj.root.style[Drag.obj.vmode ? "top" : "bottom"]));
		Drag.obj = null;
	},

	fixE : function(e)
	{
		if (typeof e == 'undefined') e = window.event;
		if (typeof e.layerX == 'undefined') e.layerX = e.offsetX;
		if (typeof e.layerY == 'undefined') e.layerY = e.offsetY;
		return e;
	}
};
/*
var divName = 'theBox2'; // div that is to follow the mouse
                       // (must be position:absolute)
var offX = 15;          // X offset from mouse position
var offY = 15;          // Y offset from mouse position

function mouseX(evt) {if (!evt) evt = window.event; if (evt.pageX) return evt.pageX; else if (evt.clientX)return evt.clientX + (document.documentElement.scrollLeft ?  document.documentElement.scrollLeft : document.body.scrollLeft); else return 0;}
function mouseY(evt) {if (!evt) evt = window.event; if (evt.pageY) return evt.pageY; else if (evt.clientY)return evt.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop); else return 0;}

function follow(evt) {if (document.getElementById) {var obj = document.getElementById(divName).style; obj.visibility = 'visible';
obj.left = (parseInt(mouseX(evt))+offX) + 'px';
obj.top = (parseInt(mouseY(evt))+offY) + 'px';}}
document.onmousemove = follow;
*/
                    