var PLAYLIST_UNLOAD = 1;
var PLAYLIST_LOADED = 2;
var PLAYLIST_RELOADED = 4;

function playlist(params) {
	this.playlistState = PLAYLIST_UNLOAD;
	this.playerState = null;
	this.list = null;
	this.currentVideo = null;
	this.currentVideoPosition = 0;
	this.selectedVideoCounter = 0;
		
	this.callback = params.callback;
	this.callback_function = params.callback_function;
	
	this.flashPlayerObj = params.swfObj;
};

playlist.prototype.setPlayerState = function(value) {
	this.playerState = value;
};

playlist.prototype.getPlayerState = function() {
	return this.playerState;
};

playlist.prototype.setPlaylistState = function(value) {
	this.playlistState = value;
};

playlist.prototype.getPlaylistState = function() {
	return this.playlistState;
};


playlist.prototype.setPlaylist = function(list) {
	this.list = list;
};

playlist.prototype.getPlaylist = function() {
	return this.list;
};

playlist.prototype.getPlaylistLength = function() {
	return this.list.length;
};

playlist.prototype.playlistManager = function(type, index) {
	switch(type) {
		case "playnext": this.selectedVideoCounter++; break;	// Set skip forward in playlist.
		case "playprevious": this.selectedVideoCounter--; break;	// Set skip backward in playlist.
		case "playvideo": this.selectedVideoCounter = index; break;	// Set to a specific video in playlist.
		case "setlast": this.selectedVideoCounter = this.getPlaylistLength()-1; break;	// Reset to last video.
		case "setfirst": this.selectedVideoCounter = 0; break;	// Reset to first video.
	};
};

playlist.prototype.getPlaylistVideo = function(index) {
	return (this.list) ? this.list[index] : false;
};

playlist.prototype.playFirstVideo = function() {
	this.playlistManager("setfirst");
	this.playFlashVideo();
};

playlist.prototype.playFlashVideo = function(index, position, skip) {
	try {		
		if (index || index==0) this.playlistManager("playvideo", index);
		var sCounter = this.getSelectedCounter();
		var video = this.getPlaylistVideo(sCounter);
		if (video) {
			this.setCurrentVideo(video);
			this.setPlayerState(FP_STATE_PLAYING);	// SET PLAYER STATE	
			var obj = new Object();
			obj.id = video.id;			
			obj.position = position; //this.getVideoPosition()
			this.flashPlayerObj.call('playVideo', obj);

			return video;	
		} else throw "ERROR";			
	} catch(e) {
	// CAPTURE ERROR
		return false;
	};
};

playlist.prototype.playPreviousVideo = function(skip) {
	try {
		this.playlistManager("playprevious");
		var sCounter = this.getSelectedCounter();
		if (sCounter >= 0) {
			this.setVideoPosition(0);
			if (this.getPlaylistVideo(sCounter).closed == true) this.playPreviousVideo(skip);
			else this.loadCallback(null, skip);		
		} else {
		// CANNOT GO BACK FURTHER IN PLAYLIST
			this.playlistManager("setfirst");	
		};
		return true;
	} catch(e) {
		return e;
	};	
};

playlist.prototype.playNextVideo = function(skip) {
	try {
		if (this.getPlaylistState() == PLAYLIST_RELOADED) {
		// PLAYLIST JUST RELOADED, PLAY FIRST VIDEO
			this.setVideoPosition(0);
			this.playFirstVideo();
			this.setPlaylistState(PLAYLIST_LOADED);	// SET IT TO REGULAR PLAYLIST STATE
		} else {
		// PLAY NEXT VIDEO			
			this.playlistManager("playnext");
			var sCounter = this.getSelectedCounter();
			if (sCounter < this.getPlaylistLength()) {
				this.setVideoPosition(0);
				if (this.getPlaylistVideo(sCounter).closed == true) this.playNextVideo(skip);			
				else this.loadCallback(null, skip);
			} else {
			// EXCEEDING CURRENT PLAYLIST
				this.playlistManager("setlast");
			};
		};
		return true;
	} catch(e) {
		return false;
	};
};

playlist.prototype.getCurrentVideo = function() {
	return this.currentVideo;
};

playlist.prototype.setCurrentVideo = function(video) {
	this.currentVideo = video;
};

playlist.prototype.getSelectedCounter = function() {
	return this.selectedVideoCounter;
};

playlist.prototype.setSelectedCounter = function(value) {
	this.selectedVideoCounter = value;
};

playlist.prototype.loadCallback = function(index, skip) {
	eval("this.callback."+this.callback_function+"(index, skip);");
};

playlist.prototype.setVideoPosition = function(value) {
	this.currentVideoPosition = value;
};

playlist.prototype.getVideoPosition = function() {
	return this.currentVideoPosition;
};

playlist.prototype.pauseVideo = function() {
	if (this.getPlayerState() != FP_STATE_PAUSE) this.flashPlayerObj.call("pause");
};

playlist.prototype.pauseResumeVideo = function() {
	if (this.getPlayerState() == FP_STATE_PAUSE) this.flashPlayerObj.call("unpause");
	else this.flashPlayerObj.call("pause");
};