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

function VLCControl(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.prevState = 0;

	if(
		options.plugin &&
		(
			options.plugin.classid && options.plugin.classid == 'clsid:9BE31822-FDAD-461B-AD51-BE1D1C159921' ||
			options.plugin.type && options.plugin.type == 'application/x-vlc-plugin'
		)
	) this.vlc = options.plugin;
	else {
		$(this.box).empty();
		if($.browser.msie) {
//			if(!checkForVLC) { this.errorDescr = "Plugin not found"; this.onPluginLoadFailed(this); return; }
			$(this.box).html("<OBJECT"+
				" classid='clsid:9BE31822-FDAD-461B-AD51-BE1D1C159921' codebase='http://downloads.videolan.org/pub/videolan/vlc/latest/win32/axvlc.cab'"+
				" events=\"true\" width=\""+this.wd.toString()+"\" height=\""+this.ht.toString()+"\">"+
				"<PARAM name='Src' value=''>"+
				"<PARAM name='AutoPlay' value='False'>"+
				"<PARAM name='AutoLoop' value='False'>"+
			"</OBJECT>");
		} else if(navigator.plugins) {
			var found_v = false;
			var i;
			for(i = 0; i < navigator.plugins.length; i++)
				if(-1 != navigator.plugins[i].name.search(/vlc/i)) { found_v = true; break; }
			if(!found_v) { this.errorDescr = "Plugin not found"; this.onPluginLoadFailed(this); return; }
			$(this.box).html("<EMBED"+
				" type='application/x-vlc-plugin'"+
				" progid='VideoLAN.VLCPlugin.2'"+
				" pluginspage='http://www.videolan.org'"+
				" events=\"true\""+
				" width=\""+this.wd.toString()+"\""+
				" height=\""+this.ht.toString()+"\""+
				" AutoPlay='False'"+
				" AutoLoop='False'>"+
			"</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();
}

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

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

VLCControl.prototype.onPluginLoaded = function() {
	if(this.userOnPluginLoaded) this.userOnPluginLoaded(this);
}

VLCControl.prototype.waitForLoad = function () {
	var continue_waiting = true;
	if(this.userWaitForLoad) continue_waiting = this.userWaitForLoad(this); // call user defined waitForLoad function if exists
	var o = this;
	if(this.vlc.versionInfo) this.onPluginLoaded(); // plugin successfully loaded
	else {
		this.waitingIterations++;
		if(!continue_waiting) {
			this.errorDescr = "User defined onWaitForLoad function has stopped plugin load waiting";
			this.onPluginLoadFailed();
		} else if(this.waitingIterations >= this.maxWaitIterations) {
			this.errorDescr = "Plugin load waiting time has gone";
			this.onPluginLoadFailed();
		} else window.setTimeout(function() { o.waitForLoad(); }, this.waitItereationTime);
	}
};

VLCControl.prototype.versionInfo = function() { return this.vlc && this.vlc.versionInfo ? (this.vlc.versionInfo() ? this.vlc.versionInfo() : 'VLC') : null; }

VLCControl.prototype.getPlayerName = function() { return this.vlc ? 'VLC' : null; }

VLCControl.prototype.getPlayerStatus = function() {
	return (this.vlc ? this.vlc.input.state : null);
}

VLCControl.prototype.monitor = function() {
	if(!this.vlc) return;
	
	var newState = this.vlc.input.state;
	if( this.prevState != newState ) {
		switch(newState) {
			case 0: // current media has stopped
			if(this.changing_source) break;
			if(this.forceLoop && !this.manuallyStopped) this.playCurrent();
			else if(this.onPlayerStop) this.onPlayerStop(this);
			break;

			case 1: // current media is openning/connecting
				if(this.onConnectingBuffering) this.onConnectingBuffering(this);
			break;

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

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

			case 4: // current media is now paused
			break;
		}
		this.prevState = newState;
		this.changing_source = false;
	} else if( newState == 3 ) { // current media is playing
		if(this.onPlaying) this.onPlaying(this);
	}
	var o = this;
	if(!this.monitorTimer) {
		this.monitorTimer = setInterval(function() { o.monitor(); }, 100);
	}
}

VLCControl.prototype.playCurrent = function() {
	if(!this.vlc) return;
	this.vlc.playlist.play();
}

VLCControl.prototype.play = function(src) {
	if(!this.vlc) return;
	if(!src) return;
	src = src.replace(/^mms:/i, "mmsh:");
//	this.stop();
	this.changing_source = true;
	this.vlc.playlist.items.clear();
    var it = this.vlc.playlist.add(src);
	if(it == -1) {
		alert("VLC error!");
		return;
	}
	this.vlc.playlist.playItem(it);
	if(!this.monitorTimer) this.monitor();
}

VLCControl.prototype.stop = function() {
	if(!this.vlc) return;
	if(!this.vlc.playlist.isPlaying) return;
	this.manuallyStopped = true;
	this.vlc.playlist.stop();
}

VLCControl.prototype.setPosition = function(pos) { // set position in seekable media to pos percent
	if(!this.vlc) return;
	this.vlc.input.position = parseFloat(pos) / 100.0;
}

VLCControl.prototype.getPosition = function() { // get position in seekable media in percent
	if(!this.vlc) return 0;
	return Math.round(this.vlc.input.position * 100);
}

VLCControl.prototype.volumeChange = function(dv) { // change volume with dv percent
	if(!this.vlc) return;
	var v = this.vlc.audio.volume+dv;
	if(v < 0) v = 0;
	else if(v > 100) v = 100;
	if(v == this.vlc.audio.volume) return;
	this.vlc.audio.volume = v;
	if(this.onVolumeChanged) this.onVolumeChanged(this);
}

VLCControl.prototype.setVolume = function(vp) { // change volume to vp percent
	if(!this.vlc) return;
	vp = vp;
	if(vp < 0) vp = 0;
	else if(vp > 100) vp = 100;
	if(vp == this.vlc.audio.volume) return;
	this.vlc.audio.volume = vp;
	if(this.onVolumeChanged) this.onVolumeChanged(this);
}

VLCControl.prototype.getVolume = function() {
	if(!this.vlc) return 0;
	return this.vlc.audio.volume;
}

VLCControl.prototype.isMuted = function() {
	if(!this.vlc) return null;
	return this.vlc.audio.mute;
}

VLCControl.prototype.mute = function() {
	if(this.vlc) this.vlc.audio.mute = true;
//	if(this.onVolumeChanged) this.onVolumeChanged(this);
}

VLCControl.prototype.unMute = function() {
	if(this.vlc) this.vlc.audio.mute = false;
//	if(this.onVolumeChanged) this.onVolumeChanged(this);
}

VLCControl.prototype.toggleFullScreen = function() {
	if(!this.vlc) return;
	this.vlc.video.toggleFullscreen();
}

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

