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

//console.log("manualanalog");

(function() {

	function ManualanalogClockFunctions(argument) {
			
	}
	
	ManualanalogClockFunctions.prototype.getInterval = function(){
		return interval;
	}
	//var rate = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1.1,.9,1];
	var miniRate = [1.1,.9,1];
	//var rate = [0.001,0.001,0.001,0.001,0.001,0.001,0.001,0.001,0.001,0.001,0.001,0.001,0.001,0.001,1.5,1,1,1,1,1];
	var minuteHand;
	var secondHand;
	var hourHand;
	var interval = 1000;
	var miniInterval = 50;
	var self;
	var runningSlow = false;

	ManualanalogClockFunctions.prototype.setup = function(controller){
		minuteHand = controller.get('minute');
		secondHand = controller.get('second');
		miniSecondHand = controller.get('miniSecond');
		hourHand = controller.get('hour');
		// WORKAROUND: AM/PM is showing up here when it shouldn't be
		controller.get('clock-container').removeClassName('am');
		controller.get('clock-container').removeClassName('pm');
		miniSecondHand.hide();
		if (controller.get('am-pm')) {
			controller.get('am-pm').hide();
		}
	}
	
	actaullyUpdateSecondHand = function(second, controller, step) {
		var secondAngle = 6 * (second - 1);
		var percievedSecondAngle = secondAngle + (6 * miniRate[step]);
		secondHand.setStyle({'-webkit-transform':"rotate("+percievedSecondAngle+"deg)"});
		//secondHand.getWidth();
		step += 1;
		if(step < miniRate.size()){
			var nextStep = actaullyUpdateSecondHand.curry(second, controller, step);
			controller.window.setTimeout(nextStep, miniInterval);
		}
		
	}
	
	ManualanalogClockFunctions.prototype.updateTime = function(args) {

			var now = new Date();
			var millisecond = now.getMilliseconds();
			var secondAdjustment = Math.ceil(millisecond / 50) - 1;
			var hour = now.getHours();
			if (Mojo.Format.using12HrTime()) {
				if (hour > 12) 
					hour -= 12;
				else if (hour == 0)
					hour = 12;
			}
			var minute = now.getMinutes();
			var second = now.getSeconds();
			
			var secondAngle = 6 * (second - 1);
			var minuteAngle = 6 * minute + secondAngle / 60;
			var hourAngle = 30 * hour + minuteAngle / 12;
			
			minuteHand.setStyle({'-webkit-transform':"rotate("+minuteAngle+"deg)"});
			hourHand.setStyle({'-webkit-transform':"rotate("+hourAngle+"deg)"});
			
			//getting an attribute of the element forces it to redraw, and that prevents sawing
			//minuteHand.getWidth();
			//hourHand.getWidth();
			
			actaullyUpdateSecondHand(second, this.controller, 0);
			if(args && !args.slow && runningSlow){
				//secondHand.removeClassName('slow');
				secondHand.show();
				miniSecondHand.hide();
				runningSlow = false;
			}else if(args && args.slow && !runningSlow){
				//secondHand.addClassName('slow');
				secondHand.hide();
				miniSecondHand.show();
				runningSlow = true;
			}else{
				secondHand.setStyle({'-webkit-transition':"scale 0s"});
			}
			// format date
		
			//this.controller.get('date').textContent = Mojo.Format.formatDate(now, {"date":"short"})
			//this.controller.get('day').textContent = Mojo.Format.getDateTimeHash().medium.day[now.getDay()];
	}

	ManualanalogClockFunctions.prototype.alarm = function() {

	}

	ManualanalogClockFunctions.prototype.refresh = function() {

	}
	
	return ManualanalogClockFunctions;
})()

