// Update a particular HTML element with a new value
function updateHTML(elmId, value) {
  document.getElementById(elmId).innerHTML = value;
}

// This function is called when an error is thrown by the player
function onPlayerError(errorCode) {
  alert("An error occured of type:" + errorCode);
}

// This function is called when the player changes state
function onPlayerStateChange(newState) {
  updateHTML("playerState", newState);
}

// Display information about the current state of the player
function updatePlayerInfo() {
  // Also check that at least one function exists since when IE unloads the
  // page, it will destroy the SWF before clearing the interval.
  if(ytplayer && ytplayer.getDuration) {
    updateHTML("videoDuration", ytplayer.getDuration()/60);
    updateHTML("videoCurrentTime", ytplayer.getCurrentTime()/60);
    updateHTML("bytesTotal", ytplayer.getVideoBytesTotal());
    updateHTML("startBytes", ytplayer.getVideoStartBytes());
    updateHTML("volume", ytplayer.getVolume());
  }
}

// Allow the user to set the volume from 0-100
function setVideoVolume() {
  var volume = parseInt(document.getElementById("volumeSetting").value);
  if(isNaN(volume) || volume < 0 || volume > 100) {
    alert("Please enter a valid volume between 0 and 100.");
  }
  else if(ytplayer){
    ytplayer.setVolume(volume);
  }
}

function playVideo() {
  if (ytplayer) {
    ytplayer.playVideo();
  }
}
function loadVideoId(vid, quality) {
  if (ytplayer) {
    ytplayer.cueVideoById(vid ,0 , quality);
    
  }
}
function playVideoId(vid, quality) {
  if (ytplayer) {
    ytplayer.loadVideoById(vid ,0 , quality);
    
  }
}

function pauseVideo() {
  if (ytplayer) {
    ytplayer.pauseVideo();
  }
}

function muteVideo() {
  if(ytplayer) {
    ytplayer.mute();
  }
}

function unMuteVideo() {
  if(ytplayer) {
    ytplayer.unMute();
  }
}
function getVideoID() {
  	if(ytplayer) {
		return ytplayer.getVideoEmbedCode();
	}
}
// This function is automatically called by the player once it loads
function onYouTubePlayerReady(playerId) {
  ytplayer = document.getElementById("ytPlayer");
  // This causes the updatePlayerInfo function to be called every 250ms to
  // get fresh data from the player
  setInterval(updatePlayerInfo, 250);
  updatePlayerInfo();
  ytplayer.addEventListener("onStateChange", "onPlayerStateChange");
  ytplayer.addEventListener("onError", "onPlayerError");
  ytplayer.cueVideoById("fz9DvaR42SM", 0, "large");
}

function loadPlayer() {
  var params = { allowScriptAccess: "always" };
  var atts = { id: "ytPlayer" };
  swfobject.embedSWF("http://www.youtube.com/apiplayer?" +
                     "&enablejsapi=1&playerapiid=player1",
                     "videoDiv", "480", "295", "8", null, null, params, atts);
}

function getW() {
  	if(ytplayer) {
	  curBWidth = (ytplayer.getVideoBytesLoaded()/ytplayer.getVideoBytesTotal()) * 250;
	  return curBWidth;
	}
}
function getT() {
  	if(ytplayer) {
	  curBWidth = (ytplayer.getCurrentTime()/ytplayer.getDuration()) * 250;
	  return curBWidth;
	}
}
function dutataTotale() {
  	if(ytplayer) {
  		var t = ytplayer.getDuration();
	  	return t;
	}
}
function tempoRiproduzione() {
  	if(ytplayer) {
  		var t = ytplayer.getCurrentTime();
	  	return t; 
	}
}


function getCCurVol() {
 	return ytplayer.getVolume();
}

function _run() {
	loadPlayer();
}

function prepTime(s) {
  var hours=Math.floor(s/3600);
  var minutes=Math.floor(s/60)-(hours*60);
  var seconds=Math.floor(s-(hours*3600)-(minutes*60));
  if (minutes<10) minutes = "0"+minutes;
  if (seconds<10) seconds = "0"+seconds;
  hours = (hours<1) ? "" : hours + ":";
  return minutes +":"+ seconds;
}

function playStatus() {
  	if(ytplayer) {
  		return ytplayer.getPlayerState()
	}
}

function setVolumeVideo( v ) {
	if(ytplayer){
   	ytplayer.setVolume( v );
  	}
}


//google.setOnLoadCallback(_run);


$(document).ready( function() {
	
	_run();
	volumeLevel = 60;
	
	
	$(".m_vpl").click(function() {
		// "hd720"
		curVid = $(this).attr("vid");
		loadVideoId(curVid, "large");
		status="pause";
		$("#playpause").removeClass("pp_pause").addClass("pp_play");
		$(".m_vpl").removeClass("m_vpl_selected");
		$(this).addClass("m_vpl_selected");
		$(".img_vpl").removeClass("active");
	});
	
	
	$("#playpause").click(function() {
		if(playStatus()==-1 || playStatus()==0 || playStatus()==2 || playStatus()==3 || playStatus()==5) {
			playVideo();
			$("#playpause").removeClass("pp_play").addClass("pp_pause");
		} else if(playStatus()==1) {
			pauseVideo();
			$("#playpause").removeClass("pp_pause").addClass("pp_play");
		}
	});
	
	$("#volBtnPlus").click(function() {
		if(volumeLevel<100) {
			volumeLevel = volumeLevel + 20;	
		}	
	});	
	$("#volBtnMinus").click(function() {
		if(volumeLevel>0) {
			volumeLevel = volumeLevel - 20;	
		}	
	});	


	$("#pane1").load( dir_url + '/seltvlib/func.list_video_vert.php','',function() {
		$("#pane1").jScrollPane({showArrows : false , scrollbarWidth : 10 });
	});
	$("#category_list").load( dir_url + '/seltvlib/func.cat_list.php');
	
/*
	$("#items_list").load( dir_url + '/seltvlib/func.list_video_horiz.php', { idcat : 1 }, function() {
		$("#infinite").scrollable({size : 7}).circular();
	});
*/
	$("#items_scroll").load( dir_url + '/seltvlib/func.list_video_horiz.php', { idcat : 1 }, function() {
		$("#infinite").scrollable({size : 7}).circular();
	});		

	

	


	setInterval(function() {
		var w = getW();
		var t = getT();
		$("#loadingb").css({ width : w + 'px'});
		$("#loadingt").css({ width : t + 'px'});
		$("#loadstatusdata #buffering").html( 'Buffering: ' + (Math.round((w * 100)/250)) + '%' );
		var tr = 0;
		var dt = 0;


		if(playStatus() == 1) {
			$("#playpause").removeClass("pp_play").addClass("pp_pause");
		}
		
		tempoRiproduzione()>0?tr = prepTime(tempoRiproduzione()):tr = 0;
		dutataTotale()>0?dt = prepTime(dutataTotale()):dt = 0;
		$("#loadstatusdata #timing").html('Tempo: ' + tr + ' - Durata: ' + dt );
		setVolumeVideo(volumeLevel);
		$("#volDisplay").removeClass("volDisplay0 volDisplay20 volDisplay40 volDisplay60 volDisplay80 volDisplay100").addClass("volDisplay" + volumeLevel );
	},50);

	
});



