
// Add event handler to body when window loads
function addLoadEvent(func) {
	var oldonload = window.onload;
	
	if (typeof window.onload != "function") {
		window.onload = func;
	} else {
		window.onload = function () {
			oldonload();
			func();
		}
	}
}

addLoadEvent(function () {
	Callouts.fix();
	DocLinks.init();
/*	Styleswitcher.init(); */
});


/*---------------------------------------------------------------------------+
 | AnyFields - Add/remove "Any" value to fields that can accept blank values |
 +---------------------------------------------------------------------------*/
var AnyFields = {
	init : function () {
		// Check for functionality
		if (!document.getElementById || !document.getElementsByTagName) return false;
		
		// Get all inputs
		var fields = document.getElementsByTagName("input");
		
		for (var i = 0; i < fields.length; i++) {
			var theField = fields[i];
			
			// Check for class
			if (theField.className.indexOf("any") > -1) {
				// Only change value if it's initially empty
				if (theField.value == "") {
					theField.value = (theField.id == "q") ? "Search POSNA.org" : "Any";
				}
				
				// Add behaviors
				AnyFields.addBehaviors(theField);
			}
		}
	},
	
	addBehaviors : function(field) {
		// When focus is set to fields, remove default value and class so it looks normal
		field.onfocus = function() {
			if (this.value == "" || this.value == "Any" || this.value == "Search POSNA.org") {
				this.value = "";
			}
			
			this.className = this.className.replace(" any", "");
		};
		
		// When focus is removed, reset class and default value only if blank
		field.onblur = function() {
			if (this.value == "") {
				this.className += " any";
				
				this.value = (this.id == "q") ? "Search POSNA.org" : "Any";
			}
		};
	}
};


/*------------------------------------------------------------------------+
 | Callouts - Adjust widths of callouts depending on size of image within |
 +------------------------------------------------------------------------*/
var Callouts = {
	fix : function() {
		// Check for functionality
		if (!document.getElementById || !document.getElementsByTagName) return false;
		
		var bin = document.getElementById("content-primary");
		var arrBins = bin.getElementsByTagName("*");
		var classRE = /call-[lr]/gi;
		
		// Set div width = largest image width
		for (var i = 0; i < arrBins.length; i++) {
			if (classRE.test(arrBins[i].className)) {
				var images = arrBins[i].getElementsByTagName("img");
				
				if (images.length >= 1) {
					var maxWidth = images[0].offsetWidth;
					
					for (var j = 0; j < images.length; j++) {
						var curWidth = images[j].offsetWidth;
						if (curWidth > maxWidth) maxWidth = curWidth;
					}
					
					//if (maxWidth > 100) {
						arrBins[i].style.width = maxWidth + "px";
					//}
				}
			}
		}
		
		return false;
	}
};


/*----------------------------------------------+
 | DocLinks - Add icon after links to documents |
 +----------------------------------------------*/
var DocLinks = {
	init : function() {
		// Find all links
		var links = document.getElementsByTagName("a");
		
		for (var i = 0; i < links.length; i++) {
			var theLink = links[i];
			var address = theLink.href.toLowerCase();
			
			// Check if link points to files with common extensions
			var matches = address.match(/\.(doc|pdf|xls|ppt)/);
			
			if (matches) {
				// Using "match" always returns two results (not sure why)
				var ext = matches[0].substr(1, 3);
				
				// Create new image and insert it
				var newImg = document.createElement("img");
				newImg.alt = "(" + ext.toUpperCase() + ")";
				newImg.className = "icon";
				newImg.src = "/images/icon-" + ext + ".gif";
				newImg.title = newImg.alt;
				
				if (theLink.getElementsByTagName("img").length <= 0)
					theLink.parentNode.insertBefore(newImg, theLink);
				
				// Make link open in new window/tab
				theLink.onclick = function () {
					window.open(this.href);
					return false;
				};
			}
		}
	}
};



/*----------------------------------------------+
 | Styleswitcher - Create styleswitcher widgit  |
 +----------------------------------------------*/
/*
var Styleswitcher = {
	setActiveStyleSheet : function (title) {
		var i, a, main;
		for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
			if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
				a.disabled = true;
				if(a.getAttribute("title") == title) a.disabled = false;
			}
		}
	},
	
	getActiveStyleSheet : function () {
		var i, a;
		for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
			if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
		}
		return null;
	},
	
	getPreferredStyleSheet : function () {
		var i, a;
		for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
			if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("rel").indexOf("alt") == -1 && a.getAttribute("title")) return a.getAttribute("title");
		}
		return null;
	},
	
	createCookie : function (name,value,days) {
		if (days) {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
		} else expires = "";
		document.cookie = name+"="+value+expires+"; path=/";
	},
	
	readCookie : function (name) {
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		return null;
	},
	
	init : function () {
		var cookie = Styleswitcher.readCookie("style");
		var title = cookie ? cookie : Styleswitcher.getPreferredStyleSheet();
		Styleswitcher.setActiveStyleSheet(title);
		
		var ss = document.createElement("div");
		var ul = document.createElement("ul");
		var links = document.getElementsByTagName("link");
		
		for (var i = 0; i < links.length; i++) {
			if (links[i].rel.indexOf("stylesheet") > -1 && links[i].media == "screen") {
				var li = document.createElement("li");
				var a = document.createElement("a");
				var txt = document.createTextNode(links[i].title);
				
				a.href = "#";
				a.title = links[i].title;
				
				a.onclick = function() {
					Styleswitcher.setActiveStyleSheet(this.title);
					return false;
				}
				
				a.appendChild(txt);
				li.appendChild(a);
				ul.appendChild(li);
			}
		}
		
		ss.id = "ss";
		ss.appendChild(ul);
		document.body.appendChild(ss);
	}
};

window.onunload = function(e) {
	var title = Styleswitcher.getActiveStyleSheet();
	Styleswitcher.createCookie("style", title, 365);
}
*/