
(function() {
    function getFlashVersion() {
        var UNDEF = "undefined",
            OBJECT = "object",
            SHOCKWAVE_FLASH = "Shockwave Flash",
            SHOCKWAVE_FLASH_AX = "ShockwaveFlash.ShockwaveFlash",
            FLASH_MIME_TYPE = "application/x-shockwave-flash",
            win = window,
            doc = document,
            nav = navigator;

        // Detect the flash version
        var w3cdom = typeof doc.getElementById != UNDEF && typeof doc.getElementsByTagName != UNDEF && typeof doc.createElement != UNDEF,
            playerVersion = [0,0,0],
            d = null;
        if (typeof nav.plugins != UNDEF && typeof nav.plugins[SHOCKWAVE_FLASH] == OBJECT) {
            d = nav.plugins[SHOCKWAVE_FLASH].description;

            if (d && !(typeof nav.mimeTypes != UNDEF && nav.mimeTypes[FLASH_MIME_TYPE] && !nav.mimeTypes[FLASH_MIME_TYPE].enabledPlugin)) {
                d = d.replace(/^.*\s+(\S+\s+\S+$)/, "$1");
                playerVersion[0] = parseInt(d.replace(/^(.*)\..*$/, "$1"), 10);
                playerVersion[1] = parseInt(d.replace(/^.*\.(.*)\s.*$/, "$1"), 10);
                playerVersion[2] = /r/.test(d) ? parseInt(d.replace(/^.*r(.*)$/, "$1"), 10) : 0;
            }
        }
        else if (typeof win.ActiveXObject != UNDEF) {
            var a = null, fp6Crash = false;
            try {
                a = new ActiveXObject(SHOCKWAVE_FLASH_AX + ".7");
            }
            catch(e) {
                try { 
                    a = new ActiveXObject(SHOCKWAVE_FLASH_AX + ".6");
                    playerVersion = [6,0,21];
                    a.AllowScriptAccess = "always";  // Introduced in fp6.0.47
                }
                catch(e) {
                    if (playerVersion[0] == 6) {
                        fp6Crash = true;
                    }
                }
                if (!fp6Crash) {
                    try {
                        a = new ActiveXObject(SHOCKWAVE_FLASH_AX);
                    }
                    catch(e) {}
                }
            }
            if (!fp6Crash && a) { // a will return null when ActiveX is disabled
                try {
                    d = a.GetVariable("$version");  // Will crash fp6.0.21/23/29
                    if (d) {
                        d = d.split(" ")[1].split(",");
                        playerVersion = [parseInt(d[0], 10), parseInt(d[1], 10), parseInt(d[2], 10)];
                    }
                }
                catch(e) {}
            }
        }

        return playerVersion;
    }

    var aFlashVersion = getFlashVersion(),
        strAdURL = 'http://search.spotxchange.com/ad_player/easi.swf',
        strObjectID = 'spotx_ad_container_99387ccc426b2b0f81a14f4b7469ed58',
        strFlashVars = 'env=&autoplay=1&channel_id=' + spotx_channel_id;

    if(typeof(spotx_sos_id) != 'undefined')
        strFlashVars += '&html_id=' + strObjectID;

    if(typeof(spotx_content_category) != 'undefined' && spotx_content_category != '')
	    strFlashVars += '&content_category=' + spotx_content_category;

    if(typeof(spotx_ad_type) == 'undefined' || spotx_ad_type == '')
	    spotx_ad_type = 'preroll';

    if(typeof(spotx_ad_max_duration) == 'undefined' || spotx_ad_max_duration == '')
	    spotx_ad_max_duration = '30';

    if(typeof(spotx_ad_volume) == 'undefined' || spotx_ad_volume == '')
	    spotx_ad_volume = '75';

    if(typeof(spotx_ad_bitrate) == 'undefined' || spotx_ad_bitrate == '')
	    spotx_ad_bitrate = 'high';

    if(typeof(spotx_content_format) == 'undefined' || spotx_content_format == '')
	    spotx_content_format = 'FlashVideo';

    // Use MP4 for Quicktime and when we have flash version 9.0.115 or higher
    if(spotx_content_format == 'Quicktime' ||
	    (aFlashVersion[0]>9 || (aFlashVersion[0]==9 && (aFlashVersion[1]>0 || (aFlashVersion[1]==0 && aFlashVersion[2]>=115)))))
	    strFlashVars+= '&media_format[]=MP4&media_format[]=FlashVideo';
    else
	    strFlashVars+= '&media_format=FlashVideo';

    strFlashVars+= '&content_format=' + spotx_content_format;
    strFlashVars+= '&ad_type=' + spotx_ad_type + '&VMaxd=' + spotx_ad_max_duration;
    strFlashVars+= '&media_transcoding=' + spotx_ad_bitrate;

    if(typeof(spotx_banner_size) != 'undefined' && spotx_banner_size != '')
    {
	    strFlashVars+= '&iab_imu=' + spotx_banner_size;

	    if(typeof(spotx_banner_container_id) != 'undefined' && spotx_banner_container_id != '')
		    strFlashVars+= '&banner_container_id=' + spotx_banner_container_id;
    }

    if(typeof(spotx_content_container_id) != 'undefined' && spotx_content_container_id != '')
    {
	    strFlashVars+= '&content_container_id=' + spotx_content_container_id;

	    if(typeof(spotx_content_width) == 'undefined' || typeof(spotx_content_height) == 'undefined' ||
		    spotx_content_width == '' || spotx_content_height == ''
            )
	    {
		    spotx_content_width = "320";
		    spotx_content_height = "240";
	    }
	    strFlashVars+= '&content_width=' + spotx_content_width;
	    strFlashVars+= '&content_height=' + spotx_content_height;
	    strFlashVars+= '&ad_volume=' + spotx_ad_volume;
    }

    if(typeof(spotx_content_page_url) == 'undefined' || spotx_content_page_url == '')
    {
        spotx_content_page_url = "";
    }
    strFlashVars+= '&content_page_url=' + spotx_content_page_url;

    /* Only include the spotx ad / banner generation code if we need to */
    function spotx_wait_for_element(e_id, func)
    {
	    var wait_handle = setInterval(function()
	    {
		    if(document.getElementById(e_id))
		    {
			    func();
			    clearInterval(wait_handle)
		    }
	    }, 250);
    };

    /* If we aren't generating an ad, provide a content sponsoring function for use by the publisher */
    if(typeof(spotx_content_container_id) == 'undefined' || spotx_content_container_id == '')
    {
        var spotx_exception_url = "http://search.spotxchange.com/exception/" + spotx_channel_id + "?error=Deprecated+use+of+EASI.+No+content+container+id+specified!";
        document.write('<scr'+'ipt type="text/javascript" src="' + spotx_exception_url + '"></scr'+'ipt>');
	    
        spotx_get_sponsor_url = function(content_url)
	    {
		    return content_url;
	    };
    }
    else
    {
        /* put our syndicator into the container! */      
        spotx_wait_for_element(spotx_content_container_id, function() {
            document.getElementById(spotx_content_container_id).innerHTML = '<object ' + 
                'id="' + strObjectID + '" type="application/x-shockwave-flash" ' +
                'data="' + strAdURL + '" width="' + spotx_content_width + '" height="' + spotx_content_height + 
                '">' +
                    '<param name="movie" value="' + strAdURL + '" />' +
                    '<param name="wmode" value="transparent"/>' +
                    '<param name="allowScriptAccess" value="always" />' +
                    '<param name="FlashVars" value="' + strFlashVars + '" />' +
                '</object>';
        });
    }
})();

