// Kerrang Radio On Demand
// Copyright 2005 Emap Performance Ltd.

var width = 210;

// Global Variables
var positionID = 0;
var currentPosition = 0;
var newPosition = currentPosition;
var movingSlider = 0;
var dragOffset = 0;
var slider;
var isPlaying = 1;


// preload Play and Pause buttom images.
var playImage = new Image();
playImage.src = "img/c_play.gif";
var pauseImage = new Image();
pauseImage.src = "img/c_pause.gif";




// Functions
function init() {
	
	// as we're using ActiveX (yuck) we need to be on IE under Windows.
	if ((navigator.appName.indexOf("Microsoft") != -1) && (navigator.userAgent.toLowerCase().indexOf("win") != -1)) {

		// we need media player controls to be available.
		if (RadioPlayer.controls != null) {
			// Setup default slider handlers.
			document.onmousedown = startSliderDrag;
			document.onmouseup = stopSliderDrag;

			// pump up the volume, pump up the volume!
			RadioPlayer.settings.volume = 100;
	
			// get the radio playing.
			showStatus("Loading... " + stream);
			RadioPlayer.URL = stream;
			updatePosition();

		} else {
			// alert("You need to upgrade your media player");
		}
	}
}

function updatePosition() {


	// if we are running as a timed event...
	if (positionID) {
		clearTimeout(positionID);
		positionID = 0;
	}

	
	// if no-one is moving the slider by hand...
	if (movingSlider == 0) {

		// display the current status
		showStatus(statusMessage());

		// update the current position.
		currentPosition = Math.floor(RadioPlayer.controls.currentPosition);

		// move the slider.
		document.getElementById("positionSlider").style.pixelLeft = Math.floor((width / RadioPlayer.currentMedia.duration) * currentPosition);
	}
	
	// set the timer to call us again in 1 seconds time.
	positionID = setTimeout("updatePosition()", 1000);
}


function startSliderDrag() {
	if (event.button == 1 && event.srcElement.id == "positionSlider") {
		movingSlider = 1;
		slider = event.srcElement;
		dragOffset = event.clientX - slider.style.pixelLeft;
		document.onmousemove = doSliderDrag;
	}
}


function stopSliderDrag() {
	if (movingSlider == 1) {
		RadioPlayer.controls.currentPosition = newPosition;
		movingSlider = 0;
		document.onmousemove = null;	
	}
}


function doSliderDrag() {
	newPosition = currentPosition;

	var newPos = event.clientX - dragOffset;

	// keep the position in range.
	if (newPos < 0) {
		newPos = 0;
	} else if (newPos > width) {
		newPos = width;
	}

	// update according to position.
	slider.style.pixelLeft = newPos;
	newPosition = (Math.floor(RadioPlayer.currentMedia.duration / width)) * newPos;
	showStatus(formatDate(newPosition));

	return false;
}


function playPause() {
	if (isPlaying) {
		RadioPlayer.controls.pause();
		isPlaying = 0;
		event.srcElement.src = playImage.src;
	} else {
		RadioPlayer.controls.play();
		isPlaying = 1;
		event.srcElement.src = pauseImage.src;
	}
}


// show a status message.
function showStatus(message) {
	status = message;
	document.getElementById("positionText").innerText = message;
}


// generate and return a status message.
function statusMessage() {
	var message = "";
	switch(RadioPlayer.playState) {
		case 1:
			message = "Stopped";
			break;
		case 2:
			message = "Paused - " + formatDate(currentPosition);
			break;
		case 3:
			message = "Playing - " + formatDate(currentPosition);
			break;
		case 6:
			message = "Buffering";
			break;
		case 7:
			message = "Waiting";
			break;
		case 8:
			message = "Finished";
			break;
		case 9:
			message = "Preparing";
			break;
		case 10:
			message ="Ready";
			break;
		case 11:
			message = "Reconnecting";
			break;
		default:
			break;
	}
	return message;
}


// format a a string to HH:MM:SS format.
// we'll fake it using a Date object.
function formatDate(totalSeconds) {
	var tempDate = new Date(totalSeconds * 1000);
	return (tempDate.getHours() < 10 ? "0" : "") + tempDate.getHours() + ":" + (tempDate.getMinutes() < 10 ? "0" : "") + tempDate.getMinutes() + ":" + (tempDate.getSeconds() < 10 ? "0" : "") + tempDate.getSeconds();		
}
