/*********************************************/
/*********************************************/
/** COPYRIGHTED - COPYRIGHTED - COPYRIGHTED **/
/** THIS FILE IS STRICTLY COPYRIGHTED       **/
/** OWNER:                                  **/
/** David Lorenz                            **/
/** Schloßstraße 3                          **/
/** 72351 Geislingen                        **/
/** Country: GER                            **/
/**                                         **/
/** www.vaib.de                             **/
/** VAIB INTERNET TECHNOLOGIES              **/
/*********************************************/
/*********************************************/

var _PLAYLIST	=	new Object();
_PLAYLIST.db	=	new Object(); //database containing tracks
_PLAYLIST.db.objects	=	[];

/* UNIQUE ID Creator:Start */
_PLAYLIST.unqInc	=	1;
_PLAYLIST.unique	=	function(){
	_PLAYLIST.unqInc++;
	return '_plstUId.'+_PLAYLIST.unqInc;
};
/* UNIQUE ID Creator:End */

_PLAYLIST.playNext	=	function(){
	var acId			=	_PLAYER.Video.currentId;
	var countTracks		=	_PLAYLIST.db.objects.length;
	var acId_found		=	false;
	
	for(var i=0; i<countTracks; i++){
		if(_PLAYLIST.db.objects[i]){
			if(_PLAYLIST.db.objects[i][0] == acId){
				// "i" is the Index for the current playing video
				acId_found		=	true;
				var tryIndex	=	i+1;
				
				if(_PLAYLIST.db.objects[tryIndex] && _PLAYLIST.db.objects[tryIndex][0] != 0){
					//play this next track
					_PLAYER.Video.Cue(_PLAYLIST.db.objects[tryIndex][0],_PLAYLIST.db.objects[tryIndex][1]);
				}else{
					//nex track is "undefined" play first track of Playlist
					_PLAYER.Video.Cue(_PLAYLIST.db.objects[0][0],_PLAYLIST.db.objects[0][1]);
				}
				
				break;
			}
		}else{
			
		}
	}
	
	if(!acId_found){
		//active Video/Track was not in the Playlist-List, play first one of playlist
		_PLAYER.Video.Cue(_PLAYLIST.db.objects[0][0],_PLAYLIST.db.objects[0][1]);
	}
};


_PLAYLIST.playLast	=	function(){
	var acId			=	_PLAYER.Video.currentId;
	var countTracks		=	_PLAYLIST.db.objects.length;
	var acId_found		=	false;
	
	for(var i=0; i<countTracks; i++){
		if(_PLAYLIST.db.objects[i]){
			if(_PLAYLIST.db.objects[i][0] == acId){
				// "i" is the Index for the current playing video
				acId_found		=	true;
				var tryIndex	=	i-1;
				
				if(_PLAYLIST.db.objects[tryIndex] && _PLAYLIST.db.objects[tryIndex][0] != 0){
					//play this next track
					_PLAYER.Video.Cue(_PLAYLIST.db.objects[tryIndex][0],_PLAYLIST.db.objects[tryIndex][1]);
				}else{
					//there is no "last" track, play the very last of Playlist
					_PLAYER.Video.Cue(_PLAYLIST.db.objects[countTracks-1][0],_PLAYLIST.db.objects[countTracks-1][1]);
				}
				
				break;
			}
		}else{
			
		}
	}
	
	if(!acId_found){
		//active Video/Track was not in the Playlist-List, play first one of playlist
		_PLAYER.Video.Cue(_PLAYLIST.db.objects[0][0],_PLAYLIST.db.objects[0][1]);
	}
};


//adds a track to the playlist
_PLAYLIST.addTrack	=	function(vId,title,bb){
	var countTracks	=	_PLAYLIST.db.objects.length;
	
	if(countTracks > 0){
		//search if is already in playlist
		var is_InPlaylist	=	false;
		for(var i=0; i<countTracks; i++){
			if(_PLAYLIST.db.objects[i]){
				if(_PLAYLIST.db.objects[i][0] == vId){
					//track was found in playlist, delete Now
					is_InPlaylist	=	true;
					base.tooltipShow('Already added');
					break;
				}
			}
		}
		
		if(!is_InPlaylist){
			_PLAYLIST.db.objects[countTracks]		=	[vId,title];
			base.tooltipShow('Track added');
		}
	}else{
		base.tooltipShow('Track added');
		_PLAYLIST.db.objects[0]		=	[vId,title];
	}
};

//removes a track from the playlist
_PLAYLIST.rmTrack	=	function(vId,bb){
	var countTracks	=	_PLAYLIST.db.objects.length;
	if(countTracks < 1){
		base.tooltipShow('No track in playlist');
	}else{
		var is_InPlaylist	=	false;
		for(var i=0; i<countTracks; i++){
			if(_PLAYLIST.db.objects[i]){
				if(_PLAYLIST.db.objects[i][0] == vId){
					_PLAYLIST.reCreate(vId,bb);
					is_InPlaylist=true;
					break;
				}
			}
		}
		
		if(!is_InPlaylist){
			base.tooltipShow('This track is not in the playlist');
		}
	}
};

_PLAYLIST.reCreate	=	function(without,bb){
	var acPlaylistLength	=	_PLAYLIST.db.objects.length;
	var numNewPlaylist		=	0;
	var	newPlaylistObj		=	[];
	if(without){
		//one track will be left out, so the new playlist will have one track less
		newPlaylistObj.length	=	acPlaylistLength-1;
	}
	
	for(var i=0; i < acPlaylistLength; i++){
		if(without){
			if(_PLAYLIST.db.objects[i][0] == without){
				//do nothing - by doing nothing it is not added 
				//and therefore removed out of the playlist
			}else{
				//this is not the track to remove - add it to the new playlist.
				newPlaylistObj[numNewPlaylist]	=	[_PLAYLIST.db.objects[i][0],_PLAYLIST.db.objects[i][1]];
				numNewPlaylist++;
			}
		}else{
			//there is no track to leave out, just reCreate
			newPlaylistObj[numNewPlaylist]	=	[_PLAYLIST.db.objects[i][0],_PLAYLIST.db.objects[i][1]];
			numNewPlaylist++;
		}
	}
	
	_PLAYLIST.db.objects	=	newPlaylistObj;  //INIT new playlist - done.
	
	if(without){
		if(bb){
			bb.onclick	=	base.voided;
			$(bb.parentNode).slideUp('fast',function(){
				_PLAYLIST.playlist.refreshHTML();
			});
		}else{
			base.tooltipShow('Track removed');
			_PLAYLIST.playlist.refreshHTML();
		}
	}
	
};

//shuffles the tracks
_PLAYLIST.shuffle	=	function(){
	
};

_PLAYLIST.DeleteAll	=	function(){
	_PLAYLIST.db.objects	=	[];
	_PLAYLIST.reCreate();
	_PLAYLIST.playlist.refreshHTML();
};


_PLAYLIST.playlist			=	new Object();
_PLAYLIST.playlist.open		=	false;
_PLAYLIST.playlist.toggle	=	function(bb){
	var bb	=	(!bb) ? $('#plstButton')[0] : bb;
	
	if(_PLAYLIST.playlist.open){
		//do close
		_PLAYLIST.playlist.hide();
		bb.style.background		=	'url(img/playlistb_sw.gif)';
		_PLAYLIST.playlist.open	=	false;
	}else{
		//do open
		_PLAYLIST.playlist.show();
		bb.style.background		=	'url(img/playlistb_clrd.gif)';
		_PLAYLIST.playlist.open	=	true;
	}
};

_PLAYLIST.playlist.show	=	function(){
	//first create list
	_PLAYLIST.playlist.refreshHTML();
	
	$('#searchContents').animate({
		marginLeft: '-547px'
	},400);
	
	var bb	=	(!bb) ? $('#plstButton')[0] : bb;
	bb.style.background		=	'url(img/playlistb_clrd.gif)';
	_PLAYLIST.playlist.open	=	true;
};

_PLAYLIST.playlist.hide	=	function(){
	if($('#searchContents').css('margin-left') != '0px'){
		$('#searchContents').animate({
			marginLeft: '0px'
		},250);
	}
	
	var bb	=	(!bb) ? $('#plstButton')[0] : bb;
	bb.style.background		=	'url(img/playlistb_sw.gif)';
	_PLAYLIST.playlist.open	=	false;
};

_PLAYLIST.playlist.refreshHTML	=	function(){
	var countTracks	=	_PLAYLIST.db.objects.length;
	if(countTracks < 1){
		$('#PLAYLIST_UL').html('<li style="display:none"><span>No tracks in playlist</span></li>');
		$('#PLAYLIST_UL>li:first').slideDown('fast');
	}else{
		var ulInnerHTML	=	'';
		var acAdd		=	'';
		for(var i=0; i<countTracks; i++){
			if(_PLAYLIST.db.objects[i]){
				if(_PLAYLIST.db.objects[i][0] == _PLAYER.Video.currentId){
					acAdd	=	'style="background:url(img/playlist_isplaying.gif)"';
				}else{
					acAdd	=	'';
				}
				ulInnerHTML	+=	'<li id="'+_PLAYLIST.db.objects[i][0]+'">'
								+'<span>'+Base64.decode(_PLAYLIST.db.objects[i][1])+'</span>'
								+'<a href="javascript:void(0)" class="play" '+acAdd+' onclick="_PLAYER.Video.Cue(\''+_PLAYLIST.db.objects[i][0]+'\',\''+_PLAYLIST.db.objects[i][1]+'\')">&nbsp;</a>'
								+'<a href="javascript:void(0)" class="rmv" onclick="_PLAYLIST.rmTrack(\''+_PLAYLIST.db.objects[i][0]+'\',this)">&nbsp;</a>'
								+'<div class="clearer"></div>'
								+'</li>';
			}
		}
		
		$('#PLAYLIST_UL').html(ulInnerHTML);
	}
	
	_PLAYLIST.makeSortable();
};

_PLAYLIST.makeSortable	=	function(){
	$('#PLAYLIST_UL').sortable({
		opacity: 0.7, 
		revert: false,
		scroll: false,
		zIndex: 1000,
		axis: 'y',
		handle: $('#PLAYLIST_UL>li'),
		update: function(){
			var newOrder	=	$('#PLAYLIST_UL').sortable('toArray');
			var cnt			=	newOrder.length;
			var countTracks	=	_PLAYLIST.db.objects.length;
			var newSortedObj=	[];
			var n;
			for(var i=0; i < cnt; i++){
				for(n=0; n < countTracks; n++){
					if(_PLAYLIST.db.objects[n][0]==newOrder[i]){
						newSortedObj[newSortedObj.length]	=	[newOrder[i],_PLAYLIST.db.objects[n][1]];
						break;
					}
				}
			}
			
			_PLAYLIST.db.objects	=	newSortedObj;
			_PLAYLIST.playlist.refreshHTML();
		}
	});
};
