var PortalPlayer = Class.create();

PortalPlayer.prototype = {
	initialize: function(trackList, streamUrl) {
		this.trackList = trackList;
		this.soundObj = null;
		this.streamUrl = streamUrl;
		this.timeMarkerMoving = false;
		this.volumeSliderMoving = false;
		this.volPercentage = 80;
		this.loadSoundHook = null;
		
		this.setupTracks();
	},
	
	setupTracks: function() {
		var listEl = $('tracklist');
		var output = "";
		var i = 0;
		this.trackList.each(function (track) {
			i++;
			output += '<li id="t-item-' + track["id"] + '"><a href="#" onclick="player.loadSound(\'' + track["id"] + '\');return false">' + i + '. &nbsp;' + track["title"] + "</a></li>";
		});
		listEl.innerHTML = output;
		$('total-tracks-label').innerHTML = this.trackList.length + " Tracks Total"
	},
	
	loadSound: function(soundId) {
		var trackName = 'track' + soundId;
		if (this.soundObj) {
			var en = 't-item-' + this.soundObj.sID.replace("track", "");
			$(en).className = "";
			soundManager.destroySound(this.soundObj.sID);
		}
		this.soundObj = soundManager.createSound({id: trackName, url: this.streamUrl + '/' + soundId, whileplaying: this.whileplaying, onfinish: this.finish, volume: this.volPercentage});
		soundManager.play(trackName);
//		soundManager.play(trackName);$('the-status').innerHTML='Stop!';
		var pB = $('play-button');
		pB.src = pB.src.replace("play-", "pause-");
		pB.imageRollover.images['play-button'] = pB.imageRollover.images['play-button'].replace("play-", "pause-");
		$('t-item-' + soundId).className = "selected";
		
		this.trackList.each(function(track) {
			if (track["id"] == soundId) {
				$('track-title').innerHTML = track["title"];
			}
		});
		
		if (this.loadSoundHook) this.loadSoundHook(soundId);
	},

	togglePause: function() {
		if (this.soundObj) {
			this.soundObj.togglePause();
			var pB = $('play-button');
			if (this.soundObj.paused) {
				pB.src = pB.src.replace("pause-", "play-");
				pB.imageRollover.images['play-button'] = pB.imageRollover.images['play-button'].replace("pause-", "play-");
			}
			else {
				pB.src = pB.src.replace("play-", "pause-");
				pB.imageRollover.images['play-button'] = pB.imageRollover.images['play-button'].replace("play-", "pause-");
			}
		}
		else {
			this.loadSound(this.trackList[0]["id"]);
		}
	},
	
	nextTrack: function() {
		if (this.soundObj) {
			var en = this.soundObj.sID.replace("track", "");
			var useNext = false;
			this.trackList.each(function(track) {
				if (useNext == true) {
					this.loadSound(track["id"]);
					throw $break;
				}
				if (track["id"] == en) {
					useNext = true;
				}
			}, this);
		}
		else {
			this.loadSound(this.trackList[0]["id"]);
		}
	},
	
	whileplaying: function(cxt) {
		var pos = Math.floor(this.position / 1000);
		
		var minute = Math.floor(pos / 60);
		var second = pos - minute * 60;
		
		if (second < 10) second = "0" + second;
		
		var oldHTML = $('elapsed-time').innerHTML;
		var newHTML = minute + ":" + second;
		$('elapsed-time').innerHTML = newHTML;
		
		// now we move on to remaining time (but only if the position is updated, so there
		// isn't a weird lag time
		if (oldHTML != newHTML || newHTML == "0:00") {
			var pos = Math.floor((this.durationEstimate - this.position) / 1000);
			
			var minute = Math.floor(pos / 60);
			var second = pos - minute * 60;
			
			if (second < 10) second = "0" + second;
			$('remaining-time').innerHTML = "-" + minute + ":" + second;
		}
		
		
		var ratio = (this.durationEstimate / this.position);
//		window.status = this.bytesTotal + " | " + this.bytesLoaded;
		var progress = 271 / ratio;
		$('time-progressbar').style.width = progress + "px";
		
		if (!player.timeMarkerMoving) {
			var markerPos = progress + 216;
			if (markerPos < 222) { markerPos = 222; }
			if (markerPos > 482) { markerPos = 482; }
			$('time-marker').style.visibility = "visible";
			$('time-marker').style.left = markerPos + "px";
		}
	}, 
	
	finish: function(cxt) {
		player.nextTrack();
	},
	
	handleMouseMove: function(widget, evt) {
		if (this.timeMarkerMoving) {
			this.moveTimePosition(widget, evt);
		}
		else if (this.volumeSliderMoving) {
			this.moveVolume(widget, evt);		
		}
	},
	
	moveTimePosition: function(widget, evt) {
		var minL = 222;
		var maxR = 482;
		var nl = (evt.clientX - widget.offsetLeft) - 7;
		if (nl < minL) {
			nl = minL;
		} else if (nl > maxR) {
			nl = maxR;
		}			
		$('time-marker').style.left = nl + 'px';
		
		// adjust the curve slightly
		minL = minL - 6;
		maxR = maxR + 4;
		var baseL = nl - minL;	// this is where we go from 0 to whatever
		var ratio = (maxR - minL) / 100;

		posPercentage = Math.floor(baseL / ratio);
		if (this.soundObj) {
//			window.status = baseL;
			if (baseL == 6) {
				times = 0;
			} else {
				var times = posPercentage / 100;
			}
//			window.status = times;
			this.soundObj.setPosition(this.soundObj.duration * times);
		}
	},
	
	moveVolume: function(widget, evt) {
		//alert(evt.clientX);
		var minL = 484;
		var maxR = 527;
		var nl = (evt.clientX - widget.offsetLeft) - 9;
		if (nl < minL) {
			nl = minL;
		} else if (nl > maxR) {
			nl = maxR;
		}			
		$('volume-slider').style.left = nl + 'px';
		
		var baseL = nl - minL;	// this is where we go from 0 to whatever
		var ratio = (maxR - minL) / 100;

		this.volPercentage = Math.floor(baseL / ratio);
//			window.status = this.volPercentage;
		window.status = evt.button;
		if (this.soundObj) {
			this.soundObj.setVolume(this.volPercentage);
		}
	}
}