var MediaPlayerManager = Class.create();

MediaPlayerManager.prototype = {

	initialize: function () {
	},

	initializePlayers: function() {
		// Gather list of media players in the page
		this.mediaPlayerDivs = document.getElementsByClassName('mediaPlayer');

		this.mediaPlayers = new Array();
		
		// Store them in our own array
		for (i = 0; i < this.mediaPlayerDivs.length; i++) {
			this.mediaPlayers[i] = new MediaPlayer(this.mediaPlayerDivs[i]);
		}
		return true;
		
	},
	
	switchPlayer: function(mediaFileURL) {
		for (i = 0; i < this.mediaPlayers.length; i++) {
			var mediaPlayer = this.mediaPlayers[i];
			// Check whether this is the player we need to show
			if (mediaPlayer.mediaFile == mediaFileURL) {
				mediaPlayer.buildPlayer();
			} else {
				mediaPlayer.removePlayer();
			}
		}
		return true;
	}

}

var MediaPlayer = Class.create();

MediaPlayer.prototype = {
	
	initialize: function(parentDiv) {
		this.parentDiv = parentDiv;
		this.playerDiv = parentDiv.getElementsByTagName('div')[0];
		this.link = parentDiv.getElementsByTagName('a')[0];
		this.mediaFile = this.link.getAttribute('href');
		this.link.mediaFile = this.mediaFile;
		this.mediaType = this.mediaFile.substring(this.mediaFile.lastIndexOf('.') + 1);
		if (this.mediaType.length == 3) {
			this.playerType = this.mediaType;
		} else {
			this.playerType = 'vimeo';
		}

		// Assign actions to "play" link
		this.link.onclick = function() {
			mediaPlayerManager.switchPlayer(this.mediaFile);
			return false;
		}
		return true;
	},
	
	buildPlayer: function() {
		// First, check whether there is already an object tag
		if (this.parentDiv.getElementsByTagName('object').length) {
			return false;
		}
		
		// Highlight box
		this.parentDiv.parentNode.className = 'nowPlaying';
		
		// Build player according to type
		switch (this.playerType) {
			case 'mp3':
				this.playerDiv.innerHTML = '<object type="application/x-shockwave-flash" data="images/audioPlayer.swf?songFile=' + this.mediaFile + '" width="266" height="17"><param name="movie" value="images/audioPlayer.swf?songFile=' + this.mediaFile + '" /><param name="bgcolor" value="#9eafa8" /></object>';
				return true;
				break;
			case 'flv':
				// We need to use an absolute URL here because we suck
				var domainArray = new Array();
				domainArray = document.URL.split('/');
				var baseURL = domainArray[domainArray.length - 2];
				var stupidURL = 'http://' + baseURL + '/' + this.mediaFile;
				this.playerDiv.innerHTML = '<object type="application/x-shockwave-flash" data="images/videoPlayer.swf?source=' + stupidURL + '" width="376" height="301"><param name="movie" value="images/videoPlayer.swf?source=' + this.mediaFile + '" /><param name="bgcolor" value="#9eafa8" /></object>';
				return true;
				break;
			case 'vimeo':
				videoID = this.mediaFile.substring(this.mediaFile.lastIndexOf('/') + 1);
				this.playerDiv.innerHTML = '<object type="application/x-shockwave-flash" width="376" height="212" data="http://www.vimeo.com/moogaloop.swf?clip_id=' + videoID + '&amp;server=www.vimeo.com&amp;fullscreen=1&amp;show_title=1&amp;show_byline=1&amp;show_portrait=1&amp;color=00ADEF"><param name="quality" value="best" /><param name="allowfullscreen" value="true" /><param name="scale" value="showAll" /><param name="movie" value="http://www.vimeo.com/moogaloop.swf?clip_id=' + videoID + '&amp;server=www.vimeo.com&amp;fullscreen=1&amp;show_title=1&amp;show_byline=1&amp;show_portrait=1&amp;color=00ADEF" /></object>';
				return true;
				break;
		}
		
		return false
	},
	
	removePlayer: function() {
		// First, check whether we need to remove anything
		var playerObject = this.playerDiv.firstChild;
		if (!playerObject) {
			return false;
		}
		
		// Delete object
		var deletedObject = this.playerDiv.removeChild(playerObject);
		
		// Remove box highlight
		this.parentDiv.parentNode.className = '';
		
		return true;
	}
	
}

var mediaPlayerManager = new MediaPlayerManager();

window.onload = function () {
	mediaPlayerManager.initializePlayers();
//	var mediaPlayerManager = new MediaPlayerManager();
}
