<?xml version="1.0"?>
<bindings xmlns="http://www.mozilla.org/xbl"
	xmlns:xbl="http://www.mozilla.org/xbl"
	xmlns:xul="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
    
	<binding id="datepicker-month" extends="xul:label">
		<content value=""/>
	</binding>
	<binding id="datepicker-year" extends="xul:label">
		<content value=""/>
	</binding>
    
	<binding id="datepicker-day" extends="xul:label">
        <content value=""/>
		<implementation>
            <constructor><![CDATA[
                this._datepicker = this.parentNode;
				while(this._datepicker.localName!="datepicker") this._datepicker = this._datepicker.parentNode; 
            ]]></constructor>
			<field name="_datepicker">null</field>
			<property name="datepicker">
				<getter><![CDATA[
					return this._datepicker;
				]]></getter>
			</property>
		</implementation>
		<handlers>
			<handler event="click" phase="target"><![CDATA[
				var val = this.getAttribute("value");
				if(val!="")	{
					this.datepicker.currentDay=val;
				}
                return true;
			]]></handler>
		</handlers>
	</binding>
    
    
	<binding id="datepicker">
		<content orient="vertical" currentday="" currentmonth="" currentyear="" ismonth="false" isyear="false">
            <xul:toolbar anonid="datepicker-month-toolbar">
                <xul:datepicker-button class="previous-month" oncommand="this.parentNode.parentNode.currentMonth--"/>
                <xul:datepicker-button class="today" oncommand="this.parentNode.parentNode.setDate(new Date())"/>
                <xul:toolbaritem orient="vertical" pack="center">
                    <xul:datepicker-month anonid="month"/>
                </xul:toolbaritem>
                <xul:toolbarspacer width="5px"/>
                <xul:toolbaritem>
                    <xul:vbox pack="center">
                        <xul:datepicker-year  xbl:inherits="value=currentyear"/>
                    </xul:vbox>
                    <xul:spacer width="3px"/>
                    <xul:spinbutton onclickup="this.parentNode.parentNode.parentNode.currentYear++"
                                    onclickdn="this.parentNode.parentNode.parentNode.currentYear--"/>
                </xul:toolbaritem>
                <xul:toolbarspacer flex="1"/>
                <xul:datepicker-button class="next-month" oncommand="this.parentNode.parentNode.currentMonth++"/>
            </xul:toolbar>

            <xul:grid flex="1">
				<xul:columns>
					<xul:column flex="1"/><xul:column flex="1"/><xul:column flex="1"/><xul:column flex="1"/><xul:column flex="1"/><xul:column flex="1"/><xul:column flex="1"/>
				</xul:columns>
				<xul:rows flex="1" anonid="datepicker-rows">
					<xul:row>
						<xul:label flex="1"/><xul:label flex="1"/>
                        <xul:label flex="1"/><xul:label flex="1"/>
						<xul:label flex="1"/><xul:label flex="1"/>
                        <xul:label flex="1"/>
					</xul:row>
					<xul:row><xul:datepicker-day/><xul:datepicker-day/><xul:datepicker-day/><xul:datepicker-day/><xul:datepicker-day/><xul:datepicker-day/><xul:datepicker-day/></xul:row>
					<xul:row><xul:datepicker-day/><xul:datepicker-day/><xul:datepicker-day/><xul:datepicker-day/><xul:datepicker-day/><xul:datepicker-day/><xul:datepicker-day/></xul:row>
					<xul:row><xul:datepicker-day/><xul:datepicker-day/><xul:datepicker-day/><xul:datepicker-day/><xul:datepicker-day/><xul:datepicker-day/><xul:datepicker-day/></xul:row>
					<xul:row><xul:datepicker-day/><xul:datepicker-day/><xul:datepicker-day/><xul:datepicker-day/><xul:datepicker-day/><xul:datepicker-day/><xul:datepicker-day/></xul:row>
					<xul:row><xul:datepicker-day/><xul:datepicker-day/><xul:datepicker-day/><xul:datepicker-day/><xul:datepicker-day/><xul:datepicker-day/><xul:datepicker-day/></xul:row>
					<xul:row><xul:datepicker-day/><xul:datepicker-day/><xul:datepicker-day/><xul:datepicker-day/><xul:datepicker-day/><xul:datepicker-day/><xul:datepicker-day/></xul:row>
				</xul:rows>
			</xul:grid>
		</content>
		<implementation>
			<constructor><![CDATA[
				var today = new Date();
                document.getAnonymousElementByAttribute(this ,"class", "today")
                    .setAttribute("tooltiptext", today.toLocaleDateString()); 
				if(this.getAttribute("currentyear")==""||isNaN(this.getAttribute("currentyear"))) 
                    this.setAttribute("currentyear", today.getFullYear());
				if(this.getAttribute("currentmonth")==""||isNaN(this.getAttribute("currentmonth"))) 
                    this.setAttribute("currentmonth", today.getMonth());

				// mise a jour des label des jours
				var j;
				var rows = document.getAnonymousElementByAttribute(this ,"anonid", "datepicker-rows");
				var title = rows.firstChild;
				var labels = title.getElementsByTagName("label");
				for(var i=0; i<7; i++) {
					j = today.toLocaleString().split(" ")[0];
					labels[today.getDay()].setAttribute("value", j.substring(0, 1).toUpperCase()+j.substring(1, 2)); 
					labels[today.getDay()].setAttribute("tooltiptext", j); 
					today.setDate(today.getDate()+1);
				} 
                this.updatedDate(false);
			]]></constructor>
			<property name="currentMonth">
				<getter><![CDATA[
					return this.getAttribute("currentmonth");
				]]></getter>
				<setter><![CDATA[
					if(isNaN(val))
                        val = new Date().getMonth();
					this.setAttribute("currentmonth", val);
                    this.updatedDate(true);
				]]></setter>
			</property>
			<property name="currentYear">
				<getter><![CDATA[
					return this.getAttribute("currentyear");
				]]></getter>
				<setter><![CDATA[
					if(isNaN(val)) val = new Date().getFullYear();
					this.setAttribute("currentyear", val);
                    this.updatedDate(true);
				]]></setter>
			</property>
			<property name="currentDay">
				<getter><![CDATA[
					return this.getAttribute("currentday");
				]]></getter>
				<setter><![CDATA[
					if(isNaN(val))
                        val = new Date().getDate();
					this.setAttribute("currentday", val);
                    this.updatedDate(true);
				]]></setter>
			</property>
			<method name="isSelected">
				<body><![CDATA[
					return this.getDate()!=null;
				]]></body>
			</method>
			<method name="getDate">
				<body><![CDATA[
					if(this.currentDay!=""&&this.currentMonth!=""&&this.currentYear!="") {
						return new Date(this.currentYear, this.currentMonth, this.currentDay, 0, 0, 0, 0, 0);
					}
					return null;
				]]></body>
			</method>
			<method name="setDate">
				<parameter name="date"/>
				<body><![CDATA[
					try {
                        this.setAttribute("currentday", date.getDate());
						this.setAttribute("currentmonth",  date.getMonth());
						this.setAttribute("currentyear", date.getFullYear());
					} catch(e) {}
                    this.updatedDate(true);
				]]></body>
			</method>
            <method name="updatedDate">
                <parameter name="doEvent"/>
				<body><![CDATA[
                    var today = new Date();
                    var y = this.getAttribute("currentyear")
                    if( y == today.getFullYear())
                        this.setAttribute("isyear", "true");
                    else
                        this.removeAttribute("isyear");
                    var m = this.getAttribute("currentmonth");
                    if(m>11) {
                        m = 0;
                        this.setAttribute("currentmonth", 0);
                        this.setAttribute("currentyear", ++y);
                    } else if(m<0) {
                         m=11;
                         this.setAttribute("currentmonth", 11);
                         this.setAttribute("currentyear", --y);
                    }
					
					if(m ==today.getMonth()) 
                        this.setAttribute("ismonth", true);
					else this.removeAttribute("ismonth");

					var date = new Date(y, m, 1, 0, 0, 0, 0);
					var rows = document.getAnonymousElementByAttribute(this ,"anonid", "datepicker-rows");
					var days = rows.getElementsByTagName("datepicker-day");
					for(var i=0; i<days.length; i++)
                         days[i].removeAttribute("value");
					var sem = 0, j =0, d = 0;
					while(m==date.getMonth()) {
						j = date.getDay(); // jour de la semaine; 0-6
						d = date.getDate() // jour du mois 1-31
						days[sem*7+j].setAttribute("value", d);
                        if( d == this.getAttribute('currentday'))
                            days[sem*7+j].setAttribute("selected", "true");
                        else
                            days[sem*7+j].removeAttribute("selected");
						if(j==6)
                            sem++;
						date.setDate(++d);
					}
                    
                    var idx = 1;
                    var fields = today.toLocaleString().split(" ");
                    if(isNaN(fields[2])) idx = 2;
                    var mlabel = document.getAnonymousElementByAttribute(this ,"anonid", "month");
                    mlabel.setAttribute("value", fields[idx].substring(0, 1).toUpperCase()+fields[idx].substring(1)); 
                    mlabel.value = fields[idx].substring(0, 1).toUpperCase()+fields[idx].substring(1); 

                    if(doEvent) {
                        if(this.parentNode.localName=="menupopup")
                            this.parentNode.hidePopup();
                        var evt = document.createEvent("HTMLEvents");
                        evt.initEvent("select", true, true);
                        this.dispatchEvent(evt);
                    }
				]]></body>
			</method>
		</implementation>

		<handlers>
			<handler event="select" phase="target"><![CDATA[
                var select = this.getAttribute("onselect");
                event.stopPropagation();
                event.preventDefault();
                if( select){
                    var func = new Function("event", select+"\n return true;");
                    if(!func.call(this, event))
                        return false;
                }
                return true;
			]]></handler>
			<handler event="click"><![CDATA[
			]]></handler>
		</handlers>
        <resources>
            <stylesheet src="datepicker.css"/>
        </resources>
	</binding>
    
    
	<binding id="datepicker-button">
		<content>
			<xul:image class="toolbarbutton-icon" xbl:inherits="validate,src=image,toolbarmode,buttonstyle"/>
		</content>
		<implementation>
			<constructor><![CDATA[
				this.setAttribute("id", "spinbutton-arrow"+Math.random());
				this.id=this.getAttribute("id");
			]]></constructor>
			<field name="_timer">null</field>
			<field name="_delay">400</field>
			<field name="id"></field>
			<method name="_fireEvent">
				<body><![CDATA[
				var evt = document.createEvent("MouseEvents");
				try {
					evt.initMouseEvent("mousedown", true, true, window, 1, 0, 0, 0, 0, false, false, false, false, 0, this);
					this.dispatchEvent(evt);
				} catch(e) {};
			]]></body>
			</method>
		</implementation>
		<handlers>
			<handler event="click"><![CDATA[
				this._delay = 400;
				clearTimeout(this._timer);
			]]></handler>
			<handler event="mouseout"><![CDATA[
				this._delay = 400;
				clearTimeout(this._timer);
			]]></handler>
			<handler event="mouseup"><![CDATA[
				this._delay = 400;
				clearTimeout(this._timer);
			]]></handler>
			<handler event="mousedown" button="0"><![CDATA[
				var evt = document.createEvent("MouseEvents");
				try {
					evt.initMouseEvent("action", true, true, window, 1, event.screenX, event.screenY, event.clientX, event.clientY, false, false, false, false, 0, this);
				} catch(e) {};
				this.dispatchEvent(evt);
				this._timer = setTimeout("document.getElementById('"+this.id+"')._fireEvent()", this._delay);
				if(this._delay>50) this._delay = this._delay*0.8;
			]]></handler>
			<handler event="action"><![CDATA[
				var command = this.getAttribute("oncommand");
				if(command!="") try {eval(command);} catch(e) {}
			]]></handler>
		</handlers>
	</binding>
</bindings>