/**
 *  Function used to print flash and movies inline
 *  
 *  Local version for Bondelaget is used to enable flashVars to be sent to the flashplayer for flv files. 
 *
 *  *) These settings might not work in all browser plugins
 *
 *  @src         string The full URL to the video
 *  @sessionName string The session name where users sessionid is stored (used for IE)
 *  @width       int The width
 *  @height      int The height
 *  @mimetype    string The files mimetype
 *  @loop        boolean Whether or not to loop the video *
 *  @autoplay    boolean Whether or not to start the video when it's loaded *
 *  @controller  boolean Whether or not to include plugin controllers *
 */
function BondeWriteMediaObject(src, sessionName, width, height, mimeType, loop, autoplay, controller, objId, flashVars, skipIEWindowOnLoad) {
    var objectSettings = '';
    
    if(typeof skipIEWindowOnLoad == 'undefied' && skipIEWindowOnLoad != true) {
        skipIEWindowOnLoad = false;
    }
    
    // Detect protocol for codebase
    var codebaseProtocol = (src.substr(0,5)=='https'?'https':'http');

    // Check if this is a flash movie
    var isFlashMovie = new Object();
    isFlashMovie['video/x-flv'] = true;
    isFlashMovie['application/x-flash-video'] = true;
    isFlashMovie['video/flv'] = true;
    if (isFlashMovie[mimeType] != null){
        var displayheight = null;
        // set to same as height for floating controls
        if(typeof flashVars != 'undefined' && typeof flashVars['floatingcontrols'] != 'undefined' && flashVars['floatingcontrols'] == 'true') {
	        var displayheight = height;
        }
        
	    //var flashVars = new Object();
	    flashVars["file"] = encodeURIComponent(src);
	    flashVars["width"] = width;
	    flashVars["height"] = height;
	    
	    if(displayheight != null) {
	        flashVars['displayheight'] = height;
	    }
	    
	    flashVars["overstrech"] = 'true';
	    flashVars["frontcolor"] = '0xFFFFFF';
	    flashVars["backcolor"] = '0x000000';
	    flashVars["lightcolor"] = '0xFFFFFF';
	    flashVars["autostart"] = autoplay?'true':'false';
	    flashVars["repeat"] = loop?'true':'false';
        flashVars["usefullscreen"] = 'true';

        // Add flash movie parameters
        objectSettings += '<param name="allowscriptaccess" value="always" />';
        objectSettings += '<param name="allowfullscreen" value="true" />';
        objectSettings += '<param name="flashvars" value="' + bondeGetQueryParamsFromObject(flashVars) + '" />';

        mimeType = 'application/x-shockwave-flash';
        src = CPLIBHTMLROOT +'/mediaplayer.swf';
    }

    // Handling IE specialties
    var rewriteMimeType = new Object();
    if (cpNavigatorUserAgentInfo.isIE){
        rewriteMimeType['audio/mpeg'] = 'application/x-mplayer2';
        rewriteMimeType['video/mpeg'] = 'application/x-mplayer2';
        rewriteMimeType['video/x-msvideo'] = 'video/x-ms-wmv';
    }
    // In some cases we must change the mimetype for IE :/
    if (rewriteMimeType[mimeType] != null){
        mimeType = rewriteMimeType[mimeType];
    }

    // Add object parameters
    if (mimeType != 'application/x-shockwave-flash') {
        objectSettings += '<param name="loop" value="' +(loop?'true':'false') +'" />';
        objectSettings += '<param name="autostart" value="' +(autoplay?'true':'false') +'" />';
        objectSettings += '<param name="controller" value="' +(controller?'true':'false') +'" />';
    } else {
        objectSettings += '<param name="wmode" value="opaque" />';
        if(typeof flashVars != "undefined") {
            objectSettings += '<param name="flashVars" value="' + bondeGetQueryParamsFromObject(flashVars) + '" />';
        }
    }

    // IE: We need to add sessionid to the URL in order for the video plugin to use the same session
    //     as IE. If sessionid is not sent, the video plugin will try to load the URL without a valid
    //     session causing the session to be destoyed.
    //     -----------------------------
    //     NOTE from Kurt: The session id broke together with the new JW FLV player. I first tried removing
    //     this alltogether, and it seem to work, event with user protected files. I can't get it
    //     to break whatever I do. To be sure not to reintroduce old bugs, I move this session stuff
    //     down here. This way it will be added for the non JW FLV players.
    if (cpNavigatorUserAgentInfo.isIE){
        var sep = (src.indexOf('?') > -1?'&':'?');
        var sessionID = cpReadCookie(sessionName);
        if (sessionID != null && sessionID.length > 0){
            var src = src +sep +sessionName +'=' +sessionID;
        }
    }

	// Build object tag and content
    var objectString = '';
    var objectStringStart = '<object type="' +mimeType +'" data="' +src +'" width="' +width +'" height="' +height +'">';
    var objectStringName = '<param name="movie" value="' +src +'" />';
    var objectStringFallback = '<p>(No plugin detected for your OS/browser)</p>';
    var objectStringEnd = '</object>';
    switch (mimeType) {
        case 'application/x-shockwave-flash':
            if (cpNavigatorUserAgentInfo.isIE){
                var objectStringStart = '<object codebase="'+codebaseProtocol+'://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="' +width +'" height="' +height +'">';
            }
            break;
        case 'application/x-mplayer2':
        case 'video/x-ms-wmv':
        case 'video/x-msvideo':
    		// WMV, MPEG
            objectStringName = '<param name="src" value="' +src +'" />';
    		break;

    	case 'video/quicktime':
    	    // Quicktime in IE
    	    if (cpNavigatorUserAgentInfo.isIE){
    	        objectStringStart = '<object classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" codebase="'+codebaseProtocol+'://www.apple.com/qtactivex/qtplugin.cab" width="' +width +'" height="' +height +'">';
                objectStringName  = '<param name="src" value="' +src +'" />';
                break;
    	    }
    	    break;

    	case 'video/x-ms-asf':
    	    // ASF in IE
    	    if (cpNavigatorUserAgentInfo.isIE){
                objectStringName = '<param name="FileName" value="' +src +'" />';
                break;
    	    }
    	    break;

    	default:
    		break;
    }

    objectString += objectStringStart;
    objectString += objectStringName;
    objectString += objectSettings;
    objectString += objectStringFallback;
    objectString += objectStringEnd;
    
    // If no objId is provided, the object is written directly to the document
    // at the current rendering position
    if(objId == null || objId == false) {
        document.write(objectString);
    } else {
        try {
        	// When altering the DOM while the DOM is still loading, Internet
        	// Explorer will sometimes trigger the DOM Ready event to early. This
        	// can cause the infamous Operation Aborted error. We prevent this by
        	// delaying the innerHTML assigning until the DOM is fully loaded.
        	if(cpNavigatorUserAgentInfo.isIE && !skipIEWindowOnLoad) {
    	    	CpAddEvent(window, 'load', function() {
    	    		document.getElementById(objId).innerHTML = objectString;
    	    	});
        	} else {
        		document.getElementById(objId).innerHTML = objectString;
        	}
        } catch(e){
    		document.write(objectString);
        }
    }
}

/**
 *  Whenever possible use prototype.js Object.toQueryString(object) instead.
 *  This function is used in cpWriteMediaObject to ensure backward compatibility
 *  with old sites that do not use prototype.js
 *
 *  Returns a query string (useful as flashvars) from a object and its
 *  attributes.
 *
 *  Example:
 *
 *  obj = new Object();
 *  obj["file"] = "testfile";
 *  obj["showControls"] = "true";
 *
 *  getQueryParamsFromObjectAttributes(obj) ==>
 *  file=testfile&showControls=true
 *
 *  @deprecated Use prototype.js Object.toQueryString(object) instead
 */
function bondeGetQueryParamsFromObject(object) {
    var query = "";
    for(var attr in object) {
        if(query.length > 0) {
            query += "&";
        }
        query += attr +"=" + encodeURIComponent(object[attr]);
    }
    return query;
}
