if (!window.MediaPlayer)
	window.MediaPlayer = {};

MediaPlayer.Page = function(controlId, mediaData, startVolume) 
{
	this.controlId = controlId;
	this.mediaData = mediaData;
	this.startVolume = startVolume;
}

MediaPlayer.Page.prototype =
{
	positionSliderOffset: 150,
	volumneSliderOffset: 275,
	maxProgressWidth: 100,
	maxVolumeWidth: 40,
	playListIndex : 0,
	playListSlots : 8,
	currentMediaIndex: 0,
	mediaLoaded: false,
	mediaPlaying: false,
	playlistSticky: true,
		
	handleLoad: function(control, userContext, rootElement) 
	{
		this.control = control;
		this.root = rootElement;
		
		this.playlist = this.root.findName("Playlist");
		this.videoCanvas = this.root.findName("Video");
		this.mediaElement = this.root.findName("VideoWindow");
		this.rectProgressSlider = this.root.findName("DownloadProgressSlider");
		this.timeThumb = this.root.findName("TimeThumb");
		this.volumeThumb = this.root.findName("VolumeThumb");
		
		this.positionTimer = this.root.findName("PositionTimer");

		this.mediaElement.addEventListener("MediaOpened", Silverlight.createDelegate(this, this.handleMediaOpened));
		this.mediaElement.addEventListener("MediaEnded", Silverlight.createDelegate(this, this.handleMediaEnded));
		this.mediaElement.addEventListener("MediaFailed", Silverlight.createDelegate(this, this.handleMediaFailed));
		this.mediaElement.addEventListener("CurrentStateChanged", Silverlight.createDelegate(this, this.handleMediaCurrentStateChanged));
		this.mediaElement.addEventListener("BufferingProgressChanged", Silverlight.createDelegate(this, this.handleMediaBufferingProgressChanged));
		this.mediaElement.addEventListener("DownloadProgressChanged", Silverlight.createDelegate(this, this.handleMediaDownloadProgressChanged));
		
		this.positionTimer.addEventListener("Completed", Silverlight.createDelegate(this, this.handlePositionTimer));
		
		this.root.findName("PlayPauseButton").addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.handlePlayClick));
		this.root.findName("PreviousButton").addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.handlePreviousClick));
		this.root.findName("StopButton").addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.handleStopClick));
		this.root.findName("NextButton").addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.handleNextClick));
		
		this.root.findName("Playlist_Up").addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.handlePlaylistUpClick));
		this.root.findName("Playlist_Down").addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.handlePlaylistDownClick));
		
		this.root.findName("TimeSlider").addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.handleTimeSliderClick));
		this.root.findName("VolumeSlider").addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.handleVolumeSliderClick));
		
		this.playlist.addEventListener("MouseEnter", Silverlight.createDelegate(this, this.handlePlaylistMouseEnter));
		this.playlist.addEventListener("MouseLeave", Silverlight.createDelegate(this, this.handlePlaylistMouseLeave));
		
		this.createPlaylistEntryEvents();
		
		this.init();
		this.displayPlaylist();
	},
	
	
	// Initialisieren
	init: function() {
		this.rectProgressSlider.Width = 0;
		this.mediaElement.Volume = this.startVolume;
		this.volumeThumb["Canvas.Left"] = this.mediaElement.Volume * this.maxVolumeWidth;
	},
	
	
	// Laden und abspielen
	playMedia: function() {
		if ((this.mediaData.length == 0) || (this.currentMediaIndex >= this.mediaData.length)) return;		
		this.mediaElement.Stop();
		this.mediaElement.Source = this.mediaData[this.currentMediaIndex].Url;		
		this.mediaElement.Play();
	},
	
	

	// MEDIA-EREIGNISSE
	// ------------------------------------------
		
	
	// Song beginnt
    handleMediaOpened: function(sender, args) {
		this.mediaLoaded = true;
		this.displayPlaylist();
		this.showMedia();
    },
		
		
	// Song ist zu ende
    handleMediaEnded: function(sender, args) {
		this.mediaPlaying = false;
        window.setTimeout(this.controlId + ".handleNextClick(null, null)", 100);
    },
	
	
	// Song konnte nicht geladen werden
    handleMediaFailed: function(sender, args) {
		this.mediaLoaded = false;
        alert("Das Medium konnte nicht geöffnet werden");
    },
	
	
	// Änderung des Status und der Position
	handleMediaCurrentStateChanged: function(sender, args) {
		if (this.mediaElement.CurrentState == "Playing") {
			this.mediaLoaded = true;
			this.mediaPlaying = true;
			this.showPlayButton(true);
			this.playlistSticky = false;			
			this.positionTimer.begin();
		} else if (this.mediaElement.CurrentState == "Opening") {
			this.mediaLoaded = false;
			this.mediaPlaying = false;			
			this.showPlayButton(true);
		} else if (this.mediaElement.CurrentState == "Closed") {
			this.mediaLoaded = false;
			this.mediaPlaying = false;
			this.showPlayButton(true);
		} else if (this.mediaElement.CurrentState == "Paused") {
			this.mediaPlaying = false;
			this.showPlayButton(false);
		} else if (this.mediaElement.CurrentState == "Stopped") {
			this.mediaPlaying = false;
			this.playlistSticky = true;
			this.playlist.Opacity = 1;			
			this.showPlayButton(true);
		}
	},
	
	
	
	// Änderung beim Puffern
	handleMediaBufferingProgressChanged: function(sender, args) {
	},
	
	
	// Änderung beim Vorladen
	handleMediaDownloadProgressChanged: function(sender, args) {
		this.rectProgressSlider.Width = this.maxProgressWidth * this.mediaElement.DownloadProgress;
	},
	
	
	// Position hat sich geändert
	handlePositionTimer: function(sender, args) {
		if (this.mediaPlaying) {
			var pos = this.mediaElement.position.seconds;
			var len = this.mediaElement.naturalDuration.seconds;
			var percent = this.maxProgressWidth / len;
			
			this.timeThumb["Canvas.Left"] = Math.floor(percent * pos);
			window.setTimeout(this.controlId + ".handlePositionTimer(null, null)", 500);
		}
	},
	
	
	// Playlist, Video oder Titel zeigen
	showMedia: function() {
		var media = this.mediaData[this.currentMediaIndex];
		
		if (media.Typ == 1) { // MP3
			this.root.findName("VideoAuthor").Text = media.Author;
			this.root.findName("VideoTitle").Text = media.Title;
		} else if (media.Typ == 2) { // Video
			this.playlist.Opacity = 0;
		}
	},
	
	

	
	// BUTTONS
	// --------------------------------------------
	
	
	// Playbutton oder Pause anzeigen
	showPlayButton: function(showPlay) {
		if (showPlay) {
			this.root.findName("PauseSymbol_Hide").begin();
			this.root.findName("PlaySymbol_Show").begin();
		} else {
			this.root.findName("PauseSymbol_Show").begin();
			this.root.findName("PlaySymbol_Hide").begin();			
		}
	},
	

	// Play	
	handlePlayClick: function(sender, args) {
		if (!this.mediaLoaded) {
			this.playMedia();
			return;
		}
		if (this.mediaElement.CurrentState == "Playing") {
			this.mediaElement.Pause();
		} else {
			this.mediaElement.Play();
		}
	},
	
	
	// Previous
	handlePreviousClick: function(sender, args) {
		if (this.currentMediaIndex > 0) {
			this.currentMediaIndex--;
			if (this.currentMediaIndex < this.playListIndex) this.playListIndex = this.currentMediaIndex;
			this.playMedia();
		}
	},
	
	
	// Stop
	handleStopClick: function(sender, args) {
		if (this.mediaLoaded) this.mediaElement.Stop();
		this.timeThumb["Canvas.Left"] = 0;
	},
	
	
	// Next
	handleNextClick: function(sender, args) {
		if (this.currentMediaIndex < this.mediaData.length - 1) {
			this.currentMediaIndex++;
			if (this.currentMediaIndex >= this.playListIndex + this.playListSlots) this.playListIndex = this.currentMediaIndex - this.playListSlots + 1;
			this.playMedia();
		}
	},

	
	// Nächstes Lied spielen
    playNextVideo: function() {
		this.currentMediaIndex++;
        if (this.currentMediaIndex >= this.mediaData.length) this.currentMediaIndex = 0;
    },
	
	
	// Klick auf Position
	handleTimeSliderClick: function(sender, args) {
		if ((!this.mediaPlaying) || (!this.mediaElement.CanSeek)) return;
		
		var x = args.getPosition(null).x - this.positionSliderOffset;
		if (x < 0) x = 0;
		if (x > this.maxProgressWidth) x = this.maxProgressWidth;
		
		var pos = this.mediaElement.position;
		var duration = this.mediaElement.naturalDuration.seconds;
		var percent = duration / this.maxProgressWidth;
		
		pos.seconds = percent * x;
		this.mediaElement.position = pos;
	},
	
	
	// Klick auf Lautstärke
	handleVolumeSliderClick: function(sender, args) {
		var x = args.getPosition(null).x - this.volumneSliderOffset;
		if (x < 0) x = 0;
		if (x > this.maxVolumeWidth) x = this.maxVolumeWidth;
		this.mediaElement.Volume = x / this.maxVolumeWidth;
		this.volumeThumb["Canvas.Left"] = this.mediaElement.Volume * this.maxVolumeWidth;
	},
	
	
	
	// PLAYLIST
	// -----------------------------------------------------
	
	
	createPlaylistEntryEvents: function() {
		for (i = 1; i <= this.playListSlots; i++) {
			this.playlist.findName("Titel" + i).addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.handlePlaylistClick));
		}
	},
	
	
	// Playlist aktualisieren
	displayPlaylist: function() {
		for (i = 0; i < this.playListSlots; i++) {			
			var songIndex = i + this.playListIndex;
			if (songIndex >= this.mediaData.length) break;
			
			var media = this.mediaData[songIndex];
			if (media.Author != "") {
			    s = media.Author + " - " + media.Title;
			} else {
			    s = media.Title;
			}
			if (media.Typ == 2) s = "[Video] " + s;
			
			var textField = this.playlist.findName("Titel" + (i + 1));
			textField.Text = (songIndex + 1) + ". " + s;
			if (songIndex == this.currentMediaIndex) {
				textField.Foreground = "#FF00FF00";
			} else {
				textField.Foreground = "white";
			}
		}
	},
	
	
	
	// Auf Eintrag in Playlist geklickt
	handlePlaylistClick: function(sender, args) {
		var textNr = parseInt(sender.Tag);
		var mediaNr = textNr - 1 + this.playListIndex;	
		this.currentMediaIndex = mediaNr;
		this.playMedia();
	},


	// Prüft, ob der Playlist-Up-Button gedrückt werden kann
	playlistUpUsable: function() {
		return (this.playListIndex > 0);
	},


	// Prüft, ob der Playlist-Down-Button gedrückt werden kann
	playlistDownUsable: function() {
		return (this.playListIndex < this.mediaData.length - this.playListSlots);
	},


	// Playlist nach unten scrollen
	handlePlaylistDownClick: function(sender, args) {
		if (!this.playlistDownUsable()) return;
		this.playListIndex++;
		this.displayPlaylist();
	},

	
	// Playlist nach oben scrollen
	handlePlaylistUpClick: function(sender, args) {
		if (!this.playlistUpUsable()) return;	
		this.playListIndex--;
		this.displayPlaylist();	
	},
	
	
	// Playlist einblenden
	handlePlaylistMouseEnter: function(sender, args) {
		this.playlist.Opacity = 1;
	},
	
	
	// Playlist ausblenden, wenn Medium gespielt wird
	handlePlaylistMouseLeave: function(sender, args) {
		if (this.playlistSticky) return;
		this.root.findName("HidePlaylist").begin();
	}
}



// Automatische Animationen
// ------------------------------------------


function PlayPauseButton_MouseEnter(sender, args) {
	sender.findName("PlayPauseButton_MouseEnter").begin();
}

function PlayPauseButton_MouseLeave(sender, args) {
	sender.findName("PlayPauseButton_MouseLeave").begin();
}

function PreviousButton_MouseEnter(sender, args) {
	sender.findName("PreviousButton_MouseEnter").begin();
}

function PreviousButton_MouseLeave(sender, args) {
	sender.findName("PreviousButton_MouseLeave").begin();
}

function StopButton_MouseEnter(sender, args) {
	sender.findName("StopButton_MouseEnter").begin();
}

function StopButton_MouseLeave(sender, args) {
	sender.findName("StopButton_MouseLeave").begin();
}

function NextButton_MouseEnter(sender, args) {
	sender.findName("NextButton_MouseEnter").begin();
}

function NextButton_MouseLeave(sender, args) {
	sender.findName("NextButton_MouseLeave").begin();
}

function Titel_MouseLeave(sender, args) {
	sender.findName("Titel" + sender.Tag + "_MouseLeave").begin();
}

function Titel_MouseEnter(sender, args) {
	sender.findName("Titel" + sender.Tag + "_MouseEnter").begin();
}

function playlistDown_MouseLeave(sender, args) {
	sender.findName("Playlist_Down_MouseLeave").begin();
}

function playlistDown_MouseEnter(sender, args) {
	sender.findName("Playlist_Down_MouseEnter").begin();
}

function playlistUp_MouseLeave(sender, args) {
	sender.findName("Playlist_Up_MouseLeave").begin();
}

function playlistUp_MouseEnter(sender, args) {
	sender.findName("Playlist_Up_MouseEnter").begin();
}