// XSPF Player JavaScript Control
//
// By Michael Chaney, Michael Chaney Consulting Corporation
// Copyright 2009 Michael Chaney Consulting Corporation

var ie_5_or_6_window_int_timer = null;

function thisMovie(movieName) { 
	return window[movieName] || document[movieName];
}

function XSPFPlayer(mdiv, width, height) {
  // swfobject stuff
  var flashvars={ 'autoplay': 'true', 'enablejs': 'true', 'javascriptid': 'player', 'song_url': ' ', 'song_title': ' ' };
  var flashparams={ 'allowfullscreen': 'false', 'allowscriptaccess': 'sameDomain' };
  var flashattr={ 'name': 'player' };
  swfobject.embedSWF('/flash/mp3player.swf', mdiv, width, height, "10", false, flashvars, flashparams, flashattr);
  this.$last_song_url = '';
}

XSPFPlayer.prototype.play_song = function(title, artist, song_url) {
	/* minor hack - if they try to play the same song again it'll stop
	 * instead. */
	if (this.$last_song_url == song_url) {
		this.stop();
		//Element.hide('popup-player');
      this.$last_song_url = '';
	} else {
		//Element.show('popup-player');
		function try_to_play() {
			/* interesting hack here - If we just did "Element.show" on the
			 * surrounding div to the player, then playSong isn't available
			 * until we've had a chance to run events.  So this just checks
			 * every 100 milliseconds to see if playSong is around, and, if
			 * so, plays the song. */
			if (thisMovie('player').playSong) {
				thisMovie('player').playSong(song_url, title + ' - ' + artist);
				this.$last_song_url = song_url;
				//if (ie_version>0 && ie_version<7) {
				//	if (!ie_5_or_6_window_int_timer) {
				//		function move_box() {
				//			var offset = -10; // set offset (likely equal to your css top)
				//			var el= document.getElementById('popup-player');
				//			if (el) {
				//				el.style.top = (document.documentElement.scrollTop + offset) + 'px';
				//			}
				//		}
				//		ie_5_or_6_window_int_timer = window.setInterval(move_box, 500);
				//	}
				//}
			} else {
				window.setTimeout(try_to_play, 100);
			}
		}
  		this.$last_song_url = song_url;
		try_to_play();
  }
}

XSPFPlayer.prototype.stop = function() {
   this.$last_song_url = '';
   thisMovie('player').playSong(' ', 'Select a song to play...');
   //thisMovie('player').playPause();
	//Element.hide('popup-player');
	//if (ie_5_or_6_window_int_timer) {
	//	window.clearInterval(ie_5_or_6_window_int_timer);
	//	ie_5_or_6_window_int_timer = null;
	//}
}
