YAHOO.namespace("jmaki.widget");

YAHOO.jmaki.widget.YearSelectCalendar = function(id, containerId, config) {
    YAHOO.jmaki.widget.YearSelectCalendar.superclass.constructor.call(this, id, containerId, config);
    this.renderEvent.subscribe(this.syncYear, this, true);    
};

YAHOO.lang.extend(YAHOO.jmaki.widget.YearSelectCalendar, YAHOO.widget.Calendar);

//overrides YAHOO.widget.Calendar.prototype.renderFooter = function(html)
YAHOO.jmaki.widget.YearSelectCalendar.prototype.renderFooter = function(html) {
    html[html.length] = '<tfoot>';
    html[html.length] = '<tr>';
    html[html.length] = '<th colspan="7" class="calhead">';
    html[html.length] = '<div class="calheader">';
    html[html.length] = '<a id="' + this.id + '-prevyear" class="calnavleft"></a>';
    
    html[html.length] = '<select name="' + this.id + '-yearselect" id="' + this.id + '-yearselect">';
    // the default value
    html[html.length] = '<option>----------</option>';
    
    var maxYear = this.getMaxYear();
    for(i = this.getMinYear(); i <= maxYear; i++) {
        html[html.length] = '<option value="' + i + '">' + i + '</option>';
    }
    html[html.length] = '</select>';
    html[html.length] = '<a id="' + this.id + '-nextyear" class="calnavright"></a>';
    html[html.length] = '</div>';
    html[html.length] = '</th>';
    html[html.length] = '</tr>';
    html[html.length] = '</tfoot>';
    return html;
}

YAHOO.jmaki.widget.YearSelectCalendar.prototype.getMaxYear = function() {
    var maxdate = this.cfg.getProperty("maxdate");
    return maxdate.getFullYear();
}

YAHOO.jmaki.widget.YearSelectCalendar.prototype.getMinYear = function() {
    var mindate = this.cfg.getProperty("mindate");
    return mindate.getFullYear();
}


YAHOO.jmaki.widget.YearSelectCalendar.prototype.changeYear = function() {
    //get the value of the <select> menu as an integer, send to setYear:
    this.setYear(parseInt(YAHOO.util.Dom.get("" + this.id + "-yearselect").value));
    //have to rerender to make this visible:
    this.render();
}

YAHOO.jmaki.widget.YearSelectCalendar.prototype.applyListeners = function() {
    YAHOO.jmaki.widget.YearSelectCalendar.superclass.applyListeners.call(this);
    YAHOO.util.Event.addListener('' + this.id + '-nextyear', "click", this.nextYear, this, true);    
    YAHOO.util.Event.addListener('' + this.id + '-prevyear', "click", this.previousYear, this, true);    
    YAHOO.util.Event.addListener('' + this.id + '-yearselect', "change", this.changeYear, this, true);    
}

    
//our syncing function:
YAHOO.jmaki.widget.YearSelectCalendar.prototype.syncYear = function(type) {
        var fullYear = this.cellDates[7][0];
        var yearSelect = YAHOO.util.Dom.get('' + this.id + '-yearselect');
        yearSelect.value = parseInt(fullYear);
        
    }
