/*
    SiteComponents version:
    6.7.0, tag SC_6_7_0, created Thu Jan 07 10:17:49 +0100 2010

    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.
    
*/

/*jslint laxbreak: true, sub: true, white: false, browser: true,
onevar: false, nomen: false, noindent: true, eqeqeq: false, plusplus: false,
forin: true */
/*global siteComponentsConfig: false, getSiteComponentsConfig: false,
Ajax: false, $$: false, $: false, Element: false, window: false, Class: false,
Effect: false, lightbox: false, PeriodicalExecuter: false, Event: false,
CtCookie: false, cpKeywords: false, CtTooltip: false, Prototype: false,
cpWriteMediaObject: false, getThemeName: false, Hash: false, $H: false,
$break: false */

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'] - 250) + "px",
                    top: (link.positionedOffset()['top'] + 18) + "px"
                });
                

                try {
                    new Effect.Grow(popupElement, {
                        direction: 'top-right',
                        duration: 0.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: 0.3,
            pulses: 1
        });
    },

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

});