   /*********************************************************/
  /*   Calendar version 1.00 for prototype/script.aculo.us */
 /*   by Mumualex - http://www.mumualex.fr  2008/10/08    */
/*********************************************************/

/*

	@require prototype.js > 1.6.0
	@require scriptaculous.js > 1.8.1
		with 	builder.js
				dragdrop.js
				effects.js

    Calendar Class Declaration
    - initialize()
    - initializeCalendarFunctions()
    - u()
    - slideLeft()
	- slideRight()
	- fade()
	- switchLoaders()
	- resetArrows()
	- hideLeftArrow()
	- hideRightArrow()

    CalendarPicker Class Declaration
    - initialize()
    - position()
    - show()
    - hide()
	- toggle()
	- pick()
	- getInputDate()
	- linkedUpdate()
	- outsideClick()
	- isNumber()
*/

//check prototype & scriptaculous
var convertVersionString = function(versionString){
  var r = versionString.split('.');
  return parseInt(r[0])*100000 + parseInt(r[1])*1000 + parseInt(r[2]);
};

if((typeof Prototype=='undefined') ||
   (typeof Effect == 'undefined') ||
   (typeof Element == 'undefined') ||
   (typeof Element.Methods=='undefined') ||
   (convertVersionString(Prototype.Version) <
    convertVersionString('1.6.0')) ||
   (convertVersionString(Scriptaculous.Version) <
    convertVersionString('1.8.1')))
   throw("calendar.js requires the Prototype JavaScript framework >= 1.6.0 and script.aculo.us 1.8.1 extended framework (with builder, dragdrop, effects)");

Object.extend(Event, {
	wheel:function (event){
		var delta = 0;
		if (!event) event = window.event;
		if (event.wheelDelta) {
			delta = event.wheelDelta/120;
			if (window.opera) delta = -delta;
		} else if (event.detail) { delta = -event.detail/3;     }
		return Math.round(delta); //Safari Round
	}
});

var hCalendar = Class.create({

	initialize: function(_container, _options) {

		// options loading
		this.options = Object.extend({
			slideDuration: 		.5,
			fadeDuration: 		.5,
			startMonday: 		true,
			filePath: 			'inc/hcalendar/',
			addItemFile: 		'add.php',
			modItemFile: 		'mod.php',
			scriptParam: 		'',
			defaultView: 		'month',
			style: 				'',	//example: adobe_cs3, apple_widget
			wheel:				true,
			opacity:			.9
		}, _options || {});

		this.loading = false;
		this.container = _container = $(_container);
		this.width = this.container.getWidth();
		if(this.width == 0) {this.container.setStyle({width:700+"px"}); this.width = this.container.getWidth();}

		var _class = this;

		//Insert the base into the container and initialize elements
		var  pars = 'defaultView='+ this.options.defaultView;
		if(this.picker) {
			if((this.options.prefillDate) && this.getInputDate(this.options.prefillDate)) pars += '&pickedDate='+ this.getInputDate(this.options.prefillDate);
			if(this.options.linkWithInput) pars += '&gotoPickedDate=1';
		}
		this.u('base', pars, function() {
			_class.mainLoader 		=  _container.select('div.loaderA')[0];
			_class.tempLoader 		=  _container.select('div.loaderB')[0];
			_class.label 			=  _container.select('span.label')[0];
			_class.today 			=  _container.select('div.today')[0];
			_class.arrowLeft 		=  _container.select('div.arrowLeft')[0];
			_class.arrowRight 		=  _container.select('div.arrowRight')[0];
			_class.hBubble	 		=  _container.select('div.hBubble')[0];
			_class.hBubblePointer	=  _container.select('div.hBubble div.pointer')[0];
			_class.hBubbleContent	=  _container.select('div.hBubble div.middle')[0];
			_class.hBubbleClose		=  _container.select('div.hBubble div.close')[0];
			_class.initializeCalendarFunctions();

			_class.mainLoader.setStyle({width:_class.width + "px"});
			_class.tempLoader.setStyle({width:_class.width + "px"});
			(_container.select('div.container')[0]).setStyle({width:_class.width + "px"});
			_class.label.setStyle({width:(_class.width - 239) + "px"});

			//Prefill/load picker date elements
			if(_class.picker) {
				if(_class.options.prefillDate && _class.getInputDate(_class.options.prefillDate)) _class.pick(_class.options.prefillDate);
				//else if(_class.options.prefillDate == false) _class.pick((_class.label.readAttribute('date')).evalJSON());
			}
		}, _container);
	},

	initializeCalendarFunctions: function() {
		this.resetArrows();

		//Retrieve data (label, timestamp etc) which are stored as a Json string in the table attribute summary
		var vars =  (this.mainLoader.getElementsBySelector('table')[0].readAttribute('summary')).evalJSON();

		var _class = this;

		Effect.Fade(_class.hBubble, { duration: .5 });

		//Change the label
		this.label.removeClassName('noHover');
		this.label.update(vars.label);

		this.label.onclick = function(e){
			if(vars.parent) _class.u(vars.parent, 'ts=' + vars.ts + '&parent=' + vars.current, function() { _class.fade() });
		};
		//Hide arrows if necessary and add arrow click events
		if(vars.hide_left_arrow) _class.hideLeftArrow();
		else if(vars.hide_right_arrow) _class.hideRightArrow();

		this.today.onclick =  function(){_class.u(vars.current, '', function() { _class.fade() }) };
		this.arrowLeft.onclick =  function(){_class.u(vars.current, 'ts=' + vars.pr_ts, function() { _class.slideLeft() }) };
		this.arrowRight.onclick = function(){_class.u(vars.current, 'ts=' + vars.nx_ts, function() { _class.slideRight() }) };
		this.hBubbleClose.onclick = function(){Effect.Fade(_class.hBubble, { duration: .5 });};

		if(this.options.wheel){
			var fctWheelCalendar = function(event){
				var vars =  (_class.mainLoader.getElementsBySelector('table')[0].readAttribute('summary')).evalJSON();
				if(Event.wheel(event) < 0)
					_class.u(vars.current, 'ts=' + vars.pr_ts, function() { _class.slideLeft() });
				else
					_class.u(vars.current, 'ts=' + vars.nx_ts, function() { _class.slideRight() })
				event.stop();
				return false;
			};
			Event.observe(this.label, "mousewheel", fctWheelCalendar, false);
			Event.observe(this.label, "DOMMouseScroll", fctWheelCalendar, false); // Firefox
		}

		var clickables_rdv = this.mainLoader.select('div.rdv');
		var clickables_add = this.mainLoader.select('div.add');
/*			_class.hBubble	 		=  _container.select('div.hBubble')[0];
			_class.hBubblePointer	=  _container.select('div.hBubble div.pointer')[0];
			_class.hBubbleContent	=  _container.select('div.hBubble div.middle')[0];
			_class.hBubbleClose		=  _container.select('div.hBubble div.close')[0];
*/
		switch(vars.current) {
			case 'month':
				clickables_rdv.each(function(_clickable) {
					_clickable.onclick = function(){
						date = (_clickable.readAttribute('date')).evalJSON();

						if(_clickable.hasClassName('col6')
							 || _clickable.hasClassName('col7')){
							_class.hBubblePointer.removeClassName('p_left')
								.removeClassName('p_right')
								.removeClassName('p_bottom')
								.removeClassName('p_top')
								.addClassName('p_right');
							Element.clonePosition(
								_class.hBubble,
								_clickable,{
									setWidth:false,
									setHeight:false,
									offsetLeft:-248,
									offsetTop:-58
								}
							);
						} else {
							_class.hBubblePointer.removeClassName('p_left')
								.removeClassName('p_right')
								.removeClassName('p_bottom')
								.removeClassName('p_top')
								.addClassName('p_left');
							Element.clonePosition(
								_class.hBubble,
								_clickable,{
									setWidth:false,
									setHeight:false,
									offsetLeft:(((_class.width - 3)/7) - 25),
									offsetTop:-58
								}
							);
						}
						_class.hBubbleContent.update('');
						new Ajax.Updater(
							_class.hBubbleContent,
							_class.options.modItemFile, 	{
								method:'post',
								asynchronous:true,
								parameters:"date="+date.date+"&ts="+date.ts+"&rdv="+date.id+"&"+_class.options.scriptParam,
								evalScripts:true
							}
						);
						if(!Prototype.Browser.IE)$('hBubble').appear(); else $('hBubble').show();
					};
				});
				clickables_add.each(function(_clickable) {
					_clickable.onclick = function(){
						date = (_clickable.readAttribute('date')).evalJSON();

						if(_clickable.hasClassName('col5')
							 || _clickable.hasClassName('col6')
							 || _clickable.hasClassName('col7')){
							_class.hBubblePointer.removeClassName('p_left')
								.removeClassName('p_right')
								.removeClassName('p_bottom')
								.removeClassName('p_top')
								.addClassName('p_right');
							Element.clonePosition(
								_class.hBubble,
								_clickable,{
									setWidth:false,
									setHeight:false,
									offsetLeft:-248,
									offsetTop:-54
								}
							);
						} else {
							_class.hBubblePointer.removeClassName('p_left')
								.removeClassName('p_right')
								.removeClassName('p_bottom')
								.removeClassName('p_top')
								.addClassName('p_left');
							Element.clonePosition(
								_class.hBubble,
								_clickable,{
									setWidth:false,
									setHeight:false,
									offsetLeft:75,
									offsetTop:-58
								}
							);
						}
						_class.hBubbleContent.update('');
						new Ajax.Updater(
							_class.hBubbleContent,
							_class.options.addItemFile, 	{
								method:'post',
								asynchronous:true,
								parameters:"date="+date.date+"&ts="+date.ts+"&"+_class.options.scriptParam,
								evalScripts:true
							}
						);

						if(!Prototype.Browser.IE)$('hBubble').appear(); else $('hBubble').show();
					};
				});
			break;
		}

		//Add cell click events
		var clickables = this.mainLoader.select('td.clickable');
		var tables = this.mainLoader.select('table');
		var miniTables = this.mainLoader.select('table.lstMinimonth table');
		switch(vars.current) {
			case 'month':
				if(this.picker) {
					clickables.each(function(_clickable) {
						_clickable.onclick = function(){
							_class.pick((_clickable.readAttribute('date')).evalJSON());
							_class.mainLoader.getElementsBySelector('td').each(function(_clickable) { _clickable.removeClassName('selected') });
							this.addClassName('selected');
						};
					});
				} else {
					miniTables[0].onclick =  function(){_class.u(vars.current, 'ts=' + vars.pr_ts, function() { _class.fade() }) };
					miniTables[2].onclick =  function(){_class.u(vars.current, 'ts=' + vars.nx_ts, function() { _class.fade() }) };
					miniTables[3].onclick =  function(){_class.u(vars.current, 'ts=' + vars.nx2_ts, function() { _class.fade() }) };
				}
				break;
			case 'year':
				clickables.each(function(_clickable) {
					_clickable.onclick = function(){
						_class.u('month', 'ts=' + _clickable.readAttribute('ts'), function() { _class.fade() })
					};
				});
				break;
			case 'decade':
				this.label.addClassName('noHover');
				clickables.each(function(_clickable) {
					_clickable.onclick = function(){
						_class.u('year', 'ts=' + _clickable.readAttribute('ts') + '&m_ts=' + _clickable.readAttribute('m_ts'), function() { _class.fade() })
					};
				});
				break;
		}/*
		$$('.hCalendar td .rdv').each(function(item){
			item.onmouseover = this.style.zIndex = 15;
			item.onmouseout = this.style.zIndex = 10;
		});*/
	},

	//Ajax updater function which handles all requests
	u: function(_url, _pars, _onComplete, _id) {
		if(!this.loading && !this.transitioning) {
			var _class = this;
			this.loading = true;
			var element = $(_id ? _id : this.tempLoader);
			_pars += '&picker=' + (this.picker ? 1 : 0) + '&startMonday=' + (this.options.startMonday ? 1 : 0) + '&style=' +  this.options.style;
			if(this.picker && this.getInputDate()) _pars += '&pickedDate='+ this.getInputDate();

			new Ajax.Request(this.options.filePath + _url + '.php', 	{
				method:'post',
				asynchronous:true,
				parameters:_pars+"&"+_class.options.scriptParam,
				onSuccess: function(t) {
					if(t.responseText != ""){
						element.update(t.responseText);
						_class.container.select('div.container table.month, div.container table.year').each(function(item){item.setStyle({width:(_class.width - 3) + "px"});});
						_class.container.select('div.container table.month td .lib').each(function(item){item.setStyle({width:(((_class.width - 3)/7) - 6) + "px"});});
						_class.container.select('div.container table.month td .rdv').each(function(item){
							item.setStyle({width:(((_class.width - 3)/7) - 7) + "px"});
							item.onmouseover = function(){if(item.getWidth() < 200) item.setStyle({width:200+'px', height:'auto', zIndex:5});(item.ancestors()[0]).setStyle({zIndex:5});}
							item.onmouseout = function(){item.setStyle({width:(((_class.width - 3)/7) - 7) + "px", height:'16px', zIndex:0});(item.ancestors()[0]).setStyle({zIndex:0});}
						});
					} else {
						// alert("Unable to load calendar");
					}
					_onComplete();
					_class.loading = false;
				},
				evalScripts:true
			});
		}
	},
	slideLeft: function() {
		if(this.arrowLeft.getStyle('visibility') != "hidden"){
			var _class = this;
			_class.transitioning = true;

			this.tempLoader.setStyle({position:'absolute', left:-(this.container.getWidth())+"px", opacity : 1});

			_class.tempLoader.setStyle({display:'block'});
			_class.mainLoader.setStyle({display:'block'});

			new Effect.Move(this.tempLoader, { x: 0, y: 0, mode: 'absolute' });

			this.mainLoader.setStyle({position:'absolute', left:0+"px", opacity : 1});
			new Effect.Move(this.mainLoader, { x: (this.container.getWidth()), y: 0, mode: 'absolute', afterFinish:function(){
				_class.transitioning = false;
				_class.tempLoader.setStyle({display:'none', left: 0+"px"});
				_class.mainLoader.setStyle({left: 0+"px", display:'block', opacity : 1});
			} });

			this.switchLoaders();
		}
	},
	slideRight: function() {
		if(this.arrowRight.getStyle('visibility') != "hidden"){
			var _class = this;
			_class.transitioning = true;

			this.mainLoader.setStyle({position:'absolute', left:0+"px", opacity : 1});

			_class.tempLoader.setStyle({display:'block'});
			_class.mainLoader.setStyle({display:'block'});

			new Effect.Move(this.mainLoader, { x: -(this.container.getWidth()), y: 0, mode: 'absolute' });

			this.tempLoader.setStyle({position:'absolute', left:(this.container.getWidth())+"px", opacity : 1});
			new Effect.Move(this.tempLoader, { x: 0, y: 0, mode: 'absolute', afterFinish:function(){
				_class.transitioning = false;
				_class.tempLoader.setStyle({display:'none', left: 0+"px"});
				_class.mainLoader.setStyle({left: 0+"px", display:'block', opacity : 1});
			} });

			this.switchLoaders();
		}
	},
	fade: function(overRuleTrans) {
		var _class = this;
		this.transitioning = overRuleTrans ? false : true;

		new Effect.Fade(this.mainLoader, {
			from:this.options.opacity,
			duration:this.options.fadeDuration,
			afterFinish:function(){}
		});

		new Effect.Appear(this.tempLoader, {
			to:this.options.opacity,
			duration:this.options.fadeDuration,
			afterFinish:function(){
				_class.transitioning = false;
				_class.mainLoader.setStyle({display:'block', opacity : 1});
			}
		});

		this.switchLoaders();
	},

	switchLoaders: function() {
		this.mainLoader = this.mainLoader.className == 'loaderA' ? this.container.getElementsBySelector('div[class=loaderB]')[0] : this.container.getElementsBySelector('div[class=loaderA]')[0];
		this.tempLoader = this.tempLoader.className == 'loaderA' ? this.container.getElementsBySelector('div[class=loaderB]')[0] : this.container.getElementsBySelector('div[class=loaderA]')[0];
		this.initializeCalendarFunctions();
	},

	resetArrows: function() {
		this.arrowLeft.setStyle({visibility : 'visible'});
		this.arrowRight.setStyle({visibility : 'visible'});
	},

	hideLeftArrow: function() {
		this.arrowLeft.setStyle({visibility : 'hidden'});
	},

	hideRightArrow: function() {
		this.arrowRight.setStyle({visibility : 'hidden'});
	}
});

var map, myCal, point, marker, icon;

// GMAP
function initializeGMAP(id, iLat, iLng) {
	if (GBrowserIsCompatible()) {
		map = new GMap2($(id));
		map.setCenter(new GLatLng(47.15984, 2.988281), 5);
		geocoder = new GClientGeocoder();
		map.addMapType(G_PHYSICAL_MAP);
		map.addControl(new GSmallMapControl());
		map.enableDoubleClickZoom();
		map.enableContinuousZoom();
		new GKeyboardHandler(map);

		icon = new GIcon();
		icon.image = "img/lightblue.png";
       	icon.shadow= "img/mm_20_shadow.png";
		icon.iconSize = new GSize(12, 20);
		icon.shadowSize = new GSize(22, 20);
		icon.iconAnchor = new GPoint(6, 20);
		icon.infoWindowAnchor = new GPoint(8, 1);

		if(iLat.value != "" && iLng.value != ""){
			point = new GLatLng(iLat.value, iLng.value);
			marker = new GMarker(point,{icon:icon});
	    	map.addOverlay(marker);
	    	map.setCenter(point, 16);
  		} else $('mapContainer').hide();
	}
}