var searchField;
var logo;

// Minimum pixel width of page at various sizes
var SMALL = 750
var MEDIUM = 925
var LARGE = 1250

// Used when enforcing minimum page width
var minWidth = SMALL;
var wrapper;	

function init() {
	swapHeaderImages();
	keyDocuments();
	if(!document.getElementById("homepage")){
		widthLimiter();
	}
}

function keyDocuments() {

	var DISPLAY_COUNT = 5;
	var HIDDEN_CLASS = 'hiddenDocument';
	var SHOW_CLASS = 'document';

	var keyDocumentsList = document.getElementById('keyDocuments');
	if (keyDocumentsList) {
		var myDocuments = keyDocumentsList.getElementsByTagName('li');
		if (myDocuments) {
			// Hide all the documents.
			// Only the first 5 should be displayed by default, but just to be safe...
			// (Last item is a normal link so we don't touch it)
			for(var i = 0; i < myDocuments.length - 1; i++) {
				myDocuments[i].className = HIDDEN_CLASS;
			}
			// If there's no point in being random then display all the documents
			if (myDocuments.length - 1 <= DISPLAY_COUNT) {
				for(var i = 0; i < myDocuments.length - 1; i++) {
					myDocuments[i].className = SHOW_CLASS;	
				}
			} else {
				// Pick documents to display at random (changes their CSS styling)
				for(var i = 0; i < DISPLAY_COUNT; i++) {
					var pickingNext = true;
					// Pick documents until a hidden one is found
					while(pickingNext) {
						// Minus one because last list item is a non-document link
						var myRandom = Math.ceil(Math.random() * myDocuments.length - 1);
						var nextDocument = myDocuments[myRandom];
						// If this document is hidden then display it
						if(nextDocument.className==HIDDEN_CLASS) {
							nextDocument.className=SHOW_CLASS;
							pickingNext=false;
						}
					}				
				}
			}
		}
	}
	
}

function initSearchfield() {
	if (document.getElementById) {
		searchField = document.getElementById("terms");
		if (searchField) {
			searchField.onfocus = function() {
				if (searchField.value == "search") {
					searchField.value="";
				}
			}
		}
	}
}

function swapHeaderImages() {
	logo = document.getElementById("logo");
	var date = new Date();
	//imageNo = Math.ceil(Math.random() * 9);
	//logo.className = "christmasimage" + imageNo;
	imageNo = Math.ceil(Math.random() * 26);
	logo.className = "image" + imageNo;
}

function normalText() {
	var wrapper = document.getElementById("wrapper");
	minWidth = SMALL
	if (wrapper.className.search("normalText") < 0) {
		wrapper.style.display="none";
		wrapper.className = wrapper.className.replace("mediumText", "");
		wrapper.className = wrapper.className.replace("largeText", "");
		wrapper.className += " normalText";
		wrapper.style.display="block";
	}
}

function mediumText() {
	var wrapper = document.getElementById("wrapper");
	minWidth = MEDIUM
	if (wrapper.className.search("mediumText") < 0) {
		wrapper.style.display="none";
		wrapper.className = wrapper.className.replace("normalText", "");
		wrapper.className = wrapper.className.replace("largeText", "");
		wrapper.className += " mediumText";
		wrapper.style.display="block";
	}
}

function largeText() {
	var wrapper = document.getElementById("wrapper");
	minWidth = LARGE
	if (wrapper.className.search("largeText") < 0) {
		wrapper.style.display="none";
		wrapper.className = wrapper.className.replace("normalText", "");
		wrapper.className = wrapper.className.replace("mediumText", "");
		wrapper.className += " largeText";
		wrapper.style.display="block";
	}
}

// Minimum Width Limit
// Prevents the content pane from getting narrower than 750px in IE
// Other browsers support the css min-width property

function widthLimiter() {

	wrapper=document.getElementById('wrapper');	
	
	if(document.getElementById && navigator.appVersion.indexOf("MSIE")>-1 && !window.opera) {
		if(window.attachEvent) {
			window.attachEvent("onresize",doLimitWidth);
			window.attachEvent("onload",doLimitWidth);
		}
		else {
			onload=doLimitWidth;
			onresize=doLimitWidth;
		}
	}
	
}

function doLimitWidth() {

	// Sets the width of the page wrapper div if it gets too low

	if(wrapper && document.body && document.body.clientWidth) {
	
		if(parseInt(document.body.clientWidth)<=minWidth) {
			wrapper.style.width=minWidth+"px";
		} else {
			wrapper.style.width="auto";
		}
	}

}
