/** OnClick popup window link handler.
  * This event handler is bound to the document's [on]click event, and catches clicks
  * on HTML A links. It checks to see if the link has been styled as a popup and
  * executes some nice standards compliant and best practice popup code. 
  *
  * User-agents that do not support JavaScript or a modern DOM or event handlers.
  * Browers that are not supported by this method are: Netscape Navigator 4 and IE3. 
  * 
  * This file should be referenced like so:
  * <script type="text/javascript" defer="defer" src="http://images.tvnz.co.nz/tvnz_site_images/scripts/common.js"/>
  * The defer="defer" means that this script will only be executed after the rest of the page has loaded. 
  * This means that DOM will be complete and it will not hold up page loading and rendering. 
  * 
  * To easily utilise this functionality, use the <tvnz-js:link/> element (see template.xsl for examples).
  * If you want to use it the hard way - <a href="..." class="...">...</a> elements should have 
  * these classes applied to it:
  * - aPopup - mandatory - indicates a popup is required
  * - aPopupXXX - mandatory - XXX is a content type: e.g. Url, Image, Video, Audio, Screensaver, Wallpaper  
  * - aPopupWidthXXX - optional - XXX is the pixel size of the popup's width. 
  * - aPopupHeightXXX - optional - XXX is the pixel size of the popup's height. 
  * - aPopupTopXXX - optional - XXX is the pixel size of the popup's top offset. 
  * - aPopupLeftXXX - optional - XXX is the pixel size of the popup's left offset.
  * - aPopupScrollbars - option - enables scrollbars (not specified by default).
  */
var popupHandler = {};
popupHandler.features = function (features) {
	var s = '';
	for (f in features) {
		if (s !== '') s += ',';
		s += f + '=' + features[f];
	}
	return s;
};

/** (on)click event handler.
 * Check that the target is a &lt;a href="..."&gt; then 
 * executes a window.open with the appropriate window
 * features defined by 'content type'
 * @param {Object} e : Event object
 */
popupHandler.click = function (e) {
	try {
		var a = e.target ? e.target : (e.srcElement ? e.srcElement : null);
		// if it's an element and an A tag
		if (a && a.nodeType === 1 && a.nodeName === 'A') {
			// check if it's got the/a aPopup class 
			var classes = a.className;
			var rep = new RegExp('\\b(aPopup(\\w*))\\b','g');
			var p = null;
			var features = {};
			var popup = false;
			while ((p = rep.exec(classes)) !== null) {
				// is this a popup?
				popup = popup || p[1] === 'aPopup';
				// set window feature defaults based on the 'content type' of the popup					 
				if (p[2] !== null) switch (p[2].toLowerCase()) {
					case 'video': features = {width:410,height:503}; break; //left:50,right:50
					case 'audio': features = {width:300,height:177}; break; //left:50,right:50
					case 'competition': features = {width:544,height:480,scrollbars:'yes'}; break; //left:50,right:50,status:'yes'
					case 'wallpaper':
					case 'screensaver': features = {width:524,height:339}; break; //left:50,right:50
				}
			}
			// images, URLs, and custom popups have their features encoded (and overridden) as classes
			var rewhtl = new RegExp('\\baPopup(Width|Height|Top|Left)(\\d+)\\b','g');
			var whtl = null;
			while ((whtl = rewhtl.exec(classes)) !== null) features[whtl[1].toLowerCase()] = whtl[2];
			var res = new RegExp('\\baPopupScrollbars\\b');
			if (res.test(classes)) features.scrollbars = 'yes';
			if (features.length) features.resizable = 'yes'; // always resizeable
			if (popup) open(a.href,a.target,popupHandler.features(features)).focus();
		}
	} catch (e) {
		// if there is an error stop (collaborate and listen).
		//e.preventDefault();
		//e.stopPropagation();
		// actually do nothing - the link will still work							
	}
}

if (document.addEventListener) {
	document.addEventListener('click',popupHandler.click,false);
} else if (document.attachEvent) {
	document.attachEvent('onclick',popupHandler.click);
}

