/*
JavaScript Library for Virginia Nursing History
Andrew Bain, albain@vcu.edu
*/

/* 
getWindowProperties: Returns browser appropriate values for available
display width and height. Thank you, Microsoft, for making this 
necessary. We all really appreciate it.

Parameters: whichValue -- String. Pass "width" and width will be returned, 
                          and its probably obvious what happens if "height"
                          is passed.
Returns: integer value of how wide or tall the viewable part of the window is.

Andrew Bain, albain@vcu.edu
October 2006
Originally developed with Virginia Nursing History web site
*/
function getWindowProperties(whichValue) {
	var ourHeight, ourWidth=0; // initialize variables
	if(typeof(window.innerHeight)=='number') { // Have to do it like this because IE pretends it knows what to do with this value, but it returns something bogus.
		ourHeight=window.innerHeight;  // works for, like, every browser but IE
		ourWidth=window.innerWidth;
		} 
	else if(document.documentElement&&(document.documentElement.clientHeight)) {
		ourHeight=document.documentElement.clientHeight; // works for IE 6
		ourWidth=window.innerWidth;
		}
	else if(document.body&&(document.body.clientHeight)) {
		ourHeight=document.body.clientHeight; // works for IE 4
		ourWidth=document.body.clientWidth;
		}
	if(whichValue=="width") { 
		return ourWidth; 
		}
	else { 
		return ourHeight; 
		}
	}

/* 
whereDidIPutThatThing: Figures out the x and y coordinates of a given 
element on a page.

Parameters: whichThing -- Some kind of element on the page. 
            whichValue -- String. Pass "x" and the x coords will be returned, 
                          and its probably obvious what happens if "y"
                          is passed.
Returns: integer, value of the relevant position of the given object.

Example: I want to know where a div with the id "picture-of-potatoes" is on the page and then move it three pixels to the left.

var whereThePotatoesAre=whereDidIPutThatThing("picture-of-potatoes","x");
document.getElementById("picture-of-potatoes").style.left=whereThePotatoesAre-3;

Andrew Bain, albain@vcu.edu
October 2006
Originally developed with Virginia Nursing History web site
*/
function whereDidIPutThatThing(whichThing,whichValue) {
	var ourLeft,ourTop=0; // Initialize variables
	if (whichThing.offsetParent) {
		ourLeft=whichThing.offsetLeft;
		ourTop=whichThing.offsetTop;
		while (whichThing=whichThing.offsetParent) {
			ourLeft+=whichThing.offsetLeft;
			ourTop+=whichThing.offsetTop;
		}
	}
	if (whichValue=='x') { 
		return ourLeft; 
		}
	else {
		return ourTop;
		}
	}

/* 
displayPin: Shows or hides one of the spiffy nursing pins. Actually, perhaps the 
            function would be more accurately named 
             displayOrStopDisplayingPinDependingOnTheCircumstances
            but, well, that's bulky.

Parameters: callingElement -- the element (usually a span) that's calling this
                              function.
            whichPin       -- String, gives the id of the particular pin we wanna
                              show

Andrew Bain, albain@vcu.edu
October 2006
Originally developed with Virginia Nursing History website
*/

var isThisThingOn=0; // Global variable to tell if a pin is already showing or not.
/* Note that this makes the kind of silly assumption that people are only going to click on one 
pin image at a time -- which is somewhat reasonable, since the things are so huge, but it will also 
be worth going back later and revising this code to account for multiple images being displayed. */

function displayPin(callingElement,whichPin) {
	xPosition=whereDidIPutThatThing(callingElement,'x'); // X-coordinate of the button calling for the pin
	yPosition=whereDidIPutThatThing(callingElement,'y'); // Y-coordinate of the button calling for the pin
	if(!isThisThingOn){ // If we're not already showing a pin, well then, show one!
		document.getElementById(whichPin).style.display='block';
		document.getElementById(whichPin).style.left=xPosition+'px';
		document.getElementById(whichPin).style.top=(yPosition+15)+'px';
		callingElement.style.backgroundImage="url(/tml/speccoll/nursing/media/pv-off.png)"; // Change button from "View Pin" to "Hide Pin"
		isThisThingOn=1; // Indicate that yes, a pin is currently being shown 
		}
	else {
		document.getElementById(whichPin).style.display='none'; // Stash it away
		callingElement.style.backgroundImage="url(/tml/speccoll/nursing/media/pv-on.png)"; // Change button back to "View Pin"
		isThisThingOn=0; // Reset indicator of whether or not a pin is visible
		}
	}

/* 
grabFromQueryString: Peters out a value from a variable name in the query string (foo.html?variable=value)

Parameters: which -- variable we want to get, so if the query string was expected to be, like:
                       potato.html?kind=yukongold
					 then we would get the value of "kind" like:
					   var kindOfPotato=grabFromQueryString("kind");
					 and kindOfPotato's value would then be "yukongold." tada!

Andrew Bain, albain@vcu.edu
October 2006
Originally developed for Virginia Nursing History web site
*/
function grabFromQueryString(which) {
	var queryString=window.location.search.substring(1); // This is so we start AFTER the "?"
	var variablesInString=queryString.split("&"); // Break at every ampersand
	for(i=0;i<variablesInString.length;i++) {
		var keyPair=variablesInString[i].split("="); // Then, break at the =
		if(keyPair[0]==which) {
			return keyPair[1];
			}
		}
	}

