//
//	Kompatibilität zur Version Prototype-Version < '1.6' wahren
//
if (Prototype.Version < '1.6') {
	Object.extend(Hash.prototype, {
		get: function(key) {
			return this[key];
		},
		set: function(key, value) {
			return this[key] = value;
		},
		unset: function(key) {
			return this.remove(key);
		}
	});

	Object.extend(Object,	{
		isUndefined: function(object)	{
			return typeof	object ==	"undefined";
		}
	});
}


//
//
//
Ajax.LocalPeriodicalUpdater = Class.create(Ajax.Base, {
  initialize: function($super, container, url, options) {
    $super(options);
		this.container = {
			success: (container.success || container),
			failure: (container.failure || (container.success ? null : container))
		}
		this.url = url;

		this.frequency = (this.options.frequency || 2);
		this.decay = (this.options.decay || 1);

		if (!this.options.manually) this.start();
	},

	start: function() {
		var onComplete = this.options.onComplete || Prototype.emptyFunction;
		this.options.onComplete = (function(transport, param) {
			onComplete(transport, param);
			this.updateComplete(transport);
		}).bind(this);

		this.onTimerEvent();
	},

	stop: function() {
		this.requester.options.onComplete = undefined;
		clearTimeout(this.timer);
		(this.onComplete || Prototype.emptyFunction).apply(this, arguments);
	},

	updateComplete: function(request) {
		if (this.options.decay) {
			this.decay = (request.responseText == this.lastText ?
				this.decay * this.options.decay : 1);

			this.lastText = request.responseText;
		}
		this.timer = setTimeout(this.onTimerEvent.bind(this),
		this.decay * this.frequency * 1000);
	},

	onTimerEvent: function() {
	    this.requester = new Ajax.Request(this.url, this.options);
	}
});



Object.extend(Date.prototype, {
	strftime: function(format) {
		var day = this.getUTCDay(), month = this.getUTCMonth();
		var hours = this.getUTCHours(), minutes = this.getUTCMinutes();
		function pad(num) { return num.toPaddedString(2); };

		return format.gsub(/\%([aAbBcdDHiImMpSwyY])/, function(part) {
			switch(part[1]) {
				case 'a': return $w("Sun Mon Tue Wed Thu Fri Sat")[day]; break;
				case 'A': return $w("Sunday Monday Tuesday Wednesday Thursday Friday Saturday")[day]; break;
				case 'b': return $w("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec")[month]; break;
				case 'B': return $w("January February March April May June July August September October November December")[month]; break;
				case 'c': return this.toString(); break;
				case 'd': return this.getUTCDate(); break;
				case 'D': return pad(this.getUTCDate()); break;
				case 'H': return pad(hours); break;
				case 'i': return (hours === 12 || hours === 0) ? 12 : (hours + 12) % 12; break;
				case 'I': return pad((hours === 12 || hours === 0) ? 12 : (hours + 12) % 12); break;
				case 'm': return pad(month + 1); break;
				case 'M': return pad(minutes); break;
				case 'p': return hours > 11 ? 'PM' : 'AM'; break;
				case 'S': return pad(this.getUTCSeconds()); break;
				case 'w': return day; break;
				case 'y': return pad(this.getUTCFullYear() % 100); break;
				case 'Y': return this.getUTCFullYear().toString(); break;
			}
		}.bind(this));
	}
});
