YUICalText.dates_loaded = false;
function YUICalText(name, frmt, config) {
    config = config || {};

    this.name = name;
    this.frmt = frmt || 'm/d/Y';
    this.config = config;
    this.config.navigator = config.navigator || true;
    this.config.hide_blank_weeks = config.hide_blank_weeks || true;
    //config.maxdate = config.maxdate || new Date();
    this.Dom = YAHOO.util.Dom;
    this.Event = YAHOO.util.Event;
    this.cal1 = false;
    this.over_cal = false;
    this.field = document.getElementById(this.name);
    this.total_count = 0;
    // Get required js
    if(!YUICalText.dates_loaded){
        YAHOO.util.Get.script('/js/date.js');
        YUICalText.dates_loaded = true;
    }

    this.init();
}


YUICalText.prototype.init = function() {
    this.cal1 = new YAHOO.widget.Calendar(this.name + '_container', this.config);
    this.cal1.selectEvent.subscribe(this.getDate, this, true);
    this.cal1.renderEvent.subscribe(this.setupListeners, this, true);
    this.Event.addListener(this.name, 'focus', this.showCal, this, true);
    this.Event.addListener(this.name, 'blur', this.hideCal, this, true);
    this.Event.addListener(this.name, 'keydown', this.handleTab, this, true);
    this.cal1.render();
}

YUICalText.prototype.setupListeners = function() {
    this.Event.addListener(this.name + '_container', 'mouseover', function() {
        this.over_cal = true;
    }, this, true);
    this.Event.addListener(this.name + '_container', 'mouseout', function() {
        this.over_cal = false;
    }, this, true);
}

YUICalText.prototype.getDate = function() {
    var selDate = this.cal1.getSelectedDates()[0];
    this.field.value = Dates.date(this.frmt, selDate.toUTCString());
    this.over_cal = false;
    this.hideCal();
}

YUICalText.prototype.setDate = function(date) {
    if (date.match(/\d{2}\/\d{2}\/\d{4}/)) {
        this.field.value = Dates.date(this.frmt, date);
    } else {
        this.field.value = date;
    }

}

YUICalText.prototype.showCal = function(ev) {
    var tar = this.Event.getTarget(ev);
    this.field = tar;

    var xy = this.Dom.getXY(tar), date = this.Dom.get(tar).value, ts;
    if (date) {
        ts = Dates.strtotime(date);
        if (ts) {
            date = Dates.date('m/d/Y', ts);
        } else {
            date = Dates.date('m/d/Y');
        }
        this.cal1.cfg.setProperty('selected', date);
        this.cal1.cfg.setProperty('pagedate', new Date(date), true);
    } else {
        this.cal1.cfg.setProperty('selected', '');
        this.cal1.cfg.setProperty('pagedate', new Date(), true);
    }
    this.cal1.render();
    this.Dom.setStyle(this.name + '_container', 'display', 'block');
    xy[1] = xy[1] + 20;
    this.Dom.setXY(this.name + '_container', xy);
}

YUICalText.prototype.hideCal = function() {
    var date, ts;
    if (this.field && this.field.value) {
        ts = Dates.strtotime(this.field.value);
        if (ts) {
            date = Dates.date('m/d/Y', ts);
            this.cal1.cfg.setProperty('selected', date);
            this.cal1.cfg.setProperty('pagedate', new Date(date), true);

            this.field.value = Dates.date(this.frmt, ts);
        }
    }
    if (!this.over_cal) {
        this.Dom.setStyle(this.name + '_container', 'display', 'none');
    }
}

YUICalText.prototype.handleTab = function (ev, obj) {
    if (ev.keyCode == 9) {
        obj.hideCal();
    }
}