/*
* Clock: initalizes a running clock by using the Time HH:MM:SS in the textnode of the passed element
* 
* usage: 
* settings = {}
* $('#office-selector span').officeClock(settings);
*/

/*
* Constructor 
*
* valid optional settings:
* dayHours			- [8,18] between these hours we consider day, outside we consider night
* initCallback 	- function to execute when clock is first initialized (yields instance of clock)
* pmCallback 		- function to execute when PM is reached (yields element)
* amCallback 		- function to execute when AM is reached (yields element)
* dayCallback		- function to execute when dayTime is reached (yields element)
* nightCallback - function to execute when nightTime is reached (yields element)
*/
function OfficeClock(el, officeNr, settings) {

	/*
	* default settings
	*/
	this.settings = {
		dayHours: [8, 20]
	}

	$.extend(this.settings, settings);

	/*
	* 0-based officeNumber
	*/
	this.officeNr  = officeNr;
	/*
	* the element containing the time
	*/
	this.el = el;
	/*
	* "day" || "night"
	*/
	this.timeOfDay = "";

	var timeParts = this.el.innerHTML.split(':');
	this.hours 	 = parseInt(timeParts[0]);
	this.minutes = parseInt(timeParts[1]);
	this.seconds = parseInt(timeParts[2]);
	
	// determine day or night
	if (this.hours > this.settings.dayHours[0] && this.hours < this.settings.dayHours[1]) {
		this.timeOfDay = "day";
	} else {
		this.timeOfDay = "night";
	}
	
	(this.hours > 12) ? this.setPM() : this.setAM();
	
	if (this.settings.initCallback) this.settings.initCallback(this);
	this.startUpdating();
}

OfficeClock.prototype = {

	/*
	* called every second to update the displayed time
	*/
	updateTime:function() {
		this.seconds++;
		if (this.seconds == 60) {
			this.seconds = 0;
			this.minutes++;
			if (this.minutes == 60) {
				this.minutes = 0;
				this.hours++;
				if (this.hours == 24) {
					this.hours = 0;
					this.setAM();
					if (this.settings.amCallback) this.settings.amCallback(this);

				} else if (this.hours == 12) {
					this.setPM();
					if (this.settings.pmCallback) this.settings.pmCallback(this);

				} else if (this.hours == this.settings.dayHours[0]) {
					this.timeOfDay = "day";
					if (this.settings.dayCallback) this.settings.dayCallback(this);

				} else if (this.hours == this.settings.dayHours[1]) {
					this.timeOfDay = "night";
					if (this.settings.nightCallback) this.settings.nightCallback(this);
				}
			}
		}

		var parts = [this.hours, this.minutes, this.seconds]
		
		if (this.hours < 10) parts[0]   = "0" + this.hours;
		if (this.minutes < 10) parts[1] = "0" + this.minutes;
		if (this.seconds < 10) parts[2] = "0" + this.seconds;
		
		$(this.el).text(parts.join(':'));
	},

	/*
	* kickstart the updater
	*/ 
	startUpdating: function() {
		this.updateTime();
		setTimeout(this.startUpdating.bind(this), 1000);
	},
	
	setAM:function() {
		this.pm = true;
		this.am = false;
	},
	
	setPM:function() {
		this.pm = false;
		this.am = true;
	}

}

// extend jquery 
$.extend($.fn, {
	officeClock: function(settings) {
		for(var i=0; i<this.length; i++) {
			new OfficeClock(this[i], i, settings);
		}
		return this;
	}
});

Function.prototype.bind = function(scope) {
	var method = this;
	return function() {
		return method.apply(scope, arguments);
	}
}