/******************************************************************/
/* © DMC 01 Consulting & Development GmbH - gerald.urbas@dmc01.at */
/******************************************************************/
/* Realmedia Frontend Code Plugin in                              */
/******************************************************************/
/* Object generates realmedia HTML code                           */
/* can get evolved for styled buttons. able to provide            */
/* RP-Interface player plugin                                     */
/******************************************************************/


function realmedia_Playercode(pt) {
	this.name = "RealVideo"; // player frontend name
	this.typ = "realmedia"; // Type
	this.detectionplugin = "RealPlayer"; // detection plugin string
	//this.objid = "RVOCX"; // Object ID 4 HTML
	this.objid="RVOCX"+Math.round(Math.random()*10000); // Object ID 4 HTML
	this.playertype = pt // home or extended
	this.bgcolor="#002349"; // background color Real Object
	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':'controls="ImageWindow"',
			'object':'<param name="controls" value="ImageWindow" />'
			},
			'home':{
			'embed':'controls="ImageWindow"',
			'object':'<param name="controls" value="ImageWindow" />'
			},
		'extended':{
			'embed':'controls="ImageWindow"',
			'object':'<param name="controls" value="ImageWindow,StatusBar" />'
		}
	};
	this.playercontrolsheight={ // all Players generet there own control heigts... wo correction values definined here
		'simple':{
			'embed':29,
			'object':29
			},
		'home':{
			'embed':29,
			'object':29
			},
		'extended':{
			'embed':29,
			'object':29
			}
	};
	this.addcontrolembed = ""; // temp holder empty init
	this.addcontrolobject = ""; // temp holder empty init

	this.playerstring = '<div id="'+this.objid+'" class="playerrealposition" style="">'
                    + 'Dieser Text wird durch das Realplayer Plugin ersetzt.'
                    + '</div>';

	this.debug = function (str) { // debuglayer is deativated @ moment
		dbg("Real plugin: "+str);
	};

	this.testscriptconn  = function () {
		this.debug("Script Commands Connection test")
		try {
			obj = document.getElementById(this.rpobjid);
			obj.SetEnableFullScreen(true);
			obj.SetWantMouseEvents(true);
			obj.SetWantKeyboardEvents(true);
			this.debug("Realplayer seems to be reached by scripting. SetWantMouseEvents doesnt fail");
			return true;
		} catch(e) {
			this.debug("Realplayer scripting is not avaible");
			return false;

//			this.debug("rp play failed");
//			printJsError(e)
		}
	}
	
	this.mediaobject = function(objid,size,conn,type,obj_options,embed_option,obj_attrs) {
		var options = { 
			width:     size['w'],
			height:    size['h'],
			autoplay:  this.autostart,
			src:       this.connurls[conn],
			caption:   false 
		};
		
		if (obj_attrs!=false) {
			options.attrs = this[obj_attrs](objid,size,conn,type);
		}
		if (obj_options!=false) {
			options.params = this[obj_options](objid,size,conn,type);
		}
		if (embed_option!=false) {
			options.attrs = this[embed_option](objid,size,conn,type);
		}
		return options
	}
	
	// Object Control params
	this.mediacontrolobjectoptions = function (objid,size,conn,type) {
		return { controls:type ,console:  'one' }
	}
	// Object Video params
	this.mediaobjectoptions = function (objid,size,conn,type) {
		return { controls:type ,console:  'one',  autostart:  'true',  center:  'true',  backgroundcolor:  this.bgcolor }
	}
	// Object Video attrs
	this.mediaobjectattr = function (objid,size,conn,type) {
		return { id:  objid }
	}
	// Embed Video & Control params
	this.mediaembedattr = function (objid,size,conn,type) {
		return { controls:type ,id:  objid, console:  'one',  autostart:  'true',  center:  'true',  backgroundcolor:  this.bgcolor, SCRIPTCALLBACKS:'OnLButtonUp,OnKeyUp' }
	}
	
	this.playercall= function() { // callback after rendering. RP doesnt use it here.
		var obj = arguments[0]; // get conn param
		var conn = arguments[1]; // get conn param
		if (rpieaddded==true && conn!=3) {//$j.browser.msie && 
			location.reload();
		}
		if (rpieaddded==true && conn==3) {//$j.browser.msie && 
			this.addRealMedia(obj,conn,false);
		}
		if (rpieaddded==false) {
			this.addRealMedia(obj,conn,true);
		}
		$j("#"+obj).show();
	};
	
	this.addRealMedia = function(obj,conn,hide) {
		this.debug("obj:"+obj+" conn:"+conn+" hide:"+hide)
		if (hide==true) {
			$j("#"+obj).empty();
		}
		this.rpobjid = 'rpplugin';
		this.videoid="rv"+this.rpobjid;
		this.controlid="rc"+this.rpobjid;
		if (conn==3) {
			this.rpobjid = 'hqrpplugin';
			this.videoid="hqrv"+this.rpobjid;
			this.controlid="hqrc"+this.rpobjid;
		}
		this.debug("div target: "+this.rpobjid)
		$j("#"+obj).append('<div id="'+this.videoid+'"></div>');
		$j("#"+obj).append('<div id="'+this.controlid+'"></div>');
		var size = this.vsizes[conn];
		this.debug("Sizes w:"+size["w"]+"h:"+size["h"]);
		if (!$j.browser.msie) {
			$j("#"+this.videoid).media(
				this.mediaobject(this.rpobjid,{'w':size['w'],'h':size['h']},conn,'Imagewindow',false,"mediaembedattr",false)
			);
			$j("#"+this.controlid).media(
				this.mediaobject("rpcontrol",{'w':size['w'],'h':this.playercontrolsheight[this.playertype]['embed']},conn,'ControlPanel',false,"mediaembedattr",false)
			);
		} else { // = MSIE
			$j("#"+this.videoid).media(
				this.mediaobject(this.rpobjid,{'w':size['w'],'h':size['h']},conn,'Imagewindow',"mediaobjectoptions",false,"mediaobjectattr")
			);
			$j("#"+this.controlid).media(
				this.mediaobject("rpcontrol",{'w':size['w'],'h':this.playercontrolsheight[this.playertype]['embed']},conn,'ControlPanel',"mediacontrolobjectoptions",false,false)
			);
		}
		this.debug("Layers created Object:"+this.rpobjid+" Videodiv:"+this.videoid+" controldiv:"+this.controlid)
		rpieaddded = true;
	};
	
	this.enabled = true; // we are here -  so we use this object.
	 // add public methods
	this.write = writerealplayercode; 
	this.replace = replaceVars;
	this.setsizes = setsizes;
	this.setconn = setconn;
	this.setplayertype = setplayertype;
	this.setautostart = setautostart;
	this.setglobid = setglobid;
	this.call_popup = playerPopup;
	this.close_popup = playerPopupClose;
	this.call_carrier = carrierPopup
	this.setPreviewimage = setPreviewImage;
	this.getPreviewimage = getPreviewImage;
	this.previewImage = previewImagecall;
	// add inline methods 
	this.popup = function() { // this function is used by the popup window... (see doc there)
		conn=3;
		return '$j("#popupcontent").empty();$j("#popupcontent").media(obj.mediaobject('+conn+',"'+mediaobject+'"))';
	}

	this.getconn = function () {
		return this.connurls;
	}
	
	this.shutdown = function () {
		this.debug("shutdown Realplayer");
		this.pause();
		if (rpieaddded==true) {
			location.reload();
		}
		$j('#'+this.rpobjid).hide();
	}
	
	this.report = function () {
		return "Player:"+this.name+" obid:"+this.objid+" url:"+this.connurls;
	};
	
	// later this can be evolved to provide transparent and identically Interfaces
	// play button
	this.play = function () {
		try {
			obj = document.getElementById(this.rpobjid);
			obj.DoPlay();
		} catch(e) {
			this.debug("rp play failed");
			printJsError(e)
		}
	};
	// pause button
	this.pause = function () {
		try {
		obj = document.getElementById(this.rpobjid);
		obj.DoPause();
		} catch(e) {
			this.debug("rp pause failed");
			printJsError(e)
		}
	};
	
	this.stop = function() {
		try {
			obj = document.getElementById(this.rpobjid);
			obj.DoStop();
		} catch(e) {
			this.debug("rp stop failed");
			printJsError(e)
		}
	}
	this.fullscreen = function(conn) {
		try {
			obj = document.getElementById(this.rpobjid);
			obj.DoPlay();
			obj.SetEnableFullScreen(true);
			obj.SetFullScreen();
		} catch(e) {
			this.debug("rp fullscreen failed");
			printJsError(e)
		}
	}
	
	this.carrier = function () {
		this.call_carrier(220);
	}
	
}



var rpieaddded = false;

function writerealplayercode(targetobjid,conn) {
		var pstring = this.playerstring;
		pstring=this.replace(pstring,conn);
		//$j("#"+this.objid).empty();
		//$j("#"+targetobjid).empty();
		if (rpieaddded==false) {
			$j("#"+targetobjid).append(pstring);
		}
		this.playercall(targetobjid,conn);
} 


// Prototype of this object is playercode
realmedia_Playercode.prototpe = new playercode();

var rp_fullscreen = false;

function OnLButtonUp(button_flags,x_pos,y_pos) {
	//alert("2");
	dbg("RP LB "+button_flags+" x:"+x_pos+" y:"+y_pos)
	if (player.player_type=='home') {
		player.fullscreen();
		rp_fullscreen == true
	}
}


function OnKeyUp(key){ 
	dbg("RP Key "+key)
	if (rp_fullscreen == false && player.player_type=='extended' && $j('#'+player.layerhq_id).html()!=null) {
		player.closeHqPlayer();
	} else {
		rp_fullscreen =false;
	}
	
}


