glow.ready(function() {
//
var video;
var playState;
var vidTimer;
var durationSlide;
var volIcon;
var volumeSlide;
function setValues() {
//	video = document.getElementsByTagName('video');
	video = glow.dom.get('video');
    video[0].controls = false;
	playState = glow.dom.get('#playstate');
	vidTimer = glow.dom.get('#vid_timer');
	volIcon = glow.dom.get('#vol-icon');
	volumeSlide = new glow.widgets.Slider('#volume',{
		bindTo:'#vol_count',
		buttons:false,
		step: 0.1,
		min: 0,
		max: 1,
		size: 70,
		val: 1
	});
	t = window.setInterval(function() {
//    console.log(video[0].readyState,video[0].networkState);
		if (video[0].readyState >= 1) {
			window.clearInterval(t);
			durationSlide = new glow.widgets.Slider('#vid_duration',{
				bindTo:'#duration_count',
				buttons:false,
				step: 1,
				min: 1,
				max: Math.round(video[0].duration - 1),
				size: 260,
				val: 0
			});
			playVid();
		}
	},500);
}

function playVid() {
	var initialPlay = glow.dom.create('<div class="overlay play"></div>');
	glow.dom.get('#video').prepend(initialPlay);
//	playState.addEventListener('click',playControl,false);
	glow.events.addListener(playState,'click',playControl);
	glow.events.addListener('.play','click',playControl);
	glow.events.addListener(volIcon,'click',muteToggle);
	glow.events.addListener(durationSlide,'slideStop',function(event){
		video[0].currentTime = event.currentVal;
		var currentSecs = secondsToTime(event.currentVal);
		vidTimer.text(currentSecs.h + ':' + currentSecs.m + ':' + currentSecs.s);
	});
	glow.events.addListener(volumeSlide,'slideStop',function(event){
		video[0].volume = event.currentVal;
		volumeIcons();
	});
}

function playControl() {
	if (video[0].paused === true) {
		video[0].play();
		startCount();
		playControls('Pause');
		if (glow.dom.get('.paused')) {
			glow.dom.get('.paused').remove();
		};
		if (glow.dom.get('.play')) {
			glow.dom.get('.play').remove();
		};
	} else if (video[0].ended === true) {
		durationSlide.val(0);
		video[0].play();
		startCount();
		playControls('Pause');
	} else {
		video[0].pause();
		pauseCount();
		playControls('Play');
		var paused = glow.dom.create('<div class="overlay paused"></div>');
		glow.dom.get('#video').prepend(paused);
	}
}

function startCount() {
	t = window.setInterval(function() {
		if (video[0].ended === true) {
			window.clearInterval(t);
			playControls('Play');
		} else {
			var timeNow = Math.round(video[0].currentTime);
			durationSlide.val(timeNow);
			var currentSecs = secondsToTime(timeNow);
			vidTimer.text(currentSecs.h + ':' + currentSecs.m + ':' + currentSecs.s);
		}
	},1000);		
}

function pauseCount() {
	window.clearInterval(t);
}

function playControls(playStatus) {
	playState.attr({
		src:'controls/' + playStatus.toLowerCase() + '.png',
		alt:playStatus,
		title:playStatus
	});
}

function muteToggle() {
	if(video[0].muted === false) {
		video[0].muted = true;
		volumeSlide.disabled(true);
	} else {
		video[0].muted = false;
		volumeSlide.disabled(false);
	}
	volumeIcons();
}

function volumeIcons() {
	if ((video[0].muted === true) || (video[0].volume === 0)) {
		volIcon.attr({
		src:'controls/s-vol-mute.png',
		alt:'Muted',
		title:'Muted'
		});
	} else if (video[0].volume >= 0.8) {
		volIcon.attr({
		src:'controls/s-vol-up.png',
		alt:'Max',
		title:'Max'
		});
	} else if (video[0].volume <= 0.3) {
		volIcon.attr({
		src:'controls/s-vol-down.png',
		alt:'Min',
		title:'Min'
		});
	} else {
		volIcon.attr({
		src:'controls/s-vol-med.png',
		alt:'Volume',
		title:'Volume'
		});
	}
}

function secondsToTime(secs) {
	var hours = Math.floor(secs / (60 * 60));
	var divisor_for_minutes = secs % (60 * 60);
	var minutes = Math.floor(divisor_for_minutes / 60);
	var divisor_for_seconds = divisor_for_minutes % 60;
	var seconds = Math.ceil(divisor_for_seconds);
	if (hours <= 9) { hours = '0' + hours; }
	if (minutes <= 9) { minutes = '0' + minutes; }
	if (seconds <= 9) { seconds = '0' + seconds; }
	var obj = {
		'h': hours,
		'm': minutes,
		's': seconds
	};
	return obj;
}

window.addEventListener('load',setValues,false);

//
});

