function CalendarPopUp(){}
function LandmarkCalendar(targetId,sourceId){
  this.targetId = targetId;
  this.sourceId = sourceId;
  this.popup.items.push(this);
}
LandmarkCalendar.prototype.popup = new CalendarPopUp;
CalendarPopUp.prototype.items = [];
CalendarPopUp.prototype.popShow = function(e){
  var offset = Position.cumulativeOffset(this.current.source)
  var offsetx = (window.innerWidth-25||document.body.clientWidth) - (offset[0] + this.popwindow.offsetWidth) + window.scrollX||document.body.scrollLeft;
  var offsety = ((window.innerHeight||document.body.clientHeight)-25) - (offset[1] + this.popwindow.offsetHeight) + window.scrollY||document.body.scrollTop;
  if(offsetx>0) offsetx = 0;
  if(offsety>0) offsety = 0;
  this.popwindow.style.left = offset[0] + offsetx + "px";
  this.popwindow.style.top = offset[1] + offsety + "px";
  if(document.all){
    $A(document.getElementsByTagName("SELECT")).each(function(box){
      var replacer = document.createElement("div");
      with(replacer.style){ position="absolute";display="inline-block";width=box.offsetWidth;marginBottom="0px";height=box.offsetHeight-2;border="1px solid threedshadow";background="white"; }
      box.style.visibility = "hidden";
      box.insertAdjacentElement("beforeBegin",replacer);
    })
  }
  this.popwindow.style.visibility = "visible";
}
CalendarPopUp.prototype.popHide = function(e){
  if(!this.current) return;
  var node = (e) ? Event.element(e) : document;
  while(node = node.parentNode)
    if(node == this.popwindow) return false;
  this.popwindow.style.visibility = "hidden";
  if(document.all){
    $A(document.getElementsByTagName("SELECT")).each(function(box){
      box.parentNode.removeChild(box.previousSibling);
      box.style.visibility = "visible";
    })
  }
  this.current = null;
  return true;
}
Event.observe(window, "load", function(){
  this.popwindow = $("landmark-calendar-popup");
  Event.observe(document, "mousedown", function(e){
    this.popHide(e);
  }.bind(this));
  this.calendar = new YAHOO.widget.Calendar("popupcalendar.calendar","landmark-calendar");
  this.calendar.Config.Options.NAV_ARROW_LEFT = "chrome/default/scripts/calendar/src/img/callt.gif";
  this.calendar.Config.Options.NAV_ARROW_RIGHT = "chrome/default/scripts/calendar/src/img/calrt.gif";
  this.calendar.minDate = new Date;
  this.calendar.onSelect = function(){
    this.current.target.value = this.calendar._toFieldArray(this.calendar.getSelectedDates()).toString().replace(/(\d+?),(\d+?),(\d+)/,"$2/$3/$1");
    this.popHide();
  }.bind(this)
  this.calendar.render();
  this.items.each(function(item){
    item.target = $(item.targetId);
    item.source = $(item.sourceId);
    [item.target,item.source].each(function(source){
      Event.observe(source, "click", function(e){
        Event.stop(e);
        this.current = item;
        this.popShow(e);
      }.bind(this))
    }.bind(this))
  }.bind(this));
}.bind(CalendarPopUp.prototype));
var popupcalendar = new CalendarPopUp;

