if(!YAHOO.Mevia){ YAHOO.namespace("Mevia"); }

YAHOO.Mevia.Calendar = function(fieldname,contid,attach_to,value){
	var yud = YAHOO.util.Dom;
	var yue = YAHOO.util.Event;
	
	this.field = yud.get(fieldname)
	this.attach_to = attach_to
	this.config = {
			close:true,
			navigator:true
	};
	this.showtime = false;
	this.selectedDate = []
	
	this.calendar = new YAHOO.widget.Calendar( "calendar_"+fieldname, contid, this.config);
	
	this.calendar.cfg.setProperty("LOCALE_WEEKDAYS", "short");
	this.calendar.cfg.setProperty("START_WEEKDAY", 1);
	this.calendar.cfg.setProperty("MULTI_SELECT", false);
	
	this.calendar.cfg.setProperty("DATE_FIELD_DELIMITER", "/");
	this.calendar.cfg.setProperty("MDY_DAY_POSITION", 1);
	this.calendar.cfg.setProperty("MDY_MONTH_POSITION", 2);
	this.calendar.cfg.setProperty("MDY_YEAR_POSITION", 3);
	this.calendar.cfg.setProperty("MD_DAY_POSITION", 1);
	this.calendar.cfg.setProperty("MD_MONTH_POSITION", 2);
	
	this.calendar.cfg.setProperty("MONTHS_SHORT",   ["Janv", "Févr", "Mars", "Avr", "Mai", "Juin", "Juil", "Août", "Sept", "Oct", "Nov", "Déc"]);
	this.calendar.cfg.setProperty("MONTHS_LONG",    ["Janvier", "Février", "Mars", "Avril", "Mai", "Juin", "Juillet", "Août", "Septembre", "Octobre", "Novembre", "Décembre"]);
	this.calendar.cfg.setProperty("WEEKDAYS_1CHAR", ["D", "L", "M", "M", "J", "V", "S"]);
	this.calendar.cfg.setProperty("WEEKDAYS_SHORT", ["Di", "Lu", "Ma", "Me", "Je", "Ve", "Sa"]);
	this.calendar.cfg.setProperty("WEEKDAYS_MEDIUM",["Dim", "Lun", "Mar", "Mer", "Jeu", "Ven", "Sam"]);
	this.calendar.cfg.setProperty("WEEKDAYS_LONG",  ["Dimanche", "Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi", "Samedi"]);
	
	YAHOO.widget.CalendarNavigator._DEFAULT_CFG.strings.month = "Mois : ";
	YAHOO.widget.CalendarNavigator._DEFAULT_CFG.strings.year = "Année : ";
	YAHOO.widget.CalendarNavigator._DEFAULT_CFG.strings.submit= "Ok";
	YAHOO.widget.CalendarNavigator._DEFAULT_CFG.strings.cancel= "Annuler";
	YAHOO.widget.CalendarNavigator._DEFAULT_CFG.strings.invalidYear = "L'année doit être un nombre";
	
	this.render = Calendar_Render;
	this.handleSelect = Calendar_HandleSelect;
	this.renderTime = Calendar_renderTime;
	this.turnTimeOn = Calendar_TurnTimeOn
	
	if(this.attach_to){
		yue.addListener(attach_to,"click",function(e){
			var showBtn = yud.get(attach_to);
			this.show();
			yue.on(document, "click", function(e) {
				var el = yue.getTarget(e);
				var dialogEl = this.oDomContainer;
				if (el != dialogEl && !yud.isAncestor(dialogEl, el) && el != showBtn && !yud.isAncestor(showBtn, el)) {
					this.hide();
				}
			},this,true);
		},this.calendar,true)
		// yue.addListener(attach_to,"click",this.calendar.show,this.calendar,true);
	}
	this.calendar.selectEvent.subscribe(this.handleSelect,this,true);
};

Calendar_HandleSelect = function(type,args,obj){
	var dates = args[0];
	var date = dates[0];
	var year = date[0], month = date[1], day = date[2];

	if (month.toString().length < 2) {
		month = "0" + month;
	};

	if (day.toString().length < 2) {
		day = "0" + day;
	};
	
	if(!this.showtime){		
		// this.field.value = year + "-" + month + "-" + day
		this.field.value = day + "-" + month + "-" + year
		this.calendar.hide()
	}else{
		this.selectedDate['year'] = year;
		this.selectedDate['month'] = month;
		this.selectedDate['day'] = day;
	}
};

Calendar_renderTime = function(){
	var container = this.calendar.oDomContainer;
	var hselect = document.createElement('select');
	hselect.id = this.field.id+"_h";
	hselect.name = this.field.id+"_h";

	var hour_option = document.createElement('option');
	hour_option.value = "";
	hour_option.text = "heure";
	hselect.options[0] = hour_option;
	
	
	for(var i=0;i<24;i++){
		var option = document.createElement('option');
		option.value = option.text = i;
		hselect.options[i+1] = option;
	}

	var mselect = document.createElement('select');
	mselect.id = this.field.id+"_m";
	mselect.name = this.field.id+"_m";
	
	var min_option = document.createElement('option');
	min_option.value = "";
	min_option.text = "minute";
	mselect.options[0] = min_option;
	
	for(var i=0;i<60;i+=10){
		var option = document.createElement('option');
		option.value = option.text = i;
		mselect.options[parseInt(i/10)+1] = option;
	}
	
	var ok = document.createElement('button');	
	ok.innerHTML = "ok";
	container.appendChild(hselect);
	container.appendChild(mselect);
	container.appendChild(ok);
	
	var dates = this.calendar.getSelectedDates();
	var date = (dates.length>0)? dates[0]:new Date();
	hselect.selectedIndex = parseInt(date.getHours()) + 1;
	mselect.selectedIndex = (parseInt(date.getMinutes()) / 10) + 1;

	YAHOO.util.Event.addListener(ok,"click",function(){
		var dates = this.calendar.getSelectedDates();
		var date;
		if(dates.length>0){
			date = dates[0];			
		}else{
			date = new Date();
		}
		this.field.value = date.getDate() + "-" + parseInt(date.getMonth()+1) + "-" + date.getFullYear();
		this.field.value += " "+hselect.value+":"+mselect.value;
		// this.field.value = date.getFullYear() + "-" + parseInt(date.getMonth()+1) + "-" + date.getDate();
		// this.field.value += " "+hselect.value+":"+mselect.value;
		this.calendar.hide()
	},this,true)
}

Calendar_TurnTimeOn = function(){
	this.showtime = true;
}

Calendar_Render = function(showtime){
	this.calendar.render();
	if(showtime){
		this.turnTimeOn()
		this.renderTime();
		// this.calendar.changePageEvent.subscribe(this.renderTime,this,true);
		this.calendar.renderEvent.subscribe(this.renderTime,this,true);
	}
};
