'use strict';

var Clock = (function () {

	var element = null;
	var isClockVisible = true;

	function CurrentTime () {
		var time = Preferences.Lights.TIME_OF_THE_DAY;
		var hours = parseInt(time);
		var minutes = Math.round((time - hours) * 60.0);

		if(hours < 10) {
			hours = '0' + hours;
		}

		if(minutes < 10) {
			minutes = '0' + minutes;
		}

		return {
			hours: hours,
			minutes: minutes,
		};
	}

	function FormatedTime () {
		var timeObj = CurrentTime ();

		var str = '';
		str += '<span class="hours">' + timeObj.hours + '</span>';
		str += '<span class="delimiter">:</span>';
		str += '<span class="minutes">' + timeObj.minutes + '</span>';

		return str;
	}

	function Run () {
		element.html(FormatedTime());

		var interval = setInterval(function () {
			element.html(FormatedTime());
		}, 330);
	}

	function HideClock () {
		element.fadeOut(250);
		isClockVisible = false;
	}

	function ShowClock () {
		element.fadeIn(250);
		isClockVisible = true;
	}

	function ToggleClock (show) {
		if (typeof show !== 'undefined') {
			ShowClock();
		} else {
			isClockVisible ? HideClock : ShowClock();
		}
	}

	function Init (guiElement) {
		element = $('<div class="clock-container onscreenGui"></div>');
		guiElement.append(element);

		Run();

		// open menu with lights / time tab visible
		element.click(function() {
			gui.getMainMenu().showLights();
		});

		return {
			ToggleClock: ToggleClock,
		};
	}

	return Init;

})();
