var gMasthead;


function initPage(backColor, mastheadTextColor, footerTextColor)
	{
	initMasthead(backColor, mastheadTextColor);
	document.getElementById('footerContainer').style.color = footerTextColor;
	padDropCapsInBoxes();
	}
			
			
function padDropCapsInBoxes()
	// This puts a non-breaking space on either side of the drop caps text
	// to provide some padding on both the left and the right of the drop cap itself.
	// We do this because IE6 does not honor the CSS text-indent. 
	{
	var x;
	var dropCaps = getElementsByClass("dropCapInBox");
	
	for (x = 0; x < dropCaps.length; x++)
		{
		dropCaps[x].innerHTML = "&nbsp;" + dropCaps[x].innerHTML +  "&nbsp;";
		}
	}
	
	
function initMasthead(backColor, mastheadTextColor)
	{
	gMasthead = new Masthead();
	gMasthead.setColors(backColor, mastheadTextColor);
	document.getElementById('curtain').style.visibility = "visible";
	document.getElementById('dockContainer').style.visibility = "visible";
	var elements = getElementsByClass("dockImage");
	var numElements = elements.length;
	var x;
	var element;
	for (x = 0; x < numElements; x++)
		{
		element = elements[x];
		element.onclick = function(event){clickedMasthead(event);};
		element.onmouseover = function(event){var element = mouseEventToElement(event); rollover(element, 2);};
		element.onmouseout = function(event){var element = mouseEventToElement(event); rollover(element, 1); element.blur();};
		element.onmouseup = function(event){var element = mouseEventToElement(event); element.blur();};
		}
	}
	

function getValidEvent(rawEvent)
	{
	return rawEvent || window.event;
	}
	
	
function clickedElement(rawEvent)
	{
	var goodEvent = getValidEvent(rawEvent);
	return goodEvent.target || goodEvent.srcElement;
	}

function clickedMasthead(rawEvent)
	{
	var validEvent = getValidEvent(rawEvent);
	var elementClicked = clickedElement(rawEvent);
	
	if (elementClicked.nodeName == 'IMG' && 
		elementClicked.className == 'dockImage')
		{
		if (validEvent.stopPropagation)
			validEvent.stopPropagation();
		else
			validEvent.cancelBubble = true;
		}
	else
		{
		gMasthead.moveCurtain();
		}
	}



// See comments appended below

function Masthead() 
	{
	// Private Variables ------------------------------------------------
	var my = this;			// Needed because JavaScript sets "this" incorrectly for nested functions.
	var mCurtainElement = document.getElementById('curtain');
	var mLogoElement = document.getElementById('logo');
	var mCurtainRaiseTask = null;
	var mCurtainLowerTask = null;
	var mCurrentOffset = 0;
	var mMaxOffset = -100;
	var mMoveInterval = 10;
	var mMoveAmount = 4;
	var mRaiseCurtain = false;
		
	
	// Public Variables ------------------------------------------------
	//this.publicVariable = null;
	
				
	// Private Functions ------------------------------------------------
		
	function raise() 
		{	
		if (mCurrentOffset > mMaxOffset)
			{
			mCurrentOffset -= mMoveAmount;
			if (mCurrentOffset < mMaxOffset)
				mCurrentOffset = mMaxOffset;
			mCurtainElement.style.top = mCurrentOffset + "px";
			return;
			}
		if (mCurtainRaiseTask)
			{
			clearInterval(mCurtainRaiseTask);
			mCurtainRaiseTask = null;
			}
		}
		
		
	function lower() 
		{	
		if (mCurrentOffset < 0)
			{
			mCurrentOffset += mMoveAmount;
			if (mCurrentOffset > 0)
				mCurrentOffset = 0;
			mCurtainElement.style.top = mCurrentOffset + "px";
			return;
			}
		if (mCurtainLowerTask)
			{
			clearInterval(mCurtainLowerTask);
			mCurtainLowerTask = null;
			}
		mLogoElement.src = mLogoElement.src;		// force animated gif logo to replay
		}
		
	
	// Privileged Functions ------------------------------------------------
	
	
	this.moveCurtain = function() 
		{
		if (mCurtainRaiseTask)
			{
			clearInterval(mCurtainRaiseTask);
			mCurtainRaiseTask = null;
			}
		if (mCurtainLowerTask)
			{
			clearInterval(mCurtainLowerTask);
			mCurtainLowerTask = null;
			}
		mRaiseCurtain = !mRaiseCurtain;
		if (mRaiseCurtain)
			mCurtainRaiseTask = setInterval(raise, mMoveInterval);
		else
			mCurtainLowerTask = setInterval(lower, mMoveInterval);
		};
		
		
	this.setColors = function(backColor, textColor)
		{
		document.getElementById('masthead').style.backgroundColor = backColor;
		document.getElementById('dockContainer').style.backgroundColor = backColor;
		document.getElementById('dock').style.backgroundColor = backColor;
		document.getElementById('curtain').style.backgroundColor = backColor;
		
		document.getElementById('curtainTitle').style.color = textColor;
		document.getElementById('hammurabi').style.color = textColor;
		labels = getElementsByClass('dockText');
		var x;
		for (x = 0; x < labels.length; x++)
			labels[x].style.color = textColor;
		};
		
	}
	
	
	// Public Functions
	//Dock.prototype.publicMethod = function (string) return "hi"; } 
	
	
// The object pattern above provides private, privileged, & public scope for
// functions and variables
//
// PRIVATE VARIABLES
//		Accessible only to private and privileged functions of this object
// PUBLIC VARIABLES
//		Accessible to all functions everywhere
// PRIVATE FUNCTIONS
//		Accessible only to private & privileged functions of this object
//		Can access private, privileged, and public functions & variables of this object
// PRIVILEGED FUNCTIONS
// 		Accessible to functions outside this object
// 		Has access to private, privileged, & public functions & variables of this object
// PUBLIC FUNCTIONS
//		Accessible to all functions everywhere
//
// Due to scope issues, refrain from using public functions & variables; 
// use privileged functions instead to access private variables.
//
// One good reason to use public functions and public variables, instead of 
// simply using privileged functions and private variables everywhere, is 
// to save storage - every object will have its own copy of every 
// private & privileged function & variable, but there will be only one 
// copy of each public function & variable per class.
	
	
	
// ---------------------------------------------------------


function removeIEPngFixCruft(imgSrc)
	{
	var offsetToImageSrc = 0; 
	var iePngFixCruft = imgSrc.lastIndexOf('=');
	
	if (iePngFixCruft !== -1)
		offsetToImageSrc = iePngFixCruft + 1;
	return imgSrc.slice(offsetToImageSrc, imgSrc.length);
	}
	
	
function rollover(img, version)
	// Call this function from both onmouseover and onmouseout event handlers attached to an image element.
	// Pass in the image element itself, i.e., "this", and the version of the image to swap in.
	// For this to work we assume 4 things:
	//		1) There are 2 image files used for the rollover effect.
	//		2) Both files end with the same 3 character extension, i.e., .png, .gif, or .jpg (not .jpeg).
	//		3) The names of the files are exactly the same except for the last character before the extension.
	//  	4) The last character of the file name serves as the version indicator.
	// So, for example, if we had 2 files, fred1.png and fred2.png, we would 
	// call this rollover function like this:
	//		<img src='fred1.png' onmouseover='rollover(this, 2);' onmouseout='rollover(this, 1);'>
	{
	var	imgSrc = removeIEPngFixCruft(img.src);
	var filenameMinusVersion = imgSrc.slice(0, imgSrc.length - 5);
	var extension = imgSrc.slice(imgSrc.length - 4, imgSrc.length);
	img.src = filenameMinusVersion + version + extension;
	}
	
	
	
	
	