/*
 * 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(playerUrl)
{
	this.id = "fmp";
	this.layerId = "mp-holder";
	this.audioOnlyImageId = 'audioonly-image';
	this.playerUrl = playerUrl ? playerUrl : "swf/axplayer-fmp-1.swf";
	this.version = 6;
	this.isReady = false;
	
	this.autoStart = true;
	this.bufferProgress = 0;
	this.currentPosition = 0;
	this.duration = 0;
	this.fps = 15;
	this.playState = 0;
	this.uiMode = "none";
	this.url = null;
	this.width = 320;
	this.height = 240;
	this.origHeight = this.height;
	this.reqFlashVersion = 6;
	this.canGetFlashVariable = false;
	this.canSetFlashVariable = false;
	
	//parameters
	this.salign = null; //T TL;
	this.wmode = null; //opaque ;
	this.menu = true;
	
	this.updateTimer = null;
	
	this.mp = this.getFlashMovieObject(this.id);
	
	if(document.addEventListener && !window.opera && !(!document.all && document.childNodes && !navigator.taintEnabled))
	{
	    //this.testtt = setTimeout(this.update, 1000);
	    //document.addEventListener('DOMContentLoaded', window.ttt(), null);
	    setTimeout("mp.update()", 1000);
	}
	else
	{
	    this.update();
	}	
}
FlashMediaPlayer.prototype.stopUpdate = function()
{
    if(this.updateTimer != null)
    {
        clearInterval(this.updateTimer);
        this.updateTimer = null;
    }
}
FlashMediaPlayer.prototype.update = function()
{
	try
	{
	    //alert("dsdfs");
	    var x = this.getPlayState();
        if(x != null && (x == 6 || x != this.playState))
        {
	        this.playState = x;
	        if(this.playStateChange != null)
	        {
		        this.playStateChange(this.playState);
	        }
        }
        var y = this.getPosition();
        if(y != null && y >= 0 && y != this.currentPosition)
        {
	        this.currentPosition = y;
	        if(this.positionChange != null)
	        {
		        this.positionChange(this.currentPosition);
	        }
        }
    	
        if(this.duration != null)
        {
            this.duration = this.getDuration();
        }
	}
	catch(ex){} 
	
	this.updateTimer = setTimeout("mp.update()", 1000);
}
FlashMediaPlayer.prototype.addVariable = function(key, value)
{
	if(this.fo != null)
	{
		this.fo.addVariable(key, value);
	}
}
FlashMediaPlayer.prototype.destroy = function()
{
	this.fo = null;
}
FlashMediaPlayer.prototype.fsCommandTest = function(v)
{
	if(this.mp != null){
		//alert('flash media player :'+v);
		this.isReady = true;
		this.flashCommand("set", "_root.ready", "true");
	}
}
FlashMediaPlayer.prototype.getDuration = function()
{
	return this.flashCommand("get", "_root.duration");
}
FlashMediaPlayer.prototype.flashCommand = function(type,o,value)
{
    try
    {
        if(type == "get")
        {
            return this.mp.GetVariable(o);
        }
        else if(type == "set")
        {
            this.mp.SetVariable(o, value);
        }
    }
    catch(ex)
    {
    }
}
FlashMediaPlayer.prototype.setImage = function(url)
{
    var img = document.getElementById(this.audioOnlyImageId);
    if(img != null && img.src != url)
    {
	    img.src = url;
    } 
    
    //show
    img = document.getElementById("audioonly");
    if(img != null)
    {
        img.style.display = "block";
    }
}
FlashMediaPlayer.prototype.getPlayState = function()
{
	return parseInt(this.flashCommand("get", "_root.playState"));
}
FlashMediaPlayer.prototype.getPosition = function()
{
	return parseFloat(this.flashCommand("get", "_root.currentPosition"));
}
FlashMediaPlayer.prototype.setPosition = function(s)
{
    this.currentPosition = s;
    this.flashCommand("set", "_root.mediaplayer", "setPosition("+this.currentPosition+")")
}
FlashMediaPlayer.prototype.setZoom = function(p)
{
	if(this.width != "100%" || this.height != "100%")
	{
		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.flashCommand("set", "_root.mediaplayer", "setUrl('"+url+"')"); 
}
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.getVolume = function()
{
	this.flashCommand("get", "_root.volume");
}
FlashMediaPlayer.prototype.setVolume = function(level)
{
	this.flashCommand("set", "_root.mediaplayer", "setVolume("+level+")");
}
FlashMediaPlayer.prototype.play = function()
{
	if(this.playState == 3)
	{
		this.pause();
	}
	else
	{
		this.flashCommand("set", "_root.mediaplayer", "play()");
	}
}
FlashMediaPlayer.prototype.pause = function()
{
	this.flashCommand("set", "_root.mediaplayer", "pause()");
}
FlashMediaPlayer.prototype.stop = function()
{
	this.flashCommand("set", "_root.mediaplayer", "stop()");
}
FlashMediaPlayer.prototype.write = function(layerId, width, height)
{
    //debugger
	this.layerId = layerId ? layerId : this.layerId;
	this.width = width ? width : this.width;
	this.height = height ? height : this.height;
	
	//check version
	if(this.url.indexOf(".flv") != -1){
		this.reqFlashVersion = 6;
	}
	
	//var flashvars;
	//if(this.fo != null){
	//	flashvars = this.fo.getVariables();
	//}
	this.fo = new FlashObject(this.playerUrl, this.id, this.width, this.height, this.reqFlashVersion);
	
	//Parameters
	//for js comm
	if(this.version > 7)
	{
	    this.fo.addParam("allowScriptAccess", "always");
	}
	else
	{
	    this.fo.addParam("allowScriptAccess", "sameDomain");	
	}
	if(!this.menu)
	{
	    this.fo.addParam("menu", this.menu);
	}
	if(this.salign != null)
	{
	    this.fo.addParam("salign", this.salign);    
	}
	if(this.wmode != null)
	{
	    this.fo.addParam("wmode", this.wmode);
	}
		
	//FlashVars
	/*
	if(flashvars != null)
	{
		for(var i in flashvars)
		{
			this.fo.addVariable(i, flashvars[i]);
		}
	}
	*/
	
	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 != null){
		this.fo.addVariable("url",this.url);
	}
	
	//write to container
	this.fo.write(this.layerId);
	this.fo = null;
	
	this.mp = this.getFlashMovieObject(this.id);
}
FlashMediaPlayer.prototype.getFlashMovieObject = function(id)
{
    if (window.document[id]) 
    {
        return window.document[id];
    }
    if (navigator.appName.indexOf("Microsoft Internet")==-1)
    {
        if (document.embeds && document.embeds[id])
        {
            return document.embeds[id]; 
        } 
    }
    else
    {
        return document.getElementById(id);
    }
}
FlashMediaPlayer.prototype.waitUntilReady = function()
{
    alert(this.mp.ReadyState + " " + this.mp.readyState);
    if(this.mp == null || this.mp.readyState != 4)
    {
        this.readyStateTimer = setTimeout("mp.waitUntilReady()", 100);
    }
    else
    {
        alert("ready");
        this.update();
        clearInterval(this.readyStateTimer);
        //do compatibility test
	    try
	    {
	        this.mp.setVariable("_root.test", "test");
	        this.canSetFlashVariable = true;
	    }
	    catch(ex)
	    {
	        this.canSetFlashVariable = false;
	    }
	    try
	    {
	        var x = this.mp.getVariable("_root.test");
	        if(x == "test")
	        {
	            this.canGetFlashVariable = true;
	        }
	        else
	        {
	            this.canGetFlashVariable = false;
	        }
	    }
	    catch(ex)
	    {
	        this.canGetFlashVariable = false;
	    }
    }
}
//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);
	}
}
