var vibelist = null;

jQuery.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img>").attr("src", arguments[i]);
  }
}

function onYouTubePlayerReady(playerId)
{
	/* Instanciate player */
	vibelist.player = document.getElementById("ui-tube-player");
	
	/* Attach event listeners */
	vibelist.player.addEventListener("onStateChange", "changeHandler");
	vibelist.player.addEventListener("onError", "errorHandler");
	
	$('#ui-media-kit').show();
}

function errorHandler(state)
{
	switch(state)
	{
		case 100:
			break;
		case 150:
			$('li[id=' + vibelist.controls.id + ']').removeClass('active-clip').addClass('failure');
			vibelist.loadNextClip();
			break;
		case 101:
			break;
	}
}

function changeHandler(state) 
{
   switch(state)
   {
   		case -1:
   			break;
   		case 1:
   			vibelist.state.last = 1;
   			$(vibelist.controls.icons.play).addClass('pause');
   			break;
   		case 2:
   			vibelist.state.last = 2;
   			break;
   		case 3:
   			break;
   		case 5:
   			break;
   		case 0:
   			if (1 == vibelist.state.last)
   			{
   				vibelist.loadNextClip();
   			}
   			break;
   }
}

function controlsHandler()
{
	switch(vibelist.controls.shuffle)
	{
		case true:
			$('.ico-shuffle').removeClass('shuffle-off');
			break;
		case false:	
			$('.ico-shuffle').addClass('shuffle-off');
			break;
	}
	
	switch(vibelist.controls.repeat)
	{
		case true:
			$('.ico-repeat').removeClass('repeat-off');
			break;
		case false:	
			$('.ico-repeat').addClass('repeat-off');
			break;
	}
}


$(function()
{
	$.preloadImages(
		"http://www.viberoll.com/templates/themes/default/images/clip.hovered.jpg", 
		"http://www.viberoll.com/templates/themes/default/images/copy-clip.jpg",
		"http://www.viberoll.com/templates/themes/default/images/prevclips-over.jpg",
		"http://www.viberoll.com/templates/themes/default/images/prevplaylist-over.jpg",
		"http://www.viberoll.com/templates/themes/default/images/ico-forwards-over.gif",
		"http://www.viberoll.com/templates/themes/default/images/ico-play-over.gif",
		"http://www.viberoll.com/templates/themes/default/images/ico-stop-over.gif",
		"http://www.viberoll.com/templates/themes/default/images/ico-backwards-over.gif",
		"http://www.viberoll.com/templates/themes/default/images/nextplaylist-over.jpg",
		"http://www.viberoll.com/templates/themes/default/images/grayloader.gif",
		"http://www.viberoll.com/templates/themes/default/images/nextresults.jpg",
		"http://www.viberoll.com/templates/themes/default/images/plus.jpg",
		"http://www.viberoll.com/templates/themes/default/images/nextclips-over.jpg",
		"http://www.viberoll.com/templates/themes/default/images/ico-pause.gif",
		"http://www.viberoll.com/templates/themes/default/images/play-clip.jpg",
		"http://www.viberoll.com/templates/themes/default/images/clip.selected.jpg",
		"http://www.viberoll.com/templates/themes/default/images/copy-clip-active.jpg",
		"http://www.viberoll.com/templates/themes/default/images/playing.gif",
		"http://www.viberoll.com/templates/themes/default/images/ico-pause-over.gif",
		"http://www.viberoll.com/templates/themes/default/images/register.jpg");
		
	/* Enable tooltips */
	$('a.tool').tooltip();

	vibelist = new playlist();
	
	vibelist.init();
	
	$('#ui-tube-player').tube();
	$('#master').slider(
    {
    	min:0, 
    	max:100,
    	range: "min",
    	value: vibelist.controls.volume,
		orientation: "horizontal",
		animate:true,
		stop: function(event, ui) 
		{
			vibelist.controls.volume = ui.value;
			vibelist.player.setVolume(ui.value);

			if (0 == ui.value)
			{
				$('a.ico-sound').addClass('sound-off');
			}
			else 
			{
				$('a.ico-sound').removeClass('sound-off');
			}
		},
		slide: function(event, ui) 
		{
			vibelist.controls.volume = ui.value;
			vibelist.player.setVolume(ui.value);
			
			if (0 == ui.value)
			{
				$('a.ico-sound').addClass('sound-off');
			}
			else 
			{
				$('a.ico-sound').removeClass('sound-off');
			}
		}
	});
	
	$('.ico-play').click(function()
	{
		switch(vibelist.state.last)
		{
			case 0:
				$clip = $('.cliplist li').eq(0);
				$clip.addClass('active-clip');
				
				vibelist.playClip($clip.attr('id'));
				break;
			case 1:
				vibelist.player.pauseVideo();
				$('a.play-clip').addClass('stopped-clip');
				$(vibelist.controls.icons.play).removeClass('pause');
				break;
			case 2:
				vibelist.player.playVideo();
				$('a.stopped-clip').removeClass('stopped-clip');
				$(vibelist.controls.icons.play).addClass('pause');
				break;
			case -1:
				vibelist.playClip($('.cliplist li').filter('.active-clip').attr('id'));
				break;
		}
	});
	
	$('.ico-backwards').click(function()
	{
		vibelist.state.last = -1;
		vibelist.loadPrevClip();
	});
	
	$('.ico-forwards').click(function()
	{
		vibelist.state.last = -1;
		vibelist.loadNextClip();
	});
	
	$('.ico-stop').click(function()
	{
		vibelist.state.last = -1;
		vibelist.state.play = -1;
		
		vibelist.stopVideo();
		
		$(vibelist.controls.icons.play).removeClass('pause');
	});
	
	$('.ico-shuffle').click(function()
	{
		vibelist.controls.shuffle = !vibelist.controls.shuffle;
		controlsHandler();
	});
	
	$('.ico-repeat').click(function()
	{
		vibelist.controls.repeat = !vibelist.controls.repeat;
		controlsHandler();
	});
	
	$('.ico-sound').click(function()
	{
		if (0 == vibelist.controls.volume)
		{
			vibelist.controls.volume = vibelist.controls.lastvolume;
			vibelist.player.setVolume(vibelist.controls.lastvolume);
			
			$('#master').slider('value',vibelist.controls.lastvolume);
			
			$('a.ico-sound').removeClass('sound-off');
		}
		else 
		{
			vibelist.controls.lastvolume = vibelist.controls.volume;
			vibelist.controls.volume = 0;
			vibelist.player.setVolume(0);
			
			$('#master').slider('value',0);
			
			$('a.ico-sound').addClass('sound-off');
		}
	});
	

	$('.ico-fullscreen').click(function()
	{	
		$player = $('#ui-tube-player');
		
		/* Enlarge player */
		$player.css(
		{
			top:51,
			left:-483,
			width: 870,
			height:653
		});
		
		
		/* Append new controlls */
		var $playerdiv = $player.parents().filter('div[id=player]');
		
		$playerdiv.append('<div class="full-screen-controls"></div>');
		$playerdiv.append('<div class="bottom-screen-controls"></div>');
		
		/* Move player controls to new location */
		var states = 
		{
			play:
			{
				top: $('.ico-play').css('top'),
				left: $('.ico-play').css('left')
			},
			backwards: 
			{
				top: $('.ico-backwards').css('top'),
				left: $('.ico-backwards').css('left')
				
			},
			forwards: 
			{
				top: $('.ico-forwards').css('top'),
				left:  $('.ico-forwards').css('left')
			},
			stop:
			{
				top: $('.ico-stop').css('top'),
				left:  $('.ico-stop').css('left')
			},
			sound:
			{
				top: $('.ico-sound').css('top'),
				left:  $('.ico-sound').css('left')
			},
			repeat:
			{
				top: $('.ico-repeat').css('top'),
				left:  $('.ico-repeat').css('left')
			},
			shuffle:
			{
				top: $('.ico-shuffle').css('top'),
				left:  $('.ico-shuffle').css('left')
			},
			controls:
			{
				top: $('#player-controls').css('top'),
				left:  $('#player-controls').css('left')
			},
			volume:
			{
				top: $('#player-volume').css('top'),
				left:  $('#player-volume').css('left')
			},
			fullscreen:
			{
				top: $('.ico-fullscreen').css('top'),
				left:  $('.ico-fullscreen').css('left')
			}
		}
		
		

		
		$('.ico-backwards').css({ top: 10, left:-465});
		$('.ico-stop').css({ top: 10, left:-435});
		$('.ico-play').css({ top: 2, left:-405});
		$('.ico-forwards').css({ top: 10, left:-360});
		$('.ico-sound').css({ top: 15, left:-320});
		$('.ico-repeat').css({ top: 10, left:150});
		$('.ico-shuffle').css({ top: 10, left:180});
		$('.ico-fullscreen').hide();

		$('#player-controls').css({ top:20, left:-200});
		$('#player-volume').css({ top:20, left:-290});

		$('#search').css({height: 292}).children().hide();
		
		$(document).keyup(function(event)
		{
			if (event.keyCode == 27) {
		        $player.css({
					top:20,
					left:20,
					width: 344,
					height:253
				});
				
				$('.ico-play').css({ top: states.play.top, left: states.play.left});
				$('.ico-backwards').css({ top: states.backwards.top, left: states.backwards.left});
				$('.ico-forwards').css({ top: states.forwards.top, left: states.forwards.left});
				$('.ico-stop').css({ top: states.stop.top, left: states.stop.left});
				$('.ico-sound').css({ top: states.sound.top, left: states.sound.left});
				$('.ico-repeat').css({ top: states.repeat.top, left: states.repeat.left});
				$('.ico-shuffle').css({ top: states.shuffle.top, left: states.shuffle.left});
				$('.ico-fullscreen').show();

				$('#player-controls').css({ top: states.controls.top, left: states.controls.left});
				$('#player-volume').css({ top: states.volume.top, left: states.volume.left});
				
				$('.full-screen-controls').remove();
				$('.bottom-screen-controls').remove();
				$('#search').css({ height: 'auto'}).children().show();
		    }
		});
	});
	
	$('.player-buffer').click(function(e)
	{
		mouseX = e.pageX - $(this).offset().left;
		
		seek = (mouseX/$(this).parents().eq(0).width())*100;
		
		vibelist.seekClip(seek.toFixed(2));
	});
	
	
	$('.player-progress').click(function(e)
	{
		mouseX = e.pageX - $(this).offset().left;
		
		seek = (mouseX/$(this).parents().eq(0).width())*100;
		
		vibelist.seekClip(seek.toFixed(2));
	});
	
	$('.player-controls-holder').click(function(e)
	{
		mouseX = e.pageX - $(this).offset().left;
		
		seek = (mouseX/$(this).width())*100;
		
		vibelist.seekClip(seek.toFixed(2));
	});
	
	controlsHandler();
	
	switch(_u)
	{
		case 0:
			/* Non-registered users will be shown featured lists by defaut */
			
			if ($('p.shared').length == 1) /* Check if shared playlist exists */
			{
				$('.accordion').accordion('activate',1);
			}
			else 
			{
				$('.accordion').accordion('activate',0);
			}
			
			vibelist.getAPI().click(0);
			break;
		case 1:
			/* 
				Registered users will be shown My Playlists or Create Playlists depending 
				on the number of previously created playlists 
			*/
			if ($('p.shared').length == 1) /* Check if shared playlist exists */
			{
				$('.accordion').accordion('activate',3);
				vibelist.getAPI().click(0);
			}
			else 
			{
				if ($('.allmyplaylists p').length > 0)
				{
					$('.accordion').accordion('activate',1);
					vibelist.getAPI().click(0);
				}
				else 
				{
					$('.accordion').accordion('activate',2);
				}
			}
			break;
	}	
});