var ytplayer;
var _pause = false;
/*function embedPlayer() {
// Hack for playing embedded YouTube videos in high quality. I do not guarantee that the use of this solution will work in the future.
var VideoQuality= document.getElementById('VideoQuality').value;
var ytQuality = '&amp;ap=%2526fmt%3D18';
	if (VideoQuality == 'high') {
		ytQuality = ytQuality;
	} else {
 		ytQuality = '';
}
//High Quality Video code end
    var params = { allowScriptAccess: 'always', bgcolor: '#D5DDB3' };
    var atts = { id: 'myytplayer' };
    swfobject.embedSWF('http://www.youtube.com/apiplayer?enablejsapi=1&amp;playerapiid=ytplayer' + ytQuality, 
            'ytapiplayer', '463', '365', '8', null, null, params, atts);
}*/

$(document).ready(function()
{
	simple_tooltip("a.tip","tooltip");
	HideContent('VideoPause');
	HideContent('VideoUnMute');	
	
	jQuery.fn.fadeToggle = function(speed, easing, callback) {
	   return this.animate({opacity: 'toggle'}, speed, easing, callback);
	
	};
	
	$('#playList').jScrollPane({showArrows:true,scrollbarWidth:8});
	$('#linkvidinfo').click(function(){$('#videoinfo').slideToggle(800);return false;});
	
	
	
	function simple_tooltip(target_items, name){
	 $(target_items).each(function(i){
			$("body").append("<div class='"+name+"' id='"+name+i+"'><p>"+$(this).attr('title')+"</p></div>");
			var my_tooltip = $("#"+name+i);
			
			if($(this).attr("title") != "" && $(this).attr("title") != "undefined" ){
			
			$(this).removeAttr("title").mouseover(function(){
						my_tooltip.css({opacity:0.8, display:"none"}).fadeIn(400);
			}).mousemove(function(kmouse){
					var border_top = $(window).scrollTop(); 
					var border_right = $(window).width();
					var left_pos;
					var top_pos;
					var offset = 20;
					if(border_right - (offset *2) >= my_tooltip.width() + kmouse.pageX){
						left_pos = kmouse.pageX+offset;
						} else{
						left_pos = border_right-my_tooltip.width()-offset;
						}
						
					if(border_top + (offset *2)>= kmouse.pageY - my_tooltip.height()){
						top_pos = border_top +offset;
						} else{
						top_pos = kmouse.pageY-my_tooltip.height()-offset;
						}	
			my_tooltip.css({left:left_pos, top:top_pos});
			}).mouseout(function(){
					my_tooltip.css({left:"-9999px"});				  
			});
			
			}
					
		});
	}
	
});

function updateHTML(elmId, value) {
  document.getElementById(elmId).innerHTML = value;
}

function setytplayerState(newState) {
  updateHTML("playerstate", newState);
}

function onytplayerStateChange(newState) {
  setytplayerState(newState);
}

function onPlayerError(errorCode) {
  alert("An error occured: " + errorCode);
}

function loadNewVideo(id, startSeconds) {
  if (ytplayer) {
	ytplayer.loadVideoById(id, parseInt(startSeconds));
  }  
}

function cueNewVideo(id, startSeconds) {
  if (ytplayer) {
	ytplayer.cueVideoById(id, startSeconds);
	//player.cueVideoById(videoId:String, startSeconds:Number, suggestedQuality:String):Void;
  }
}



function play() {
  if (ytplayer) {
	ytplayer.playVideo();	
  }
  	ShowContent('VideoPause');
	HideContent('VideoPlay');
}

function pause() {
  if (ytplayer) {
	ytplayer.pauseVideo();
  }
  ShowContent('VideoPlay');
  HideContent('VideoPause');
}

function stop() {
if (ytplayer) {
	ytplayer.stopVideo();
	}
}

function getPlayerState() {
  if (ytplayer) {
	return ytplayer.getPlayerState();
  }
}

function seekTo(seconds) {
  if (ytplayer) {
	ytplayer.seekTo(seconds, true);
  }
}

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) {
		return ytplayer.getDuration();
	}
}

function getStartBytes() {
  if (ytplayer) {
	return ytplayer.getVideoStartBytes();
  }
}

function mute() {
  if (ytplayer) {
	ytplayer.mute();
  }
  ShowContent('VideoUnMute');
  HideContent('VideoMute');
}

function unMute() {
  if (ytplayer) {
	ytplayer.unMute();
	ShowContent('VideoMute');
	HideContent('VideoUnMute');
  }
}

/*function getEmbedCode() {
  ShowContent ('embedcode');
  //HideContent ('info');
  //HideContent ('channels');
  //alert(ytplayer.getVideoEmbedCode());
}*/

function getVideoUrl() {
	//alert(ytplayer.getVideoUrl());
	ytplayer.stopVideo();
	window.open(ytplayer.getVideoUrl());
}

function clearVideo() {
  if (ytplayer) {
	ytplayer.clearVideo();
  }
}


// FORWARD
function fwd(fwdSeconds, allowSeekAhead) {
	if (ytplayer) {
	var fwdSeconds = getCurrentTime()+10;
	ytplayer.seekTo(fwdSeconds);
	}
}

// REWIND
function rwd(rwdSeconds) {
	if (ytplayer) {
	var rwdSeconds = getCurrentTime()-10;
	ytplayer.seekTo(rwdSeconds);
	}
}

		
// Volume slider. //Not part of the original YouTube or Event Tracking code. The slider code is working together with slider.js
var slider=new Array();
	slider[1]=new Object();
	slider[1].min=0;
	slider[1].max=100;
	slider[1].slidervalue=70;
	slider[1].onchange=setSliderValue;
function setSliderValue(slidervalue) {
	var b=document.getElementById('output1');
	slidervalue=Math.round(slidervalue*1000)/1000;
	b.value=Math.round(slidervalue);

	if (ytplayer) {
		ytplayer.setVolume(slidervalue);
	}
}
		
function getVolume() {
	if (ytplayer) {
		return ytplayer.getVolume();
	}
}

//Calculating time so time less than 10 are shown with a 0 in front, ex. 09.		
function splitTime(a)
	{
		var tm=new Date(a*1000)
		var hours=Math.round(tm.getUTCHours());
		var minutes=Math.round(tm.getUTCMinutes());
		var seconds=Math.round(tm.getUTCSeconds()); 

	if (hours > 0){
		timeStr= "" + hours;
		timeStr+= ((minutes < 10) ? ":0" : ":") + minutes;
		timeStr+= ((seconds < 10) ? ":0" : ":") + seconds;
	}
	else{
		timeStr= "";
		timeStr= ((minutes < 10) ? "0" : ":") + minutes;
		timeStr+= ((seconds < 10) ? ":0" : ":") + seconds;
		} 
	
return timeStr
}

function updateytplayerInfo() {
  updateHTML("bytesloaded", getBytesLoaded());
  updateHTML("bytestotal", getBytesTotal());
  updateTimebar();
  updateBytesbar();
  updateHTML("videoduration", splitTime(getDuration()));
  updateHTML("videotime", splitTime(getCurrentTime()));
  updateHTML("startbytes", getStartBytes());
  //updateHTML("volume", getVolume());
}

function updateTimebar() {
    var all = ytplayer.getDuration();
    var part = ytplayer.getCurrentTime();
    var percent = getPercent1(all, part);
    var timebarWidth = 100;
    document.getElementById('timebarIndicator').style.left = percent * (timebarWidth / 100) + "%";
}

function updateBytesbar() {
    var all = ytplayer.getVideoBytesTotal();
    var partBytes = ytplayer.getVideoBytesLoaded();
    var percentBytes = getPercent2(all, partBytes);
    var bytesbarWidth = 100;
    document.getElementById('bytesbarIndicator').style.width = percentBytes * (bytesbarWidth / 100) + "%";
}

function getPercent1(all, part) {
	return (all > 0) ? (100 / all) * part : 0;
}

function getPercent2(all, partBytes) {
	return (all > 0) ? (100 / all) * partBytes : 0;
}


// Show/hide content on the video HTML page. Not part of the YouTube or Google Analytics Javascript
function HideContent(d) {
	if(d.length < 1) { return; }
		document.getElementById(d).style.display = "none";
}
function ShowContent(d) {
	if(d.length < 1) { return; }
		document.getElementById(d).style.display = "block";
}
function ReverseContentDisplay(d) {
	if(d.length < 1) { return; }
	if(document.getElementById(d).style.display == "none") { document.getElementById(d).style.display = "block"; }
	else { document.getElementById(d).style.display = "none"; }
}