var tl;
var oldLabelInterval;

// called by the eventhandler for "onload"
function timeline_onload(path,xml_url,period_years) {
	// Timeline bugfix:
	// in IE7 (the problem is worse in XP than Vista),
	// links in the popup window do not always work when the page is scrolled down a bit.
	SimileAjax.WindowManager._onBodyMouseDown=function(elmt,evt,target){
		if(!("eventPhase"in evt)||evt.eventPhase==evt.BUBBLING_PHASE){
//		SimileAjax.WindowManager.cancelPopups(evt);
		}
	};

	// override Timeline javascript prototype to replace "BC" with "fvt" ("før hvor tidsregning")
	oldLabelInterval = Timeline.GregorianDateLabeller.prototype.labelInterval;
	Timeline.GregorianDateLabeller.prototype.labelInterval =
		function(date, intervalUnit) {
	        var r = oldLabelInterval.call(this, date, intervalUnit);
	        if(r && r.text && r.text.replace) {
	        	r.text = r.text.replace(/BC/g, " fvt.");
	        }
	        return r;
		};

   var empty_event_source = new Timeline.DefaultEventSource(0);
   var event_source = new Timeline.DefaultEventSource(0);
   var start_date = "Jan 10 2009 00:00:00 GMT";

   var band_infos = [
     Timeline.createBandInfo({
         eventSource:    empty_event_source,
         date:           start_date,
    	 width:          "10%",
         intervalUnit:   Timeline.DateTime.CENTURY,
         intervalPixels: 600
     }),
     Timeline.createBandInfo({
         eventSource:    event_source,
         date:           start_date,
         width:          "90%",
         intervalUnit:   Timeline.DateTime.CENTURY,
         intervalPixels: 600
     })
   ];

   band_infos[1].syncWith = 0;
   band_infos[1].highlight = true;

   var theme = Timeline.ClassicTheme.create();
   theme.event.bubble.width = 250;

   // create decorators
   var decorators = new Array();
/*
   var period_data = period_years.split("|");
   for(var i = 0; i < period_data.length; i += 3) {
       // example of date format: Fri Nov 22 1963 12:30:00 GMT-0600
	   var from = period_data[i];
	   var to = period_data[i + 1];
	   var label = period_data[i + 2];

	   var decorator = new Timeline.SpanHighlightDecorator({
	       startDate:  from,
	       endDate:    to,
	       color:      "#FFC080",
	       opacity:    0,
	       startLabel: "",
	       endLabel:   label,
	       theme:      theme
	   });
	   decorators[decorators.length] = decorator;
   }
*/

   // add decorators (mark all historic periods)
   band_infos[0].decorators = decorators;

   var dom_node = $(path).get(0);
   tl = Timeline.create(dom_node, band_infos);
   xml_url += "&no_cache=1";
   tl.loadXML(xml_url, function(xml, url) { event_source.loadXML(xml, url); });
}

// eventhandler for "onresize"
var resizeTimerID = null;
function timeline_onresize() {
     if (resizeTimerID == null) {
         resizeTimerID = window.setTimeout(function() {
             resizeTimerID = null;
             tl.layout();
         }, 500);
     }
}

// center the timeline on the given year
function timeline_center(year) {
	tl.getBand(0).setCenterVisibleDate(new Date(year,0,1));
}
