function aClock(id,instanceName) {
	this.id = id;
	this.dataSource = null;
	this.blinkState = false;
	this.blinkColins = null;
	this.hours = null;
	this.minutes = null;
	this.dataSourceObject = null;
	this.instanceName = instanceName;
	this.clockState = false;
	this.runLoop = 0;
	performObjectMethodOnLoad(this,"awakeFromLoad");
}

aClock.prototype.start = function(){
	if (this.clockState) return;
	if (!this.dataSource){
		alert("Error : no data source for clock object!");
		return;
	}	
	this.moveClock();
};

aClock.prototype.stop = function(){
	clearTimeout(this.timeoutID);
};

aClock.prototype.moveClock = function(){	
	if (!this.getDataSourceObject()){
		this.refreshDataSource();
		this.timeoutID = setTimeout(this.instanceName + ".moveClock();",1000);
		return;
	}
		
	if (isNaN(this.getDataSourceObject().getHours())){
		this.refreshDataSource();
		this.timeoutID = setTimeout(this.instanceName + ".moveClock();",1000);
		return;
	}
	
	var newHours   = parseInt(this.getDataSourceObject().getHours());
	var newMinutes = parseInt(this.getDataSourceObject().getMinutes());
	
	if ( newHours >= 0 && newHours <= 9 ){
		// This means there is a leading zero inside the digit , then the first letter is
		// surely a 0 :-)
		this.hoursFirst.src = "images/clock/0.jpg";
		this.hoursSecond.src = "images/clock/" + newHours + ".jpg";
	} else {
		// The first digit is not a 0 for sure , so we need to split it 
		// inorder to set both digits.
		this.hoursFirst.src = "images/clock/" + newHours.toString().charAt(0) + ".jpg";
		this.hoursSecond.src = "images/clock/" + newHours.toString().charAt(1) + ".jpg";	
	}
		
	if ( newMinutes >= 0 && newMinutes <= 9){
		this.minutesFirst.src = "images/clock/0.jpg";
		this.minutesSecond.src = "images/clock/" + newMinutes + ".jpg";
	} else {
		this.minutesFirst.src = "images/clock/" + newMinutes.toString().charAt(0) + ".jpg";
		this.minutesSecond.src = "images/clock/" + newMinutes.toString().charAt(1) + ".jpg";
	}	

	this.blink();
	if ( this.runLoop % 60 == 0 ) this.refreshDataSource();
	this.runLoop++;
	this.incrementDataSource();
	this.timeoutID = setTimeout(this.instanceName + ".moveClock();",1000);
};

aClock.prototype.refreshDataSource = function(){
	this.dataSourceObject = new Date(parseInt(this.dataSource.innerHTML)*1000);
};

aClock.prototype.incrementDataSource = function(){
	this.dataSourceObject = new Date(this.dataSourceObject.getTime() + 1000);
};

aClock.prototype.getDataSourceObject = function(){
	return this.dataSourceObject;
};

aClock.prototype.setDataSource = function(o){
	this.dataSource = o;
};

aClock.prototype.awakeFromLoad = function(){
	this.blinkColins = $(this.id + "_colins");
	this.hoursFirst		 = $(this.id + "_hoursFirst");
	this.hoursSecond	 = $(this.id + "_hoursSecond");
	this.minutesFirst	 = $(this.id + "_minutesFirst");
	this.minutesSecond	 = $(this.id + "_minutesSecond");
	this.setDataSource($("datetimeSource"));
	this.start();
};

aClock.prototype.blink = function(){
	if ( this.blinkState ){
		// Blinking has now a ':' inside
		this.blinkColins.src = "images/clock/2dots.jpg";
		this.blinkState = false;
	} else {
		// Blinking has now a '' inside
		this.blinkColins.src = "images/clock/nodots.jpg";
		this.blinkState = true;
	}
};
