
/**
 * clock class
 *
 * @param Integer, time difference in minutes for a specific locale or empty
 * for the current locale (on browser pc)
 *
 * @param Integer, current UTC time on server,
 * may be empty
 * server time will be compared with the time on browsers pc
 * to ensure that time on browser pc is correct
 *
 */
function Clock( intTimeDiff, intUTC )
{
	this.getTime = _getLocalTime;
	this.registerClockFunc = _registerCallbackTime;
	this._updateTime = _updateTime;
	this.priv_setDif = _setTimeDifference;
	this._objCurTime = new CurrentTimeExt( );
	this._funcCallback = null;
	this._objDate = new Date( );
	this._intTimeDiff = intTimeDiff ? intTimeDiff : this._objDate.getTimezoneOffset( ) * -1;
	this._objDate = this.priv_setDif( this._objDate, this._intTimeDiff );
	this.id = "clock@" + Math.random( );
}



/**
 * @return CurrentTime, a CurrentTime object for the given time difference
 */
function _getLocalTime( objDate )
{	
	if( !objDate )
		objDate = this.priv_setDif( new Date( ), this._intTimeDiff );
	this._objCurTime._setTime( objDate.getUTCHours(), objDate.getUTCMinutes(), objDate.getUTCSeconds(), objDate );
	return this._objCurTime ;
}

/**
 * register a callback function to get updated time information
 *
 * @param Function, callback function that is given every
 * second an updated CurrentTime object, may be null
 *
 */
function _registerCallbackTime( funcCallback )
{
	this._funcCallback = funcCallback;
	window[ this.id ] = this;
	setInterval( "window[ \"" + this.id + "\" ]._updateTime( )",1000 );
}

/**
 * updates current time information
 *
 */
function _updateTime( )
{
	this._objDate.setTime( this._objDate.getTime( ) + 1000 );
	this._funcCallback( this.getTime( this._objDate ) );
}

/**
 * converts minutes in milliseconds
 *
 * @param Integer, minutes
 *
 * @return Integer, given minutes in milliseconds
 */
function _setTimeDifference( objDate, intTimeDiff )
{
	objDate.setTime( objDate.getTime( ) + intTimeDiff * 60 * 1000 );
	return objDate;
}

/**
 * contains current time
 *
 */
function CurrentTime( )
{
	this.hours = 0;
	this. minutes = 0;
	this.seconds = 0;
	this.time = "";
	this._setTime = _setCurTime;
}

/**
 * extends current time
 *
 */
function CurrentTimeExt( )
{
	this.getTime = _parseTime;
	this.curTime = new CurrentTime( );
	this.objDate = null;
}
CurrentTimeExt.prototype = new CurrentTime;

/**
 * @return CurrentTime, a CurrentTime object for the given time difference
 */
function _parseTime( strLocale, strFormat, blnAsObject )
{
	this.curTime.hours = this.hours;
	this.curTime.minutes = this.minutes;
	this.curTime.seconds = this.seconds;
	strFormat = strFormat ? strFormat : "hh:mm:ss";
	if( !( strLocale && strLocale.toLowerCase( ) == "de" ) )
	{
		if( this.curTime.hours > 11 )
		{
			this.curTime.time = "PM";
			if( this.curTime.hours > 12 ) {
			   this.curTime.hours -=  12;
		  }
		}
		else
			this.curTime.time = "AM";
	}
	
	this.curTime.hours = this.curTime.hours < 10 ? "0" + this.curTime.hours : this.curTime.hours;
	this.curTime.minutes = this.curTime.minutes < 10 ? "0" + this.curTime.minutes : this.curTime.minutes;
	this.curTime.seconds = this.curTime.seconds < 10 ? "0" + this.curTime.seconds : this.curTime.seconds;
	if( blnAsObject )
		return this.curTime;
	return ( strFormat.replace( /hh/, this.curTime.hours ).replace( /mm/, this.curTime.minutes ).replace( /ss/, this.curTime.seconds ) ) + " " + this.curTime.time;
}

/**
 * set the current time
 *
 * @param Integer, hour component of current time
 *
 * @param Integer, minute component of current time
 *
 * @param Integer, second component of current time
 *
 * @param Date, date object containing current time
 */
function _setCurTime( intHours, intMinutes, intSeconds, objDate )
{
	this.hours = intHours;
	this. minutes = intMinutes;
	this.seconds = intSeconds;
	this.objDate = objDate;
}
