var timeschedule =
{
  //ofset top
  offsetTop : 0,
  
  /**
   * constructors
   */
  init : function()
  {
    //init interface
    timeschedule.initInterface();
    
    //init eventListeners
    timeschedule.initEventListeners();
  },
  
  
  
  /**
   * init interface
   */
  initInterface : function()
  {
    //only show links when the page is fully loaded to prevent
    //the url from beeing followed without modalbox
    $$('a.modalbox-open-timeschedule').each(function(obj)
    {
      obj.show();
    });
  },
  
  
  /**
   * init eventlisteners
   */  
  initEventListeners : function()
  {
    //base eventlsitener for content that was added to the dom after dom:loaded
    document.observe('click', function(e)
    {
      obj = e.target;
      
      if(obj)
        timeschedule.ajaxContentCheck(e, obj);      
    });
    
    //open modalbox
    $$('a.modalbox-open-timeschedule').each(function(obj)
    {
      obj.observe('click', function(e){ Event.stop(e); timeschedule.onModalboxOpen(e, obj, 1020); return false; }, false); 
    });    
  },
  
  
  
  /**
   * add prototype wrapping options for ajaxLoaded content
   */
  ajaxContentCheck : function(e, obj)
  {
    if(Element.hasClassName(obj, 'ts-right-item-part'))
      timeschedule.showTooltip(e, obj);
    if(Element.hasClassName(obj, 'ts-left-item'))
      timeschedule.showTooltip(e, obj);
    if(Element.hasClassName(obj, 'ts-tooltip-close'))
      timeschedule.hideTooltip(e);
  },
  
  
  /**
   * showTooltip
   */
  showTooltip : function(e, obj)
  {
    Event.stop(e);
    
    if(obj)
    {
      //get text object for tooltip
      var tooltipTextObj = obj.select('.ts-text-tooltip');
      
      if(tooltipTextObj.length >= 1)
      {        
        //get mouse coordinates
        var mouseX = e.clientX + 10;
        var mouseY = e.clientY + 10;        
        var tooltipShow = $('ts-tooltip');
        var textToShow = tooltipTextObj[0].innerHTML;
        
        //if text provided and tooltip located
        if(tooltipShow && textToShow)
        {
          tooltipShow.setStyle
          ({
            'display' : 'block',
            'left' :    mouseX + 'px',
            'top' :    mouseY + 'px'
          });
          tooltipShow.innerHTML = textToShow;
        }
      }
    }
  },
  
  
  /**
   * acts / events mouseover
   */
  onMouseOverItem : function(obj)
  {
    if(obj)
    {
      //get size of p element within
      var widthP = obj.select('p');
      
      if(widthP && widthP.length >= 1)
      {
        obj.setStyle
        ({
          'overflow' : 'visible',
          'width' : 'auto'
        });
      }
    }
  },
  
  
  /**
   * hide tooltip
   */
  hideTooltip : function(e)
  {
    Event.stop(e);
    
    var tooltipObj = $('ts-tooltip');
    
    if(tooltipObj)
      tooltipObj.hide();
  },
  
  
  /**
   * open modalbox for timeschedule
   */
  onModalboxOpen : function(e, obj, objWidth)
  {
    //get current y scroll position
    timeschedule.offsetTop = obj.offsetTop;
    
    //scroll to top of the screen
    scroll(0,0);
    
    var objTitle = obj.title;
    var objHref = obj.href;
    
    if(objHref && objTitle)
    {
      Modalbox.show( objHref,
      {
        width: objWidth,
        title: objTitle,
        afterHide : function()
        {
          //back to init scroll
          scroll(0, timeschedule.offsetTop);
        },
        autoFocusing: false
      });
    }    
  }
};

//on dom loaded
document.observe('dom:loaded', function() { timeschedule.init(); }, false);
