var PopupFromFlash = function()
{
    // keep track of all the popups this object has popped up
	var popups = [];	
    
    // 'getter' function to access the popups array
	this.getPopups = function()
    {
        return popups;
    }	
    
    /**
     * Open a popup window using the url supplied in the parameter.
     *
     * @param url               The url to display in the new popup
     * @param params            Params for the new popup (ie, width, height, chrome settings, etc)
     * @param popupId           Unique number for each popup
     * @param flashContainerId  The id of the div in which the flash is embedded
     */
	this.openPopup = function( url, params, popupId, flashContainerId )
    {
        // create a popupname
		var popupName = "popupFromFlash_" + popupId;
        
        // window.open returns a reference to the popup, which we're going to store in newPopup
		var newPopup = window.open( url, popupName, params );
		if ( newPopup )
        {	
            // and if that popup worked, newPage will reflect that success
            // if you need a reference back to that popup, you can look in the popups array, so we'll add our recently popped up popup to that array
			popups.push( { id: popupId, popup: newPopup } );
				
            // and focus on it, to make sure it doesn't just disapear behind the other popups.
			newPopup.focus();
		}
        else
        {	
            // otherwise, the popup failed - which means we call the function specified by the actionscript PopupFromFlash class, which essentially defaults to opening a new popup in the normal manner
			if ( !document.getElementById( flashContainerId ) )
            {	
                // if we can't get this element, then either getElementById isn't supported, or the id is incorrect
				alert('Error: PopupFromFlash could not find a flash element with the id "' + flashContainerId + '" while attempting to pop up "' + url + '".');
			}
			document.getElementById( flashContainerId ).popupFailed( url );	// occasionally generates an error/warning, but doesn't prevent it from executing.
		}
	}
    
    /**
     * Open a popup window using the html contents supplied in the parameter.
     *
     * @param html              The html contents to display
     * @param popupId           The popupId to manipulate
     * @param flashContainerId  The id of the div in which the flash is embedded
     */
	this.setHtmlOfPopup = function( html, popupId, flashContainerId )
    {
        // create a popupname based on browser, default name, randomSeed options
		var popupToManipulate = this.getPopupById( popupId ).popup;
           
		if ( popupToManipulate )
        {	
            // the FR magic: write the supplied html into the freshly opened popup:
            popupToManipulate.document.write( html );
            popupToManipulate.document.close();
        }
        else
        {
            // otherwise, opening of the popup originally failed, make flash open a new window
			if ( !document.getElementById( flashContainerId ) )
            {	
                // if we can't get this element, then either getElementById isn't supported, or the id is incorrect
				alert('Error: PopupFromFlash could not find a flash element with the id "' + flashContainerId + '" while attempting to pop up "' + url + '".');
			}
			document.getElementById( flashContainerId ).popupSetHtmlFailed( html );
        }
	}
    
    this.getPopupById = function( id )
    {
        for ( var i=0; i < popups.length; i++ )  
        {            
            if ( popups[ i ].id == id ) 
            {                
                return popups[ i ]
            }
        }
        return null;
    }
}

// this ensures that there is an instance of the PopupFromFlash object available in the DOM for the .swf to access
this.popupFromFlash = new PopupFromFlash();	
