
/** globals */
var AUTOPOP_CLASS = 'autopop-';
var AUTOPOP_EVENT = 'click';
	// note, AUTOPOP_EVENT - Opera uses onClick to trigger the href traversal, so in using onMouseUp duplicate windows open. click seems safer

/** stub */
function TRACE( m ) { }


/***
   * attachAutoPopEvents()
   * - for each anchor with a class that follows the auto-pop convention attach a pop-up window function
   */
function attachAutoPopEvents( )
	{
	
	/** check function dependencies & warn if not found */
	if ( typeof( JS_DOMCOMMON_LOADED ) != 'boolean' )
		{ TRACE("required library missing, have you loaded JS_DOMCOMMON in your page? Errors might follow."); }
	
	/** library code to assign auto-pop class name convention */
	var x = getElementsByAttribute( { attr : "class", value : AUTOPOP_CLASS, tag : "a" } );

	TRACE("attachAutoPopEvents() found "+x.length+" element containing '"+AUTOPOP_CLASS+"'");
	
	/** append 'on' prefix to event handler where it doesn't exist */
	if ( AUTOPOP_EVENT.substr( 1, 2 ) != 'on' )
		AUTOPOP_EVENT = 'on' + AUTOPOP_EVENT;
	
	/** attach event to each item */
	for ( var i = 0; i < x.length; i++) {
		/** wrap inside function to cater for discrepancies in event propegation - TODO url */
		x[i][AUTOPOP_EVENT] = function() { return autoPopWin( this ); }
		}
	
	return true;
	}

	
/***
   * autoPopWin()
   * - fairly standard popup window functionality, but dimensions are obtained from a given objects class attribute
   * - can be used independently to the above function, but obstensibly this is a private method & might change in the future
   * @obj - DOM node, expects an HTML anchor with href, target, and class attributes
   */
function autoPopWin( obj )
	{
	
	/** check for appropriate DOM functionality */
	if (! obj.getAttribute ) 
		{ TRACAE(); return false; }
	
	var objHref = obj.getAttribute("href");
	var objTarget = obj.getAttribute("target");
	var objClass = obj.className;
   	
	TRACE("autoPopWin - attributes href/target/class equal "+objHref+"/"+objTarget+"/"+objClass);
	
	/** check for empty or non-existent href's */
	if ( objHref == '' )
		{ TRACE("This anchor doesn't have a href, so i'm not opening a popup window"); return false; }

	/** check for target attribute, if not found set to _blank (ie. new window) */	
	if ( objTarget == '' )
		{ objTarget = '_blank';
		  TRACE("This anchor doesn't have a target, so i'm setting it to _blank"); }
	
	/** check for appropriate values in class */
	var w, h, s;
	var re = /-(\d+)x(\d+)(-s)?/;
	var e = re.exec( objClass );

	/** if regexp is matched */
	if ( e != null ) 
		{
		w = RegExp.$1;
    	h = RegExp.$2;

	    /** set scrollbar property to 'yes' is present in class name, defaults to 'no' */
   		s = RegExp.$3;
		s = ( s == "-s" ) ? "yes" : "no";
		}
	else 
		{ return true; } /** return true to ensure link is traversed */
		
	TRACE("autoPopWin - found width/height "+w+"/"+h);

	if ( ( isNaN( h ) || h == '' ) || ( isNaN( w ) || w == '' ) ) 
		{ TRACE("width or height is NaN"); return true; /** return true to ensure link is traversed */ }

	/** destroy references to existing window */
   	var win = null;	
			
	/** open window method, with the various params */
	win = window.open( objHref, objTarget, "resizable=yes,scrollbars="+s+",width="+w+",height="+h);
	
	/** in cases where window is already open we should resize, but not if document is making reference to itself */
	if( win && ( objTarget != '_self' ) )
		{ win.resizeTo( w, h ); }
		
	/** and finally bring the window to the forefront */
	win.focus();

	/** return false to prevent href onclick propagation */
	return false;

	}
