/* Copyright 2009 Palm, Inc.  All rights reserved. */

(function() {
	var interval = 1000;
	function DigitalClockFunctions() {
	
	}
	
	DigitalClockFunctions.prototype.getInterval = function(){
		return interval;
	}

	DigitalClockFunctions.prototype.setup = function(controller){
	
	} 

	DigitalClockFunctions.prototype.updateTime = function() {

		// list of divs to update
		var divs = ["h1", "h2", "m1", "m2"]; //, "s1", "s2"];
		// default content of digits for each of the divs
		var digits = ["", "", "0", ""]; // ,"0", ""];
	
		var now = new Date();
	
		var hour = now.getHours();
		if (Mojo.Format.using12HrTime()) {
			if (hour > 12) 
				hour -= 12;
			else if (hour == 0)
				hour = 12;
		}
	
		hour += "";
		if (hour.length == 1) {
			digits[1] = hour;
		} else {
			digits[0] = hour.charAt(0);
			digits[1] = hour.charAt(1);
		}
	
		var minute = now.getMinutes() + "";
		if (minute.length == 1) {			
			digits[3] = minute;
		} else {
			digits[2] = minute.charAt(0);
			digits[3] = minute.charAt(1);
		}
	
		/*
		var seconds = now.getSeconds() + "";
		if (seconds.length == 1) {
			digits[5] = seconds;
		} else {
			digits[4] = seconds.charAt(0);
			digits[5] = seconds.charAt(1);
		}
		*/
		
		hour = now.getHours();
		var clock = this.controller.get('clock-container');
		var ampm = this.controller.get('am-pm');
		
		// set AM/PM style  
		if (Mojo.Format.using12HrTime()) {
			if (hour < 12) {
				clock.addClassName('am');
				clock.removeClassName('pm');
				ampm.addClassName('am');
				ampm.removeClassName('pm');
			} else {
				clock.addClassName('pm');
				clock.removeClassName('am');
				ampm.addClassName('pm');
				ampm.removeClassName('am');
			}
		} else {
			clock.removeClassName('am');
			clock.removeClassName('pm');
			ampm.removeClassName('am');
			ampm.removeClassName('pm');
		}
		
	
		// set night style between 6PM & 6AM
		if (hour < 6 || hour > 17) {
			clock.addClassName('night');
			clock.removeClassName('day');
		} else {
			clock.addClassName('day');
			clock.removeClassName('night');
		}
		this.controller.get('full-date').textContent = Mojo.Format.formatDate(now, {"date":"full"})
	
	
		// 
	
		for (var i = 0; i < digits.length; i++) {

			this.controller.get(divs[i]).className = "digital-digit digit-" + digits[i];
		}
	}

	DigitalClockFunctions.prototype.alarm = function() {

	}

	DigitalClockFunctions.prototype.refresh = function() {

	}

	return DigitalClockFunctions;
})()