var ytplayer = null;

function mycarousel_itemFirstInCallback(carousel, item, idx, state) {
	if (state == 'init')
		carousel.scroll(0, false);
};

jQuery.easing['BounceEaseOut'] = function(p, t, b, c, d) {
	if ((t/=d) < (1/2.75)) {
		return c*(7.5625*t*t) + b;
	} else if (t < (2/2.75)) {
		return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
	} else if (t < (2.5/2.75)) {
		return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
	} else {
		return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
	}
};

jQuery(document).ready(function() {
	jQuery('#carousel').jcarousel({
		easing: 'BounceEaseOut',
		animation: 1500,
		scroll: 6
	});
});

function onYouTubePlayerReady(playerId) {
	ytplayer = document.getElementById("myytplayer");
	setInterval(setProgressBar, 200);
	cueNewVideo(0,0);
}


function setProgressBar() {
	var progress = document.getElementById("played");
	var loaded = document.getElementById("loaded");
	var skipbar = document.getElementById("skipbar");
	if (ytplayer) {
		if (ytplayer.getPlayerState() == 1 || ytplayer.getPlayerState() == 2) {
			var bytesloaded = Math.round((ytplayer.getVideoBytesLoaded() / ytplayer.getVideoBytesTotal())*100);
			var width = Math.round((ytplayer.getCurrentTime() / ytplayer.getDuration())*100);
			loaded.style.width = bytesloaded+"%";
			progress.style.width = width+"%";
			skipbar.style.width = bytesloaded+"%";
		}
	}
}

function showVideoInformation(id) {
	var title = document.getElementById("vid-title");
	var desc = document.getElementById("vid-desc");
	if(title && desc) {
		title.innerHTML = "<p><strong>" + videos[id][1] + "</strong><p>"
		desc.innerHTML = "<p>" + videos[id][2] + "<p>"
	}
}

// functions for the api calls
function loadNewVideo(id, startSeconds) {
	if (ytplayer) {
		ytplayer.loadVideoById(videos[id][0], parseInt(startSeconds));
		showVideoInformation(id);
	}
}

function cueNewVideo(id, startSeconds) {
	if (ytplayer) {
		ytplayer.cueVideoById(videos[id][0], startSeconds);
		showVideoInformation(id);
	}
}

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

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

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

function skipBack() {
	if (ytplayer)
		ytplayer.seekTo(Math.round(ytplayer.getCurrentTime() - 10), true);
}

function skipForward() {
	if (ytplayer)
		ytplayer.seekTo(Math.round(ytplayer.getCurrentTime() + 10), true);
}



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();
}
}

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



function setVolume(newVolume) {
if (ytplayer) {
ytplayer.setVolume(newVolume);
}
}

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


function skipTo(e) {
	var bars = document.getElementById("progress-bars");
	var width = 0;
	e = e || window.event;
	var tgt = e.target || e.srcElement;
	// Get top lef co-ords of div
	var divX = findPosX(tgt);
	// Workout if page has been scrolled
	var pXo = getPXoffset();
	// Subtract div co-ords from event co-ords
	var clickX = e.clientX - divX + pXo;


	if(document.defaultView)
		width = document.defaultView.getComputedStyle(bars, "").getPropertyValue("width");
	else if(bars.currentStyle)
		width = bars.currentStyle.width;

	
	width = parseInt(width.replace("px",""));

	var percent = (clickX/width);
	var duration = ytplayer.getDuration();
	var time = duration*percent;

	if (ytplayer)
		ytplayer.seekTo(time, true);
}

function findPosX(obj) {
var curleft = 0;
if (obj.offsetParent) {
while (obj.offsetParent) {
curleft += obj.offsetLeft
obj = obj.offsetParent;
}
} else if (obj.x) {
curleft += obj.x;
}
return curleft;
}



function getPXoffset(){
if (self.pageXOffset) { // all except Explorer
return self.pageXOffset;
} else if (document.documentElement && document.documentElement.scrollTop) {// Explorer 6 Strict
return document.documentElement.scrollLeft;
} else if (document.body) { // all other Explorers
return document.body.scrollLeft;
}
}