/*
   SiteComponents version:
   6.6.4.1, tag SC_6_6_4_1, created Tue Oct 27 21:28:23 +0100 2009
   $Name:  $
   
   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 DatePicker = Class.create({
   
   initialize: function(element) {
       this.element = $(element);
       this.dateField = this.element.select('input').first();
       this.popup = this.element.select('.datepicker-popup').first();

       this.element.select('noscript').first().replace('<a class="datepicker-select"><span>Select date</span></a>');
       this.openLink = this.element.select('a.datepicker-select').first();
    
       this.popup.setStyle({
           position: 'absolute',
           zIndex: 1000
       });
       
       this.initializePopupLinks();
       
       this.popup.observe('datepicker:contentChanged', this.initializePopupLinks.bindAsEventListener(this));
       this.openLink.observe('click', this.openLinkListener.bindAsEventListener(this))       
       Event.observe(document, 'click', this.windowClickListener.bindAsEventListener(this));
       
       this.openLinkListenerObj = this.openLinkListener.bindAsEventListener(this);
   }, 
   
   initializePopupLinks: function() {
       this.popup.select('a.day-link').each(function(element) {
           element.observe('click', function(event) {
               event.stop();
               
               this.dateField.value = event.element().hash.substr(10);
               new Effect.Highlight(this.dateField, {
                   queue: {
                       scope: this.element.id,
                       position: 'end',
                       limit: 2
                   }
               });
               
               this.hide();
           }.bindAsEventListener(this));
       }.bind(this));
   },
   
   openLinkListener: function(event) {
       event.stop();
       
       this.popup.setStyle({
           left: this.openLink.positionedOffset()['left'] + 'px',
           top: this.openLink.positionedOffset()['top'] + 'px'
       });
    
       try {
           new Effect.Grow(this.popup, {
               direction: 'top-left',
               duration: .3,
               queue: {
                   scope: this.element.id,
                   position: 'end',
                   limit: 1
               }
           });
       } catch (err) {     
           this.popup.show();
       }
   },
   
   /**
    * Used to close the popup
    */
   hide: function() {
       this.popup.hide();
   },
   
   /**
    * If there is a window click event, we hide the popup if the click
    * occured outside the popup itself
    */
   windowClickListener: function(event) {
       if(!event.element().descendantOf(this.popup)) {
           this.hide();
       }
   }
    
});
