/*
 * FlashMediaPlayer
 * copyright 2005 Axisto Media Ltd. All rights reserved.
 * by Andreas Rimbe, Axisto Media Ltd (andreas.rimbe@axisto.com, http://www.axisto.com/)
 *
 * v1.5 - 06-05-2005
 *
 * Creates a flash video player
 *
 * Usage:
 *
 *	myFlash = new FlashObject("path/to/swf.swf", "swfid", "width", "height", flashversion, "backgroundcolor");
 *	myFlash.write("objId");
 *
 */
FlashMediaPlayer = function(parentDiv, id, width, height, version)
{
	this.id = id ? id : "mp";
	this.layerId = parentDiv ? parentDiv : "video";
	this.audioOnlyImageId = 'audioonly-image';
	this.playerPath = "swf/axplayer-fmp-1.swf";
	
	this.isReady = false;
	
	this.autoStart = true;
	this.bufferProgress = 0;
	this.currentPosition = 0;
	this.fps = 15;
	this.playState = 0;
	this.uiMode = "none";
	this.url = "";
	this.width = width ? width : 320;
	this.height = height ? height : 240;
	this.origHeight = this.height;
}
FlashMediaPlayer.prototype.addVariable = function(key, value)
{
	if(this.fo != null)
	{
		this.fo.addVariable(key, value);
	}
}
FlashMediaPlayer.prototype.destroy = function()
{
	this.fo = null;
}
FlashMediaPlayer.prototype.init = function()
{
	//check version
	this.reqFlashVersion = 6;
	if(this.url.indexOf(".flv") != -1){
		this.reqFlashVersion = 7;
	}
	
	var flashvars;
	if(this.fo != null){
		flashvars = this.fo.getVariables();
	}
	this.fo = new FlashObject(this.playerPath, this.id, this.width, this.height, this.reqFlashVersion);
	//add pars
	if(flashvars != null)
	{
		for(var i in flashvars)
		{
			this.fo.addVariable(i, flashvars[i]);
		}
	}
	//default true
	if(!this.autoStart)
	{
		this.fo.addVariable("autostart",this.autoStart);
	}
	if(this.fps != 15)
	{
		this.fo.addVariable("fps",this.fps);
	}
	if(this.currentPosition != 0)
	{
		this.fo.addVariable("position",this.currentPosition);
	}
	if(this.uiMode != "full")
	{
		this.fo.addVariable("uimode",this.uiMode);
	}
	if(this.url != ""){
		this.fo.addVariable("url",this.url);
	}
	this.write(this.layerId);
}
FlashMediaPlayer.prototype.fsCommandTest = function(v)
{
	if(this.mp != null){
		//alert('flash media player :'+v);
		this.isReady = true;
		this.mp.setVariable("_root.ready", "true");
	}
}
FlashMediaPlayer.prototype.getDuration = function()
{
	return 483;
}
FlashMediaPlayer.prototype.getPosition = function()
{
	if(this.mp != null)
	{
		return this.currentPosition;
	}
}
FlashMediaPlayer.prototype.setPosition = function(s)
{
	this.currentPosition = s;
	if(isIE)
	{
		this.mp.setVariable("_root.mediaplayer", "setPosition("+s+")");
	}
	else
	{
		if(this.playState == 2)
			this.fo.addVariable("autostart",false);
		this.init();
	}
}
/*
FlashMediaPlayer.prototype.setSize = function(w, h)
{
	if(this.mp != null)
	{
		this.mp.setVariable("_root.mediaplayer", "setSize("+w+","+h+")");
	}
}
*/
FlashMediaPlayer.prototype.setZoom = function(p)
{
	var w = this.width*(p/100.0);
	w = w>0?w:1;
	var h = this.height*(p/100.0);
	h = h>0?h:1;
	this.setSize(w, h);
}
FlashMediaPlayer.prototype.setSize = function(w, h)
{
	if(this.mp != null)
	{
		if(document.all && !document.getElementById)
		{
			document.all[this.id].style.pixelWidth = w;
			document.all[this.id].style.pixelHeight = h;
		}
		else
		{
			document.getElementById(this.id).style.width = w+"px";
			document.getElementById(this.id).style.height = h+"px";
		}
	}
	//hide audioonly image
	var img = findObj(this.audioOnlyImageId);
	if(img != null)
	{
		if(this.uiMode == "audio" && w != 1 && h != 1)
		{
			img.style.display = 'block';
		}
		else
		{
			img.style.display = 'none';
		}
	}
}
FlashMediaPlayer.prototype.setUrl = function(url)
{
	this.url = url;
	this.currentPosition = 0;
	this.fo = null;
	if(isIE)
	{
		if(this.fo == null)
			this.init();
		if(this.mp != null)
		{
			if(this.isReady)
				this.mp.setVariable("_root.mediaplayer", "setUrl('"+url+"')");
		}
	}
	else
	{	
		this.init();
	}
}
FlashMediaPlayer.prototype.setUIMode = function(mode)
{
	if(this.uiMode != mode)
	{
		this.uiMode = mode;
		var img = findObj(this.audioOnlyImageId);
		switch(mode)
		{
			case "audio":
			{
				this.height = 20;
				break;
			}
			case "none":
			{
				this.height = this.origHeight;
				break;
			}
			case "full":
			{
				this.height = this.origHeight+20;
				break;
			}
		}
		if(this.mp != null)
		{
			this.setSize(this.width, this.height);
		}
		if(img != null && mode == "audio")
		{
			img.style.display = 'block';
		}
		else if(img != null)
		{
			img.style.display = 'none';
		}
		//audio - show image and controls
		//none - video window only
		//full - with controls
		//invisible
	}
}
FlashMediaPlayer.prototype.setVolume = function(v)
{
	if(this.mp != null)
	{
		//this.mp.setVariable("_root.mediaplayer", "setUrl('"+url+"')");
	}
}
FlashMediaPlayer.prototype.play = function()
{
	if(this.playState == 3)
	{
		this.pause();
	}
	else
	{
		this.playState = 3;
		if(isIE)
		{
			this.mp.setVariable("_root.mediaplayer", "play()");
		}
		else
		{
			this.fo = null;
			this.init();
		}
	}
}
FlashMediaPlayer.prototype.pause = function()
{
	this.playState = 2;
	if(this.mp != null)
	{
		if(isIE)
		{
			this.mp.setVariable("_root.mediaplayer", "pause()");
		}
		else{
			this.fo.addVariable("autostart",false);
			this.fo.addVariable("position", this.currentPosition);
			this.init();
			//this.write(this.layerId);
		}
	}
}
FlashMediaPlayer.prototype.stop = function()
{
	this.playState = 1;
	if(this.mp != null)
	{
		this.mp.setVariable("_root.mediaplayer", "stop()");
	}
}
FlashMediaPlayer.prototype.write = function(layerId)
{
	this.layerId = layerId;
	if(this.fo != null)
	{
		this.fo.write(layerId);
		this.mp = document.getElementById(this.id);
	}
}
//EVENTS
FlashMediaPlayer.prototype.as_bufferProgress = function(b)
{
	this.bufferProgress = parseInt(b);
}
FlashMediaPlayer.prototype.as_downloadProgress = function(b)
{
	this.downloadProgress = parseInt(b);
	if(this.downloadProgressChange != null)
	{
		this.downloadProgressChange(this.downloadProgress);
	}
}
FlashMediaPlayer.prototype.as_positionChange = function(s)
{
	this.currentPosition = parseFloat(s);
	if(this.positionChange != null)
	{
		this.positionChange(this.currentPosition);
	}
}
FlashMediaPlayer.prototype.as_playStateChange = function(s)
{
	//alert(s);
	this.playState = parseInt(s);
	if(this.playStateChange != null)
	{
		this.playStateChange(this.playState);
	}
}
