SG.namespace("SG.time");

/*
 * sg-timeline.js depends on the SIMILE Timeline API. The API is included like this:
 *
 * <ui:staticFileInclude file="/javascript/timeline/timeline_js/timeline-api.js" />
 *
 * It is important that the SIMILE Timeline API is included after this file!
 *
 * The API loads a couple of other files itself. In order to support caching and
 * updates of the files timeline-api.js and simile-ajax-api.js need to be adapted.
 * The respective file names need to enhanced with a version parameter (e. g.
 * timeline.js?version=2.3.0).
 *
 * Also the Timeline_ajax_url attribute below needs to be enhanced with the according
 * version.
 *
 */

(function() {
	Timeline_ajax_url= "/silbergrau/assets/logic/timeline_ajax/simile-ajax-api.js?version=2.2.0";
	Timeline_urlPrefix= '/silbergrau/assets/logic/timeline_js/';
	Timeline_parameters='bundle=true';
})();

SG.time.timelines = {};

SG.time.Timeline = function(config, events) {
	this.init(config, events);
};

SG.time.Timeline.prototype = {

	init : function(config, events) {
		SimileAjax.History.enabled = false; // if history is enabled it tries to load a __history__.html file.
		this.config = config;
		this.events = events;
		this.eventSource = new Timeline.DefaultEventSource();

		this.bandInfos = [];
		this.createBandInfos(config.bands);

		this.createTimeline();
		this.loadEvents();
	},

	createTimeline : function() {
		this.timeline = Timeline.create(document.getElementById(this.config.id), this.bandInfos, this.config.orientation);
	},

	createBandInfos : function() {
		if(this.config.bands) {
			for(var i = 0; i < this.config.bands.length; i++) {
				var band = this.config.bands[i];
				band.eventSource = this.eventSource;
				if(band.zones) {
					this.createHotzoneBandInfo(band);
				} else {
					this.createBandInfo(band);
				}
			}
		}
		this.synchronizeBands();
	},

	createBandInfo : function(bandConfig) {
		this.bandInfos.push(Timeline.createBandInfo(bandConfig));
	},

	createHotzoneBandInfo : function(bandConfig) {
		this.bandInfos.push(Timeline.createHotZoneBandInfo(bandConfig));
	},

	synchronizeBands : function() {
		for(var i = 1; i < this.bandInfos.length; i++) {
			this.bandInfos[i].syncWith = 0;
			this.bandInfos[i].highlight = true;
		}
	},

	loadEvents: function() {
		this.eventSource.loadJSON(this.events, document.location.href);
	}
};
