/**
 * Dex DHTML multimedia player
 * @package CMS
 * @author Jonathan Dortheimer
 * @copyright Dex technologies L.T.D
 * @version 1.0
 */


function dexPlayer(filename,Counter,link,thumb,portalid){

	this.setLink='';
	this.ext='';
	//get file properties
	this.setFilename = function(val){
		this.path=val;

		var temp=val.split('/');
		templen=temp.length
		this.filename=temp[(templen-1)];
		this.nodeid=temp[(templen-2)];
		this.typeid=temp[(templen-3)];

		if (!this.ext){
			var temp=this.filename.split('.');
			templen=temp.length;
			this.setExt(temp[(templen-1)]);
		}
		this.nodename='';
		for(i=0;i<templen-1;i++)
			this.nodename+=temp[i];
	}
	//set media extension
	this.setExt = function(ext){
		this.ext=ext;
		if (ext=='mp3')
			this.music=1;
		else
			this.music=0;
	}
	this.setLink = function(link){
		this.link=link;
	}
	this.setMediaserver = function(server){
		this.mediaserver=server;
	}

	//set html placeholder
	this.setHtmlObj = function(val){
		this.Counter=val;
		this.HtmlObjName='placeholder'+val;
		this.HtmlObj=document.getElementById(this.HtmlObjName);
	}
	//recursive find width
	this.getDim = function(){
		width=get_width(this.HtmlObj);
		width=parseInt(width*0.95);
		if (this.music)
			height=0;
		else
			height=parseInt(width*3/4);
		this.setDim(width,height);
	}
	//Set dimensions
	this.setDim = function(width,height){
		this.width=width;
		this.height=height;
	}
	//get possible player array and best player
	this.getPlayer = function(){
		this.mplayers = new Array(0,-1,-1,-1,-1,-1,-1,-1);
		var playerSupport = new Array();
		//browser
		playerSupport[1] = new Array('jpeg','jpg','gif','tif','tiff','png','bmp');
		//flash
		playerSupport[2] = new Array('swf');
		//quicktime
		playerSupport[3] = new Array('mov','qt','qtm','avi','vfw','wav','bwf','aif','aiff','au','mid','midi','smf','mpeg','mpg','m1s','m1v','m1a','mpm','mpv','mpa','mp4','mpg4','swa');
		//media player
		playerSupport[4] = new Array('asf','asx','wpl','wm','wmx','wmd','wmz','wma','wax','wmv','wvx','avi','wav','mpeg','mpg','mpe','m1v','mp2','mp2v','mpa','mid','midi','rmi','au','snd');
		//real player
		playerSupport[5] = new Array('rm','rt','ra','ram','rp','rv','avi','mpeg','mpg','m1s','m1v','m1a','mpm','mpv','mpa','mp4','mpg4','wav','mid','midi','mov','qt');
		//flv player
		playerSupport[6] = new Array('flv');
		//swf mp3 player
		playerSupport[7] = new Array('mp3');

		var browserSupport = new Array();
		browserSupport[1]=true;
		if (ie && win){
			detectableWithVB=detect();
			browserSupport[2] = browserSupport[6] = browserSupport[7] = detectActiveXControl('ShockwaveFlash.ShockwaveFlash.1');
			browserSupport[3] = detectQuickTimeActiveXControl();
			browserSupport[4] = detectActiveXControl('MediaPlayer.MediaPlayer.1');
			browserSupport[5] = (detectActiveXControl('rmocx.RealPlayer G2 Control') || detectActiveXControl('RealPlayer.RealPlayer(tm) ActiveX Control (32-bit)') || detectActiveXControl('RealVideo.RealVideo(tm) ActiveX Control (32-bit)'));
		}
		if(navigator.plugins && navigator.plugins.length > 0) {
    		//check flash
			browserSupport[2] = browserSupport[6] = browserSupport[7] = detectPlugin('Shockwave','Flash');
 			browserSupport[3] = detectPlugin('QuickTime');
   			browserSupport[4] = detectPlugin('Windows Media');
			browserSupport[5] = detectPlugin('RealPlayer');
    	}

    	//loop on installed plugins
		for (i=1;i<8;i++){
			//loop on plugin ext
			for (j=0;playerSupport[i][j];j++){
				if (playerSupport[i][j]==this.ext)
					this.mplayers[i]=1;
			}
		}
		//set windows
		if (win && ie && this.mplayers[4]>0)
			this.player=4;
		else if (win && this.mplayers[3]>0)
			this.player=3;
		else if (mac && this.mplayers[3]>0)
			this.player=3;
		else if (this.mplayers[5]>0)
			this.player=5;
		else{
			for (i=1;i<8;i++){
				if (this.mplayers[i]>0 && browserSupport[i]){
					this.player=i;
				}
			}
		}
	}

	this.setPortal = function(portalid){
		this.portalid=portalid;
	}
	this.setThumb = function(thumbnail){
		this.thumb=thumbnail;
		this.thumbStyle="background-image:url("+thumbnail+");background-repeat: no-repeat;background-position: 50% 50%";
	}
	this.getHtml= function(){
		this.htmlString='';
		if (this.player==1){
			//image
			if (this.link)
				this.htmlString+='<a id="'+this.HtmlObjName+'_player" href="'+this.link+'">';
			if (this.ext=='jpg')
				this.htmlString+='<img onLoad="dex_player_fix_image(this)" style="visibility:hidden" width="'+this.width+'" src="'+this.path+'" />';
			else if (this.ext=='png')
				this.htmlString+='<img onLoad="dex_player_fix_image(this)" style="visibility:hidden" width="'+this.width+'" src="'+this.path+'" />';
			else
				this.htmlString+='<img onLoad="dex_player_fix_image(this)" style="visibility:hidden" width="'+this.width+'" src="'+this.path+'" />';
			if (this.link)
				this.htmlString+='</a>';
		}
		else if (this.player==2 && ie && win){
			//flash
			this.htmlString+= '<object id="'+this.HtmlObjName+'_player" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0"  width="'+this.width+'"  height="'+this.height+'">';
			this.htmlString+= '<param name="movie" value="'+this.path+'">';
			this.htmlString+= '<param name="wmode" value="opaque" />';
			this.htmlString+= '</object>';
		}
		else if (this.player==2){
			this.htmlString+= '<embed src="'+this.path+'" ';
			this.htmlString+= 'quality="best" wmode="opaque" width="'+this.width+'"  height="'+this.height+'" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />';
		}
		else if (this.player==3){
			this.htmlString+= '<EMBED id="'+this.HtmlObjName+'_player" SRC="'+this.path+'"  width="'+this.width+'"  height="'+this.height+'"  TYPE="video/quicktime" PLUGINSPAGE="http://www.apple.com/quicktime/download/" ';
			this.htmlString+='autoplay="true" ';
			this.htmlString+='scale"="Aspect" ';
			this.htmlString+='controller="false" ';
			this.htmlString+='EnableJavaSript="true" ';
			this.htmlString+='stretchToFit"="false" ';
			this.htmlString+='/>';
		}
		else if (this.player==4 && ie && win){
			//windows media
			this.htmlString+= '<object id="'+this.HtmlObjName+'_player" classid="CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95"  width="'+this.width+'"  height="'+this.height+'" ';
			this.htmlString+= 'codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701" ';
			this.htmlString+= 'standby="Loading Microsoft Windows Media Player components..."';
			this.htmlString+= 'TYPE="application/x-oleobject">';
			this.htmlString+= '<param name="fileName" value="'+this.path+'" />';
			this.htmlString+= '<param name="animationatStart" value="false" />';
			this.htmlString+= '<param name="transparentatStart" value="false" />';
			this.htmlString+= '<param name="autoStart" value="true" />';
			this.htmlString+= '<param name="DisplaySize" value="mpDefaultSize" />';
			this.htmlString+= '<param name="ShowCaptioning" value="false" />';
			this.htmlString+= '<param name="showControls" value="false" />';
			this.htmlString+= '</object>';
		}
		else if (this.player==4){
			this.htmlString+='<embed id="'+this.HtmlObjName+'_player" type="application/x-mplayer2" width="'+this.width+'"  height="'+this.height+'" ';
			this.htmlString+='pluginspage="http://microsoft.com/windows/mediaplayer/en/download/" ';
			this.htmlString+='displaysize="4" autosize="-1" ';
			this.htmlString+='bgcolor="darkblue" showcontrols="false" showtracker="-1" ';
			this.htmlString+='showdisplay="0" showstatusbar="-1" videoborder3d="-1" ';
			this.htmlString+='src="'+this.path+'" ';
			this.htmlString+='designtimesp="5311" loop="false"  ';
			this.htmlString+='filename="'+this.path+'" ';
			this.htmlString+='animationatstart="false" transparentatstart="true" ';
			this.htmlString+='autostart="true" ';
			this.htmlString+='></EMBED>';
		}
		else if (this.player==5 && ie && win){
			//real
			this.htmlString+= '<OBJECT id="'+this.HtmlObjName+'_player" CLASSID="clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA"  width="'+this.width+'"  height="'+this.height+'">';
			this.htmlString+= '<PARAM NAME="CONTROLS" VALUE="ImageWindow" />';
			this.htmlString+= '<PARAM NAME="CONSOLE" VALUE="one">';
			this.htmlString+= '<PARAM NAME="AUTOSTART" Value="true" />';
			this.htmlString+= '<PARAM NAME="MAINTAINASPECT" Value="true" />';
			this.htmlString+= '<PARAM NAME="SRC" VALUE="'+this.path+'" /></OBJECT>';
		}
		else if (this.player==5){
			this.htmlString+= '<embed  id="'+this.HtmlObjName+'_player"  width="'+this.width+'"  height="'+this.height+'" '
			this.htmlString+= 'CONSOLE="one"  type="audio/x-pn-realaudio-plugin" ';
			this.htmlString+= 'controls="ImageWindow" ';
			this.htmlString+= 'src="'+this.path+'?embed"  ';
			this.htmlString+= 'autostart="true" ';
			this.htmlString+= 'MAINTAINASPECT="true" ';
			this.htmlString+= ' ></embed>';
		}
		else if (this.player==6){
			//flash
			this.htmlString+= '<embed src="/media/mediaplayer.swf" width="'+this.width+'" height="'+this.height+'" allowscriptaccess="always" ';
			this.htmlString+= 'allowfullscreen="true" flashvars="height='+this.height+'&width='+this.width+'&file='+this.path+'&logo=/media/'+portalid+'.png&volume='+this.volume+'&showstop=true&autostart='+(this.doPlay?"true":"false")+'" />';
		}
		else if (this.player==7){
			//flash mp3
			this.htmlString+= '<embed src="/media/mediaplayer.swf" width="'+this.width+'" height="20" allowscriptaccess="always" ';
			this.htmlString+= 'allowfullscreen="true" flashvars="height=20&width='+this.width+'&file='+this.path+'&showstop=true&volume='+this.volume+'&autostart='+(this.doPlay?"true":"false")+'" />';
		}
	}

	this.getHtmlStop= function(){
		this.htmlStopString='<div id="'+this.HtmlObjName+'_player" onClick="player'+this.Counter+'.doplay()" class="thumb" style="height:'+this.height+'px;width:'+this.width+'px;'+this.thumbStyle+'"></div>';
	}
	this.getHtmlNav= function(){
		this.htmlNav='';
		if (this.player>2 && this.player<6){
			this.htmlNav+='<ul class="dex_player_nav">';
			this.htmlNav+='<li class="play"><a href="javascript:player'+this.Counter+'.doplay()" /></a></li>';
			this.htmlNav+='<li class="stop"><a href="javascript:player'+this.Counter+'.dostop()" ></a></li>';
			this.htmlNav+='<li id="player_status"></li>';
			//this.htmlNav+='<li><a href="'+this.path+'" target="_blank"><img src="/template/download.png" alt="download" /></a></li>';
			this.htmlNav+= '</li></ul>';
		}
	}
	this.doplay= function(){
		this.play=1;
		this.drawPlayer();
	}
	this.dostop= function(){
		this.play=0;
		this.drawPlayer();
	}
	this.drawPlayer= function(){
		if (!this.player)
			this.getPlayer();

		this.getHtmlNav();

		if (!this.width){
			this.getDim();
		}

		this.getHtml();
		this.getHtmlStop();
		var temp='';
		if (!this.play && this.player>2 && this.player<5){
			temp+=this.htmlStopString;
		}else
			temp+=this.htmlString;

		if (this.player>2)
			temp='<div class="dex_player">'+temp+this.htmlNav+'</div>';
		this.HtmlObj.innerHTML=temp

	}
	//Construct
	if (filename && Counter!='undefined'){
		this.setHtmlObj(Counter);
	}
	this.frame = function(mediaserver,doPlay){
		if (!this.width){
			this.getDim();
		}
		if (mediaserver)
			this.setMediaserver(mediaserver);
		this.htmlString= '<iframe marginwidth="0" marginheight="0" scrolling="no" frameborder="0" ';
		this.htmlString+= 'style="border:0" id="'+this.HtmlObjName+'_frame" name="'+this.HtmlObjName+'_frame" ';
		//25px for player controls
		this.htmlString+= 'width="'+this.width+'"  height="'+(this.height+25)+'" ';
		this.htmlString+= 'src="about:blank">';
		this.htmlString+= '</iframe>';
		this.htmlString+= '<form style="height:1px" action= "'+this.mediaserver+'" target="'+this.HtmlObjName+'_frame" id="'+this.HtmlObjName+'_form">';
		this.htmlString+= '<input type="hidden" name="width" value="'+this.width+'" />';
		this.htmlString+= '<input type="hidden" name="thumb" value="'+this.thumb+'" />'
		this.htmlString+= '<input type="hidden" name="height" value="'+this.height+'" />'
		this.htmlString+= '<input type="hidden" name="ext" value="'+this.ext+'" />';
		this.htmlString+= '<input type="hidden" name="url" value="'+this.path+'" />';
		if (this.portalid)
			this.htmlString+= '<input type="hidden" name="m" value="'+this.portalid+'" />';
		this.htmlString+= '<input type="hidden" name="play" value="'+doPlay+'" />';
		this.htmlString+= '</form>';
		this.HtmlObj.innerHTML=this.htmlString;
	}
	this.frameOrImage = function(mediaserver,doPlay,volume){

		this.doPlay=doPlay;
		this.volume=volume;
		this.getPlayer();
		if (mediaserver)
			this.setMediaserver(mediaserver);
		if (this.player<2 || this.player>4)
		{
			this.drawPlayer();
		}else{
			this.frame(mediaserver,doPlay);
			document.getElementById(this.HtmlObjName+'_form').submit();
		}
	}
	if (filename){
		this.setFilename(filename);
	}
	if (link)
		this.setLink(link);
	this.setThumb(thumb);
	if (portalid)
		this.setPortal(portalid);

}
