/* 
	Original script:  prepareLinks 
	From: "DOM Scripting" (Friends of Ed/Apress, 2005) by Jeremy Keith, pp. 86-88.
		[Also using addLoadEvent -- same source, pp. 102-104.]
	
	Modified version:  popupHandler
	By:  Spencer Sundell  http://spencersundell.com/  19 Feb 2006
	This script may be used for any non-evil purpose provided you do not sell it and/or 
	claim original authorship.
	If you wanna be a pal, please include this notice in your JavaScript source.  :-)


  USAGE:

	To declare custom height, append width & height vals as 3 digit combos.
	 Format: 	popupclassWWWHHH
	 Example:	popup215095	 ==  width: 215 pixels  height: 95 pixels
	 
	To declare/target a custom window name, add TARGET to your link.

	Use all defaults:
		<a href="pop.html" class="popup">launch</a>
	
	Declare custom dimensions, and use default window name:
		<a href="pop.html" class="popup200100">launch</a></p>
	
	Declare/target custom window name (optional: also declare custom dimensions as above):
		<a href="pop.html" class="popup" target="specialWindow">launch</a>
	  Warning:  this usage will launch new window no matter what. Mind your accessibility.
	  Also: if using frames/iframes, do not target an existing frame name.  Duh.
	
	Declare additional styles as desired BUT pop-up class MUST come first!
		<a href="pop.html" class="popup500100 redLink fooClass" target="widePopup">launch</a>
*/

/* [begin] */

/* Define pop-up globals and defaults: */
	/* CSS class used for ALL pop-ups */
		var popClass 	= "popup"; 
	/* pop-up defaults: optionally override in the link markup (see USAGE) */
		var popName 	= "popWin"; 
		var popW 		= 300;
		var popH		= 150;


/* 
	Initialize the pop-up handler script with onload.
	See line 116 in this file.
 */
addLoadEvent(popupHandler);


function popupHandler() {
	/* 
		Since you can define the pop-up class as anything, get it's length 
		so the script can handle later tests & value extraction.
	*/
	var cl = popClass.length;
	/* Access all the A tags on the page, then loop through them. */
	var a = document.getElementsByTagName("a");
	for (var i=0; i<a.length; i++) {
		/* 
			Now convert the link's class(es) into an array, in case there's more than 
			one declared.  I could prolly shorten this with some handler var names.
			  Note: The standard reference is .getAttribute("class"), but 
			    IE uses .className (sigh).  While .className does currently work in FF, 
				better to be forward-compatible. Thus, I added this ternary so that 
				the proper reference is used if available, while safely failing to 
				.className if we're in IE.
		*/		
		var ca = document.getAttribute ? a[i].getAttribute("class").split(" ") : a[i].className.split(" ");
		if (ca[0].substring(0, cl) == popClass) {
			a[i].onclick = function() {
				/* Now... Ick! A second array! Can I get rid of this? 
					The problem is that without it, the values of the last
					link on the page apply to ALL popup declarations.  No bueno.
					At least the first array gets overwritten with each link.
				*/
				var ca2 = document.getAttribute ? this.getAttribute("class").split(" ") : this.className.split(" ");
				/* Assign a short name to the extracted popup class for tidier code */
				var c = ca2[0];
				/* 
					Now detect the presence of custom dimension declarations.
					If present, use 'em; otherwise use the defaults set at top of file.
				*/
				var w = (c==popClass) ? popW : c.substring(cl, (cl+3));
				var h = (c==popClass) ? popH : c.substring((cl+3), (cl+6));
				/* Now ditto with the window target */
				var t = this.getAttribute("target") ? this.getAttribute("target") : popName;
				/* Now slap it all together and pass to the pop-up function */
				popUp(this.getAttribute("href"),t,w,h);
				/* And lastly, prevent the link from loading in the parent window. */
				return false;
			}
		}			
	}
}


/* Your usual popper-upper script, with centering added. */
function popUp(winURL,t,w,h) {
	var specs = "width="+w+",height="+h;
/*	Uncomment the following line and add/cutomize if desired (all defaults = 0 [off]):
		specs += ",status=1,menubar=1,location=1,toolbar=1,resizable=1,directories=1";
*/
	/* The next three lines handle the positioning: */
	var scrX=Math.round((screen.width/2)-(w/2)); var scrY=Math.round((screen.height/2)-(h/2));
	if (scrY > 100) { scrY = scrY - 40; } 
	specs += ',top='+scrY+',left='+scrX;
	var win = window.open(winURL, t, specs);	
	win.focus();
}


/* Jeremy Keith's onload initializer thingie.  See "DOM Scripting", pp. 102-104 */
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

/* [end] */
