var plcsong = null;
var subsong = null;
var sec = 0;
var min = 0;
var plctthr = null;
var pausetimer = false;
var restarttimer = false;

function startcounter()
{
    if (plctthr==null) {
		sec = 0;
		min = 0;
		pausetimer = false;
		plctthr = setInterval("showelapsedtime()", 1000);
	    $('#pctime').html('0:00');
	}
	else {
		restarttimer = true;
	}
}

function stopcounter()
{
    if (plctthr!=null) {
		clearInterval(plctthr);
		plctthr = null;
	}
    sec = 0;
    min = 0;
    $('#pctime').html('0:00');
}

function showelapsedtime()
{
	if (restarttimer) {
		restarttimer = false;
		pausetimer = false;
		sec = 0;
		min = 0;
		$('#pctime').html('0:00');
	}
	else
	if (!pausetimer) {
		sec++;
		if (sec==60) {
			sec = 0;
			min++;
		}
		$('#pctime').html(min+':'+(sec<10?'0':'')+sec);
	}
}

function startplayback(sn)
{
	if (pcchecksong()) {
		if (!sn) {
			sn = parseInt(plcsong.STARTSONG);
		}
		subsong = sn;
		document.jsidplay.playSID('http://www.hardsid.com/files/C64Music'+plcsong.PATH, sn);
		startcounter();
	}
}

function disploadedsong(sn)
{
	if (plcsong) {
		$('#pcdets').css('opacity', 0);
		if (!sn) {
			sn = parseInt(plcsong.STARTSONG);
		}
		var l = plcsong.LENGTHS.split(' ');
		var sl = (l[sn-1].split('('))[0];
		if (plctthr==null) {
			$('#pctime').html('0:00');
		}
		$('#pcrem').html(sl);
		var snum = (parseInt(plcsong.NUMSONGS)>1)?' (song #'+ sn +' of '+ plcsong.NUMSONGS +')':'';
		$('#pctitle').html(plcsong.TITLE+snum);
		var rel = plcsong.RELEASED;
		$('#pcrel').html(plcsong.AUTHOR + ((rel&&rel!='')?' ('+rel+')':''));
		$('#pcdets').fadeTo(750, 1);
	}
	else {
		alert('Please select a song first (use the PLAY buttons all over hardsid.com)');
	}
}

function preloadplayer(sid, play)
{
	var tsTimeStamp= new Date().getTime();
	$.ajax({
		dataType: 'jsonp',
		jsonp: 'songdet_callback',
		url: 'http://c64jukebox.hardsid.com/player/songdet.php',
		data: {	'u': tsTimeStamp,
				's': sid },
		success: function(data) {
			plcsong = data;
			disploadedsong();
			if (play) {
				startplayback();
			}
		}
	});
}

function pcchecksong()
{
	if (plcsong) {
		return true;
	}
	else {
		alert('Please select a song first (use the PLAY buttons all over hardsid.com)');
		return false;
	}
}

function startplayer()
{
	if (!plcints || !jsappls || plcs) {
		return;
	}
	plcs = true;

	var pc = $('#player-control');
	if (pc.length==1) {
		pc.removeClass('plc-loading');
		
		if (pc.hasClass('plc-preload')) {
			preloadplayer(pc.attr('rel'));
		}
		else
		if (pc.hasClass('plc-autoplay')) {
			preloadplayer(pc.attr('rel'), true);
		}
		else {
		}
		
		pc.html(	'<div id="pctop"><div id="pcmtitle"></div><div id="pcjslink"></div></div>'+
					'<div id="pcmid"><div id="pcfav"></div><div id="pctimes"><div id="pctime"></div><div id="pcrem"></div></div>'+
					'<div id="pcdets"><div id="pctitle"></div><div id="pcrel"></div></div></div>'+
					'<div id="pcbot"><div id="pcrew"></div><div id="pcplay"></div><div id="pcpause"></div>'+
					'<div id="pcstop"></div><div id="pcffw"></div></div>');
					
		$('#pcmtitle').click(function () {
			window.location.href = 'http://c64jukebox.hardsid.com';
		});
		$('#pcjslink').click(function () {
			window.open('http://jsidplay2.sourceforge.net/');
		});
		$('#pcrew').click(function () {
			if (!pcchecksong()) {return};
			if (subsong && subsong>1) {
				disploadedsong(subsong-1);
				startplayback(subsong-1);
			}
		});
		$('#pcplay').click(function () {
			if (!pcchecksong()) {return};
			if (subsong && subsong>0) {
				disploadedsong(subsong);
				startplayback(subsong);
			}
		});
		$('#pcpause').click(function () {
			if (!pcchecksong()) {return};
			document.jsidplay.pauseOrContinueSID();
			pausetimer = document.jsidplay.stateSID() == 2;
		});
		$('#pcstop').click(function () {
			if (!pcchecksong()) {return};
			document.jsidplay.stopSID();
			stopcounter();
		});
		$('#pcffw').click(function () {
			if (!pcchecksong()) {return};
			if (subsong && parseInt(plcsong.NUMSONGS)>subsong) {
				disploadedsong(subsong+1);
				startplayback(subsong+1);
			}
		});
	}
}

function initplayer()
{
	var pc = $('#player-control');
	if (pc.length==1) {
		pc.addClass('plc-loading');

		plcints = true;
		startplayer();
	}
}