/******************************************************************/
/*ARD Blankplayer Version 1.19.5							*/
/******************************************************************/
/* (C)  DMC 01 Consulting & Development GmbH - gerald.urbas@dmc01.at */
/******************************************************************/
/* Micorosoft Media Player Frontend Code Plugin in                */
/******************************************************************/
/* Object generates Media Player HTML code                        */
/* can get evolved for styled buttons. able to provide            */
/* MMP-Interface player plugin                                    */
/******************************************************************/

function microsoftmedia_Playercode(pt,vf) {
	this.name = "Windows Media"; // player frontend name
	this.shortname = "WMP"; // player frontend name
	this.typ = "microsoftmedia"; // Type
	this.detectionplugin = "Windows Media Player"; // detection plugin string
	this.objid = "MEDIAPLAYERCONTROL"+Math.round(Math.random()*10000); // Object ID 4 HTML
	this.playertype = pt; // home or extended
	this.videoformat = vf; // home or extended or simple
	this.connurls = false;// initially there are no urls
	this.previewimage = false;
	this.playercontrols={// special HTML tag properties for embedding, and object code in HTML frontend
		'simple':{
			'embed':'uiMode="mini" ShowControls="1" ShowStatusBar="0" ShowPositionControls="0" ShowTracker="1"',
			//'embed':'uiMode="full" EnableFullScreenControls="1"  ShowControls="1" ShowStatusBar="1" ShowPositionControls="0" ShowTracker="0"',
			'object':'<param name="uiMode" value="mini" /><param name="ShowControls" value="1" /><param name="ShowStatusBar" value="0" /><param name="ShowPositionControls" value="0" /><param name="ShowTracker" value="0" />'
			},
		'home':{
			'embed':'transparentAtStart="true" wmode="transparent" uiMode="mini"  ShowControls="1" ShowStatusBar="0" ShowPositionControls="0" ShowTracker="0"',
			'object':'<param name="transparentAtStart" value="true"> <param name="wmode" value="transparent" /><param name="uiMode" value="mini" /><param name="ShowControls" value="1" /><param name="ShowStatusBar" value="0" /><param name="ShowPositionControls" value="0" /><param name="ShowTracker" value="0" />'
			},
		'extended':{
			'embed':'uiMode="full" EnableFullScreenControls="1"  ShowControls="1" ShowStatusBar="1" ShowPositionControls="0" ShowTracker="0"',
			'object':'<param name="uiMode" value="mini" /><param name="ShowControls" value="1" /><param name="ShowStatusBar" value="1" /><param name="ShowPositionControls" value="0" /><param name="ShowTracker" value="1" />'
			}
	};
	
	this.playercontrolsheight=WMPSIZE; // all Players generet there own control heigts... wo correction values definined here
	// there are replaceable strings defined in player.js, %varname%.. used in this strings
	// embed HTML code
	this.playerstring = '<div id="'+this.objid+'" class="playerwmpposition">'
                    + 'Dieser Text wird durch das Windows Media Plugin ersetzt.'
                    + '</div> '
										+'';


	this.debug = function (str) { // debuglayer is deativated @ moment
		dbg("WMP plugin: "+str);
	};
	
	this.testscriptconn  = function () {
		if ($j.browser.msie == true || ($j.browser.mozilla ==true  && isWMVPatchInstalled()) ) {
			try {
				var obj = document.getElementById(this.objid);
				try {
					dbg("testscriptconn OK"+obj.controls.currentItem+"!");
					if (obj.controls.currentItem!=null) {
						return true;
					} else {
						return false;
					}
				} catch(e) {
					dbg("testscriptconn KO");
					return false;
				};
				dbg("testscriptconn KO"+obj);
				dbg("testscriptconn KO"+obj.currentItem);
			} catch(e) {
				return false;
			};
		}
		return false;
	};
	
	this.testversion = function() {
		this.is_scriptable = false;
		if (this.is_scriptable == false && $j.browser.msie == true && parseInt(jQuery.browser.version)>=6) {
			var teststring = '<OBJECT ID="testPlayer6" CLASSID="CLSID:22D6f312-B0F6-11D0-94AB-0080C74C7E95"><PARAM name="uiMode" value="invisible"></OBJECT>';
			$j("BODY").append(teststring);
			try {
				obj = document.getElementById('testPlayer6');
				this.exactwmpVersioninfo = this.version = 6;
				$j('#testPlayer6').remove();
			} catch(e) {
				$j('#testPlayer6').remove(); 
			};
		}
		if (this.is_scriptable == false && $j.browser.msie == true && parseInt(jQuery.browser.version)>=6) {
			var teststring = '<OBJECT ID="testPlayer7" CLASSID="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6"><PARAM name="uiMode" value="invisible"></OBJECT>';
			$j("BODY").append(teststring);
			try {
				obj = document.getElementById('testPlayer7');
				this.exactwmpVersioninfo = obj.versionInfo;
				this.version = obj.versionInfo.split('.')[0];
				$j('#testPlayer7').remove();
				if (this.version>=7) {
					this.is_scriptable = true;
				}
			} catch(e) {
				$j('#testPlayer7').remove(); 
			};
		}

		if ($j.browser.mozilla ==true && isWMVPatchInstalled()) {
			this.is_scriptable = false;
			try	{
				var teststring = '<OBJECT ID="testPlayerFF" CLASSID="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6"><PARAM name="uiMode" value="invisible"></OBJECT>';
				$j("BODY").append(teststring);
				tryobj = document.getElementById('testPlayerFF');
				dbg("!!!!Fullscreeen"+tryobj.fullscreen);
				if (tryobj.fullscreen!="undefined") {
					this.is_scriptable = true;
				}
				$j('#testPlayerFF').remove();
			} catch (e)	{
				$j('#testPlayerFF').remove();
			}
		}
	};
	
	
	this.playercall= function() {// callback after rendering. RP doesnt use it here.
		var tgtobj = arguments[0]; // get conn param
		var conn = arguments[1]; // get conn param
		this.testversion();
		this.debug("scriptable "+this.is_scriptable);
		this.debug("version full "+this.exactwmpVersioninfo);
		this.debug("version major "+this.version);
		$j("#"+tgtobj).empty();
		
		var activeXobject = '<OBJECT ID="'+this.objid+'" CLASSID="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6" type="application/x-oleobject" width="%W%" height="%OBJECTH%">';
            activeXobject += '<param name="AutoSize" value="0" />';
            activeXobject += '<param name="width" value="%W%"/>';
            activeXobject += '<param name="height" value="%OBJECTH%" />';
            activeXobject += '<param name="currentPosition" value="%STARTAT%" />';
            activeXobject += '<PARAM name="uiMode" value="full" />';
            activeXobject += '<param name="URL" value="%URL%" />';
            activeXobject += '<PARAM name="fullScreen" value="false" />';
            activeXobject += '<PARAM name="windowlessVideo" value="true" />';
            activeXobject += '<param name="AutoStart" value="%AUTOSTART%" />';
            activeXobject += '<param name="enableContextMenu" value="true" />';
            activeXobject += '<param name="stretchToFit" value="false" />';
            activeXobject += '</OBJECT>';

		//var activeXobjectFullscreen = 'activeXobjectFullscreen<OBJECT ID="'+this.objid+'" CLASSID="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6" type="application/x-oleobject" width="%W%" height="%OBJECTH%"><param name="currentPosition" value = "%STARTAT%" /><PARAM name="uiMode" value="full"><param name="URL" value="%URL%" /><PARAM name="fullScreen" value="true"><PARAM name="windowlessVideo" value="false"><param name="AutoStart" value="%AUTOSTART%" /></OBJECT>';

		var iEobject = '<object width="%W%" height="%EMBEDH%" '
					      +'classid="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6" '
					      +'codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701" '
					      +'standby="Loading Microsoft Windows Media Player components..." type="application/x-oleobject">'
					      +'<param name="url" value="%URL%"/>'
                          +'<param name="currentPosition" value = "%STARTAT%" />'
					      +'<param name="uiMode" value="full"/>'
					      +'<param name="AutoSize" value="0"/>'
					      +'<param name="AutoStart" value="%AUTOSTART%"/>'
						  +'<param name="EnableFullScreenControls" value="true"/>'
					      +'<param name="loop" value="false"/>'
					      +'<embed type="application/x-mplayer2" '
					      +'pluginspage="http://microsoft.com/windows/mediaplayer/en/download/" '
					      +'showcontrols="true" uimode="full" width="%W%" height="%EMBEDH%" '
					      +'src="%URL%" AutoStart="%AUTOSTART%" loop="false">';

		if (this.playertype=='home') {
			activeXobject = '  <object  ID="'+this.objid+'"  width="%W%" height="%OBJECTH%" classid="CLSID:22D6F312-B0F6-11D0-94AB-0080C74C7E95"><param name="url" value="%URL%"><param name="Filename" value="%URL%"><param name="currentPosition" value = "%STARTAT%" /><param name="ShowTracker" value = "false" /><PARAM name="uiMode" value="full"><PARAM name="fullScreen" value="false"><PARAM name="windowlessVideo" value="true"><PARAM name="ShowPositionControls" value="false"><PARAM name="AllowScan" value="true"></object>';
		}
		/*var iEobjectFullscreen = '<object name="'+this.objid+'" id="'+this.objid+'" '
								+ ' width="%W%" height="%EMBEDH%" classid="CLSID:22D6f312-B0F6-11D0-94AB-0080C74C7E95"'
								+ 'standby="Loading Windows Media Player components..."'
								+ 'type="application/x-oleobject"'
								// + 'codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,4,7,1112"'
								+ 'name="wmp" border="0">'
								+ '<param name="FileName" value="%URL%" />'
								+ '<param name="fullScreen" value="true">'
								// + '<param name="URL" value="%URL%" />'
								+ '%OBJECTCONTROLS%'
								+ '<param name="currentPosition" value = "%STARTAT%" /><param name="AutoStart" value="%AUTOSTART%" />'
								+ '</object>';*/
		var fFembed = '<embed type="application/x-mplayer2" name="'+this.objid+'" id="'+this.objid+'" '
								+ ''
								+ 'currentPosition="%STARTAT%" width="%W%" height="%EMBEDH%" border="0" '
								+ 'pluginspage="http://www.microsoft.com/netshow/download/player.htm" '
								+ 'src="%URL%" '
								+ '%EMBEDCONTROLS% '
								+ 'AutoSize="0" '
								+ 'AutoStart="%AUTOSTART%" DisplaySize="4" currentPosition"%STARTAT%" />'
								+ '</embed>';
								
		var SafariEmbed = '<embed type="application/x-mplayer2" name="'+this.objid+'" id="'+this.objid+'" '
								+ ''
								+ 'currentPosition="%STARTAT%" width="%W%" height="%EMBEDH%" border="0" '
								+ 'pluginspage="http://www.microsoft.com/netshow/download/player.htm" '
								+ 'src="%URL%" '
								+ '%EMBEDCONTROLS% '
								+ 'AutoSize="0" '
								+ 'AutoStart="%AUTOSTART%" DisplaySize="4" currentPosition"%STARTAT%" />'
								+ '</embed>';
								
		var FFObjectEmbed = '<object  id="'+this.objid+'" ' // name="'+this.objid+'"
								+ ' width="%W%" height="%EMBEDH%" type="application/x-ms-wmp" '
								+ 'data="%URL%" '
								+ 'name="wmp" border="0">'
								+ '<param name="SRC" value="%URL%" /><param name="URL" value="%URL%" /><param name="enableContextMenu" value="1">'
								+ '<param name="AutoSize" value="0" />'
								+ '<param name="AutoStart" value="%AUTOSTART%" /><param name="EnableFullScreenControls" value = "1" /><PARAM name="ShowPositionControls" value="true"><param name="ShowTracker" value = "true" /><param name="currentPosition" value = "%STARTAT%" /><PARAM name="uiMode" value="full"><PARAM name="windowlessVideo" value="false"><param name="stretchToFit" value="false">'
								+ '</object>';
		if (this.is_scriptable==false) {
			FFObjectEmbed = '<object  id="'+this.objid+'" ' // name="'+this.objid+'"
						+ ' width="%W%" height="%EMBEDH%" type="application/x-mplayer2" '
						+ 'data="%URL%" '
						+ 'name="wmp" border="0">'
    					+ '<param name="AutoSize" value="0" />'
						+ '<param name="SRC" value="%URL%" /><param name="URL" value="%URL%" /><param name="enableContextMenu" value="1">'
						+ '<param name="AutoStart" value="%AUTOSTART%" /><param name="EnableFullScreenControls" value = "1" /><PARAM name="ShowPositionControls" value="false"><param name="ShowTracker" value = "false" /><param name="currentPosition" value = "%STARTAT%" /><PARAM name="uiMode" value="full"><param name="URL" value="%URL%" /><PARAM name="windowlessVideo" value="false"><param name="stretchToFit" value="false">'
						+ '</object>';
		}
								//+ '%OBJECTCONTROLS%'
								//+ '<param name="currentPosition" value = "%STARTAT%" /><param name="AutoStart" value="%AUTOSTART%" />'
								//+ '</object>';
		this.debug("WMP Loading info hack");
		$j("#"+tgtobj).hide();
		$j("#"+tgtobj).parent().find('#wmploadinginfo'+tgtobj).empty();
		$j("#"+tgtobj).parent().find('#wmploadinginfo'+tgtobj).remove();
		$j("#"+tgtobj).after('<div id="wmploadinginfo'+tgtobj+'" class="statusmsg statusmsgconn'+conn+'">TEST</div>');
		try {
			loading($j("#wmploadinginfo"+tgtobj));
		} catch(e){};
		if (jQuery.browser.mozilla && this.is_scriptable==false) {
			$j("#wmploadinginfo"+tgtobj).load(ERRORPATH+'error/wmploading_ff_missingplugin'+APPHTMLEXTENSION,function() {
				$j("#wmploadinginfo"+tgtobj).fadeOut(8000,function(){
					$j("#"+tgtobj).fadeIn();
					$j("#"+tgtobj).append('<div class="ffplugininfo" style="text-align:center;" />');
					$j("#"+tgtobj+" .ffplugininfo").load(ERRORPATH+'error/wmploading_ff_missingpluginlink'+APPHTMLEXTENSION);
				});
			});
			this.playercontrolsheight.simple.embed += WMPSIZEFFPLUGIN;
			this.playercontrolsheight.home.embed += WMPSIZEFFPLUGIN;
			this.playercontrolsheight.extended.embed += WMPSIZEFFPLUGIN;
		} else {
			$j("#wmploadinginfo"+tgtobj).load(ERRORPATH+'error/wmploading'+APPHTMLEXTENSION,function() {
				$j("#wmploadinginfo"+tgtobj).fadeOut(3500,function(){
					$j("#"+tgtobj).fadeIn();
				});
			});
		};
		if ($j.browser.msie == true && this.is_scriptable==true) {
			this.debug("wmp IE sciptable "+conn);
			$j("#"+tgtobj).append(this.replace(activeXobject,conn));
		}
		if ($j.browser.msie == true && this.is_scriptable==false) {
			this.debug("wmp IE not sciptable");
			$j("#"+tgtobj).append(this.replace(iEobject,conn));
			//alert($j("#"+tgtobj).html());
		}
		this.popupcode=iEobject;
		if ($j.browser.msie != true) {
			this.debug("wmp NO IE");
			if (jQuery.browser.safari) {
				this.debug("embeding WMP 4 safari");
				$j("#"+tgtobj).append(this.replace(SafariEmbed,conn));
			} else {
				$j("#"+tgtobj).append(this.replace(FFObjectEmbed,conn));
			}
			this.popupcode=fFembed;
			this.debug("embeding WMP finished");
			
			if (this.dowmpdebug) {
				var wmp = document.getElementById(this.objid);

				str = "testing PlayerObject";
				for (i in wmp) {
					str += i+ "; <br />";
				}
				this.wmpdebug(str);
			}

		}
		//this.play();
		try	{
			//player.play();
			//window.setTimeout("player.play();", 3000);			
		} catch (e)	{ dbg(e)}
		

		
	};
	this.enabled = true; // we are here -  so we use this object.
	// add public methods
	this.write = writeplayercode;
	this.replace = replaceVars;
	this.setsizes = setsizes;
	this.setconn = setconn;
	this.setplayertype = setplayertype;
	this.setautostart = setautostart;
	this.setglobid = setglobid;
	this.getglobid = getglobid;
	this.call_popup = playerPopup;
	this.close_popup = playerPopupClose;
	this.call_carrier = carrierPopup;
	this.setPreviewimage = setPreviewImage;
	this.getPreviewimage = getPreviewImage;
	this.previewImage = previewImagecall;
	//this.genUi = generateUi;
	// add inline methods 	
	this.popupcode = "";
	this.dowmpdebug = wmpdebug;
	
	this.wmpinitDebug = function () {
		$j('#infopane').after('<p id="wmpvideodebugclear">Debugausgabe l&ouml;schen</p><p id="wmpvideodebug" style=""></p>');
		var cleareobj = $j("#wmpvideodebugclear"); // generate Clear button for info and help on bottom
		cleareobj.bind("click", function(){ // bind click
			$j("#wmpvideodebug").empty();
		});	
	};
	
	this.wmpdebug = function (str) {
		if (this.dowmpdebug) {
			if ($j("#wmpvideodebugclear").html() == null) {
				this.wmpinitDebug();
			}
			$j("#wmpvideodebugclear").show();
			$j("#wmpvideodebug").show();
			$j("#wmpvideodebug").append(str+"<br />");
		}
	};
	
	this.prepareHqMode = function() {
		dbg(startat+" wmp init prepareHqMode ");
		try {
			var obj = document.getElementById(this.objid);
			this.wmpdebug(this.starttime+" wmp this.starttime prepareHqMode 1");
			this.wmpdebug(startat+" wmp startat prepareHqMode 1");
			this.starttime = obj.controls.currentPosition;
			startat = this.starttime;
			this.wmpdebug(this.starttime+" wmp this.starttime prepareHqMode 2");
			this.wmpdebug(startat+" wmp startat prepareHqMode 2");
			obj.close();
			return this.starttime;
		} catch(e) {
			
		}
		return false;
	};
	this.prepareCloseHqMode = function() {
		try {
			var obj = document.getElementById(this.objid);
			this.wmpdebug(this.starttime+" wmp this.starttime prepareCloseHqMode 1");
			this.wmpdebug(startat+" wmp startat prepareCloseHqMode 1");
			this.starttime = obj.controls.currentPosition;
			startat = this.starttime;
			this.wmpdebug(this.starttime+" wmp this.starttime prepareCloseHqMode 2");
			this.wmpdebug(startat+" wmp startat prepareCloseHqMode 2");
			obj.close();
			return this.starttime;
		} catch(e) {
			return false;
		}
	};
	this.popup = function() { // this function is used by the popup window... (see doc there)
		var code=this.popupcode;
		conn=3;
		code = this.replace(code,conn);
		return '$j("#popupcontent").empty();$j("#popupcontent").append(\''+code+'\')';
	};
	this.getconn = function () {
		return this.connurls;
	};
	this.report = function () {
		return "Player:"+this.name+" obid:"+this.objid+" url:"+this.connurls;
	};
	this.shutdown = function () {
		var tgtobj = arguments[0]; // get conn param
		$j("#"+tgtobj).parent().find('.ffplugininfo').remove();
		window.clearInterval(WMPINTERVALLCHECKER);
		return;
	};
	// play button
	
	this.play = function () {
		if (this.is_scriptable==true) {
			this.debug("WMP internal play");
			var obj = document.getElementById(this.objid);
			try {obj.controls.play();} catch(e) {};
			try {obj.play();} catch(e) {};
		} else {
			this.debug("unscriptable WMP internal play");
		}
	};
	// pause button
	this.pause = function () {
		if (this.is_scriptable==true) {
			this.debug("WMP internal pause");
			var obj = document.getElementById(this.objid);
			try {
			obj.controls.pause();
			} catch(e) {
			obj.pause();
			}
		} else {
			this.debug("unscriptable WMP internal pause");
		}
	};
	
	// stop button
	this.stop = function () {
		if (this.is_scriptable==true) {
			this.debug("WMP internal stop");
			var obj = document.getElementById(this.objid);
			try {
			obj.controls.stop();
			} catch(e) {
			obj.stop();
			}
		} else {
			this.debug("unscriptable WMP internal stop");
		}
	};
	
	this.fastf = function () {
		if (this.is_scriptable==true) {
			this.debug("WMP internal fastf");
			var obj = document.getElementById(this.objid);
			obj.controls.fastForward();
			setPositionHandle(this.objid);
		} else {
			this.debug("unscriptable WMP internal fastf");
		}
	};
	
	this.fastr = function () {
		if (this.is_scriptable==true) {
			this.debug("WMP internal fastr");
			var obj = document.getElementById(this.objid);
			obj.controls.fastReverse();
			setPositionHandle(this.objid);
		} else {
			this.debug("unscriptable WMP internal fastr");
		}
	};
	
	this.position = function (pos) {
		if (this.is_scriptable==true) {
			this.debug("WMP internal position");
			var obj = document.getElementById(this.objid);
			var duration = obj.currentMedia.duration;
			var newpos = (duration/100)*pos;
			obj.controls.pause();
			obj.controls.currentPosition = newpos;
			obj.controls.play();
		} else {
			this.debug("unscriptable WMP internal position");
		}
	};

	this.getposition = function () {
		if (this.is_scriptable==true) {
			this.debug("WMP internal getposition");
			var obj = document.getElementById(this.objid);
			this.wmpdebug(" wmp this.starttime prepareCloseHqMode 1");
			this.wmpdebug(startat+" wmp startat prepareCloseHqMode 1");
			return obj.controls.currentPosition;
		} else {
			this.debug("unscriptable WMP internal getposition");
		}
	};
	
	this.getstate = function () {
		if (this.is_scriptable==true) {
			this.debug("WMP internal getposition");
			var obj = document.getElementById(this.objid);
			this.wmpdebug(" wmp this.starttime prepareCloseHqMode 1");
			this.wmpdebug(startat+" wmp startat prepareCloseHqMode 1");
			return obj.playState;
		} else {
			this.debug("unscriptable WMP internal getposition");
		}
	};
	
	
	this.fullscreen = function(conn) {
		if (this.is_scriptable==true) {
			var wmp = document.getElementById(this.objid);
			wmp.fullScreen = true;
		}
	};
	
	this.carrier = function () {
		this.call_carrier(240);
	},
	
	this.volume = function(vol) {
		if (this.is_scriptable==true) {
			this.debug("WMP volume");
			var obj = document.getElementById(this.objid);
			obj.settings.volume = vol;
		} else {
			this.debug("unscriptable WMP volume");
		}
	};
	
	this.mute = function(mute) {
		if (this.is_scriptable==true) {
			this.debug("WMP Mute");
			var obj = document.getElementById(this.objid);
			obj.settings.mute = mute;
		} else {
			this.debug("unscriptable WMP Mute");
		}
	};
	 // add anonn methods 
}

function isWMVPatchInstalled() {
	var isWMVPatchInstalled;
	for (var i in navigator.plugins) {
		var n = navigator.plugins[i].name;
		if (n != null && n.indexOf("Windows Media") >= 0 && n.indexOf("Microsoft") >= 0) {
			isWMVPatchInstalled = true;
		}
	}
	//alert('isWMVPatchInstalled='+isWMVPatchInstalled);
	return isWMVPatchInstalled;
}


function setPositionHandle(objid) {
		obj = document.getElementById(objid);
		var newpos = obj.controls.currentPosition;
		var slidepos = $j(".sliderpos").attr('nowpos');
		//dbg('das hats'+" "+newpos+" slpos"+slidepos +" | -:"+(newpos-10)+" +:"+(newpos+10));
		dbg("set Slider to"+newpos+" "+parseInt(newpos)+" slide:"+slidepos+" ??");
		$j('.sliderpos').SliderSetValues(
//			[[parseInt(newpos),0]]
			[[1,0]]
		);
		dbg("afterset Slider to"+newpos+" "+parseInt(newpos));
		dbg("object records Pos"+$j(".sliderpos").attr('nowpos'));
}// Prototype of this object is playercode
var WMPINTERVALLCHECKER;
microsoftmedia_Playercode.prototpe = new playercode();

