/* +--------------------------------------------
	| CLASS playerWinMedia 
	+-------------------------------------------- */
/* +--------------------------------------------
	| CLASS playerWinMedia 
	|
	| builds a windows media player
	|
	|------------------
	| constructor
	|------------------
	|
	|------------------
	| pwm
	|------------------
	| pwm.hasPlugin()
	|    >> if browser has the plugin
	|
	| pwm.hasActiveX()
	|    >> if browser supports activex
	|
	| pwm.getVersion()
	|    >> player version if browser has activex
	|
	| pwm.generate()
	|    -- output : output option [optional - default document.write()]		
	|       -- type     : type of output [object]
	|       -- id       : object's id 
	|       -- property : object's property [innerHTML]		
	|
	| pwm.getTestMulticastTime()
	|    >> get test multicast current countdown during testing 
	|
	| pwm.testMulticast(params)
	|    -- code_countdown : code to execute during countdown
	|    -- code_ok        : code to execute if multicast test succees
	|    -- code_notok     : code to execute if multicast test fails
	|
	| pwm.playPause()
	|       play / pause player
	|    
	| pwm.stop()
	|       stop player
	|
	+-------------------------------------------- */


/*try {
	new playerWinMedia();
} catch(e) {*/
	
	function playerWinMedia(params) {

		 // to not lose scope with setInterval
		 var this2 = this; 

		/* +--------------------------------------------
			| _mimetype_xmplayer2 
			+-------------------------------------------- */
		this._mimetype_xmplayer2 = function() {
			try  {
				if (navigator.mimeTypes) {
					this.tester = navigator.mimeTypes['application/x-mplayer2'].enabledPlugin;
				}
			} catch(e) {}
			return false;
		}
		
		
		/* +--------------------------------------------
			| _getBarHeight 
			+-------------------------------------------- */
		this._getBarHeight = function(uimode) {
			
			// values
			var heights = {
				activex: {
					none: 0,
					mini: 64,
					full: 64
				},
				
				noactivex: {
					none: 0,
					mini: 50,
					full: 69
				},
				
				mac: {
					none: 0,
					mini: 52,
					full: 72
				}
			};
		
			// if mac
			if (navigator.userAgent.toLowerCase().indexOf("mac")!=-1) {
				return(heights['mac'][uimode]);

			// if activex
			} else if (this.activex) {
				return(heights['activex'][uimode]);
			
			// if no activex
			} else {
				return(heights['noactivex'][uimode]);
			}
		}
		
		
		/* +--------------------------------------------
			| _testMulticast 
			+-------------------------------------------- */
		this._testMulticast = function(params) {
			this.multicastTime -= params['interval'];

			// if time's up
			if (this.multicastTime <= 0) {
				clearInterval(this.multicastInterval);
				eval(params['code_notok']);
			
			} else {
				
				try {
					var location = window.frames['multicasttester'].window.frames['ietrapper'].document.location.href;
				} catch(e) {
					var location = window.frames['multicasttester'].document.location.href;
				}
				
				// if loaded
				if (/pix\.gif/.test(location)) {
					clearInterval(this.multicastInterval);
					eval(params['code_ok']);
				
				// if still not loaded and has countdown code
				} else if (params['code_countdown']) {
					eval(params['code_countdown']);
				}
			}
		}

		
		/* +--------------------------------------------
			| _fallback 
			+-------------------------------------------- */
		this._fallback = function(params) {
		
			var data   = params['data'];
			var output = params['output'];
			var type   = params['type'];
			
			
			var msg;
	//		if (type == 'badversion') {
	//			msg = 'Macromedia Flash Player '+this.swfversion+' requis';
	//		} else if (type == 'noplugin') {
				msg = 'Windows Media Player est requis';
	//		}

			
			// if image
			if (typeof(data['image']) != 'undefined') {
				this._output({
					output: output,
					data:   
						'<a href="http://www.microsoft.com/windows/windowsmedia/download/default.asp" target="_blank">'+
						'<img src="'+data['image']+'" '+
						'id="'+this.params['id']+'" '+
						'width="'+this.params['width']+'" '+
						'height="'+this.params['height']+'" '+
						'alt="'+msg+'" '+
						'title="'+msg+'" '+
						'border="0" /></a>'
				});

			// if text
			} else {
				var text    = ((typeof(data['text'])    != 'undefined')) ? data['text']    : msg;
				var color   = ((typeof(data['color'])   != 'undefined')) ? data['color']   : '#000';
				var bgcolor = ((typeof(data['bgcolor']) != 'undefined')) ? data['bgcolor'] : '#fff';

				var _isIE  = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
				
				var ext = (_isIE) ? 'gif' : 'png'; // ie doesn't like good looking images
				

				this._output({
					output: output,
					data:   
						'<a href="http://www.microsoft.com/windows/windowsmedia/download/default.asp" target="_blank" '+
						'style="border:1px solid '+color+'; color:'+color+'; background:'+bgcolor+' url(http://www.canoe.com/js_objects/playerwinmedia/wmpicon.'+ext+') no-repeat center center; width:'+(this.params['width']-6)+'px; height:'+(this.params['height']-6)+'px;'+
						'text-align:center; display:block; padding:2px; text-decoration:none; font-style:normal; font-weight:normal; font-size:7.5pt; font-family:tahoma,verdana,arial,serif;'+
						'">'+text+'</a>'
				});
			}
			
			return false;
		}

		
		/* +--------------------------------------------
			| _output 
			+-------------------------------------------- */
		this._output = function(params) {
			var output = params['output'];
			var data   = params['data'];
			
			if (output['type'] == 'object') {

				// weird ie scope makes eval unusable
				if (output['property'] == 'innerHTML') {
					document.getElementById(output['id']).innerHTML = data;
				}
				
			} else {
				document.write(data);	
			}
		}

		
		/* +--------------------------------------------
			| hasPlugin 
			+-------------------------------------------- */
		this.hasPlugin = function() {
			return (this.tester) ? true : false;
		}

		
		/* +--------------------------------------------
			| hasActiveX 
			+-------------------------------------------- */
		this.hasActiveX = function() {
			return (this.activex) ? true : false;
		}


		/* +--------------------------------------------
			| getVersion 
			+-------------------------------------------- */
		this.getVersion = function() {
			return (this.activex) ? this.tester.versionInfo : 0;
		}


		/* +--------------------------------------------
			| getTestMulticastTime 
			+-------------------------------------------- */
		this.getTestMulticastTime = function() {
			return Math.ceil(this.multicastTime / 1000);
		}
		

		/* +--------------------------------------------
			| generate 
			+-------------------------------------------- */
		this.generate = function(params) {

			// type of output
			var output = ((typeof(params) != 'undefined') && params['output']) ? params['output'] : {type:'documentwrite'};

			// if there is a player plugin
			// -----------------------------
			if (this.hasPlugin()) {
				
				// constants
				// ----------------------------
				var html = '';
				
				
				// activex
				// ----------------------------
				if (this.hasActiveX()) {
				
					// start <object>
					html += '<object id="'+this.params['id']+'" classid="clsid:6BF52A52-394A-11d3-B153-00C04F79FAA6" type="application/x-oleobject" width="'+this.params['width']+'" height="'+(this.params['height']+this._getBarHeight(this.params['uimode']))+'">';
					html +=		'<param name="url" value="'+this.params['url']+'" />';
					
					// optional values
					for (var key in this.params) {
						if (!(key == 'id' || key == 'url' || key == 'width' || key == 'height')) {
							html += '<param name="'+key+'" value="'+this.params[key]+'" />';
						}
					}
					
					html += '</object>';
				
				
				// embed
				// ----------------------------
				} else {
				
					// uimode no activex
					var uimode;
					if (this.params['uimodenoactivex']) {
						uimode = this.params['uimodenoactivex'];
					} else {
						uimode = this.params['uimode'];
					}

					
					// <embed>
					html += '<embed type="application/x-mplayer2" id="'+this.params['id']+'" ';
					html += 'width= "'+this.params['width']+'" ';
					html += 'height="'+(this.params['height']+this._getBarHeight(uimode))+'" ';
					html += 'src=   "'+this.params['url']+'" ';
					
					
					
					// uimode
					// ----------------------------
					// none
					if (uimode == 'none') {
						html += 'showaudiocontrols=0 ';     // volume
						html += 'showcontrols=0 ';          // controls + volume
						html += 'showdisplay=0 ';           // description
						html += 'showgotobar=0 ';           // barre goto
						html += 'showpositioncontrols=0 ';  // rewind / forward
						html += 'showstatusbar=0 ';         // status bar
						html += 'showtracker=0 ';           // tracker
					
					// mini
					} else if (uimode == 'mini') {
						html += 'showaudiocontrols=1 ';
						html += 'showcontrols=1 ';
						html += 'showdisplay=0 ';
						html += 'showgotobar=0 ';
						html += 'showpositioncontrols=0 ';
						html += 'showstatusbar=1 ';
						html += 'showtracker=0 ';
					
					// full
					} else if (uimode == 'full') {
						html += 'showaudiocontrols=1 ';
						html += 'showcontrols=1 ';
						html += 'showdisplay=0 ';
						html += 'showgotobar=0 ';
						html += 'showpositioncontrols=1 ';
						html += 'showstatusbar=1 ';
						html += 'showtracker=1 ';
					}

					
					// boolean values
					for (var key in this.params) {
						if (this.params[key] == 'true') {
							html += key+'=1 ';
						} else if (this.params[key] == 'false') {
							html += key+'=0 ';
						}
					}

					html += '></embed>';
				}

				

				// write player
				// ----------------------------
				this._output({
					output: output,
					data:   html
				});

				
				// associate player
				this.player = document.getElementById(this.params['id']);
			
			
			// if there is no plugin
			// -----------------------------
			} else {
			
				return this._fallback({
					output: output,
					data:   this.noplugin,
					type:   'noplugin'
				});
			}
			
			return false;
		}


		/* +--------------------------------------------
			| testMulticast 
			+-------------------------------------------- */
		this.testMulticast = function(params) {
			
			// print an iframe -- HAS TO BE ON EXACT SAME DOMAIN ENHANCE THE RELATIVE PATH -- www.zonehautevitesse.com
			document.getElementById(params['idtester']).innerHTML = '<iframe name="multicasttester" src="/diagnostictest/multicast/" width="0" height="0" frameborder="0" scrolling="no"></iframe>';

			// test the multicast
			params['interval'] = 250;
			this.multicastTime = 10 * 1000;  // 10 sec in milliseconds

			this.multicastInterval = setInterval(function() { this2._testMulticast(params); }, params['interval']);
		}


		/* +--------------------------------------------
			|
			| CONTROLS 
			|
			+-------------------------------------------- */
		
		/* +--------------------------------------------
			| playPause 
			+-------------------------------------------- */
		this.playPause = function() {
			
			if (this.activex) {
				
				// playing
				if (this.player.playState == 3) {     
					this.player.controls.pause();
				} else {
					this.player.controls.play();
				}
			} else {
				alert('Vous navigateur ne supporte pas les activex');
			}
			return false;
		}

		/* +--------------------------------------------
			| stop 
			+-------------------------------------------- */
		this.stop = function() {
			
			if (this.activex) {
				this.player.controls.stop();
			} else {
				alert('Vous navigateur ne supporte pas les activex');
			}
			return false;
		}




		/* +--------------------------------------------
			| MAIN 
			+-------------------------------------------- */
		this.params   = params;	
		this.tester   = 0;	
		this.activex  = 0;	
		this.noplugin = ((typeof(this.params) != 'undefined') && this.params['noplugin']) ? this.params['noplugin'] : {};
		

		// try to initiate an activex
		// ----------------------------
		try {
		  
			// activex
			if (window.ActiveXObject) {
				this.tester  = new ActiveXObject("WMPlayer.OCX.7");
				this.activex = 1;
			
			// geckoactivex
			} else if (window.GeckoActiveXObject) {
				this.tester = new GeckoActiveXObject("WMPlayer.OCX.7");
				this.activex = 1;
			
			// x-mplayer2
			} else {
				this._mimetype_xmplayer2();
			}

		// if it fails try to check x-mplayer2 plugin
		// --------------------------------------------
		} catch(e) {
			this._mimetype_xmplayer2();
		}
	}
/*}*/