// Init
function qt_start (movieid) {
	var percentsofar = 0;
	var loaded = 0;
	theInt = setInterval(function() {
		qt_move_progress('currentvideo', '.qt-progress', '.qt-progress-track');
	}, 100);
	$('.video').css('background', 'black');
	$('.qt-play').click(function () {
		qt_play(movieid);
	});
	$('.qt-pause').click(function () {
		qt_pause(movieid);
	});
	$('.qt-reset').click(function () {
		qt_reset(movieid);
	});
	$('.qt-volume-on').click(function() {
		qt_volume(movieid, 0);
		$('.qt-volume-on').css('display', 'none');
		$('.qt-volume-off').css('display', 'block');
	});
	$('.qt-volume-off').click(function() {
		qt_volume(movieid, 255);
		$('.qt-volume-off').css('display', 'none');
		$('.qt-volume-on').css('display', 'block');
	});
	$('.qt-progress-track, .qt-progress-track-load').click(function(e) {
		var clicked = e.pageX - $('.qt-progress-track').offset().left;
		var progresstrack = '.qt-progress-track';
		var progressindicator = '.qt-progress';
		var endtime = getQTDur(movieid, 'unformatted');
		var totallength = $(progresstrack).css('width').replace('px', '');
		var indicatorwidth = $(progressindicator).css('width').replace('px', '');
		var newtime = ((((clicked+indicatorwidth)/totallength)*100)/1000)*endtime;
		qt_jump(movieid, newtime);
	});
	$(".qt-progress").draggable({
		containment: 'parent',
		axis: 'x',
		start: function() {
			if ($('.qt-play').css('display') == 'none') {
				qt_pause(movieid, true);
			}
		},
		drag: function(e) {
			var progresstrack = '.qt-progress-track';
			var progressindicator = '.qt-progress';
			var endtime = getQTDur(movieid, 'unformatted');
			var totallength = $(progresstrack).css('width').replace('px', '');
			var indicatorwidth = $(progressindicator).css('width').replace('px', '');
			var clicked = e.pageX - $('.qt-progress-track').offset().left + 1;
			var newtime = ((((clicked+indicatorwidth)/totallength)*100)/1000)*endtime;
			qt_jump(movieid, newtime);
		},
		stop: function() {
			if ($('.qt-play').css('display') == 'none') {
				qt_play(movieid, true);
			}
		},
	});
	setTimeout(function() {
		addListener(document.getElementById(movieid), 'qt_ended', completeVid, false);
		addListener(document.getElementById(movieid), 'qt_pause', completeVid, false);
		addListener(document.getElementById(movieid), 'qt_play', startVid, false);
		//addListener(document.getElementById(movieid), 'qt_play', moveProgress, false);
		//addListener(document.getElementById(movieid), 'qt_pause', endProgress, false);
	}, 100);
}

function moveProgress() {
	//window.theInt = 
}

function endProgress() {
	clearInterval(window.theInt);
}

function startVid() {
	if ($('.qt-play').css('display') == 'block') {
		$('.qt-play').css('display', 'none');
		$('.qt-pause').css('display', 'block');
	}
}

function completeVid() {
	if ($('.qt-pause').css('display') == 'block') {
		$('.qt-play').css('display', 'block');
		$('.qt-pause').css('display', 'none');
	}
}

function resetVideo() {
	clearInterval(theInt);
	$('.qt-controller').animate({
		opacity: 0,
		display: 'none'
	});
	$('.qt-controller .qt-progress').css('left', '0px');
	$('.video').css('background', 'black url(images/sitewide/loader/round-circles.gif) no-repeat center center');
}

// Controller Functions
function qt_play (movieid, dontchange) {
	if (!dontchange) {
		$('.qt-play').css('display', 'none');
		$('.qt-pause').css('display', 'block');
	}
	document.getElementById(movieid).Play();
}

function qt_pause (movieid, dontchange) {
	if (!dontchange) {
		$('.qt-play').css('display', 'block');
		$('.qt-pause').css('display', 'none');
	}
	document.getElementById(movieid).Stop();
}

function qt_reset (movieid) {
	document.getElementById(movieid).Stop();
	document.getElementById(movieid).SetTime(0);
		$('.qt-play').css('display', 'block');
		$('.qt-pause').css('display', 'none');
}

function qt_jump (movieid, timeval) {
	document.getElementById(movieid).SetTime(timeval);
}

function qt_volume (movieid, volume) {
	document.getElementById(movieid).SetVolume(volume);
}

function qt_togglepause (movieid) {
	
}

function qt_move_progress(movieid, progressindicator, progresstrack) {
	var endtime = getQTDur(movieid, 'unformatted');
	var curtime = getQTTime(movieid, 'unformatted');
	var totallength = $(progresstrack).css('width').replace('px', '');
	var indicatorwidth = $(progressindicator).css('width').replace('px', '');
	var percentsofar = (((100/endtime)*curtime)*(totallength/100))-indicatorwidth;
	if (percentsofar <= 0) { percentsofar = 0; }
	var loaded = qt_bufferstatus(movieid);
	if (loaded <= 0) { loaded = 0; }
	var loaded = Math.floor(loaded*(totallength/100)) + 'px';
	var where = Math.floor(percentsofar) + 'px';
	$('.qt-progress-load').css('width', loaded);
	$(progressindicator).animate({
		left: where,
	}, 50, function() {
	});
}

// Get Data Functions
function getQTTime(movieid, outputkind) {
	var curtime = document.getElementById(movieid).GetTime();
	if (outputkind != 'unformatted') {
		var curtime = time(curtime);
	}
	return curtime;
}

function getQTDur(movieid, outputkind) {
	var endtime = document.getElementById(movieid).GetEndTime();
	if (outputkind != 'unformatted') {
		var endtime = time(endtime);
	}
	return endtime
}

function qt_bufferstatus(movieid) {
    var timeLoaded = document.getElementById(movieid).GetMaxTimeLoaded()
    var duration = document.getElementById(movieid).GetDuration();
    return parseInt( timeLoaded * 100 / duration );
}

// Listeners

function addListener(obj, evt, handler, captures) {
	if ( document.addEventListener ) {
		obj.addEventListener(evt, handler, captures);
	} else {
		// IE
		obj.attachEvent('on' + evt, handler);
	}
}

// Support Functions
function time(ms) {
	ms = Math.floor(ms/239.8);
	var sec = Math.floor(ms/1000);
	ms = ms % 1000;
	t = '';
	
	var min = Math.floor(sec/60);
	sec = sec % 60;
	t = two(sec) + t;
	
	var hr = Math.floor(min/60);
	min = min % 60;
	t = two(min) + ":" + t
	
	var day = Math.floor(hr/60);
	hr = hr % 60;
	return t;
}

function two(x) {return ((x>9)?"":"0")+x}
function three(x) {return ((x>99)?"":"0")+((x>9)?"":"0")+x}

function endedqt(obj, evt, handler, captures) {
		if ( document.addEventListener ) {
			obj.addEventListener(evt, handler, captures);
		} else {
			// IE
			obj.attachEvent('on' + evt, handler);
		}
}
