function PreplayObj (displayMarkup, customDaughterWindowURL)
{
    // alert ( 'in preplay objectia #1 ' );
    var me = this;

    me.displayMarkup = displayMarkup;
    me.customDaughterWindowURL = customDaughterWindowURL;
    me.timeout = undefined;
    me.preplayDuration = 15;
    me.preload = false;
    me.preloadComplete = false;
    me.errorDuration = 15;
    me.browserSupported = 'getElementById' in document;

    me.testMovie = function ()
    {
        // alert ('testMovie at ' + new Date () + "#5" );
        // Get the preplay flash movie
        var movie = document.getElementById ("preplayMovie");

        var preloaderMovie;
        if (me.preload)
        {
            // alert ( 'preloader movie part is TRUE' );
            preloaderMovie = document.getElementById ("preloaderMovie");
        }
        else
        {
            // alert ( 'preloader movie part IS FALSE!' );
        }

        // If a movie was found (it won't be if the ad is an image), check
        // to see if it's playing
        if (movie)
        {
            // alert ("Found preplay movie.");
            //If browser supports javascript function calls into the flash plugin, then
            //the typeof FrameNum will some value other than unknown
            var noLiveConnect = (typeof(movie.FrameNum) == 'unknown' || typeof(movie.FrameNum) == 'undefined');

            // debugging code
//            var debugMovie = "typeof movie.FrameNum: " + typeof(movie.FrameNum) + "\r\n";
//            debugMovie +=    "movie currentFrame: " + movie.CurrentFrame() + "\r\n";
//            debugMovie +=    "is preload complete? " + me.isPreloadComplete ();

            // alert ( debugMovie );

            if ((noLiveConnect || movie.CurrentFrame() > 1) && me.isPreloadComplete ())
            {
                clearTimeout (me.timeout);
                clearTimeout (me.errorTimeout);
                //pad the preplay duration if no liveConnect
                me.preplayDuration += (noLiveConnect ? 5 : 0);
                // alert ( 'preplay duration: ' + me.preplayDuration );
                me.timeout = setTimeout (me.showContent, me.preplayDuration * 1000);
                //alert ("Will show content in " + me.preplayDuration + " seconds.");
            }
            // If it's not playing set a timer to check again in a second
            else
            {
                // alert ("Will check testMovie again in one second.");
                me.timeout = setTimeout (me.testMovie, 1000);
            }
        }
        else
        {
            // alert ("Did not find preplay movie--will show content in " + me.preplayDuration + " seconds.");
            clearTimeout (me.timeout);
            clearTimeout (me.errorTimeout);
            me.timeout = setTimeout (me.showContent, me.preplayDuration * 1000);
        }
    }

    me.finishPreload = function ()
    {
        // alert ( 'finished preload' );
        me.preload = true;          // probably not needed
        me.preloadComplete = true;
    }

    me.isPreloadComplete = function ()
    {
        // alert ( 'checking if preload is complete' );
        if (me.preload)
        {
            // alert ( 'checking if preloading' );
            return me.preloadComplete;
        } else
        {
            return true;
        }
    }

    // Clears out the div that is displaying the ad and fills in the
    // html of the div that's displaying the content
    me.showContent = function ()
    {
        clearTimeout (me.timeout);
        clearTimeout (me.errorTimeout);
        if (me.customDaughterWindowURL != undefined)
        {
            document.location.href = me.customDaughterWindowURL;
        } else {
            if (me.browserSupported)
            {
                // This jquery call will not work in IE for dcr games, have to use getElementById instead.  Nobody knows why!
                //$("#gameCanvas").html( me.displayMarkup );
                var divE = document.getElementById ('gameCanvas');
                if ( divE )
                {
                    divE.innerHTML = me.displayMarkup;
                }
                $("#gameCanvas").show();
                $("#adWrap").remove();
                // Check if this game wants to run a javascript function by seeing if that function is defined
                if ( typeof( gamePlayLauncher) != "undefined" )
                {
                    gamePlayLauncher();
                }
            } else {
                document.location.href = "/"; // TODO: where do we go from here?
            }
        }
    }

    me.setPreplayDuration = function (duration)
    {
        // alert ("Preplay duration - duration: " + duration + " and preplayDuration: " + me.preplayDuration + " #3 ");
        me.preplayDuration = duration;
    }

    me.updatePreplayDuration = function ( newDuration )
    {
        // alert ( "now updating duration to: " + newDuration );
        me.setPreplayDuration ( newDuration );

        // alert ( "new duration = " + me.preplayDuration );
        clearTimeout (me.timeout);
        clearTimeout (me.errorTimeout);

        me.timeout = setTimeout ( me.showContent, me.preplayDuration * 1000 );
    }

    // called after errorDuration seconds to show content just in case.
    me.errorCheck = function ()
    {
        // alert ( 'in error check' );
        var movie = document.getElementById ("preplayMovie");
        var adImage = document.getElementById ("preplayImage");
        if (!movie && !adImage)
        {
            // alert ( '! movie or ad image' );
            clearTimeout (me.timeout);
            clearTimeout (me.errorTimeout);
            me.showContent ();
        }
        else
        {
            // alert ( 'movie and ad image' );
        }
    }

    // Starts testing the preplay movie for completion
    me.start = function ()
    {
        // alert ( 'in me start #4 ' );
        me.testMovie ();
    }

    // Start an error timeout as a fall-back in case preplay never loads
    me.startErrorCheck = function ()
    {
        // alert ( 'in start Error check #2 ' );
        me.errorTimeout = setTimeout (me.errorCheck, me.errorDuration * 1000);
    }
};

