/**
* Input field qui ouvre un calendrier pour sélectionner la date
* Code basé sur l'exemple : YUI: Calendar attached to Text Input (YUI Version .12)
* http://blog.davglass.com/
* Par Dav Glass
*
* Modifié par Tommy Brière pour l'adapter au a webtitan et plus de généralité (possibité d'en mettre plusieurs dans une seule page)
**/
YAHOO.namespace("webtitan.calendrier");
YAHOO.webtitan.calendrier.CalendrierInput= function(tableId, callContainerId, inputId, onChange){
this.deltaX= 0;
this.deltaY= 20;
this.call= null;
this.over_cal= false;
this.inputId= inputId;
this.callContainerId= callContainerId;
this.tableId= tableId;
this.onChange= onChange;
YAHOO.webtitan.calendrier.calendrierInput= this;
this.init();}
YAHOO.webtitan.calendrier.CalendrierInput.prototype.init= function(){
this.cal1= new YAHOO.widget.Calendar(this.tableId,this.callContainerId);
YAHOO.webtitan.calendrier.localize(this.cal1);
this.cal1.selectEvent.subscribe(this.readDate, this, true);
this.cal1.renderEvent.subscribe(this.setupListeners, this, true);
YAHOO.util.Event.addListener(this.inputId,'focus' , this.showCal, this, true);
YAHOO.util.Event.addListener(this.inputId,'blur' , this.hideCal, this, true);
this.cal1.render();
this.hideCal();}
YAHOO.webtitan.calendrier.CalendrierInput.prototype.setupListeners= function(){
var ci= this;
YAHOO.util.Event.addListener(ci.callContainerId,'mouseover' , ci.overCal, this, true);
YAHOO.util.Event.addListener(ci.callContainerId,'mouseout' , ci.outCal, this, true);}
YAHOO.webtitan.calendrier.CalendrierInput.prototype.readDate= function(){
var ci= this;
var calDate= ci.cal1.getSelectedDates()[0];
var m= calDate.getMonth()+1;
var d= calDate.getDate();
m= m.toString();
d= d.toString();
if(m.length==1){
m="0"+m;}
if(d.length==1){
d="0"+d;}
calDate= calDate.getFullYear()+"-"+ m+"-"+ d;
YAHOO.util.Dom.get(ci.inputId).value= calDate;
ci.over_cal= false;
ci.hideCal();
if(ci.onChange){
ci.onChange();}}

YAHOO.webtitan.calendrier.parseDate= function(str){
var s1= str.indexOf("-");
if(s1>0){
var s2= str.indexOf("-", s1+1);
if(s2>s1){
var y= str.substr(0, s1);
var m= str.substr(s1+1, s2-s1-1);
var d= str.substr(s2+1);

var jour= new Date(y, m-1, d);
if((jour!=null)&&(! isNaN(jour.getDate()) ) ){
return jour;}}}
return null;}
YAHOO.webtitan.calendrier.CalendrierInput.prototype.showCal= function(){
var ci= this;
var xy= YAHOO.util.Dom.getXY(ci.inputId);
var dateStr= YAHOO.util.Dom.get(ci.inputId).value;
var date= YAHOO.webtitan.calendrier.parseDate(dateStr);
if(date!=null){
ci.cal1.setYear( date.getFullYear());
ci.cal1.setMonth( date.getMonth() );
ci.cal1.select(date);
ci.cal1.render();}
YAHOO.util.Dom.setStyle(ci.callContainerId,'display' ,'block' );
xy[0]+= ci.deltaX;
xy[1]+= ci.deltaY;
YAHOO.util.Dom.setXY(ci.callContainerId, xy);}
YAHOO.webtitan.calendrier.CalendrierInput.prototype.hideCal= function(){
var ci= this;
if(!ci.over_cal){
YAHOO.util.Dom.setStyle(ci.callContainerId,'display' ,'none' );} else{
setTimeout(function(){
YAHOO.util.Dom.setStyle(ci.callContainerId,'display' ,'none' );}, 1000)}}
YAHOO.webtitan.calendrier.CalendrierInput.prototype.overCal= function(){
var ci= this;
ci.over_cal= true;}
YAHOO.webtitan.calendrier.CalendrierInput.prototype.outCal= function(){
var ci= this;
ci.over_cal= false;}
