qhor.init_timeline = function () {
	if ($('#timeline-widget').length > 0) {

		// Override one of the Timeline functions:
		Timeline.GregorianDateLabeller.prototype.labelPrecise = function(date) {
			return SimileAjax.DateTime.removeTimeZoneOffset(
				date,
				this._timeZone //+ (new Date().getTimezoneOffset() / 60)
			).toDateString();
		};

		// Get the timeline state from cookies if possible
		var state_cookie = (qhor.cookie.get('timeline_state') || "").split('|');
		var state = {};
		state.zoom = (!state_cookie[0]) ? 15 : parseInt(state_cookie[0]);
		state.focus = (state_cookie[1] !== undefined) ? new Date(state_cookie[1]) : new Date(Math.floor(Math.random() * (new Date().getFullYear()-1890))+1890, 0, 1);

		var eventSource = new Timeline.DefaultEventSource(0);
		var theme = Timeline.ClassicTheme.create();

		// theme.ether.mouseWheel = "zoom";
		theme.event.bubble.width = 300;
		theme.event.bubble.maxHeight = 300;
		theme.event.track.offset = 16;
		theme.event.instant.icon = '/themes/site_themes/default/images/timeline-event-marker.png';
		theme.event.instant.iconHeight = 15;
		
		var zoomSteps = [
		  {pixelsPerInterval:  50,  unit: Timeline.DateTime.DAY},
		  {pixelsPerInterval: 300,  unit: Timeline.DateTime.WEEK},
		  {pixelsPerInterval: 200,  unit: Timeline.DateTime.WEEK},
		  {pixelsPerInterval: 100,  unit: Timeline.DateTime.WEEK},
		  {pixelsPerInterval:  50,  unit: Timeline.DateTime.WEEK},
		  {pixelsPerInterval: 150,  unit: Timeline.DateTime.MONTH},
		  {pixelsPerInterval: 100,  unit: Timeline.DateTime.MONTH},
		  {pixelsPerInterval:  50,  unit: Timeline.DateTime.MONTH}, // DEFAULT zoomIndex
		  {pixelsPerInterval:  30,  unit: Timeline.DateTime.MONTH},
		  {pixelsPerInterval: 300,  unit: Timeline.DateTime.YEAR},
		  {pixelsPerInterval: 200,  unit: Timeline.DateTime.YEAR},
		  {pixelsPerInterval: 100,  unit: Timeline.DateTime.YEAR},
		  {pixelsPerInterval:  50,  unit: Timeline.DateTime.YEAR},
		  {pixelsPerInterval:  30,  unit: Timeline.DateTime.YEAR},
		  {pixelsPerInterval: 200,  unit: Timeline.DateTime.DECADE},
		  {pixelsPerInterval: 100,  unit: Timeline.DateTime.DECADE}
		]

		var bandInfos = [
			Timeline.createBandInfo({
				width:          ($('#timeline-widget').hasClass('large')) ? 393 : 193,
				intervalUnit:   zoomSteps[state.zoom].unit,
				intervalPixels: zoomSteps[state.zoom].pixelsPerInterval,
				zoomIndex:      state.zoom,
				zoomSteps:      zoomSteps,
				date: state.focus.toUTCString(),
				eventSource: eventSource,
				timeZone: +10,
				theme: theme
			}),
				Timeline.createBandInfo({
				width:          29,
				intervalUnit:   Timeline.DateTime.DECADE,
				intervalPixels: 80,
				date: state.focus.toUTCString(),
				timeZone: +10,
				theme: theme
			})
		];

		bandInfos[1].syncWith = 0;
		bandInfos[1].highlight = true;
		var tl = Timeline.create(document.getElementById("timeline-widget"), bandInfos);
		tl.loadJSON("/events/json/", function(json, url) {eventSource.loadJSON(json, url);});
		tl.getBand(0).addOnScrollListener ( function(band){
			qhor.cookie.set('timeline_state', band._zoomIndex + "|" + band.getCenterVisibleDate().toUTCString());
		});


	}
}();
