 var song_id = 1;
		
 function updateHTML(elmId, value) {
    document.getElementById(elmId).innerHTML = value;
 
	if (elmId=='playerstate')
	{
		if (value==0)
		{
			ytplayer.seekTo('0', true);	
		}
	}
 }
 
 function setytplayerState(newState) {
    updateHTML("playerstate", newState);
 }
 
 function onYouTubePlayerReady(playerId) {
    ytplayer = document.getElementById("myytplayer");
    loadNewVideo(jQuery('#list_song_pos_1 a').attr('id'), '0','1');
    seekToPause(3);   
    $("#list_song_pos_1").css("color","#0054A6");
    setInterval(updateytplayerInfo, 250);
    ytplayer.addEventListener("onStateChange", "onytplayerStateChange");
    ytplayer.addEventListener("onError", "onPlayerError");
    pause();
 }
 
 function onytplayerStateChange(newState) {
	if (newState == 0)
	{
		var temp_song_id = parseInt(song_id) + 1;
 
		if ($(".song_title a").hasClass("song_pos_" + temp_song_id))
		{
			song_id = parseInt(song_id) + 1;
			selected_song = jQuery(".song_pos_" + temp_song_id).attr('id');
			loadNewVideo(selected_song,'0',song_id);
			$(".song_title .video_song_title").css("color","#000");
			$("#list_song_pos_" + temp_song_id + ' .video_song_title').css("color","#0054A6");
		}
	}
 }
 
 function onPlayerError(errorCode) {
    //alert("An error occured: " + errorCode);
 }
 
 function updateytplayerInfo() {
 
	$("#slider-duration").slider("option","max",parseInt(Math.round(ytplayer.getDuration()))); 
	$("#slider-duration").slider("option","value",parseInt(Math.round(getCurrentTime())));
			
	var total_duration = getDuration();
	var minutes = total_duration / 60;
	minutes = minutes + '';
			
	minutes.split('.');
			
	var total_minutes = minutes[0] * 60;
	var seconds = total_duration - total_minutes;
			
	if (seconds < 10)
	{
		seconds = "0" + seconds;	
	}
			
	jQuery('#total_duration').html(minutes[0] + ":" + seconds);
 			
	var total_duration = Math.round(getCurrentTime());
	var minutes = total_duration / 60;
	minutes = minutes + '';
			
	minutes.split('.');
			
	var total_minutes = minutes[0] * 60;
	var seconds = total_duration - total_minutes;
			
	if (seconds < 10)
	{
		seconds = "0" + seconds;	
	}
			
	jQuery('#current_duration').html(minutes[0] + ":" + seconds);
 }
 
 function loadNewVideo(id, startSeconds, current_song) {
    if (ytplayer) {
		song_id = current_song;
		$(".song_title .video_song_title").css("color","#000");
		$("#list_song_pos_" + song_id + " .video_song_title").css("color","#0054A6");
		$(".video_title").html($(".song_pos_" + song_id).html());
		ytplayer.loadVideoById(id, parseInt(startSeconds));
    }
 }
 
 function cueNewVideo(id, startSeconds) {
    if (ytplayer) {
        ytplayer.cueVideoById(id, startSeconds);
    }
 }
 
 function play() {
	if (ytplayer) {
        ytplayer.playVideo();
    }
 }
 
 function pause() {
    if (ytplayer) {
        ytplayer.pauseVideo();
    }
 }
 
 function stop() {
    if (ytplayer) {
        ytplayer.stopVideo();
    }
 }
 
 function getPlayerState() {
    if (ytplayer) {
    	if (ytplayer.getPlayerState()==1)
		{
			pause();
		}
		else
		{
			play();
		}
    }
 }
 
 function seekTo(seconds) {
    if (ytplayer) {
        ytplayer.seekTo(seconds, true);
    }
 }
 
 function seekToPause(seconds) {
    if (ytplayer) {
        ytplayer.seekTo(seconds, true);
	   pause();
    }
 }
  
 function getBytesLoaded() {
    if (ytplayer) {
        return ytplayer.getVideoBytesLoaded();
    }
 }
 
 function getBytesTotal() {
    if (ytplayer) {
        return ytplayer.getVideoBytesTotal();
    }
 }
 
 function getCurrentTime() {
    if (ytplayer) {
        return ytplayer.getCurrentTime();
    }
 }
 
 function getDuration() {
    if (ytplayer) {
	   $("#slider-duration").slider("option","max",parseInt(Math.round(ytplayer.getDuration()))); 
        return Math.round(ytplayer.getDuration());
    }
 }
 
 function getStartBytes() {
    if (ytplayer) {
        return ytplayer.getVideoStartBytes();
    }
 }
 
 function mute() {
    if (ytplayer) {
		ytplayer.mute();
    }
 }
 
 function unMute() {
    if (ytplayer) {
        ytplayer.unMute();
    }
 }
        
 function setVolume(newVolume) {
    if (ytplayer) {
       ytplayer.setVolume(newVolume);
    }
 }
 
 function getVolume() {
    if (ytplayer) {
        return ytplayer.getVolume();
    }
 }
 
 function clearVideo() {
    if (ytplayer) {
        ytplayer.clearVideo();
    }
 }
        
 $(function() {
	$('.eject').bind({
		click: function() {
			$('.playlist').animate({
				height: 'toggle'	
			});
		}
	});
    
	$('.play').bind({
		click: function() {
			getPlayerState()
		}
	});
    
	$('.pause').bind({
		click: function() {
			getPlayerState()
		}
	});
           
	$('.forward').bind({
		click: function() {
			var temp_song_id = parseInt(song_id) + 1;
 
			if ($(".song_title a").hasClass("song_pos_" + temp_song_id))
			{
				song_id = parseInt(song_id) + 1;
				selected_song = jQuery(".song_pos_" + temp_song_id).attr('id');
				loadNewVideo(selected_song,'0',song_id);
				$(".song_title .video_song_title").css("color","#000");
				$("#list_song_pos_" + temp_song_id + " .video_song_title").css("color","#0054A6");
			}
		}
	});
           
	$('.backwards').bind({
		click: function() {
			var temp_song_id = parseInt(song_id) - 1;
 
			if ($(".song_title a").hasClass("song_pos_" + temp_song_id))
			{
				song_id = parseInt(song_id) - 1;
				selected_song = jQuery(".song_pos_" + temp_song_id).attr('id');
				loadNewVideo(selected_song,'0',song_id);
				$(".song_title").css("color","#000");
				$("#list_song_pos_" + temp_song_id).css("color","#0054A6");
			}
		}
	});
           
	$('#volume_video').bind({
		click: function() {
			showMenu();
		}
	});
			
	$( "#slider-vertical" ).slider({
        orientation: "vertical",
        range: "min",
        min: 0,
        max: 100,
        value: 60,
        slide: function( event, ui ) {
            setVolume(ui.value)
        }
    });
					
	$("#slider-duration" ).slider({
		range: "min",
		min: 0,
		max: 100,
		value: 0,
		slide: function( event, ui ) {
			if (ytplayer.getPlayerState()!=1)
			{
				play();
			}
					
			seekTo(ui.value);
		}
	});
	
	$("#volume_slider").slider({
		range: "min",
		min: 0,
		max: 100,
		value: 100,
		slide: function( event, ui ) {
			setVolume( ui.value );	
		}
	});
});
 
 var params = { allowScriptAccess: "always", bgcolor: "#cccccc" };
 var atts = { id: "myytplayer" };
 swfobject.embedSWF("http://www.youtube.com/apiplayer?enablejsapi=1&playerapiid=ytplayer&autoplay=1&loop=1&mute=1", "mediaspace4", "617", "347", "8", null, null, params, atts);
