/* playlist
-------------------------------------------------------------------*/
list =
{
	entrySec: function()
		{
			player.jumpToSeconds(player.entrySec);
		},
	
	changeLanguage: function()
		{
			if (list.lang == 'de')
				{
					// change to english
					list.lang = 'en';
					list.refresh(list.type, 'en', list.topicId, null, list.currentFilename);
					$('a#langLink').html('Deutsche Version');
				}
			else
				{
					// change to german
					list.lang = 'de';
					list.refresh(list.type, 'de', list.topicId, null, list.currentFilename);
					$('a#langLink').html('English version');
				}
		},
	
	refresh: function(type, lang, topicId, entryId, currentFilename)
		{
			if (!list.lang) { list.lang = lang; }
			if (topicId)    { list.topicId = topicId; }
			
			
			
			$('div#playlist').html('<div id="loading"><img src="/mediaplayer/lib/images/loader.gif"" alt="" /></div>');
			
			// reset some infos
			$('div#divDrag').css('left', '0px');
			$('div#playlist').css('left', '0px');
			
			var parameter = 'type=topics&lang='+list.lang+'&topicid='+list.topicId;
			
			$.ajax({ type: "GET", url: "/mediaplayer/lib/php/playlist.php", data: parameter, cache: false,
			success: function(response)
				{
					// fill with response
					//------------------------------------------------------------------
					$('div#playlist').html(response);
					
					
					// scrollbar width
					//------------------------------------------------------------------
					var countRows       = $('div#playlist div.one').length-$('div#playlist div.one.not_show').length;
					
					// zu wenig zum scrollen
					//------------------------------------------------------------------
					if (countRows <= 4)
						{
							$('div#divDrag').draggable('disable');
							list.unbindScrollbarEvents();
							$('div#scrollbarInactive').show();
							list.unbindMousewheel();
						}
					else
						{
							$('div#divDrag').draggable('enable');
							list.unbindScrollbarEvents(); // hmm
							list.bindScrollbarEvents();
							$('div#scrollbarInactive').hide();
							list.bindMousewheel();
						}
					
					// playlist unsichtbare breite
					//------------------------------------------------------------------
					if (countRows > 0)
						{
							var playlistWidth = parseFloat($('div#playlist div.one').eq(0).width()) * countRows;
							$('div#playlist').css('width', playlistWidth);
						}
					
					
					$('div#playlist').show();
					$("div#divDrag").show();
					
					
					var realPlaylistWidth   = parseFloat($('div#playlist').css('width'));
					list.scrollPlaylistWidth = parseFloat($('div#playlist').css('width'))-740;
					list.onePercentOfPlaylist = list.scrollPlaylistWidth/100;
					//console.log('ein prozent der playlist sind: '+list.onePercentOfPlaylist+'px');
					list.applyMouseHandles();
					
					
					// reselect oder preselect a entry-row: initial or at lang. change
					//------------------------------------------------------------------
					if (list.entryId)
						{
							setTimeout(function()
								{
									$('div.entryid-'+list.entryId).trigger('click');
									list.scrollToCurrentMovie();
									
								}, 100);
						}
					else
						{

							//player.changeMovie('/mediaplayer/lib/images/clip-first-de.jpg');
						}
				}
			});
		},
		
	changePlaylistCord: function(animated)
		{
			
			
			var scrollbarWidth = parseFloat($('div#scrollbar').width())-parseFloat($('div#divDrag').width()); // wie viel ist möglich
			var trackPosition  = parseFloat($('div#divDrag').css('left')); // wie viel hab ich gerade
			var trackPositionInPercent = trackPosition/(scrollbarWidth/100);
			var negativePosition = trackPositionInPercent*list.onePercentOfPlaylist;
			
			
			
			
			
			// debug
			//console.log(list.onePercentOfPlaylist);
			
			if (animated == 'animated')
				{
					//var n = $("div#playlist").queue("fx");
					//$("body").append("Queue length is: " + n.length);
					
					$('div#playlist').dequeue().animate({left: -negativePosition}, 300);
				}
			else
				{
					$('div#playlist').css('left', -negativePosition);
				}
			
		},
	
	applyMouseHandles: function()
		{
			$('div#playlist div.one')
				.mouseover(function()
					{
						$(this).addClass('mouseover');
					})
				
				.mouseout(function()
					{
						$(this).removeClass('mouseover');
					})
				
				.click(function(ele)
					{
						
						// visual, title
						//---------------------------------------------------------------------------------------------------------------------
						$('div#playlist div.one').removeClass('click').unbind('click');
						
						$(this).addClass('click');
						
						
						var title = $(this).find('div.title span.title').html();
						var title = title.replace('-<BR>', '');
						var title = title.replace('-<br>', '');
						$('div#player div.title').html(title);
						
						
						var filename = $(this).attr('id').split('-');
						player.changeMovie(filename[1]);
						
						
						list.currentFilename = filename;
						
						//---------------------------------------------------------------
						// get size via filename
						//---------------------------------------------------------------
						$.ajax({ type: "GET", url: "/mediaplayer/lib/php/size.php", data: 'filename='+filename, cache: false, dataType: 'script' });
						
						//---------------------------------------------------------------
						// get downloads via filename
						//---------------------------------------------------------------
						$('div.downloadLink a').addClass('none');
						
						$.ajax({ type: "GET", url: "/mediaplayer/lib/php/downloads.php", data: 'filename='+filename+'&lang='+list.lang, cache: false, dataType: 'script',
						success: function(response)
							{
								downloads.setPosition();
								
								//$('div#downloads div.inner').html();
								/*
								if (response == '')
									{
										alert('no response');
										$('div.downloadLink a').addClass('none');
									}
								else
									{
										alert(response);
										$('div.downloadLink a.none').removeClass('none');
										downloads.setPosition();
									}
								*/
							}
						});
						
						//---------------------------------------------------------------
						// get chapter via filename
						//---------------------------------------------------------------
						$.ajax({ type: "GET", url: "/mediaplayer/lib/php/chapters.php", data: 'filename='+filename+'&lang='+list.lang, cache: false,
						success: function(response)
							{
								$('div#chapter div.inner').html(response);
								
								if (response == '')
									{
										$('div.chapterLink a').addClass('none');
									}
								else
									{
										$('div.chapterLink a.none').removeClass('none');
										chapters.setPosition();
									}
									
									list.applyMouseHandles();
							}
						});
						
						/*
						// visual, title
						//----------------------------------------------------------------------------------------------------------------------
						var endOfPlaylist = -($('div#playlist').height()-75);
						//console.log('endOfPlaylist: '+endOfPlaylist);
						
						// differenz zwischen wie viel platz und wo geht es hin
						//----------------------------------------------------------------------------------------------------------------------
						var gapFromKlickToEnd = -(endOfPlaylist-newKlickPosition);
						//console.log('gapFromKlickToEnd: '+gapFromKlickToEnd);
						
						//
						var onePercentOfScrollbar = (parseFloat($('div#scrollbar').width())-parseFloat($('div#divDrag').width()))/100;
						//console.log(onePercentOfScrollbar);
						
						var percentOfPlaylist = -((newKlickPosition/$('div#playlist').height())*100);
						var newPosition       = percentOfPlaylist*onePercentOfScrollbar+2;
						//console.log('new position:'+newPosition);
						*/
					});
		},
	
	scrollToCurrentMovie: function()
		{
			var onePercentOfScrollbar = (650-206)/100;
			
			// wenn das element in der liste vorhande ist, work-around for international
			if ($('div.entryid-'+list.entryId)[0])
				{
					var offsetLeft = $('div.entryid-'+list.entryId)[0].offsetLeft;
					
					
					// go not to far
					if (offsetLeft >= list.scrollPlaylistWidth)
						{
							offsetLeft = list.scrollPlaylistWidth;
							console.log(list.scrollPlaylistWidth);
							
						}
					else if (offsetLeft <= 370)
						{
							console.log('to less');
							offsetLeft = 0;
						}
					else
						{
							// normal: adjust to center
							offsetLeft = offsetLeft-291;
							console.log('normal');
						}
					
					
					
					var percent = offsetLeft/list.onePercentOfPlaylist;
					console.log('offsetLeft: '+offsetLeft+' / onePercent:'+list.onePercentOfPlaylist+' --> '+percent);
					
					
					
					list.entryId = null;
					
					console.log('offsetLeft: '+offsetLeft);
					
					
					//$('div#divDrag').css('left', 4.44+'px'); // move one percent
					$('div#divDrag').css('left', onePercentOfScrollbar*percent+'px');
					
					if (offsetLeft >= 0)
						{
							list.changePlaylistCord('animated');
						}
				}
		},
	
	scroll: function(event)
		{
			
			
			var amountOfPixelMovement     = 550; // wieviel pixel sollen nach r/l bewegt werden
			
			var playlistWidth             = parseFloat($('div#playlist').width());
			var onePercentOfPlaylist      = (parseFloat($('div#playlist').width())/100);
			var onePercentOfScrollbar     = (parseFloat($('div#scrollbar').width())-parseFloat($('div#divDrag').width()))/100;
			
			var neededPercent       = 100/(playlistWidth/amountOfPixelMovement);
			var repositioningAmount = neededPercent*onePercentOfScrollbar;
			
			
			//console.log('neededPercent: '+neededPercent + ' --- repositioningAmount:'+repositioningAmount);
			
			
			var currentPosition = $('div#divDrag').css('left');
			var scrollbarWidth = parseFloat($('div#scrollbar').width())-parseFloat($('div#divDrag').width()); // wie viel ist möglich
			
			if ($(event.target).attr('id') == 'scrollLeft' || event >= 0)
				{
					var newPosition = parseFloat(currentPosition)-repositioningAmount;
				}
			else
				{
					var newPosition = parseFloat(currentPosition)+repositioningAmount;
				}
			
			// fallback
			if (newPosition >= scrollbarWidth) { newPosition = scrollbarWidth; }
			if (newPosition <= 0) { newPosition = 0; }
			
			$('div#divDrag').css('left', newPosition+'px');
			//$('div#divDrag').animate({top: newPosition});
			list.changePlaylistCord('animated');
		}
}





/* scrollbar draggable machen
-------------------------------------------------------------------*/
$(function()
	{
		$("div#divDrag").draggable({ 
			containment: "parent",
			cursor: "pointer",
			axis: "x",
			start: function() { },
			drag: function()  { list.changePlaylistCord(); },
			stop: function()  { }
		});
	});



/* scrollLeft, scrollRight: bind click, hover
-------------------------------------------------------------------*/
list.bindScrollbarEvents = function()
	{
		$('div#scrollLeft, div#scrollRight')
			.click(function(event)
				{
					list.scroll(event);
				})
			.mouseover(function()
				{
					$(this).addClass('over');
				})
			.mouseout(function()
				{
					$(this).removeClass('over');
				});
	}
	
list.unbindScrollbarEvents = function()
	{
		$('div#scrollLeft, div#scrollRight').unbind('click').unbind('mouseover').unbind('mouseout');
	}



/* scrollUp und Down klicken
-------------------------------------------------------------------*/
list.bindMousewheel = function()
	{
		$('div#playlist').bind('mousewheel', function(event, delta)
			{
				list.scroll(delta);
			});
	}

list.unbindMousewheel = function()
	{
		$('div#playlist').unbind('mousewheel');
	}

