  
function formatDateInputValue(format, date){
  result = strtr(format, {'%year%':date.getFullYear(), '%month%':date.getMonth() + 1, '%day%':date.getDate()}); 
  return result;
}
  
var yuiCalendarButton = function(conf) {
  conf = conf == undefined ? new Array() : conf;
  
  if (YAHOO.lang.isUndefined(conf.pages)) {
    conf.pages = '1';
  }
  if (YAHOO.lang.isUndefined(conf.buttonContainer)) {
    conf.buttonContainer = YAHOO.util.Dom.generateId();
  }
  if (YAHOO.lang.isUndefined(conf.buttonId)) {
    conf.buttonId = YAHOO.util.Dom.generateId();
  }
  if (YAHOO.lang.isUndefined(conf.buttonLabel)) {
    conf.buttonLabel = '';
  }
  if (YAHOO.lang.isUndefined(conf.dialogHeader)) {
    conf.dialogHeader = '&nbsp;';
  }
  if (YAHOO.lang.isUndefined(conf.alignContext)) {
    conf.alignContext = [conf.buttonContainer,"tl","tl"]
  }
  if (YAHOO.lang.isUndefined(conf.ignoreElements)) {
    conf.ignoreElements = new Array();
  }
  
  this.panelOptions = {context:conf.alignContext, visible:false, draggable:false, close:true, autofillheight:"body", height:"260px", underlay:"none"};
  this.calendarOptions = {pages:2, close:false, iframe:false, hide_blank_weeks:true};

  if (!YAHOO.lang.isUndefined(conf.panel)) {
    for (attrname in conf.panel) { 
      this.panelOptions[attrname] = conf.panel[attrname]; 
    }
  }

  if (!YAHOO.lang.isUndefined(conf.calendar)) {
    for (attrname in conf.calendar) { 
      this.calendarOptions[attrname] = conf.calendar[attrname]; 
    }
  }
  
  this.conf = conf;
  this.init();
};

yuiCalendarButton.prototype = {
  init:function() {
    this.panelId = YAHOO.util.Dom.generateId(null, 'pnl');
    this.calendarId = YAHOO.util.Dom.generateId(null, 'cal');
    this.calendar = null;
    
    this.panel = new YAHOO.widget.Dialog(this.panelId, this.panelOptions);
    this.panel.setHeader(this.conf.dialogHeader);
    this.panel.setBody('<div id="' + this.calendarId + '"></div>');
    this.panel.showEvent.subscribe(this.panelOnShow, this, true);
    //this.panel.showEvent.subscribe(this.focusDay, this, true);
    //this.panel.renderEvent.subscribe(this.focusDay, this, true);
    this.panel.render(document.body);
    
    this.button = new YAHOO.widget.Button({type: "push", id: this.conf.buttonId, label: this.conf.buttonLabel, container: this.conf.buttonContainer});
    
    YAHOO.util.Event.addListener(this.conf.buttonContainer, "click", this.show, this, true);
    YAHOO.util.Event.addListener(document, "click", this.documentOnClick, this, true);
  },
  
  enable:function() {
    this.button.set("disabled", false);
  },
  
  disable:function() {
    this.button.set("disabled", true);
  },
  
  focusDay:function() {
    var oCalendarTBody = Dom.get(this.calendarId).tBodies[0],
        aElements = oCalendarTBody.getElementsByTagName("a"),
        oAnchor;
    
    if (aElements.length > 0) {
      Dom.batch(aElements, function (element) {
        if (Dom.hasClass(element.parentNode, "today")) {
          oAnchor = element;
        }
      });
      
      if (!oAnchor) {
        oAnchor = aElements[0];
      }

      YAHOO.lang.later(0, oAnchor, function() {
        try {
          oAnchor.focus();
        }
        catch(e) {}
      });
    }
  },

  show:function() {
    if (this.calendar != null) {
      this.calendar.destroy();
      this.calendar = null;
    }
    
    if (this.button.get("disabled")) {
      return;
    }
    
    this.calendar = new YAHOO.widget.CalendarGroup(this.calendarId, this.calendarId, this.calendarOptions);
    this.calendar.render();
    this.calendar.selectEvent.subscribe(this.calendarOnSelect, this, true);
    this.calendar.renderEvent.subscribe(this.panelOnChangeContent, this, true);
    this.calendar.show();
    this.panel.show();
  },

  hide:function() {
    this.panel.hide();
  },

  disable:function() {
    this.button.set("disabled", true);
  },
  
  enable:function() {
    this.button.set("disabled", false);
  },
  
  calendarOnSelect:function(type, args, obj) {
    if (this.calendar.getSelectedDates().length > 0) {
      if (! YAHOO.lang.isUndefined(this.conf.onSelect)) {
        this.conf.onSelect(this.calendar.getSelectedDates()[0]);
      }
    }
    this.hide();
  },
  
  panelOnShow:function(type, args, obj) {
    if (YAHOO.env.ua.ie) {
      this.panel.fireEvent("changeContent");
    }
  },
  
  panelOnChangeContent:function(type, args, obj) {
    this.panel.fireEvent("changeContent");
  },
  
  documentOnClick:function(e) {
    if (this.panel == null)  return;
    var el = Event.getTarget(e);
    var pEl = this.panel.element;
    if (el != pEl && !Dom.isAncestor(pEl, el) && el != this.button && !Dom.isAncestor(this.button, el) && !in_array(el.id,this.conf.ignoreElements)) {
      this.hide();
    }
  }
}

