/** 
 * Writes the object tag for the DMC Flash Player
 * @access public
 * @param string Id
 * @return void
 */
jQuery.fn.injectFlashPlayer = function(id, format, quality, pc, mc, layer) {
	//creates a temporary div id that will be replaced by the object tag
	//created via the swfobject.embedSWF() function, the div id is adopted
	var videoWidth = 0;
	var videoHeight = 0;
	switch(layer) {
	case 0:
		var flashId = "flash" + id;
		var flashWidth = pc.width;
		var flashHeight = pc.height;
		$j("#" + id + " ." + playerClass).append("<div id=\"" + flashId + "\"></div>");
	break;
	case 1:
		var flashId = "hqflash" + id;
		var flashWidth = pc.hqWidth;
		var flashHeight = pc.hqHeight;
		// If the video is HQ and Live, the video stream could be encoded anamorphic and
		// needs to be strechted to its native size / correct aspect ratio
		if(mc.getIsLive() == true)
		{
			videoWidth = pc.hqWidth;
			videoHeight = pc.hqHeight;
		}
		$j("#" + id + hqVideoId).addClass(hqLayerClass);
		$j("#" + id + hqVideoId + " div").addClass(hqPlayerClass);
		$j("#" + id + hqVideoId + " ." + hqPlayerClass).append("<div id=\"" + flashId + "\"></div>");
	break;
	case 2:
		var flashId = "hqflash" + id;
		var flashWidth = pc.xqWidth;
		var flashHeight = pc.xqHeight;
		// If the video is HQ and Live, the video stream could be encoded anamorphic and
		// needs to be strechted to its native size / correct aspect ratio
		if(mc.getIsLive() == true)
		{
			videoWidth = pc.xqWidth;
			videoHeight = pc.xqHeight;
		}
		$j("#" + id + hqVideoId).addClass(xqLayerClass);
		$j("#" + id + hqVideoId + " div").addClass(xqPlayerClass);
		$j("#" + id + hqVideoId + " ." + xqPlayerClass).append("<div id=\"" + flashId + "\"></div>");
	break;
	}
	
	var dmc = new Object();
	dmc.id = id;
	dmc.flashId = flashId;
	dmc.type = mc.getType();
	dmc.isLive = mc.getIsLive();
	dmc.audioImage = mc.getAudioImage();
	dmc.previewImage = mc.getPreviewImage();
	dmc.subtitleUrl = mc.getSubtitleUrl();
	dmc.subtitleOffset = mc.getSubtitleOffset();
	
	dmc.flashWidth = flashWidth;
	dmc.flashHeight = flashHeight;
	dmc.videoWidth = videoWidth;
	dmc.videoHeight = videoHeight;
	
	dmc.startTime = pc.startTime;
	dmc.endTime = pc.endTime;
	dmc.autoplay = pc.autoplay;
	dmc.rememberTime = pc.rememberTime;
	
	dmc.config = "config=" + swfPath + "swf/config/config.xml";
	if (dmc.type == "audio") {
		dmc.config = "config=" + swfPath + "swf/config/config-audio.xml";
	}
	
	dmc.config += "&objectId=" + flashId;
		
	// Check the strings for server and stream and exchange them if server is empty (required by DMC Player)
	dmc.server = mc.getMediaArray()[format].getMediaStreamArray()[quality].getServer();
	dmc.stream = mc.getMediaArray()[format].getMediaStreamArray()[quality].getStream();
	if (dmc.server == "")
	{
		dmc.server = dmc.stream;
		dmc.stream = "";
	}
	
	// Use the swfobject library to write the object tag
	swfobject.registerObject(dmc.flashId, "10.0.0", swfPath + "swf/expressInstall.swf");
	var attribs = {
		width: dmc.flashWidth,
		height: dmc.flashHeight,
		style: "visibility: visible",
		data: swfPath + "swf/Aardvark.swf"
	};
	var params = {
		scale: "exactFit",
		swliveconnect: "true",
		id: dmc.flashId,
		name: dmc.flashId,
		wmode: "transparent",
		bgcolor: "#EEEEEE",
		swliveconnect: "true",
		allowfullscreen: "true",
		allownetworking: "all",
		flashvars: dmc.config
	};	
		
	dmc.swf = swfobject.createSWF(attribs, params, dmc.flashId);
	
	// Save all playerspecific setting into an global Array, that the dmc player can call it
	dmcObjectArray[dmc.flashId] = dmc;
	
	playerReady = function(dmcCallId) {
		dmcObjectArray[dmcCallId].swf.flEnableCallback(true);
		dmcObjectArray[dmcCallId].swf.flEnableTimer(true);
		dmcObjectArray[dmcCallId].swf.flAppendPlaylist(
			dmcObjectArray[dmcCallId].server,	// URL
			dmcObjectArray[dmcCallId].stream,	// Stream
			dmcObjectArray[dmcCallId].isLive, 	// isLive hides the schieberegler
			dmcObjectArray[dmcCallId].videoWidth,				// videowidth, 0 for auto
			dmcObjectArray[dmcCallId].videoHeight,				// videoheight, 0 for auto
			dmcObjectArray[dmcCallId].previewImage,	// previewImage
			dmcObjectArray[dmcCallId].autoplay, 	// auto
			"AUTO", 		// UI Mode
			true, 			// visible
			true,			// enableControls
			true,			// showControls
			dmcObjectArray[dmcCallId].startTime,	// startTime in seconds
			dmcObjectArray[dmcCallId].endTime,		// endTime in seconds
			[]				// param
		);
		
		if (dmcObjectArray[dmcCallId].type == "audio") {
			dmcObjectArray[dmcCallId].swf.flLoadAddOn(swfPath + "swf/addon/PersistentAddOnSound.swf", [dmcObjectArray[dmcCallId].audioImage, dmcObjectArray[dmcCallId].autoplay]);
		}
		if (dmcObjectArray[dmcCallId].subtitleUrl != "") {
			dmcObjectArray[dmcCallId].swf.flLoadAddOn(swfPath + "swf/addon/PersistentAddOnSubtitle.swf", [dmcObjectArray[dmcCallId].subtitleUrl, dmcObjectArray[dmcCallId].subtitleOffset]);
		}

		if (dmcObjectArray[dmcCallId].rememberTime && playerIdCurrentTimeArray[dmcObjectArray[dmcCallId].id] != undefined) {
			var offset = 2;
			if (playerIdCurrentTimeArray[dmcObjectArray[dmcCallId].id] > offset) {
				dmcObjectArray[dmcCallId].swf.flPlay();
				dmcObjectArray[dmcCallId].swf.flSeek(playerIdCurrentTimeArray[dmcObjectArray[dmcCallId].id] - offset);
			}
		}
		
		return true;
	}
	
	onPlayerStatus = function(dmcCallId, s) {
		
		switch(s){
			case "mediaFinished":
			case "playlistFinished":
				if (dmcObjectArray[dmcCallId].isLive == false){
					dmcObjectArray[dmcCallId].swf.flAppendPlaylist(swfPath + "swf/addon/PlaylistAddOnReplay.swf",  '', false, 0, 0, '', 'AUTOPLAY', 'AUTO', false, false, false, 0, 0, ['moviestopped', dmcCallId]);
				}
				return false;
			break;
			case "play":
			case "resume":
				// For multiinstance: pause all players if another is started
				for (i in dmcObjectArray) {
					// check if the player to stop is not the calling player
					//alert(i);
					if (i != dmcCallId) { //&& dmcObjectArray[i].swf.flPause != undefined) {
						try {	
							dmcObjectArray[i].swf.flPause(true);
						}
						catch (e) {
							//alert(e);
						};
					}
				}
			break;
			case "pause":
			case "stop":
			break;
		}		 
	 }
		
	onPlayerTime = function (dmcCallId, t) {
		// Remember the current time of a div id containing a player in a golbal array
		var currentTime = t.split("/");
		var currentTimeArray = (currentTime[0]).split(":");
		var currentTimeInSeconds = Number(currentTimeArray[0])*3600 + Number(currentTimeArray[1])*60 + Number(currentTimeArray[2]);
		playerIdCurrentTimeArray[dmcObjectArray[dmcCallId].id] = currentTimeInSeconds;
		//$j("#debug").text(currentTimeInSeconds);
	};
};
