AXPositionBar = function(divId)
{
	this.positionBar = document.getElementById('position-bar');
	this.timeTxt = document.getElementById('time');
	this.playStateTxt = document.getElementById('playstate');
	this.positionArrow = document.getElementById('arrow');
	this.positionArrow.style.visibility = "hidden";
	this.bufferDiv = document.getElementById('buffer');
	this.positionDiv = document.getElementById('position');
	
	this.duration = 0;
	this.durationText = "";
	this.position = -1;
	this.playState = 0;
	this.bufferProgress = "0%";
	//events
	this.positionChange = null;
	
	this.mouseDown = function mouseDown(e)
	{
        if(this != arguments.callee._oScope){
          return arguments.callee.apply(arguments.callee._oScope, arguments);
        };

        var obj = null;
	    if (e.target)
	    {
	        obj = e.target;
	    }
		else if (e.srcElement)
		{   
		    obj = e.srcElement;
		}
		
		e = (e) ? e : window.event;
		
		var x = 0;
		if (typeof(e.pageX) != 'undefined')
		{
		    x = e.pageX - this.positionBar.offsetLeft;
		}
		else if(typeof(e.offsetX) != 'undefined')
		{
		    if(obj.id == "playstate")
		    {
		        x = e.offsetX;
		    }
		    else if(obj.id == "time")
		    {
		        x = e.offsetX + document.getElementById("playstate").offsetWidth;
		    }
		    else
		    {
		        x = e.offsetX;
		    }
		}
	
	 	var pos = Math.round(this.duration*(x/this.positionBar.clientWidth));
	 	//fire event
	 	if(this.positionChange != null)
		{
			this.positionChange(pos);
		}
  };
	
	this.mouseDown._oScope = this;
	
	if(EventManager != null)
	{
		EventManager.Add('position-bar', 'mouseover', this.mouseRollOver);
		EventManager.Add('position-bar', 'mouseout', this.mouseRollOut);
		EventManager.Add('position-bar', 'mousedown', this.mouseDown);
	}
	
	this.bufferDiv.style.width = "0%";
	this.positionDiv.style.visibility = "hidden";
	this.positionDiv.style.width = "0%";
	this.positionDiv.style.visibility = "hidden";
	this.timeTxt.innerHTML = "";
	this.timeTxt.style.visibility = "hidden";
}
AXPositionBar.prototype.setBuffering = function(p)
{
	if(p >= 0)
	{
		if(p >= 100)
		{ 
			p = "100%";
		}
		else
		{
			 p = p+"%";
		}
	}
	else
	{
		p = "0%";
	}
	this.bufferProgress = p;
	this.bufferDiv.style.width = p;
	this.bufferDiv.style.visibility = "visible";
}
AXPositionBar.prototype.setDownloadProgress = function(p)
{
	if(p >= 0)
	{
		if(p >= 100)
		{ 
			p = "100%";
		}
		else
		{
			 p = p+"%";
		}
	}
	else
	{
		p = "0%";
	}
	this.bufferProgress = p;
	this.bufferDiv.style.width = p;
	this.bufferDiv.style.visibility = "visible";
}
AXPositionBar.prototype.setDuration = function(sec)
{
	if(sec > -1)
	{
		this.duration = sec;
		this.durationText = this.toTime(sec);
		this.timeTxt.style.visibility = "visible";
		this.positionDiv.style.width = "0%";
		this.positionDiv.style.visibility = "visible";
		this.positionArrow.style.left = "0%";
	}
	else
	{
		this.durationText = "";
	}
}
AXPositionBar.prototype.setPosition = function(sec)
{
	if(this.positionBar != null && sec >= 0 && this.position != sec)
	{
		this.position = this.toTime(sec);
		if(this.durationText != "")
		{
			this.timeTxt.innerHTML = this.position + " / " + this.durationText;
			var x = Math.round(180*sec/(1.0*this.duration));
		    	x = (x>180)?180:x;
		    	x = (x<0)?0:x;
		    	this.positionDiv.style.width = x+"px";	
		    	this.positionArrow.style.left = x-4+"px";
		}
		else
		{
			this.timeTxt.innerHTML = this.position;
			this.positionDiv.style.width = "0px";
		}
	}
}
AXPositionBar.prototype.setPlayState = function(state)
{
	var playStateTxt = "Stopped";
	switch(state)
	{
		case 1://stopped
		{
			playStateTxt = "Press play to start webcast";
			break;
		}
		case 2://pause
		{
			playStateTxt = "Paused";
			break;
		}
		case 3://playing
		{
			playStateTxt = "Playing";
			break;
		}
		case 6://buffering
		{
			playStateTxt = "Buffering "+this.bufferProgress;
			break;
		}
		case 9://transitioning
		{
			playStateTxt = "Connecting";
			break;
		}
		case 10://ready
		{
			playStateTxt = "Press play to start webcast";
			break;
		}
		case 11://download progress
		{
			
			break;
		}
	}
	if(state == 2 || state == 3)
	{
		this.timeTxt.style.visibility = "visible";
		this.positionDiv.style.visibility = "visible";
		if(this.playState == 6)
		{
			this.bufferDiv.style.visibility = "hidden";
		}
		if(isLive != null && isLive)
		{
			playStateTxt = "Live webcast";
			if(this.positionArrow != null)
			{
				this.positionArrow.style.display = "none";
			}
		}
	}
	else
	{
		this.timeTxt.style.visibility = "hidden";
		this.positionDiv.style.visibility = "hidden";
	}
	this.playState = state;
	this.playStateTxt.innerHTML = playStateTxt;
}
AXPositionBar.prototype.mouseRollOver = function()
{
	var o = findObj('arrow');
	o.style.visibility = "visible";
}
AXPositionBar.prototype.mouseRollOut = function()
{
	var o = findObj('arrow');
	o.style.visibility = "hidden";
}
AXPositionBar.prototype.toTime = function(s)
{
	if(s == 0)
	{
		return "00:00";
	}
	var h = Math.floor(s/3600);
	if(h > 0)
	{
		s = s - h*3600;
	}
	var min = Math.floor(s/60);
	if(min < 10)
	{
		min = "0" + min;
	}
	var sec = s - min * 60;
	if(sec < 10)
	{
		sec = "0" + sec;
	}
	sec = sec.toString();
	sec = (sec.length > 2)?sec.substring(0,2):sec;
	
	var time;
	if(h > 0)
	{
		time = h + ":" + min + ":" + sec;
	}
	else
	{
		time = min + ":" + sec;
	}
	return time;
}
