/*
   SiteComponents version:
   Id: smallcalendartile.js,v 1.4 2009/03/27 07:54:02 kurt Exp 
   Name: SC_6_6_0 
   
   Disclaimer
   
   While we make every effort to ensure that this code is fit for its intended
   purpose, we make no guarantees as to its functionality. CoreTrek AS will
   accept no responsibility for the loss of data or any other damage or
   financial loss caused by use of this code.
   
   Copyright
   
   This programming code is copyright of CoreTrek AS. Permission to run this
   code is given to approved users of CoreTrek's publishing system CorePublish.
   
   This source code may not be copied, modified or otherwise repurposed for use
   by a third party without the written permission of CoreTrek AS.
   
   Contact webmaster@coretrek.com for information.
  
*/

var SmallCalendarTile = Class.create({

    /**
     * Called from smallcalendar.php tile template.
     */
    initialize: function(element) {
        this.element = $(element);
        
        this.initializeLinks();
        
        Event.observe(document, 'click', function(event) {
            this.closeAllTooltips();
        }.bind(this));
        
        this.element.observe('smallcalendar:contentChanged', this.contentChangedListener.bindAsEventListener(this));
    },

    initializeLinks: function() {
        this.element.select('a.calendar_popup_link').each(function(element) {
            element.observe('click', function(event) {
                event.stop();
                
                var link = event.findElement('a');
                var popupElement = $(link.id.substring(20));

                // Do not reopen if popup is visible
                if(popupElement.visible()) {
                    return;
                }
                
                this.closeAllTooltips();
                
                popupElement.setStyle({
                    left: (link.positionedOffset()['left'] - 11) + "px",
                    top: (link.positionedOffset()['top'] + 18) + "px"
                });

                try {
                    new Effect.Grow(popupElement, {
                        direction: 'top-left',
                        duration: .3,
                        queue: {
                            scope: this.element.id,
                            limit: 1
                        }
                    });
                } catch(err) {
                    popupElement.show();
                }

            }.bindAsEventListener(this));
        }.bind(this));
    },

    contentChangedListener: function(event) {
        this.initializeLinks();
        new Effect.Pulsate(event.element().select('table.calendar-header td').first().next(), {
            duration: .3,
            pulses: 1
        });
    },

    closeAllTooltips: function() {
        $$('.small-calendar-popup').each(function(element) {
            element.hide();
        });
    }

});