

var sID 		= 'areal';
var host 		= 'http://areal-records.com/';
var loudness 	= 60;

soundManager.debugMode = false;
soundManager.url = '/media/soundmanager2.swf'; // path to movie
soundManager.onerror = function() {}
soundManager.onload = function() {}

var playTime = function(msec) {
	var tmpSec 		= Math.floor(msec/1000);
	var timerMin 	= Math.floor(tmpSec/60);
	var timerSec 	= tmpSec-(timerMin*60);
	var timerSec = (timerSec<10) ? "0"+timerSec : timerSec;
	return timerMin+":"+timerSec;
}

var whileplaying = function () {
	var duration = soundManager.getSoundById(sID).duration;
	var position = soundManager.getSoundById(sID).position;
	var playingPercent  = ((position/duration)*100);
	var playingPercentFloor = Math.floor(playingPercent)+"%";
	$('bar').style.left = playingPercentFloor;
	$('mbpTimer').innerHTML = "<span>"+playTime(position)+"</span>";
}

var whileloading = function() {
	var bytesLoaded = soundManager.getSoundById(sID).bytesLoaded;
	var bytesTotal	= soundManager.getSoundById(sID).bytesTotal;
	var loadedPercent 	= ((bytesLoaded/bytesTotal)*100);
	var loadedPercentFloor = Math.floor(loadedPercent)+"%";
	$('loaded').style.display = "block";
	$('loaded').style.width = loadedPercentFloor;
}

var getMeta = function(tId, getID3) {
	if(getID3 == false) {
		var metaUrl = host + 'getmeta/tune/' + tId + '/';
		new Ajax.Request(metaUrl, {
			method: 'get',
			asynchronous: true,
			onComplete: function(transport) {
				if(typeof transport.responseText == "string") {
					$('artisttitleS').innerHTML = transport.responseText;
				} else {
					$('artisttitleS').innerHTML = "";
				}
				doVolume(loudness);
			},
			onFailure: function(transport) {
				alert('something happend during the ajax request');
			}
		});
	} 
}

var getMetr = function(tId) {
	var metaUrl = host + 'getmetr/' + tId + '/';
	new Ajax.Request(metaUrl, {
		method: 'get',
		asynchronous: true,
		onComplete: function(transport) {
			if(typeof transport.responseText == "string") {
				$('artisttitleS').innerHTML = transport.responseText;
			} else {
				$('artisttitleS').innerHTML = "";
			}
			doVolume(loudness);
		},
		onFailure: function(transport) {
			alert('something happend during the ajax request');
		}
	});
}

var getIdThree = function() {
	// id3 doeasn't workt atm
	$('artisttitleS').innerHTML = "Currently playing a remote Stream";
}

var tPlay = function(tFile,sid) {
	
	// $('player').style.display = "block";
	
	var isSid = (typeof sid == 'undefined') ? false : true;
	
	
	if($('player').style.display == "none") {
		Effect.SlideDown('player', { duration: 0.3 });
	}
	
	var tId = tFile;
	var soundLink = null;
	var isExternal = false;
	
	if(typeof soundManager.getSoundById(sID) == "object") {
		soundManager.destroySound(sID);
	}
	
	if( (/^([0-9]+)$/).test(tFile) ) {
		soundLink = (isSid==true) ? host+'promostream/'+tId+'/'+sid+'/' : host+'media/play/'+tId+'/';
		// soundLink = host+'media/play/'+tId+'/';
	} else if( ( /^(http)/).test(tFile) ) {
		soundLink = tFile;
		isExternal = true;
	} else {
		return false;
	}

// console.log(soundLink);

	if(isSid) {
		soundManager.createSound (
			{
				id: "areal",
				url: soundLink,
				//url: "",
				autoPlay: true,
				onload: getMetr(tId),
				onid3: getIdThree(),
				whileloading: whileloading,
				whileplaying: whileplaying
			}
		);
	} else {
		soundManager.createSound (
			{
				id: "areal",
				url: soundLink,
				//url: "",
				autoPlay: true,
				onload: getMeta(tId, isExternal),
				onid3: getIdThree(),
				whileloading: whileloading,
				whileplaying: whileplaying
			}
		);
	}


	soundManager.setVolume(sID,loudness);
}

// controls: 
var between = function(value,min,max) {
	if((value >= min) && (value <= max)) {
		return true;
	}
	return false;
}

var soften = function() {
	if(between(loudness,0,100)) {
		var tempVolume = (loudness-10);
		loudness = (tempVolume >= 0) ? tempVolume : 0;
		setVolume();
	}
}

var louder = function() {
	if(between(loudness,0,100)) {
		var tempVolume = (loudness+10);
		loudness = (tempVolume <= 100) ? tempVolume : 100;
		setVolume();
	}
}

var setVolume = function() {
	soundManager.setVolume(sID,loudness);
	doVolume(loudness);
}

var doVolume = function(value) {
	if(between(value,0,100)) {
		var volPos = (value > 0) ? "-"+(value-10) : value;
		$('volume').style.backgroundPosition = "0 "+volPos+"px";
	}
}

var stop = function() {
	soundManager.pause(sID);
	$('bar').style.left = "0";
	$('mbpTimer').innerHTML = "<span>0:00</span>";
	soundManager.setPosition(sID,0);
}

var togglePlayPause = function() {
	soundManager.togglePause(sID);
}