var blibs;
if(!blibs) blibs = {};

blibs.Calendar = function(eventPanelId, eventPanelTimer, eventPanelDefaultText)
{
   if(eventPanelTimer == undefined) eventPanelTimer = 500;
   if(eventPanelDefaultText == undefined) eventPanelDefaultText = '';

   this.eventPanel = new blibs.Calendar.Panel(eventPanelId, eventPanelTimer, eventPanelDefaultText);

   /**
    * List of CalendarDate objects
    */
   this.calendarDates = new Array();

   /**
    * Adds an event
    */
   this.addEvent = function(linkId, event)
   {
      // title-Attribute der Links löschen
      var linkElt = document.getElementById(linkId);
      linkElt.removeAttribute('title');

      if(!this.calendarDates[linkId])
         this.calendarDates[linkId] = new blibs.Calendar.Date(this, linkId);

      this.calendarDates[linkId].addCalEvent(event);
   };

   /**
    * Returns the EventPanel element
    */
   this.getEventPanel = function()
   {
      return this.eventPanel;
   };

};

blibs.Calendar.Date = function(calendar, linkId)
{
   this.calendar = calendar;
   this.calEvents = new Array();

   /**
    * Adds an event
    */
   this.addCalEvent = function(calEvent)
   {
      this.calEvents.push(calEvent);
   };

   /**
    * Event handler for onMouseOver events
    */
   this._onMouseOver = function()
   {
      this.calendar.getEventPanel().init(this.calEvents);
   };

   /**
    * Event handler for onMouseOut events
    */
   this._onMouseOut = function()
   {
      this.calendar.getEventPanel()._holdPanel();
   };

   /**
    * Event handler for onClick events
    */
   this._onClick = function()
   {
      return false;
   };

   this.linkElt = document.getElementById(linkId);
   this.linkElt.onmouseover = this._onMouseOver.bindEventListener(this);
   this.linkElt.onmouseout  = this._onMouseOut.bindEventListener(this);
   this.linkElt.onclick     = this._onClick.bindEventListener(this);
};

blibs.Calendar.Panel = function(panelEltId, eventPanelTimer, eventPanelDefaultText)
{
   /**
    * Inits the panel for displaying the given calendar events
    */
   this.init = function(calEvents)
   {
      this.calEvents = calEvents;
      this.currentEventIndex = 0;
      this.clearTimer();
      this.displayTimer = window.setTimeout(this.show.bind(this), eventPanelTimer);
   };

   this.buildPanel = function()
   {
      // Paragraph mit dem Datum und Link zum Event drinnen
      this.panelElt.paragraphElt = this.panelElt.appendChild(document.createElement('p'));
      this.panelElt.paragraphDOMText = this.panelElt.paragraphElt.appendChild(document.createTextNode(html_entity_decode(eventPanelDefaultText)));
      this.panelElt.paragraphElt.appendChild(document.createElement('br'));

      // Link zum Event mit Teasertext gefüllt
      this.panelElt.eventLinkElt = this.panelElt.paragraphElt.appendChild(document.createElement('a'));
      this.panelElt.eventLinkElt.style.display = 'none';

      // Pager
      this.panelElt.pager = document.createElement('div');
      this.panelElt.pager.className = 'pager';
      this.panelElt.pager.countDOMText = this.panelElt.pager.appendChild(document.createTextNode('\u00a0'));
      this.panelElt.appendChild(this.panelElt.pager);

      this.panelElt.prevLink = this.panelElt.pager.appendChild(document.createElement('a'));
      this.panelElt.prevLink.style.visibility = 'hidden';
      this.panelElt.prevLink.className = 'prevLink';
      this.panelElt.prevLink.appendChild(document.createTextNode('<<'));
      this.panelElt.prevLink.onclick = this._showPrevEvent.bindEventListener(this);

      this.panelElt.nextLink = this.panelElt.pager.appendChild(document.createElement('a'));
      this.panelElt.nextLink.style.visibility = 'hidden';
      this.panelElt.nextLink.className = 'nextLink';
      this.panelElt.nextLink.appendChild(document.createTextNode('>>'));
      this.panelElt.nextLink.onclick = this._showNextEvent.bindEventListener(this);
   }

   this._showPrevEvent = function()
   {
      this.currentEventIndex--;
      this.show();
   };

   this._showNextEvent = function()
   {
      this.currentEventIndex++;
      this.show();
   };

   /**
    * Displays the current event
    */
   this.show = function()
   {
      if(!this.calEvents[this.currentEventIndex])
         return;

      this.panelElt.pager.countDOMText.nodeValue = (this.calEvents.length <= 1)? '\u00a0' : ((this.currentEventIndex + 1) + ' / ' + this.calEvents.length);
      this.panelElt.prevLink.style.visibility = (this.currentEventIndex > 0)? 'visible' : 'hidden';
      this.panelElt.nextLink.style.visibility = (this.currentEventIndex != (this.calEvents.length - 1))? 'visible' : 'hidden';

      this.panelElt.paragraphDOMText.nodeValue = this.calEvents[this.currentEventIndex].dateTime;
      this.panelElt.eventLinkElt.innerHTML = '';
      this.panelElt.eventLinkElt.style.visibility = 'hidden';
      this.panelElt.eventLinkElt.style.display = 'inline';

      var text = this.calEvents[this.currentEventIndex].title;
      blibs.HtmlToolbox.appendStrCutToDomElt(this.panelElt.eventLinkElt, text, 2);

      this.panelElt.eventLinkElt.setAttribute('href', this.calEvents[this.currentEventIndex].url);
      this.panelElt.eventLinkElt.setAttribute('target', this.calEvents[this.currentEventIndex].targetType);
      this.panelElt.eventLinkElt.style.visibility = 'visible';
   };

   /**
    * Clears the display timer
    */
   this.clearTimer = function()
   {
      if(this.displayTimer)
         window.clearTimeout(this.displayTimer);
   };

   /**
    * Event handler for onMouseOver events
    */
   this._holdPanel = function()
   {
      this.clearTimer();
   };

   this.panelElt = document.getElementById(panelEltId);
   this.panelElt.onmouseover = this._holdPanel.bindEventListener(this);

   this.buildPanel();
};

blibs.Calendar.Event = function(dateTime, title, url, targetType)
{
   this.dateTime = dateTime;
   this.title = html_entity_decode(title);
   this.url = html_entity_decode(url);
   this.targetType = targetType;
};


