/*
 * 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(o)
{
    this.id = o.id ? o.id : "mp1";
    this.mpId = o.mpId ? o.mpId : "flv1";
	this.divId = o.divId ? o.divId : "videoph1";
	this.audioOnlyImageId = o.audioOnlyImageId ? o.audioOnlyImageId :'audioonly';
	this.playerUrl = o.playerUrl ? o.playerUrl : "swf/ax-fp1.swf";
	this.width = o.width ? o.width : 384;
	this.height = o.height ? o.height : 288;
	this.requiredVersion = 8;
	this.url = o.url ? o.url : "";
	
	this.isReady = false;
	
	this.allowFullScreen = true;	
	this.autoStart = true;
	this.autoSize = true;
	this.maintainAspectRatio = true;
	this.currentPosition = 0;
	this.duration = -1;
	this.enableCallbacks = true;
	this.fps = 0;
	this.isReady = false;
	this.isSeeking = false; //todo move functionality flash
	this.isForwarding = false;
	this.playState = "Stopped";
	this.uiMode = "none";
	this.volumeLevel = 100;
	
	this.videoWidth = this.width;
	this.videoHeight = this.height;
	
	this.updateTimer = new PeriodicalExecuter(this.update.bindAsEventListener(this), 0.25);
	this.updateTimer.start();
	
	this.bufferTime = 2;
	this.bufferTimeExtended = 30;
	this.bufferProgress = 0;
	this.bufferingTimer = new PeriodicalExecuter(this.getBufferingProgress.bindAsEventListener(this), 0.25);
	
	//create if ready
	if(this.divId != "" && this.url != "")
	{
	    this.write();
	}
	
	//fix size for div
	var ph = $(this.divId);
	if(ph != null)
	{
	    ph.style.width = this.width+"px";
	    ph.style.height = this.height+"px";
	}
}
FlashMediaPlayer.prototype.stopUpdate = function()
{
    if(this.updateTimer != null)
    {
        this.updateTimer.pause();
    }
}
FlashMediaPlayer.prototype.onPlayStateChange = function(newState)
{
    if(newState!= this.playState )
    {
        this.playState = newState;
        var stateId = -1;
        switch(this.playState)
        {
            case "MetaData":
                stateId = 10;
                var o = this.mp.getVideoSize();
                if(o != null && o.width != null && o.height != null)
                {
                    this.videoWidth = o.width;
                    this.videoHeight = o.height;
                    if(this.autoSize)
                    {
                        //alert(this.videoWidth+" "+this.videoHeight);
                        this.setSize(this.videoWidth, this.videoHeight); 
                    }
                }
                this.duration = this.getDuration();
                break;
            case "Buffering":
                stateId = 6;
                this.bufferingTimer.start();
                break;
            case "Stopped":              
                stateId = 1;
                break;
            case "Paused":              
                stateId = 2;
                break;
            case "Playing":
                stateId = 3;
                this.bufferingTimer.pause();              
                break;
            case "Ready":              
                this.isReady = true;
                break;
            default:
                this.bufferingTimer.pause();                
                break;
        }
        if(this.playStateChange != null && stateId > -1)
        {
            this.playStateChange(stateId);
        }
    }
}
FlashMediaPlayer.prototype.update = function()
{
	try
	{
        var y = this.getPosition();
        if(y != null && y >= 0)
        {
            if(this.isSeeking)
            {
                if(this.isForwarding && y > this.currentPosition + 2)
                {
                    this.isSeeking = false;
	                this.isForwarding = false;
    	            this.currentPosition = y;
                }
                else if(!this.isForwarding && y + 3 < this.currentPosition)
                {
                    this.isSeeking = false;
	                this.isForwarding = false;
    	            this.currentPosition = y;
                }
            }
            else if(y != this.currentPosition)
            {
                this.currentPosition = y;
            }
            
            if(this.duration > 0)
	        {
	            if(Math.abs(this.duration-this.currentPosition) < 1)
	            {
	                //end of stream
	                if(this.playStateChange != null)
		            {
			            this.playStateChange(5);
		            }
	            }    
	        }
            
            if(this.positionChange != null)
            {
                this.positionChange(this.currentPosition);
            }
        }
	}
	catch(ex)
	{
	} 
}
FlashMediaPlayer.prototype.getBufferingProgress = function()
{
    if(this.mp != null)
	{
	    try
	    {
	        this.bufferProgress = this.mp.getBufferingProgress2();
	        if(this.playState == "Buffering" && this.playStateChange != null)
            {
                this.playStateChange(6);
            }
	    }
	    catch(ex)
	    {
	    }
	}
}
FlashMediaPlayer.prototype.getDuration = function()
{
	var o = -1;
	if(this.mp != null)
	{
	    o = this.mp.getDuration();
	}
	this.duration = o;
	return o;
}
FlashMediaPlayer.prototype.setImage = function(url, alt)
{
    var imgDiv = $(this.audioOnlyImageId);
    var img = $("audioonly-image");
	if(img != null && url != null && url != "")
	{
		//alert(url + this.uiMode);
		img.src = url;
		img.style.display = 'block';
		if(alt != null && alt != "")
		{
		    img.setAttribute('alt', "Image of " + alt);
		    img.setAttribute('title', alt);
		}
	}
	if(imgDiv != null)
	{
	    if(this.uiMode == "invisible" || showAudioImage)
			imgDiv.style.display = 'block';
		else
		    imgDiv.style.display = 'none';
	}
}
FlashMediaPlayer.prototype.getPlayState = function()
{
	var o;
	if(this.mp != null)
	{
	    this.mp.getPlayState();
	}
	return o;
}
FlashMediaPlayer.prototype.getPosition = function()
{
	var o = 0;
	if(this.mp != null)
	{
	    o = this.mp.getPosition();
	}
	return o;
}
FlashMediaPlayer.prototype.setPosition = function(s)
{
    if(s > -1 && s != this.currentPosition)
    {
        this.isSeeking = true;
        if(s > this.currentPosition)
        {
            this.isForwarding = true;
        }
        this.currentPosition = s;
        if(this.mp != null)
	    {
	        this.mp.setPosition(this.currentPosition);
	    }
    }
}
FlashMediaPlayer.prototype.getZoom = function()
{
	var o;
	if(this.mp != null)
	{
	    this.mp.getZoom();
	}
	return o;	
}
FlashMediaPlayer.prototype.setZoom = function(p)
{
    var w = this.videoWidth*(p/100.0);
	w = w>0?w:1;
	var h = this.videoHeight*(p/100.0);
	h = h>0?h:1;
	
	if(this.uiMode != "invisible")
	{
	    this.setSize(w, h);
	}
	//hide audioonly image
	var img = $(this.audioOnlyImageId);
	if(p == 0 && img != null)
	{
		img.style.display = 'none';
	}
	else if(img != null && (this.uiMode == "audio" || this.uiMode == "invisible" || showAudioImage))
	{
		img.style.display = 'block';
	}
}
FlashMediaPlayer.prototype.getSize = function()
{
	var o;
	if(this.mp != null)
	{
	    this.mp.getSize();
	}
	return o;
}
FlashMediaPlayer.prototype.setSize = function(w, h)
{
    this.height = h>0?h:1;
    this.width = w>0?w:1;

    if(this.mp != null)
	{
	    this.mp.style.width = w+"px";
	    this.mp.style.height = h+"px";
	}
}
FlashMediaPlayer.prototype.setUrl = function(url)
{
	this.url = url;
	if(this.mp != null && this.isReady)
	{
	    this.mp.setMediaUrl(this.url);
	}
}
FlashMediaPlayer.prototype.setUIMode = function(mode)
{
    if(this.uiMode != mode)
	{
		this.uiMode = mode;
		switch(mode)
		{
		    case "invisible":
			case "audio":
			{
			    this.autoSize = false;
				this.height = 1;
				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);
		}
		
		//audio - show image and controls
		//none - video window only
		//full - with controls
		//invisible
	}
	
	var imgDiv = $(this.audioOnlyImageId);
	if(imgDiv != null)
    {
        if(this.uiMode == "invisible" || showAudioImage)
        {
		    imgDiv.style.display = 'block';
	    }
	    else
	    {
	        imgDiv.style.display = 'none';
	    }
    }
    var img = $("audioonly-image");
    if(img != null)
    {
	    img.style.display = 'block';
    }
}
FlashMediaPlayer.prototype.getVolume = function()
{
    var o = 0;
	if(this.mp != null)
	{
	    o = this.mp.getVolume();
	}
	return o;
}
FlashMediaPlayer.prototype.setVolume = function(level)
{
	if(this.mp != null)
	{
	    this.mp.setVolume(level);
	}
}
FlashMediaPlayer.prototype.play = function()
{
	if(this.mp != null)
	{
	    this.mp.MediaPlay();
	}
}
FlashMediaPlayer.prototype.pause = function()
{
	if(this.mp != null)
	{
	    this.mp.MediaPause();
	}
}
FlashMediaPlayer.prototype.stop = function()
{
	if(this.mp != null)
	{
	    this.mp.MediaStop();
	}
}
FlashMediaPlayer.prototype.write = function(divId)
{
    this.divId = divId ? divId : this.divId;
	
	//check for swf video
	if(this.url.indexOf(".swf") > -1)
	{
		this.requiredVersion = 6;
	}
	
	var fo = new FlashObject(this.playerUrl, this.mpId, this.width, this.height, this.requiredVersion);
	
	//Parameters 
	if(this.allowFullScreen)
	{
	    fo.addParam("allowFullScreen", this.allowFullScreen);    
	}
	
	if(this.requiredVersion > 7)
	{
	    fo.addParam("allowScriptAccess", "always");
	}
	else
	{
	    fo.addParam("allowScriptAccess", "sameDomain");	
	}
	
	if(this.salign != null)
	{
	    fo.addParam("salign", this.salign);    
	}
	
	if(this.wmode != null)
	{
	    fo.addParam("wmode", this.wmode);
	}
	
	//FlashVars
	if(!this.autoStart)
	{
		fo.addVariable("autoStart",this.autoStart.toString());
	}
	
	if(this.bufferTime != null)
	{
	    fo.addVariable("bufferDuration", this.bufferTime);    
	}
	
	if(this.bufferTime != null)
	{
	    fo.addVariable("bufferDuration2", this.bufferTimeExtended);    
	}
	
	if(this.currentPosition > 0)
	{
		fo.addVariable("currentPosition",this.currentPosition);
	}
	
	if(this.enableCallbacks)
	{
	    fo.addVariable("enableCallbacks", this.enableCallbacks);    
	}
	
	if(this.fps > 0)
	{
		fo.addVariable("framerate",this.fps);
	}
	
	if(this.id != null)
	{
		fo.addVariable("id", this.id);
	}
	
	if(this.uiMode != "full")
	{
		//this.fo.addVariable("uimode",this.uiMode);
	}
	
	if(this.url != null)
	{
		fo.addVariable("mediaUrl",this.url);
	}
	
	if(this.volumeLevel != 70)
	{
		fo.addVariable("volumeLevel", this.volumeLevel);
	}
	
	//write to container
	var ph = $(this.divId);
	ph.style.width = "";
	ph.style.height = "";
	
	fo.write(this.divId);
	this.mp = this.getFlashMovieObject(this.mpId);
}
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);
    }
}
//EVENTS
function fp_playStateChange(mp, newState)
{
    //alert(mp + " " + newState);
    if(window[mp] != null)
    {
        window[mp].onPlayStateChange(newState);
    }
}

