/*
    SiteComponents version:
    6.8.0.1, tag SC_6_8_0_1, created Fri Aug 20 11:18:33 +0200 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 validation config
/*jslint laxbreak: true, sub: true, white: false, browser: true,
onevar: false, nomen: false, noindent: true, eqeqeq: false, plusplus: false */
/*global siteComponentsConfig: false, Ajax: false, $$: false, $: false,
Element: false, window: false, Class: false, Effect: false, lightbox: false,
PeriodicalExecuter: false, Event: false, Prototype: false */

/**
 * JavaScript for AppbaseDatePickerUtil
 */
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 href="#select_date" 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();
       
       var offset = this.openLink.positionedOffset();
       
       // IE6 and 7 needs some adjustement to the detected position
       if(Prototype.Browser.IE && !Prototype.BrowserFeatures.ElementExtensions) {
           offset['top'] -= 14;
       }
       // Firefox also needs a minor adjustement to the left position
       if(Prototype.Browser.Gecko) {
           offset['left'] -= 1;
       }
       
       this.popup.setStyle({
           position: 'absolute',
           left: offset['left']+'px',
           top: offset['top']+'px'
       });
       
       try {
           new Effect.Grow(this.popup, {
               direction: 'top-left',
               duration: 0.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();
       }
   }
    
});
