/********************************
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 = {};
	if(!options.wd) options.wd = 432;
	if(!options.ht) options.ht = 324;
	if(!options.volume) options.volume = 50;
	this.maxWaitIterations = (options.maxWaitIterations ? options.maxWaitIterations : 8);
	options.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.wd = options.wd;
	this.ht = options.ht;
	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;

/*	box.innerHTML = "<OBJECT "+
		(-1 != navigator.userAgent.indexOf("MSIE") && -1 == navigator.userAgent.indexOf("Opera") ? 
			"classid='clsid:9BE31822-FDAD-461B-AD51-BE1D1C159921' codebase='http://downloads.videolan.org/pub/videolan/vlc/latest/win32/axvlc.cab#Version=0,8,6,0'"
			: "type='application/x-vlc-plugin' progid='VideoLAN.VLCPlugin.2' pluginspage='http://www.videolan.org'"
		)+" events=\"true\" width=\""+options.wd+"\" height=\""+options.ht+"\">"+
		"<PARAM name='AutoPlay' value='False'>"+
		"<PARAM name='AutoLoop' value='False'>"+
	"</OBJECT>";
*/
	if(options.plugin) this.vlc = options.plugin;
	else {
		while(box.childNodes.length) box.removeChild(box.lastChild);
		if(-1 != navigator.userAgent.indexOf("MSIE") && -1 == navigator.userAgent.indexOf("Opera")) {
			box.innerHTML = "<OBJECT"+
				" classid='clsid:9BE31822-FDAD-461B-AD51-BE1D1C159921' codebase='http://downloads.videolan.org/pub/videolan/vlc/latest/win32/axvlc.cab'"+
				" events=\"true\" width=\""+options.wd+"\" height=\""+options.ht+"\">"+
				"<PARAM name='Src' value=''>"+
				"<PARAM name='AutoPlay' value='False'>"+
				"<PARAM name='AutoLoop' value='False'>"+
			"</OBJECT>";
		} else {
				box.innerHTML = "<EMBED"+
				" type='application/x-vlc-plugin'"+
				" progid='VideoLAN.VLCPlugin.2'"+
				" pluginspage='http://www.videolan.org'"+
				" events=\"true\""+
				" width=\""+options.wd+"\""+
				" height=\""+options.ht+"\""+
				" AutoPlay='False'"+
				" AutoLoop='False'>"+
			"</EMBED>";
		}
		this.vlc = box.lastChild;
	}

	if(navigator.plugins && navigator.plugins.length) { // for Netscape-style 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.onPluginLoadFailed(this);
		else {
			this.waitingIterations = 0;
			this.waitForLoad();
		}
	} else { // for IE, sequential load
		if(!this.vlc.versionInfo) this.onPluginLoadFailed();
		else this.onPluginLoaded();
	}
}

VLCControl.prototype.onPluginLoadFailed = function() {
	var box = this.vlc.parentNode;
	while(box.childNodes.length) box.removeChild(box.lastChild);
	this.vlc = null;
	if(this.userOnPluginLoadFailed) this.userOnPluginLoadFailed(this);
}

VLCControl.prototype.onPluginLoaded = function() {
	this.vlc.style.width  = this.wd+"px";
	this.vlc.style.height = this.ht+"px";
	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 {
		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 vlc object
};

VLCControl.prototype.versionInfo = function() {	return this.vlc ? this.vlc.versionInfo : 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.forceLoop && !this.manuallyStopped) this.playCurrent();
			else if(this.onPlayerStop) this.onPlayerStop(this);
			break;

			case 1: // current media is openning/connecting
			break;

			case 2:
			// current media is buffering data
			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;
	} 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_after_clear = function(src) {
	this.plWaitIterations++;
	if(this.vlc.playlist.itemCount) {
		if(this.plWaitIterations > 4) {
			alert("VLC error!");
			return;
		}
		var o = this;
		var s = src;
		window.setTimeout(function () { o.play_after_clear(s); }, 250);
		return;
	}
	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.play = function(src) {
//alert(src+"\n"+(this.vlc ? "Ok" : "Bad"));
	if(!this.vlc) return;
	if(!src) return;
	src = src.replace(/^mms:/i, "mmsh:");
	this.stop();
	this.vlc.playlist.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();
/*	this.plWaitIterations = 0;
	this.play_after_clear(src);
*/
}

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.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;
}
