/********************************
JavaScript WMPControl module
(c) 2008-2009 by Ether J.S.C.
developed by Anton J. Garnik
*********************************/

function WMPControl(box, options) {
	this.box = box;

	if(!options) options = {};
	// default wait-for-load time is 2 seconds. query interval is 0.25s
	this.maxWaitIterations = (options.maxWaitIterations ? options.maxWaitIterations : 8);
	this.waitIterationTime = (options.waitIterationTime ? options.waitIterationTime : 250);

	if(options.volume === null || options.volume === undefined) options.volume = 50;
	this.forceLoop = options.forceLoop ? true : false;
	this.onVolumeChanged = options.onVolumeChanged ? options.onVolumeChanged : null;
	this.userOnPluginLoaded = options.onPluginLoaded ? options.onPluginLoaded : null;
	this.userOnPluginLoadFailed = options.onPluginLoadFailed ? options.onPluginLoadFailed : null;
	this.userWaitForLoad = options.waitForLoad ? options.waitForLoad : null;
	this.onConnectingBuffering = options.onConnectingBuffering ? options.onConnectingBuffering : null;
	this.wd = (options.wd && options.ht) ? options.wd : 432;
	this.ht = (options.ht && options.wd) ? options.ht : 324;
	this.manuallyStopped = false;

	// event handlers
	this.onPlayerStart = options.onPlayerStart ? options.onPlayerStart : null;
	this.onPlayerStop = options.onPlayerStop ? options.onPlayerStop : null;
	this.onPlaying = options.onPlaying ? options.onPlaying : null;

	this.monitorTimer = null;
	this.forceLoop = options.forceLoop;
	this.prevState = 0;

	if(
		options.plugin &&
		(
			options.plugin.classid && options.plugin.classid == 'clsid:6BF52A52-394A-11d3-B153-00C04F79FAA6' ||
			options.plugin.type && options.plugin.type == 'application/x-ms-wmp'
		)
	) this.wmp = options.plugin;
	else {
		$(box).empty();
		if($.browser.msie) {
//			if(!checkForWMP) { this.errorDescr = "Plugin not found"; this.onPluginLoadFailed(this); return; }
			$(box).html("<OBJECT "+
			// {22D6F312-B0F6-11D0-94AB-0080C74C7E95}
			"classid='clsid:6BF52A52-394A-11d3-B153-00C04F79FAA6'"+
			" width="+this.wd.toString()+" height="+this.ht.toString()+">"+
			"<PARAM name='uiMode' value='none'/>"+
			"<PARAM name='autoStart' value='false'/>"+
			"<PARAM name='stretchToFit' value='true'/>"+
			"</OBJECT>");
		} else if(navigator.plugins) {
			var found_w = false;
			var i;
			for(i = 0; i < navigator.plugins.length; i++)
				if(-1 != navigator.plugins[i].name.search(/windows media player/i)) { found_w = true; break; }
			if(!found_w) { this.errorDescr = "Plugin not found"; this.onPluginLoadFailed(this); return; }
			$(this.box).html("<embed"+
			" type='application/x-ms-wmp'"+
			" pluginspage='http://port25.technet.com/pages/windows-media-player-firefox-plugin-download.aspx'"+
			" width="+this.wd.toString()+" height="+this.ht.toString()+
			" uiMode='none'"+
			" autoStart='false'"+
			" stretchToFit='true'>"+
			"</embed>");
		} else {
			this.errorDescr = "Your browser is not IE, and not 'navigator.plugins' property is set";
			this.onPluginLoadFailed(this);
			return;
		}
	}
	if($.browser.opera) { window.setTimeout(function(o) { return function() { o.finalizePlugin(); } }(this), 100); return; }
	else this.finalizePlugin();
}

WMPControl.prototype.finalizePlugin = function() {
	if(!this.wmp) this.wmp = $(this.box).children()[0];
	if($.browser.msie) {
		if(this.wmp.readyState != 4) { this.errorDescr = "Plugin ready state = "+this.wmp.readyState; this.onPluginLoadFailed(this); }
		else if(!this.wmp.versionInfo) { this.errorDescr = "No plugin properties available"; this.onPluginLoadFailed(this); }
		else this.onPluginLoaded();
	} else { // for Netscape-style plugins
		this.waitingIterations = 0;
		this.waitForLoad();
	}
}

WMPControl.prototype.onPluginLoadFailed = function() {
	$(this.box).empty();
	this.wmp = null;
	if(this.userOnPluginLoadFailed) this.userOnPluginLoadFailed(this);
}

WMPControl.prototype.onPluginLoaded = function() {
//	this.wmp.style.width  = this.wd+"px";
//	this.wmp.style.height = this.ht+"px";
	if(this.userOnPluginLoaded) this.userOnPluginLoaded(this);
}

WMPControl.prototype.waitForLoad = function () {
	this.waitingIterations++;
	var continue_waiting = true;
	if(this.userWaitForLoad) continue_waiting = this.userWaitForLoad(this); // call user defined waitForLoad function if exists
	var o = this;
	if(this.wmp.controls) this.onPluginLoaded(); // plugin successfully loaded
	else {
		if(this.waitingIterations >= this.maxWaitIterations || !continue_waiting) 
			this.onPluginLoadFailed();
		else 
			window.setTimeout(function() { o.waitForLoad(); }, 250);
	}	
	// if user defined waitForLoad has not been returned false, continue polling wmp object
};

WMPControl.prototype.versionInfo = function() { return this.wmp ? this.wmp.versionInfo : null; }

WMPControl.prototype.getPlayerName = function() { return this.wmp ? 'WMP' : null; }

WMPControl.prototype.getPlayerStatus = function() {
	if(!this.wmp) return null;
	switch(this.wmp.playState) {
		case 0: return null;
		case 1: return 0;
		case 2: return 4;
		case 3: return 3;
		case 6: return 2;
		case 7: return 1;
		default: return null;
	}
}

WMPControl.prototype.monitor = function() {
	if(!this.wmp) return;
	var newState = -1;
	if(!this.wmp.playState) newState = 0;
	else newState = this.wmp.playState;
	
	if( this.prevState != newState ) {
//		alert(this.wmp.status);
		switch(newState) {
			case 1: // current media has stopped
			if(this.forceLoop && !this.manuallyStopped) this.playCurrent();
			else if(this.onPlayerStop) this.onPlayerStop(this);
			break;

			case 6: // current media is buffering
				if(this.onConnectingBuffering) this.onConnectingBuffering(this);
			break;

			case 7: // waiting for data
				if(this.onConnectingBuffering) this.onConnectingBuffering(this);
			break;
			
			case 2:	// current media paused
			break;

			case 3: // current media is now playing
			this.manuallyStopped = false;
			if(this.onPlayerStart) this.onPlayerStart(this);
			break;

			default:
//			alert(this.wmp.status);
			break;
		}
		this.prevState = newState;
	} else if( newState == 3 ) { // current media is playing
		if(this.onPlaying) this.onPlaying(this);
	}
	var o = this;
	if(!this.monitorTimer) {
//		if(this.monitorTimer) clearInterval(this.monitorTimer);
		this.monitorTimer = setInterval(function() { o.monitor(); }, 100);
	}
}

WMPControl.prototype.playCurrent = function() {
	if(!this.wmp) return;
	this.wmp.controls.play();
	this.monitor();
}

WMPControl.prototype.play = function(src) {
	if(!this.wmp) return;
	this.stop();
	src = src.replace(/^mmsh:/i, "mms:");
	this.wmp.URL = src;
	this.wmp.controls.play();
	this.monitor();
}

WMPControl.prototype.stop = function() {
	if(!this.wmp) return;
//	if(this.wmp.status == 'stopped') return;
//	this.forceLoop = false;
	this.manuallyStopped = true;
	this.wmp.controls.stop();
}

WMPControl.prototype.setPosition = function(pos) { // set position in seekable media to pos percent
	if(!this.wmp) return;
	this.wmp.controls.currentPosition = parseFloat(pos) / 100.0 * this.wmp.currentMedia.duration;
}

WMPControl.prototype.getPosition = function() { // get position in seekable media in percent
	if(!this.wmp) return 0;
	return this.wmp.controls.currentPosition / this.wmp.currentMedia.duration * 100.0;
}

WMPControl.prototype.volumeChange = function(dv) { // change volume with dv units (signed integer)
	if(!this.wmp) return;
	var v = this.wmp.settings.volume+dv;
	if(v < 0) v = 0;
	else if(v > 100) v = 100;
	if(v == this.wmp.settings.volume) return;
	this.wmp.settings.volume = v;
	if(this.onVolumeChanged) this.onVolumeChanged(this);
}

WMPControl.prototype.setVolume = function(vp) { // change volume to vp percent
//debugger;
	if(!this.wmp) return;
	if(vp < 0) vp = 0;
	else if(vp > 100) vp = 100;
	if(vp == this.wmp.settings.volume) return;
	this.wmp.settings.volume = vp;
//	alert("Vol: "+this.wmp.settings.volume);
	if(this.onVolumeChanged) this.onVolumeChanged(this);
}

WMPControl.prototype.getVolume = function() {
	if(!this.wmp) return 0;
	return this.wmp.settings.volume;
}

WMPControl.prototype.isMuted = function() {
	if(!this.wmp) return null;
	return this.wmp.settings.mute;
}

WMPControl.prototype.mute = function() {
	if(this.wmp) this.wmp.settings.mute = true;
//	if(this.onVolumeChanged) this.onVolumeChanged(this);
}

WMPControl.prototype.unMute = function() {
	if(this.wmp) this.wmp.settings.mute = false;
//	if(this.onVolumeChanged) this.onVolumeChanged(this);
}

WMPControl.prototype.toggleFullScreen = function() {
	if(!this.wmp) return;
	if(this.wmp.playState != 3) return;
	this.wmp.fullScreen = this.wmp.fullScreen ? false : true;
}

WMPControl.prototype.destroy = function() {
	var o = this;
	if(this.monitorTimer) window.clearInterval(this.monitorTimer);
	this.monitorTimer = null;
	this.stop();
	while(this.box.childNodes.length) this.box.removeChild(this.box.lastChild);
	delete o;
}
