$(document).ready(researchPopupInit);
		


function researchPopupInit() {

    // options
    var hideDelay = 350;
    var hideDelayTimer = null;
    var showDelay = 500;
    var showDelayTimer = null;
    var fadeSpeed = "fast";

    // tracker
    var trackedItem = null;
    var showItem = null;

    $('.researchPopup').each(function(i){ 
        $(this).mouseover(function(){
		    // stops the hide event if we move from the trigger to the popup element
            if (hideDelayTimer) clearTimeout(hideDelayTimer);
        });
        $(this).mouseout(hidePopup);
    });

	$('.termTrigger').each(function(i){ 
		$(this).mouseover(function(){
		    // stops the hide event if we move from the trigger to the popup element
            if (hideDelayTimer) clearTimeout(hideDelayTimer);
            // if we move over, out and then come back, reset the show delay
            if (showDelayTimer) clearTimeout(showDelayTimer);
                     
            showItem = this;
            showDelayTimer = setTimeout(showPopup, showDelay);
			
			return false;
		});
		$(this).mouseout(hidePopup);
	});
	
	function showPopup() {
	    if (showDelayTimer) clearTimeout(showDelayTimer);

	    // If the popup is being shown for the current item, no need to do anything
	    if ($("div.ajaxDetails").is(':animated,:visible') && trackedItem == showItem) {
            return;
        } else {
            // If the popup is currently being shown, lets nicely hide it, then show it
            // for the new item
            if ($("div.ajaxDetails").is(':animated,:visible')) {
                $("div.ajaxDetails").fadeOut(fadeSpeed, function(){
                  trackedItem = null;
                  show();
                });
            }
            else {
                show();
            }
	    }
	}
	
	function show() {
        // Save the tracked item
        trackedItem = showItem;

	    var $pos = $(showItem).position();
	    $("div.ajaxDetails").css('left', $pos.left);
	    $("div.ajaxDetails").css('top', $pos.top + $(showItem).height());
	    $("div.ajaxLoader").css('display','block');
	    $("div.innerAjaxDetails").html("");
	    $("div.ajaxDetails").fadeIn(fadeSpeed);
	    
	    var href = $(showItem).attr("href");
	    if (href.indexOf("javascript") != -1) {
	        if (href.indexOf("biographicalDetails") != -1) {
	            var pos = href.indexOf("(");
	            var pos1 = href.indexOf(")");
	            var num = href.substring(pos+1, pos1);
	        
	            href = bioURL + "?bioId=" + Ids[num];
	        }
	        else {
	            var re = new RegExp("[',;)(]");
	            var arr = href.split(re);
    	        
	            var scopeType = "";
	            var idName = "scopeId";
	            if (href.indexOf("SubmitBio") != -1) { idName = "bioId"; }
	            else if (href.indexOf("SubmitBib") != -1) { idName = "bibId"; }
	            else { scopeType = "&scopeType=" + arr[5]; }
    	        
	            href = arr[2] + "?" + idName + "=" + arr[arr.length-3] + scopeType;
	        }
	    }

	    $("div.innerAjaxDetails").load(href + "&ajax=true", null, function(){ 
            $("div.ajaxLoader").css('display','none');
	    });
	    
	    // biographicalDetails(2);
	    /*
	    function biographicalDetails(num){
  document.thesauriform.action = "/BritishMuseum3/research/search_the_collection_database/term_details.aspx";
  document.getElementById("personId").value= Ids[num] ;
  document.thesauriform.submit();
}
	    */
	}
	
	function hidePopup() {
        // reset the timer if we get fired again - avoids double animations
        if (hideDelayTimer) clearTimeout(hideDelayTimer);
        if (showDelayTimer) clearTimeout(showDelayTimer);

        if ($("div.ajaxDetails").is(':visible')) {
            hideDelayTimer = setTimeout(function () {
                hideDelayTimer = null;
                
                $("div.ajaxDetails").fadeOut(fadeSpeed, function () {
                  // once the animation is complete, reset the tracker variables
                  trackedItem = null;
                });
              }, hideDelay);
        }

	    return false;
    }
}


