
var timeline = null;
var timelineTimerID = null;

function getTimelineTheme() {
  var theme = Timeline.ClassicTheme.create();
  theme.event.label.width = 140; // px
  theme.event.bubble.width = 320;
  theme.event.bubble.height = 100;
  return theme;
}

function createTimelineWidget(id, orientation, initialDate, timeZone, url, feedType) {
  var eventSource = new Timeline.DefaultEventSource();
  var theme = getTimelineTheme();

  var bandInfos = [
    Timeline.createBandInfo({
      width:          '70%',
      intervalUnit:   Timeline.DateTime.WEEK,
      intervalPixels: 160,
      eventSource:    eventSource,
      date:           initialDate,
      timeZone:       timeZone,
      theme:          theme
    }),
    Timeline.createBandInfo({
      width:          '30%',
      intervalUnit:   Timeline.DateTime.MONTH,
      intervalPixels: 43,
      eventSource:    eventSource,
      date:           initialDate,
      timeZone:       timeZone,
      theme:          theme,
      showEventText:  false,
      trackHeight:    0.5,
      trackGap:       0.2
    })
  ];

  bandInfos[1].syncWith = 0;
  bandInfos[1].highlight = true;
  bandInfos[1].eventPainter.setLayout(bandInfos[0].eventPainter.getLayout());
  timeline = Timeline.create(document.getElementById(id), bandInfos, orientation);

  if (feedType == 'json') {
    timeline.loadJSON(url, function(json, url) { 
        eventSource.loadJSON(json, url); 
	});
  }
  else {
    timeline.loadXML(url + "?" + Math.random(), function(xml, url) {
         eventSource.loadXML(xml, url);
      });
  }
  return timeline;
}

function resizeTimeline() {
  if (timelineTimerID == null) {
    timelineTimerID = window.setTimeout(function() {
      timelineTimerID = null;
      timeline.layout();
    }, 500);
  }
}

function toggleCat(catType,festivalID,festivalTitle) {
    K2.ajaxGet('cat=' + festivalID + '&k2dynamic=1',
	    function(data) {
            jQuery('#featured-posts-3').hide();
            jQuery('body.home .primary').css('margin-top','15px');
		    jQuery('.content').html(data);
		    self.loading.fadeTo('fast', 0);
	    }
    );
}

Timeline.DurationEventPainter.prototype._showBubble = function(x,y,evt){

  if (evt.getLink () != window.location) {
       
      festivalid = evt.getLink();
      festivaltitle = evt.getText();

      if (festivalid.indexOf("?cat=")>0) {       
        festivalid = festivalid.substring( festivalid.indexOf("?cat=") + 5, festivalid.length );
        toggleCat( "festival", festivalid, festivaltitle);
        
        document.getElementById('contextinfo').innerHTML = '<h4>' + evt.getText() + '</h4><br/>' + evt.getDescription() + '<br/><br/>'; //<a href="http://tagr.tv/" title="back">show all festivals</a>
      }
/*      else if (festivalid.indexOf("intersect=1&p=")>0) {
        rolling.switchCats( "&" + festivalid.substring( festivalid.indexOf("intersect=1"), festivalid.length ), "");   
        window.filtrfestival=9999;
        window.catstr="";
        $('rollingarchives').style.display = 'none';
      } */
      else {   /* default showBubble */
        var div=this._band.openBubbleForPoint(x,y,this._theme.event.bubble.width,this._theme.event.bubble.height);
        evt.fillInfoBubble(div,this._theme,this._band.getLabeller());
      }
  }
};


