// Div tag for player.
var player = null;
// Div tag for control label.
var control = null;
// Div tag for inner bar loading.
var inner = null;
// Div tag for knob button.
var knob = null;
// Div tag for time label.
var time = null;
// Maximum relative position of the knob button.
var maxPos = 0;
// Maximum width for the inner loading bar.
var maxWidth = 0;
// Max length sound in string.
var maxTime = "";
// Player status whether playing or stopping.
var status = "play";
// Current position of the playback.
var position = 0;
// Load ratio of the sound.
var loadRatio = 0;
var length = 0;
var isPanning = false;

// Load sound by specifying location of the sound and buffer length.
function Load(location, buffer)
{
    player.Load(location, buffer);
}

// Start sound by specifying position or not.
function Start(position)
{
	status = "play";
	control.innerHTML = "Pause";
    player.Start(position);
}

// Stop sound.
function Stop()
{
	status = "pause";
	control.innerHTML = "Play";
    player.Pause();
}

// Constantly check the loading ratio
function LoadingMonitor(/*Number*/ interval)
{
    var monitor = function() {
        loadRatio = player.GetLoadRatio();
        
        inner.style.width = loadRatio * maxWidth + "px";
        
		if (loadRatio != 1)
		{
     		setTimeout(monitor, interval);
		}
    }

    monitor();
}

// Constantly check the playback time
function PlaybackMonitor(/*Number*/ interval)
{
    var monitor = function() {
		position = player.GetPlayTime();

		if (isPanning == false)
		{
            knob.style.left = (position / length * maxPos) + "px";
		}

		time.innerHTML = ToStringTime(position) + "/" + maxTime;
		setTimeout(monitor, interval);
    }

    monitor();
}

// Convert millisecond to time in string
function ToStringTime(/*Number*/ milliSec)
{
	var result = "";
	var hour = Math.floor(milliSec / 3600000);
	milliSec = milliSec % 3600000;
	var minute = Math.floor(milliSec / 60000);
	milliSec = milliSec % 60000;
	var second = Math.floor(milliSec / 1000);
	
	//Make sure to have two digit time
	if(hour < 10)
	{
		result = "0" + hour + ":";
	}
	else
	{
		result = hour + ":";
	}
	
	//Make sure to have two digit time
	if(minute < 10)
	{
		result = result + "0" + minute + ":";
	}
	else
	{
		result = result + minute + ":";
	}
	
	//Make sure to have two digit time
	if(second < 10)
	{
		result = result + "0" + second;
	}
	else
	{
		result = result + second;
	}
	
	return result;
}

function OnControlClick()
{
	if (status == "play")
	{
		Stop();
	}
	else
	{
		Start(position);
	}
}

function OnKnobDrag(/*Number*/ x, /*Number*/ y)
{
	isPanning = true;
}

function OnKnobDragEnd(/*Number*/ x, /*Number*/ y)
{
	isPanning = false;
	Start(x / maxPos * length);
}

function OnError(event)
{
    alert("Player caught error message :" + event);
}

function SetPlayer()
{
    player = document["player"];
}

// Init player by inserting new location of music.
function InitPlayer(/*String*/ location, /*String*/ name, /*String*/ author, /*Number*/ length)
{
    var title = document.getElementById("pod_title");
    var artist = document.getElementById("pod_preacher");
	knob = document.getElementById("pod_knob");
	inner = document.getElementById("pod_inner");
	control = document.getElementById("pod_control");
	time = document.getElementById("pod_timer");
    
	title.innerHTML = name;
	artist.innerHTML = author;
	this.length = length;
	maxTime = ToStringTime(length);
	
	// Get the inner width.
	maxWidth = inner.clientWidth;
	// Calculate the knob width and inner bar.
	maxPos = maxWidth - knob.clientWidth;

	// Set draggable knob.
	Drag.init(knob, null, 0, maxPos, 0, 0);
	knob.onDrag = OnKnobDrag;
	knob.onDragEnd = OnKnobDragEnd;

    var func = function()
    {
        // Get flash object by name that specified before.
        SetPlayer();
        // Load the sound.
        Load(location, 8000);
        Start(0);
        LoadingMonitor(100);
        PlaybackMonitor(100);
    }
    
	// Give time to finished load the player and auto start it.
    setTimeout(func, 1000);
}