/*citationHelper.js*/
/*use: for making citations into citation objects that fade and disappear on hover*/
/*requires:  prototype.js, scriptaculous.js, effects.js*/

//this is the parent relationship of the citation being called.
//this is specific to incorporate.com
var globalOffsetTop = 0;
var globalOffsetLeft = 0;

var hovering = new Boolean(false);
var shown = new Boolean(false);
var timeout = setTimeout("",1);
function clearTime() {
	timeout = setTimeout("",1);
}

//fix offset issues in IE
var scrollbarWidth = (document.all) ? 0 : 22;

var superCites = new Array();
var lastCitationValue = "";
var currentCitationValue = "";
var citationSpace = "";

function overCitation() {
	//set hovering parameter - can't hide or fade while hovering
	hovering = true;
}

function outCitation() {
	//not hovering - hiding and fading possible
	hovering = false;
}


function manualClear() {
	//not hovering, clear now - user manually cancelled
	hovering = false;
	clearCitation();
}

//clear the citation
function clearCitation() {
	if(hovering == false) {		//if you're not hovering over this
		if(shown == true) {		//and it's not already hidden
			//fade out
			new Effect.Opacity(citationSpace, {duration:0.7, from:0.9, to:0.0});
			//turn the element off
			setTimeout("citationSpace.style.display = 'none';",200);
			//set the status to hidden (so as not to hide by fade again)
			shown = false;
		}
	} else {		//otherwise
		//check back in 4 seconds
		timeout = setTimeout("clearCitation();",5000);
	}
}

//this re-displays the citation, but also moves it to the right space.
function showCitation() {
	//show citation by fade-in if not already shown
	if(shown == false ) {
		citationSpace.style.display = "block";
		new Effect.Opacity(citationSpace, {duration:0.7, from:0.0, to:0.90});
	}
	shown = true;

	//bump up the citation its own height
	globalOffsetTop -= citationSpace.offsetHeight;
	citationSpace.style.top = globalOffsetTop + "px";

	var screenTop = document.body.scrollTop;
	var screenRight = document.body.clientWidth + document.body.scrollLeft;
	var screenBottom = document.body.clientHeight + document.body.scrollTop;
	var screenLeft = document.body.scrollLeft;
	
	if(!document.all) {
		screenTop = window.pageYOffset;
		screenRight = window.innerWidth + window.pageXOffset;
		screenBottom = window.innerHeight + window.pageYOffset;
		screenLeft = window.pageXOffset;
	}

	//move citation helper onto the screen if the top is off screen
	if(globalOffsetTop < screenTop) {
		citationSpace.style.top = screenTop + "px";
	}

	//move citation helper onto the screen if the left is off screen
	//there's no way this can really happen except for a multi-line citation, and even then, tricky
	if(globalOffsetLeft < screenLeft) {
		citationSpace.style.left = screenLeft + "px";
	}

	//move citation helper onto the screen if the bottom is off the screen
	//you can't really make this happen either...
	if(globalOffsetTop + citationSpace.offsetHeight > screenBottom) {
		citationSpace.style.top = screenBottom - citationSpace.offsetHeight - scrollbarWidth + "px";
	}

	//move citation helper onto the screen if the right is off the screen
	if(globalOffsetLeft + citationSpace.offsetWidth > screenRight) {
		citationSpace.style.left = screenRight - citationSpace.offsetWidth - scrollbarWidth + "px";
	}

}

//attaches the citation mouseover to an element, but retains original mouseover too
function makeCitation() {
	//citation container
	citationSpace = document.getElementById("citationSpace");

	//citation body container
	var citationBody = document.getElementById("citationBody");

	//creates a citation object
	//do not want this used outside of this context
	function Citation(element, id) {
		//keep a reference to this element
		this.element = element;
		this.element.id = id;
		this.id = id;

		//remember if the source of this citation is unique
		this.unique = new Boolean(true);
		
		//assign mouseover and mouseout methods to this element
		element.onmouseover = citeSource;
		element.onmouseout = clearCitationContainer;
		
		this.source = (element.getAttribute("source")) ? element.getAttribute("source") : "default_citation";

		//give the citation a value if there isn't one to keep the script intact
		if(this.source == "default_citation") {
			element.setAttribute("source","default_citation");
		}

		//keep reminder params for Ajax calls
		this.loaded = new Boolean(false);
		this.loading = new Boolean(false);
		this.body = "loading";

		//keep this citations's object as a parameter of itself
		element.setAttribute("citation",this);
	}

	//do not want this used outside of this context
	function citeSource() {
		//keep showing as long as user is over
		hovering = new Boolean(true);

		//changes classes to over
		this.setAttribute("class","citation_over");
		this.setAttribute("className","citation_over");

		if(this !== window) {
			currentCitationValue = superCites[this.id].body;
			if(currentCitationValue != lastCitationValue) {
				//get the citation body again if needed
				//should check to see if citation has already been loaded and not load again
				citationGuts = currentCitationValue;
			}

			//remember the last value
			lastCitationValue = currentCitationValue;

		}

		
		//fill the citation area
		citationBody.innerHTML = citationGuts;

		//move the citation based on the event that triggered it
		//where was this element?  Look through its offsetParent heirarchy

		globalOffsetTop = 0;
		globalOffsetLeft = 0;
		
		var offsetParentObject = this.offsetParent;
		while(offsetParentObject != null && offsetParentObject != document) {
			globalOffsetLeft += offsetParentObject.offsetLeft;
			globalOffsetTop += offsetParentObject.offsetTop;
			offsetParentObject = offsetParentObject.offsetParent;
		}

		globalOffsetLeft = globalOffsetLeft + this.offsetLeft + this.offsetWidth;
		globalOffsetTop = globalOffsetTop + this.offsetTop;

		citationSpace.style.left = globalOffsetLeft + "px";
		citationSpace.style.top = globalOffsetTop + "px";

		//show citation space
		showCitation();

	}

	function clearCitationContainer() {
		//not hovering anymore
		hovering = new Boolean(false);
		//turn it off, and wait 4 seconds to try and clear the citation
		this.setAttribute("class","citation_off");
		this.setAttribute("className","citation_off");
		timeout = setTimeout("clearCitation();",4000);
	}

	//initial run.  do all of this
	//make all cite tags into citation objects
	main:
	{
		//find all citations by length
		var citations = document.getElementsByTagName("cite");
		superCites = new Array(citations.length);
		for(i = 0; i < superCites.length; i++) {
			superCites[i] = new Citation(citations[i],i);

			//don't make unnecessary citations.  If the source of this one
			//equals the source of a previous one, make it a reference to the previous
			//cuts down on the ajax calls
			checkDupes:
			for(j = 0; j < i; j++) {
				if (superCites[i].source == superCites[j].source) {
					superCites[i].element.setAttribute("citation", superCites[j].element.citation);
					superCites[i].element = superCites[j].element;
					superCites[i].unique = new Boolean(false);
					break checkDupes; 
				}
			}
		}
		
		//load citations through Ajax calls
		
		//function for loading sources
		function loadSources(thisCitation) {
			new Ajax.Request(thisCitation.source + ".cite", {
				method:"get",
				onSuccess: function(transport) {
					thisCitation.body = transport.responseText;
				},
				onFailure: function() {
					alert("citation request failed");
				}
			});
		}

		for(i = 0; i < superCites.length; i++) {
			//if(superCites[i].unique == true) {
				loadSources(superCites[i]);
			//}
		}
	}
}
