/**
 * @class ServerTime
 * @classDescription サーバ時間取得
 * @author Petabit Corporation.
 * @since 2008-10-16
 * @version 1.0.0.0
 */

var ServerTime = function( milliseconds ){
	this.initialize( milliseconds );
};

ServerTime.prototype = {
	
	/**
	 * 初期化
	 * @param milliseconds サーバの時間を設定する
	 * @return void
	 * @type void
	 */
	initialize: function ( milliseconds ) {
		
		this.separator.year.set("年");
		this.separator.month.set("月");
		this.separator.date.set("日 ");
		this.separator.hour.set("時");
		this.separator.minute.set("分");
		this.separator.second.set("秒");
	
		var network_delay = 400;
		this.milliseconds.set( milliseconds );
		this._local_date = new Date();
		this._offset = (milliseconds * 1000) + network_delay - this._local_date.getTime();
		this._timezone_offset = this._local_date.getTimezoneOffset() * 60000;
		if(this._timezone_offset > 12 * 3600000) {
			this._timezone_offset -= ( 24 * 3600000 );
		}
	},

	/**
	 * グリニッジ標準時刻からの日本との時差
	 * @return Number
	 */
	jst_offset: function (){
		// 9 * 60 * 60 * 1000
		return 9 * 3600000;
	},
	
	/**
	 * ミリ秒
	 */
	milliseconds: {
		/**
		 * ミリ秒を設定
		 * @param milliseconds
		 */
		set: function ( milliseconds ){
			this._milliseconds = milliseconds;
		},
		
		/**
		 * 設定されたミリ秒を取得
		 * @param milliseconds
		 */
		get: function () {
			return this._milliseconds;
		}
	},
	
	/**
	 * サーバ時間Dateオブジェクト
	 * @return Date
	 */
	getServerDate: function () {
		return this._server_date;
	},
	
	/**
	 * HTMLのID属性
	 */
	attributeId: {
		set: function (id) {
			this.attribute_id = id;
		},
		get: function () {
			return this.attribute_id;
		}
	},
	
	/**
	 * 更新間隔
	 */
	interval: {
		/**
		 * 更新間隔設定
		 * @param milliseconds(unix time)
		 */
		set: function ( milliseconds ) {
			this._interval = milliseconds;
		},
		
		/**
		 * 更新間隔取得
		 * @return int
		 */
		get: function () {
			return this._interval;
		}
	},
	
	/**
	 * サーバ時間の取得を開始する。
	 * @return void
	 */
	start: function () {
		var this_pointer = this;
		var current_date = new Date();
		this._server_date = new Date( current_date.getTime() + this._offset + this._timezone_offset + this.jst_offset() );

		if (document.getElementById(this.attributeId.get()) == null) {
			return ;
		}

		document.getElementById(this.attributeId.get()).innerHTML = this.toString();
		// 再帰（無名関数にしないと動作せず）
		window.setTimeout( function() { this_pointer.start(); } , this.interval.get());
	},
	
	/**
	 * セパレータ
	 */
	separator: {
		year: {
			set: function ( separator ) {
				this._year_separator = separator;
			},
			get: function () {
				return this._year_separator;
			}
		},
		month: {
			set: function ( separator ) {
				this._month_separator = separator;
			},
			get: function () {
				return this._month_separator;
			}
		},
		date: {
			set: function ( separator ) {
				this._date_separator = separator;
			},
			get: function () {
				return this._date_separator;
			}
		},
		hour: {
			set: function ( separator ) {
				this._hour_separator = separator;
			},
			get: function () {
				return this._hour_separator;
			}
		},
		minute: {
			set: function ( separator ) {
				this._minute_separator = separator;
			},
			get: function () {
				return this._minute_separator;
			}
		},
		second: {
			set: function ( separator ) {
				this._second_separator = separator;
			},
			get: function () {
				return this._second_separator;
			}
		}
	},
	
	/**
	 * 文字列化
	 * @return string
	 */
	toString: function () {
		var size = 2;
		return (
			  this._server_date.getFullYear()
			+ this.separator.year.get()
			+ this._zeroPadding(this._server_date.getMonth() + 1, size)
			+ this.separator.month.get()
			+ this._zeroPadding(this._server_date.getDate(), size)
			+ this.separator.date.get()
			+ this._zeroPadding(this._server_date.getHours(), size)
			+ this.separator.hour.get()
			+ this._zeroPadding(this._server_date.getMinutes(), size)
			+ this.separator.minute.get()
			+ this._zeroPadding(this._server_date.getSeconds(), size)
			+ this.separator.second.get()
		);
	},
	
	/**
	 * ゼロ埋め
	 * @param String
	 * @return string
	 */
	_zeroPadding: function ( string, length ) {
		var str = new String(string);
		return (str.length <= length)? new Array(length - str.length + 1).join("0") + str : str;
	}
};

