/*
*       Popup Window code here
*/

function get_popup_screen_coords()
{
	// get the x,y position of current window in screen coords
	var screenX = 0;
	var screenY = 0;

	// firefox rocks
	if ( window.screen.availLeft != null )
	{
		screenX = parseInt( window.screen.availLeft );
		screenY = parseInt( window.screen.availTop );
	}
	// ie sucks
	else
	{
		screenX = 0;
		screenY = 0;
	}

	return new Array( screenX, screenY );
}

function popunder( url, w, h )
{
	var screen_coords = get_popup_screen_coords();
	var x = screen_coords[ 0 ] + ( ( screen.width - w ) / 2 );
	var y = screen_coords[ 1 ] + ( ( screen.height - h ) / 2 );

	var params = "left=" + x + ",top=" + y + ",width=" + w + ",height=" + h + ",status=0,toolbar=0,scrollbars=1";
	window.open( url, "dgpopunderwindow", params ).blur();
	window.focus();
}

function OpenPopup( url, w, h )
{
	var x = ( screen.width - w ) / 2;
	var y = ( screen.height - h ) / 2;

	window.open( url, "PopupWindow", "left=" + x + ",top=" + y + ",width=" + w + ",height=" + h + ",status=0,toolbar=0,scrollbar=auto" );
}

function OpenAdminPopup( url, w, h )
{
	var x = ( screen.width - w ) / 2;
	var y = ( screen.height - h ) / 2;

	window.open( url, "PopupWindow", "left=" + x + ",top=" + y + ",width=" + w + ",height=" + h + ",status=no,toolbar=no,scroll=1" );
}

function OpenWindow( url, name, width, height, scrollbars, j )
{
	var screen_coords = get_popup_screen_coords();
	var screenX = screen_coords[ 0 ];
	var screenY = screen_coords[ 1 ];

	if ( width == 0 && height == 0 )
	{
		width = screen.width;
		height = screen.height;
	}

	// center the window based on screen coords + current window size + requested popup size
	var left = screenX + ( ( screen.width - width ) / 2 );
	var top = screenY + ( ( screen.height - height ) / 2 );
	
	if ( j == "top_right" )
	{
		left = screenX + ( screen.width - width );
		top = 0;
	}

	var params = "width=" + width + ",";
	params += "height=" + height + ",";
	params += "top=" + top + ",";
	params += "left=" + left + ",";
	params += "location=0,menubar=0,resizable=1,status=0,toolbar=0";
	params += iif ( scrollbars == true, ",scrollbars=1", ",scrollbars=0" );
	
	new_window = window.open( url, name, params );
    new_window.focus();
    
	return ( false );
}

function open_blank( url, name )
{
	window.open( url, name );
}

function CloseWindow()
{
	window.opener = self;
	window.close();
}

/*
*   useage ...
*
*   <a href="http://www.somedomain.com/folder/file.php?paramlist" rel="popup <win_name> width height top left [center] [fullscreen] [location] [menubar] [resizeable] [scrollbars] [status] [titlebar] [toolbar] ">a link</a>
*
*   The first 6 parameters are mandatory and must appear in the same order; the remaining options
*   are optional and can be in any order.
*
*   The list must be space delimited
*
*   popup: must be the first item listed in the list, this is the flag which indicates that the url is a popup
*   <win_name>: can be any valid window name or any of the values _blank, _parent, _self, or _top
*   width: the width of the popup window
*   height: the height of the popup window
*   top: the top position where the window is positioned
*   left: the left position where the window is positioned
*   [center]: optional, if specified the window will be centered in the screen, top & left will be over written
*   [fullscreen]: optional, if set top & left will be set to 0 and the width and height will be set to the screen width and height
*   [location]: optional, if set the address bar will be displayed otherwise it is off
*   [menubar]: optional, if set the menu bar will be displayed otherwise it is off
*   [resizeable]: optional, if set the window will be resizeable otherwise it is off
*   [scrollbars]: optional, if set the window will contain scrollbars when necessary otherwise it is off
*   [status]: optional, if set the window will contain status bar otherwise it is off
*   [titlebar]: optional, Whether or not to display the title bar. Ignored unless the calling application is an HTML Application or a trusted dialog box.
*   [toolbar]: optional, Whether or not to display the browser toolbar.
*
*   including the spec in the list activates that item in the new window, default is no or 0
*/

/*
*   Adds an event on load to traverse the <a> tags looking for any rel props set
*/
function addEvent(elm, evType, fn, useCapture)
{
	if ( elm.addEventListener )
	{
		elm.addEventListener( evType, fn, useCapture );
		return true;
	}
	else if ( elm.attachEvent )
	{
		var r = elm.attachEvent( 'on' + evType, fn );
		return r;
	}
	else
	{
		elm[ 'on' + evType ] = fn;
	}

    return ( false );
}

var wccWindow = null;

/*
*   closes the child window
*/
function closeWindow()
{
	if ( wccWindow != null )
	{
		if ( !wccWindow.closed )
        {
			wccWindow.close();
            wccWindow = null;
        }
	}

    return ( false );
}

/*
*   useage ...
*
*   <a href="http://www.somedomain.com/folder/file.php?paramlist" rel="popup <win_name> width height top left center fullscreen location directories menubar resizeable scrollbars status titlebar toolbar ">a link</a>
*
*   The first 6 parameters are mandatory and must appear in the same order; the remaining options
*   are optional and can be in any order.
*
*   The list must be space delimited
*
*   popup: must be the first item listed in the list, this is the flag which indicates that the url is a popup
*   <win_name>: can be any valid window name or any of the values _blank, _parent, _self, or _top
*   width: the width of the popup window; must be numeric
*   height: the height of the popup window; must be numeric
*   top: the top position where the window is positioned; must be numeric
*   left: the left position where the window is positioned; must be numeric
*   [center]: optional, if specified the window will be centered in the screen, top & left will be over written
*   [fullscreen]: optional, if set top & left will be set to 0 and the width and height will be set to the screen width and height
*   [location]: optional, if set the address bar will be displayed otherwise it is off
*   [directories]: optional, Whether or not to display the browser in full-screen mode. Default is no. A window in full-screen mode must also be in theater mode
*   [menubar]: optional, if set the menu bar will be displayed otherwise it is off
*   [resizeable]: optional, if set the window will be resizeable otherwise it is off
*   [scrollbars]: optional, if set the window will contain scrollbars when necessary otherwise it is off
*   [status]: optional, if set the window will contain status bar otherwise it is off
*   [titlebar]: optional, Whether or not to display the title bar. Ignored unless the calling application is an HTML Application or a trusted dialog box.
*   [toolbar]: optional, Whether or not to display the browser toolbar.
*   [noicon]: optional,
*
*   including the spec in the list activates that item in the new window, default is no or 0
*/

function doPopUp( e )
{
	// get the x,y position of current window in screen coords
	var screenX = 0;
	var screenY = 0;

	// firefox rocks
	if ( screen.availLeft != null )
	{
		screenX = parseInt( screen.availLeft );
		screenY = parseInt( screen.availTop );
	}

	//look for parameters
	var attribs = this.rel.split( " " );
    var win_name = attribs[ 1 ];
    var width = attribs[ 2 ];
    var height = attribs[ 3 ];
    var top = attribs[ 4 ];
    var left = attribs[ 5 ];

    var specs = "";

    if ( attribs.indexOf( "center" ) != -1 )
    {
    	// center the window based on screen coords + current window size + requested popup size
    	left = screenX + ( ( screen.width - width ) / 2 );
    	top = screenY + ( ( screen.height - height ) / 2 );
    }

    if ( attribs.indexOf( "fullscreen" ) != -1 )
    {
        top = 0;
        left = 0;
        width = screen.width;
        height = screen.height;
    }

    specs += "channelmode="+iif( attribs.indexOf( "channelmode" ) != -1, "yes", "no" );
    specs += ",location="+iif( attribs.indexOf( "location" ) != -1, "yes", "no" );
    specs += ",width="+width;
    specs += ",height="+height;
    specs += ",top="+top;
    specs += ",left="+left;
    specs += ",menubar="+iif( attribs.indexOf( "menubar" ) != -1, "yes", "no" );
    specs += ",directories="+iif( attribs.indexOf( "directories" ) != -1, "yes", "no" );
    specs += ",resizeable="+iif( attribs.indexOf( "resizeable" ) != -1, "yes", "no" );
    specs += ",scrollbars="+iif( attribs.indexOf( "scrollbars" ) != -1, "yes", "no" );
    specs += ",status="+iif( attribs.indexOf( "status" ) != -1, "yes", "no" );
    specs += ",titlebar="+iif( attribs.indexOf( "titlebar" ) != -1, "yes", "no" );
    specs += ",toolbar="+iif( attribs.indexOf( "toolbar" ) != -1, "yes", "no" );

    //call the popup script
     popUpWindow( this.href, win_name, specs );

	//cancel the default link action if pop-up activated
	if ( window.event )
	{
		window.event.returnValue = false;
		window.event.cancelBubble = true;
	}
	else if ( e )
	{
		e.stopPropagation();
		e.preventDefault();
	}

}

function popUpWindow(url, win_name, specs )
{
	closeWindow();
	wccWindow = window.open( url, win_name, specs );
	wccWindow.focus();
}

function findPopUps()
{
	var popups = document.getElementsByTagName( "a" );

	for (i = 0; i < popups.length; i++)
	{
		if ( popups[ i ].rel.indexOf( "popup" ) != -1 )
		{
			// attach popup behaviour
			popups[ i ].onclick = doPopUp;

			// add popup indicator
			if ( popups[ i ].rel.indexOf( "noicon" ) == -1 )
			{
				popups[ i ].style.backgroundPosition = "0 center";
				popups[ i ].style.backgroundRepeat = "no-repeat";
				popups[ i ].style.paddingLeft = "15px";
			}

			// add info to title attribute to alert fact that it's a pop-up window
			if ( popups[ i ].title.indexOf( "[Opens in pop-up window]", 0 ) == -1 )
			{
				popups[ i ].title = popups[ i ].title + " [Opens in pop-up window]";
			}
		}
	}
}

addEvent ( window, 'load', findPopUps, false );
