var numberOfSeconds = 0;
var debug=false;
var counter = 0;
var tile = 0;
var isDcoptToBeInserted = true;
var ord = Math.random() * 100000000000000000;
var startingTile = 1;
var commercialDiv = "preplayDiv" // div id where commercial will play
var toggleIframe = 0;
var url = "";
var contentsOfCommercial = "";
var mainUrl = "";
var advertisingSystem = "doubleClickII";
var isMac = navigator.appVersion.indexOf("Macintosh") != -1;
var AD_REFRESH_IN_MINUTES = 3;

var player;
var endTag = "";
var timeoutID;
var duration;  // may be a repeat variable

var destroyDiv = 0;

// formerly these were test ads - one swf and one flv
var myTestSwf = "http://ad.doubleclick.net/pfadx/sw.test/video;test=afvideoswf;dcmt=text/xml;sz=640x480;ord=12345";
var myTestFlv = "http://ad.doubleclick.net/pfadx/sw.nol/actionarcade/waterballoon5/;gw=actionarcade;sec_0=waterballoon5;u=|gw-actionarcade|sec_0-waterballoon5;dcmt=text/xml;sz=640x480;ord=43112821765268570";

// myTestCombo tag would display both flv and swf's upon refresh (one at a time)
var myTestCombo = "http://ad.doubleclick.net/pfadx/sw.test/video;test=afvideoflvswf;dcmt=text/xml;sz=640x480;ord=12345";

var myTestXMLUrl = myTestCombo;
var fullTag = "";

var tempSwf = "";

// global variables to render swf
var swfSrc = "";
var swfDuration = "";
var swfStartTag = "";
var swfEndTag = "";
var swfClickTag = "";

/**
 * timer function which actually converts delayInMinutes into seconds. Calls
 * signatureOfJavascriptFunction every delayInMinutes seconds.
 */
function callAfterDelay ( signatureOfJavascriptFunction, delayInSeconds )
{
    var DEFAULT_DELAY_IN_SECONDS = 300;

    if ( signatureOfJavascriptFunction == null )
    {
        return false;
    }
    else
    {
        jsSignature = signatureOfJavascriptFunction;
    }

    if ( ( delayInSeconds == null ) || ( isNaN ( delayInSeconds ) ) )
    {
        delayInSeconds = DEFAULT_DELAY_IN_SECONDS;
    }

    var millisecs = convertSecondsToMilliseconds ( delayInSeconds );

    var timerId = window.setInterval ( signatureOfJavascriptFunction, millisecs );
}

function convertSecondsToMilliseconds ( numberOfSeconds )
{
    if ( ( numberOfSeconds == null ) || ( isNaN ( numberOfSeconds ) ) )
    {
        return 0;
    }
    else
    {
        return numberOfSeconds * 1000;
    }
}

/**
 * reloads the iframe
 */
function Reload()
{
    var oldOrd = ord;
    ord = Math.random() * 100000000000000000;
    for ( var i=1; i <= tile; ++i )
    {
        var iFrame = document.getElementById( 'iframe' + i );

        if ( iFrame != null )
        {
            var newSource = iFrame.src;
            var oldStr = '&ord=' + oldOrd;
            var newStr = '&ord=' + ord;

            newSource = newSource.replace ( oldStr, newStr );

            // Holds the document for the iframe
            var IFrameDoc;

            if ( iFrame.contentDocument )
            {
                // For Mozilla broswers
                IFrameDoc = iFrame.contentDocument;
            }
            else if ( iFrame.contentWindow )
            {
                // For IE5.5 and greater
                IFrameDoc = iFrame.contentWindow.document;
            }
            else if ( iFrame.document )
            {
                // For IE5
                IFrameDoc = iFrame.document;
            }
            else
            {
                return true;
            }

            IFrameDoc.location.replace( newSource );

        }
    }
}

function includeJavascript (url)
{
    document.write ('<scr');
    document.write ('ipt type="text/javascript" language="javascript" src="');
    document.write (url);
    document.write ('">');
    document.write ('</scr');
    document.write ('ipt>');
}

/**
 * returns true if browser is iframe compatible; otherwise, returns false.
 */
function isIFrameEnabled()
{
    var inUserAgent = navigator.userAgent;

    // alert( "userAgent: " + inUserAgent );

    // IE 5.2 or greater
    if ( ( inUserAgent.indexOf( "MSIE " ) != -1 ) )
    {
        var startIndex = inUserAgent.indexOf( "MSIE ") + 5;
        var version = parseFloat( inUserAgent.substring( startIndex, startIndex + 3 ) );
        if (  version >= 5.2 )
            return true;
    }

    // Firefox 1.5 or greater
    if ( inUserAgent.indexOf( "Firefox/" ) != -1 )
    {
        var startIndex = inUserAgent.indexOf( "Firefox/") + 8;
        var version = parseFloat( inUserAgent.substring( startIndex, startIndex + 3 ) );
        if ( version >= 1.5 )
            return true;
    }

    return false;
}

/**
 * Javascript doubleClickAd object
 */
function doubleClickAd ( tag, width, height, isDcoptNecessary )
{
    destroyDiv = 0;   // this means unless we see a swf use the parser to play an flv
    contentsOfCommercial = "";
    me = this;

    ++tile;
    me.isRefreshable = true;
    me.isPreplayCommercial = false;

    fullTag = "http://ad.doubleclick.net/pfadx/" + tag + "dcmt=text/xml;sz=640x480;ord=" + ord;

    /**
     * check if this is a preplay commercial - which is 640x480
     */
    if ( ( width == 640 ) && ( height == 480 ) )
    {
        startingTile = 2;
        me.isPreplayCommercial = true;
        me.isRefreshable = false;
        isDcoptNecessary = 0;          // we do not want dcopt setting in preplay commercials
    }
    else if ( ( width == 1 ) && ( height == 1 || height == 2 ) )
    {
        me.isRefreshable = false;
        isDcoptNecessary = 0;          // we do not want dcopt setting in tracking pixels
    }

    if ( ( tile > 1 ) || ( isDcoptNecessary == 0 ) )
    {
        isDcoptToBeInserted = false;
    }

    /**
     * checks to see if the browser is iframe compatible. If it is, then renders a refreshable
     * iFrame tag. If not, displays non-refreshable DoubleClick tag.
     */
    me.renderDoubleClickAd = function ()
    {
        // Exit if no tag has been supplied
        if ( !tag )
        {
            return;
        }

        /**
         * if refreshable via iframes, construct the iframe and src dynamically,
         * passing width, height, dcopt, ord, and tag in as query string
         * parameters.
         */
        if ( isIFrameEnabled () && ( me.isRefreshable ) )
        {
            var iFrameContents = '<div id="adTag_' + tile + '">';
            iFrameContents += '<iframe id="iframe' + tile + '" allowtransparency="true" ';
            iFrameContents += 'src="/html/adiframe.html?doubleClickTag=';
            iFrameContents += tag;
            iFrameContents += '&width=' + width;
            iFrameContents += '&height=' + height;
            iFrameContents += '&tile=' + tile;
            iFrameContents += '&dcopt=' + isDcoptToBeInserted;
            iFrameContents += '&ord=' + ord + '" ';
            iFrameContents += 'width="' + width + '" ';
            iFrameContents += 'height="' + height + '" ';
            iFrameContents += 'frameborder="0" scrolling="no" marginheight="0" marginwidth="0">';
            iFrameContents += '</iframe>';
            iFrameContents += '</div>';

            document.write ( iFrameContents );

            // Only start the reload timer on the first call to this function (tile=1)
            if ( tile == startingTile )
            {
                // call Reload() iframe's function after a delay
                callAfterDelay ( 'Reload()', AD_REFRESH_IN_MINUTES*60 );
            }
        }
        else if ( width == 1 && height == 2 )
        {
            // Do nothing for 1x2 ads in the v1 doubleclick tag system
        }
        else
        {
            if ( isDcoptToBeInserted )
            {
                // dcopt=ist inserted in tag
                var fullContents =  '<!-- begin ad tag  (tile=1) --> ' +
                                    '<script language="JavaScript" type="text/javascript">' +
                                    'document.write(\'<script language="JavaScript" ' +
                                    'src="http://ad.doubleclick.net/adj/' + tag +
                                    ';tile=' + tile + ';dcopt=ist;sz=' + width + 'x' + height + ';ord=' + ord + '?" ' +
                                    'type="text/javascript"></scr\' ' +
                                    '+ \'ipt>\');' +
                                    '</script><noscript><a href= ' +
                                    '"http://ad.doubleclick.net/jump/' + tag +
                                    'tile=' + tile + ';sz=' + width + 'x' + height + ';ord=' + ord + '?" ' +
                                    'target="_blank"><img src=' +
                                    '"http://ad.doubleclick.net/ad/' + tag +
                                    ';tile=' + tile + ';sz=' + width + 'x' + height + ';ord=123456789?" ' +
                                    'width="' + width + '" height="' + height + '" border="0" alt=""></a></noscript>' +
                                    '<!-- End ad tag -->';
            }
            else if ( ( advertisingSystem == "doubleClick" ) || ( ! me.isPreplayCommercial ) )
            {
                // dcopt=ist not found in this tag - should only be displayed once on a page
                var fullContents =  '<!-- begin ad tag  (tile=1) --> ' +
                                    '<script language="JavaScript" type="text/javascript">' +
                                    'document.write(\'<script language="JavaScript" ' +
                                    'src="http://ad.doubleclick.net/adj/' + tag +
                                    ';tile=' + tile + ';sz=' + width + 'x' + height + ';ord=' + ord + '?" ' +
                                    'type="text/javascript"></scr\' ' +
                                    '+ \'ipt>\');' +
                                    '</script><noscript><a href= ' +
                                    '"http://ad.doubleclick.net/jump/' + tag +
                                    'tile=' + tile + ';sz=' + width + 'x' + height + ';ord=' + ord + '?" ' +
                                    'target="_blank"><img src=' +
                                    '"http://ad.doubleclick.net/ad/' + tag +
                                    ';tile=' + tile + ';sz=' + width + 'x' + height + ';ord=' + ord + '?" ' +
                                    'width="' + width + '" height="' + height + '" border="0" alt=""></a></noscript>' +
                                    '<!-- End ad tag -->';

                // alert ( fullContents );
            }
            else
            {
                // alert ( "In else clause" );
                var fullContents = '<div id="ad1"></div>';
                fullContents += '<div id="preplayDiv">';
                fullContents += '<object id="preplayMovie" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"';
                fullContents += 'codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0"';
                fullContents += 'width="480" height="360" align="middle">';
                fullContents += '<param name="allowScriptAccess" value="always" />';
                fullContents += '<param name="movie" value="/swf/autoparser.swf" />';
                fullContents += '<param name="quality" value="high" />';
                fullContents += '<param name="bgcolor" value="#000000" />';
                fullContents += '<param name="FlashVars" value="dc_ref=' + fullTag + '" /> ';
                fullContents += '<embed src="/swf/autoparser.swf" quality="high" bgcolor="#000000" width="480" height="360" name="preplayMovie" align="middle"';
                fullContents += ' FlashVars="dc_ref=' + fullTag + '" ';
                fullContents += 'align="middle" allowScriptAccess="always" type="application/x-shockwave-flash"';
                fullContents += 'pluginspage="http://www.macromedia.com/go/getflashplayer" />';
                fullContents += '</object>';
                fullContents += '</div>';
            }
            document.write ( fullContents );
            // alert ( fullContents );
        }

        var inUserAgent = navigator.userAgent;

        if ( ( me.isPreplayCommercial && ( inUserAgent.indexOf( "Firefox/" ) != -1 )
                        && ( advertisingSystem == "doubleClick" ) ) )
        {
            // alert ( "we are in Firefox and there is a preplay" );

            if ( typeof( startPreplay ) == 'function' )
            {
                // alert ( "startPreplay is a function: " + startPreplay );
                startPreplay();
                PREPLAY.startErrorCheck();
            }
        }
    }
}

/**
 * Javascript doubleClickAdVersion2 object using new ad tag standard as of March 1, 2008
 */
function doubleClickAdVersion2( keyword, genre, royaltyPayable, width, height, isDcoptNecessary, testMode, kidsSafeAds )
{
    //alert ( "doubleClickAdVersion2() called with keyword=" + keyword + " and genre=" + genre + " width=" + width +" height=" + height + " isDcoptNecessary=" + isDcoptNecessary  );

    destroyDiv = 0;   // this means unless we see a swf use the parser to play an flv
    contentsOfCommercial = "";
    me = this;

    ++tile;
    me.isRefreshable = true;
    me.isPreplayCommercial = false;
    me.partnerAdTagInclude = '';

    /**
     * check if this is a preplay commercial - which is 640x480
     */
    if ( ( width == 640 ) && ( height == 480 ) )
    {
        startingTile = 2;
        me.isPreplayCommercial = true;
        me.isRefreshable = false;
        isDcoptNecessary = 0;          // we do not want dcopt setting in preplay commercials
    }
    else if ( ( width == 1 ) && ( height == 1 ) )
    {
        me.isRefreshable = false;
        isDcoptNecessary = 0;          // we do not want dcopt setting in tracking pixels
    }
    else if ( width == 1 && height == 2 )
    {
        me.isRefreshable = false;
        isDcoptNecessary = 0;          // we do not want dcopt setting in surveys
    }
    else if ( width == 3 && height == 3 )
    {
        me.isRefreshable = false;
        isDcoptNecessary = 0;
    }
    else if ( ( width == 180 && height == 60 ) || ( width == 300 && height == 135 ) )
    {
        me.isRefreshable = false;
    }

    if ( ( tile > 1 ) || ( isDcoptNecessary == 0 ) )
    {
        isDcoptToBeInserted = false;
    }

    me.setPartnerAdTagInclude = function( partnerAdTagIncludeString )
    {
        me.partnerAdTagInclude = partnerAdTagIncludeString; 
    }


    /**
     * checks to see if the browser is iframe compatible. If it is, then renders a refreshable
     * iFrame tag. If not, displays non-refreshable DoubleClick tag.
     */
    me.renderDoubleClickAdVersion2 = function ( adTargetId )
    {
        //alert ( "renderDoubleClickAdVersion2 called with keyword=" + keyword + " and genre=" + genre + " width=" + width +" height=" + height + " me.isRefreshable=" + me.isRefreshable  );
        
        // Exit if keyword and genre have not been supplied
        if ( !keyword || !genre )
        {
            return;
        }

        /**
         * if refreshable via iframes, construct the iframe and src dynamically,
         * passing width, height, dcopt, ord, and tag in as query string
         * parameters.
         */
        if ( isIFrameEnabled () && ( me.isRefreshable ) )
        {
            // Custom width for 728 ads.  The iframe should be 970 wide, but the ad size is still sent to dart as 728.
            var iframeWidth = width;
            if ( iframeWidth == 728 )
            {
                iframeWidth = 970;
            }

            var iFrameContents = '<div id="adTag_' + tile + '">';
            iFrameContents += '<iframe id="iframe' + tile + '" allowtransparency="true" ';
            iFrameContents += 'src="/html/adiframe.html';
            iFrameContents += '?width=' + width;
            iFrameContents += '&height=' + height;
            iFrameContents += '&tile=' + tile;
            iFrameContents += '&dcopt=' + isDcoptToBeInserted;
            iFrameContents += '&ord=' + ord;
            iFrameContents += '&keyword=' + keyword;
            iFrameContents += '&genre=' + genre;
            iFrameContents += '&royaltyPayable=' + royaltyPayable;
            iFrameContents += '&testMode=' + testMode;
            iFrameContents += '&kidsSafeAds=' + kidsSafeAds;
            iFrameContents += '&tagVersion=2" ';
            iFrameContents += 'width="' + iframeWidth + '" ';
            iFrameContents += 'height="' + height + '" ';
            iFrameContents += 'frameborder="0" scrolling="no" marginheight="0" marginwidth="0">';
            iFrameContents += '</iframe>';
            iFrameContents += '</div>';

            //alert( "iFrameContents=" + iFrameContents );

            document.write ( iFrameContents );

            // Only start the reload timer on the first call to this function (tile=1)
            if ( tile == startingTile )
            {
                // Delay calling the Reload() function to refresh the ad
                callAfterDelay ( 'Reload()', AD_REFRESH_IN_MINUTES*60 );
            }
        }
        else
        {
            var adPlacement = '';

            var royatlyPayableTagPart = "";
            if ( royaltyPayable == "true" )
            {
                royatlyPayableTagPart = "pay/";
            }

            var partnerTagPart = "";
            if ( me.partnerAdTagInclude != "" )
            {
                partnerTagPart = me.partnerAdTagInclude + "/";
            }

            if ( me.isPreplayCommercial )
            {

                fullTag = "http://ad.doubleclick.net/pfadx/sw.nol/apv/" + royatlyPayableTagPart + "c/" + partnerTagPart + genre + "/" + keyword + ";sec0=" + genre + ";sec1=" + keyword + ";testmode=" + testMode
                        + ";pos=atf;tag=pfadx;mtype=video;" + 'sz=640x480;tile=' + tile + ';' + 'dcmt=text/xml;u=mtype-video|tile-' + tile + ';' + 'ord=' + ord;

                adPlacement = '<div id="ad1"></div>';
                adPlacement += '<div id="preplayDiv">';
                adPlacement += '<object id="preplayMovie" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"';
                adPlacement += 'codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0"';
                adPlacement += 'width="640" height="480" align="middle">';
                adPlacement += '<param name="allowScriptAccess" value="always" />';
                adPlacement += '<param name="movie" value="/swf/autoparser.swf" />';
                adPlacement += '<param name="quality" value="high" />';
                adPlacement += '<param name="bgcolor" value="#000000" />';
                adPlacement += '<param name="FlashVars" value="dc_ref=' + fullTag + '" /> ';
                adPlacement += '<embed src="/swf/autoparser.swf" quality="high" bgcolor="#000000" width="640" height="480" name="preplayMovie" align="middle"';
                adPlacement += ' FlashVars="dc_ref=' + fullTag + '" ';
                adPlacement += 'align="middle" allowScriptAccess="always" type="application/x-shockwave-flash"';
                adPlacement += 'pluginspage="http://www.macromedia.com/go/getflashplayer" />';
                adPlacement += '</object>';
                adPlacement += '</div>';
            }
            // 1x2 survey tags have special format
            else if ( width == 1 && height == 2 )
            {
                // dcopt is how AdOps controls floating ads
                var adTagSite="sw.nol";

                if ( kidsSafeAds )
                {
                    adTagSite="swk.nol";
                }

                fullTag = "http://ad.doubleclick.net/adj/" + adTagSite + "/btf_j_s/" + royatlyPayableTagPart + genre + "/" + keyword + ";sec0=" + genre + ";sec1=" + keyword + ";testmode=" + testMode + ";pos=btf;tag=adj;mtype=standard;research=survey;!category=games;"
                + 'sz=' + width + 'x' + height + ';tile=' + tile + ';' + 'u=pos-btf||tag-adj|mtype-standard|sz-'+ width + 'x' + height + '|tile-' + tile + ';' + 'ord=' + ord + '?';

                adPlacement ='<script src="' + fullTag + '" type="text/javascript"></scr' + 'ipt>';
            }
            // 3x3 dart reskin tag
            else if ( width == 3 && height == 3 )
            {
                var adTagSite="sw.nol";
				//testMode = dynamicskin; 

				//sample tag
				// http://ad.doubleclick.net/adj/sw.nol/atf_i_s/_hp/_mn_home;sec0=_hp;sec1=_mn_home;testmode=dynamicskin;pos=atf;tag=adj;mtype=standard;sz=3x3;tile=9;u=pos-btf||tag-adj|mtype-standard|sz-3x3|tile-9;ord=82176153738747310?
				
				fullTag = "http://ad.doubleclick.net/adj/" + adTagSite + "/atf_i_s/" + royatlyPayableTagPart + genre + "/" + keyword + ";sec0=" + genre + ";sec1=" + keyword + ";testmode=" + testMode + ";pos=atf;tag=adj;mtype=standard;sz=3x3;tile="
						+ tile + ";u=pos-btf||tag-adj|mtype-standard|sz-3x3|tile-" + tile + ";ord=" + ord + "?";

                adPlacement ='<script src="' + fullTag + '" type="text/javascript"></scr' + 'ipt>';
            }
            else
            {
                // dcopt is how AdOps controls floating ads
                var dcopt = "";
                var dcopt2 = "";
                var adTagSite="sw.nol";

                if ( kidsSafeAds )
                {
                    adTagSite="swk.nol";
                }

                // Set dcopt values if this tag requires it
                if ( isDcoptToBeInserted )
                {
                    dcopt = "dcopt=ist;";
                    dcopt2 = "dcopt-ist|";
                }

                // Setup special 'node' tag value
                var node = "";
                if ( width == 1 && height == 2 )
                {
                    node = ";node=survey";
                }

                fullTag = "http://ad.doubleclick.net/adj/" + adTagSite + "/atf_i_s/" + royatlyPayableTagPart + genre + "/" + keyword + ";sec0=" + genre + ";sec1=" + keyword + ";testmode=" + testMode + node + ";pos=atf;tag=adj;mtype=standard;"
                + 'sz=' + width + 'x' + height + ';tile=' + tile + ';' + dcopt + 'u=mtype-standard|tile-' + tile + '|' + dcopt2 + 'ord=' + ord + '?';

                adPlacement ='<script src="' + fullTag + '" type="text/javascript"></scr' + 'ipt>';
            }

            // write to a jquery ID, else inline in the page
            if ( adTargetId )
            {
				// This jquery call will not work in IE for dcr games, have to use getElementById instead.  Nobody knows why!
                //$( adTargetId ).html( adPlacement );
				var adTargetDiv = document.getElementById( adTargetId );
                if ( adTargetDiv )
                {
                    adTargetDiv.innerHTML = adPlacement;
                }
			}
            else
            {
                document.write( adPlacement );
            }
        }

        var inUserAgent = navigator.userAgent;

        if ( me.isPreplayCommercial && inUserAgent.indexOf( "Firefox/" ) != -1 )
        {
            if ( typeof( startPreplay ) == 'function' )
            {
                startPreplay();
                PREPLAY.startErrorCheck();
            }
        }
    }
}

/**
 * events fired from the autoparser swf are handled here. For example, before we can loadAndPlay an
 * flv file, we must be in ready state. This could be changed to return a number of the legal starting state
 */
function handlePlayStateChangeEvent(state)
{
    if (state == "ready")
    {
        if ( destroyDiv == 1 )
        {
            // alert ( "not using player as we must destroy div" );
        }
        else
        {
            player = window.document['preplayMovie'];

            // this returns a call to setDartCommercial.
            player.loadDcXML ( fullTag );
        }
    }
    if (state == "ended")
    {
        // embedGameInDiv();
    }
}

/**
 * called from autoparser. Starts the video, requests startTag, sets Timer for div switch and
 * endTag request.
 */
function setDartCommercial(dc_startTag, dc_endTag, dc_type, dc_duration, dc_src, clickTag)
{
    var dartContents =  "*** arguments for setDartCommercial: \r\n";
    dartContents +=     "dc_startTag: " + dc_startTag + "\r\n";
    dartContents +=     "dc_endTag: " + dc_endTag + "\r\n";
    dartContents +=     "dc_type: " + dc_type + "\r\n";
    dartContents +=     "dc_duration: " + dc_duration + "\r\n";
    dartContents +=     "dc_src: " + dc_src + "\r\n";
    dartContents +=     "clickTag: " + clickTag + "\r\n";

    // alert ( dartContents );
    // alert ( "startTag " + dc_startTag );
    if ( ( !dc_src )  || ( dc_duration == "undefined" ) || ( dc_duration == null ) &&
           ( clickTag == "undefined" ) || ( clickTag == null ) )
    {
        // alert("dc_src is : " + dc_src);
        // leave the div alone
    }
    else if ( dc_type == "swf" )
    {
        // 5 frame black swf to play just so parser can unload
        // fixes Macintosh issue
        player.loadAndPlay ( "/a/swf/black.swf", 0 );

        swfSrc = dc_src;
        swfDuration = dc_duration;
        swfStartTag = dc_startTag;
        swfEndTag = dc_endTag;
        swfClickTag = clickTag;

        var swfTimer = setTimeout ( "renderSwf()", 1000 );
    }
    else
    {
        // alert ( "endTag: " + dc_endTag );
        endTag = dc_endTag;

        // alert("player in setDartCommercial: " + player + " and src: " + dc_src );
        player.loadAndPlay( dc_src, 0 );

        //alert(dc_startTag);
        dc_duration=parseInt( dc_duration );

        var impressionTimer = setTimeout ( "embedGameInDiv()", ( dc_duration + 1 ) * 1000 );

        //grace period
        dc_duration += 2;
        // alert ( 'preplay duration: ' + dc_duration );
        duration = dc_duration * 1000;
        updatePreplayDuration ( dc_duration );

        //alert(duration);
        var myAd1 = document.getElementById ( 'ad1' );

        if ( myAd1 != null )
        {
            // alert ( "found myAd1! and startTag: " + dc_startTag );
            myAd1.innerHTML = '<img src="' + dc_startTag + '" height="1" width="1">';
        }
        else
        {
            // alert ( "myAd1 is null!" );
        }
    }
}

/**
 * believe this may be called from player if there are issues.
 */
function playerTrouble(desc)
{
    // alert(desc);
    embedGameInDiv();
    // alert("playerTrouble called embedGameInDiv which returned to playerTrouble");
}

function testTimer ()
{
    // alert ( "in test timer" );
}

/**
 * sets the end tag in ad1
 */
function embedGameInDiv()
{
    // alert("embedGameInDiv: " + endTag );
    var adDiv = document.getElementById ( 'ad1' );

    if ( adDiv != null )
    {
        adDiv.innerHTML = '<img src="' + endTag + '" width="1" height="1">';
    }
    else
    {
        // alert ( 'adDiv is null' );
    }
}

/**
 * alert sent by parser.swf player in case error occurs. Used mostly in
 * debugging.
 */
function triggerAlert(alertText)
{
    // alert( " #4 in trigger: " + alertText);
}

/**
 * creates an object tag with for id found in path that is width x height
 * in size.
 */
function flashEmbed(path, id, width, height)
{
    // alert ( 'in FlashEmbed' );
    writeVar='<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,22,0" WIDTH="' + width;
    writeVar+='" HEIGHT="' + height + '" id="'+ id + '">';
    writeVar+='<PARAM NAME="movie" VALUE="' + path + '"><PARAM NAME="quality" VALUE="high">';
    writeVar+='<PARAM NAME="AllowScriptAccess" VALUE="always"><EMBED src="' + path + '"';
    writeVar+=' quality="high" WIDTH="' + width + '" HEIGHT="' + height + '" NAME="' + id + '"';
    writeVar+=' AllowScriptAccess="always" TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer">';
    writeVar+='</EMBED></OBJECT>';
    // alert( "in flashEmbed: " + writeVar);
    return writeVar;
}

/**
 * puts either start or end tag in ad1 div to track impressions.
 */
function renderImpression ( startOrEndTag )
{
    if ( endTag == "" )
    {
        // alert ( "not using end tag" );
    }
    else
    {
        startOrEndTag = endTag;
        endTag = "";
        // alert ( "start or end tag: " + startOrEndTag + " or end tag: " + endTag );
    }

    if ( ( startOrEndTag != 'undefined' ) || ( startOrEndTag != null ) )
    {
        // alert ( "** now trying to track impression: " + startOrEndTag );
        var adDiv = document.getElementById ( 'ad1' );

        if ( adDiv != null )
        {
            // alert ( "*** adding the start tag" );
            adDiv.innerHTML = '<img src="' + startOrEndTag + '" width="1" height="1">';
        }
        else
        {
            // alert ( 'adDiv is null' );
        }
    }
    else
    {
        // alert ( "*** start or end tag not showing up OK" );
    }
}

/**
 * to combat Macintosh issue of swf inside a swf, we will replace the div with the autoparser.swf
 * with an object tag of the swf source from DART.
 */
function renderSwf ()
{
    // called from timer
    // alert( "*** in renderSwf: " + swfSrc );
    swfDuration = parseInt( swfDuration );

    // alert ( "about to render " + dc_src + " and update duration to: " + dc_duration );
    var preplayArea = window.document.getElementById ( "preplayDiv" );

    if ( preplayArea != null )
    {
        // this issues an impression for the start tag
        renderImpression ( swfStartTag );

        swfSrc = swfSrc + "?clickTag=" + swfClickTag;

        // alert ( "*** contents: " + flashEmbed( swfSrc , "preplayMovie", 480, 360 ) );
        preplayArea.innerHTML = flashEmbed( swfSrc , "preplayMovie", 480, 360 );

        //grace period
        duration = parseInt ( swfDuration );
        endTag = swfEndTag;

        // sets the end tag in a timer
        var impressionDuration = duration + 1;
        var impressionTimer = setTimeout ( "renderImpression()", impressionDuration * 1000 );
        duration += 2;

        // alert ( 'preplay duration: ' + dc_duration );
        updatePreplayDuration ( duration );
    }
}
