// Generated from //dynamic/visit/proxy.phtml?showstub=true&objectname=VisitController, DO NOT EDIT!
if(typeof(VisitController) == 'undefined')
    VisitController = new Object();

Object.extend(VisitController,
{
    _callback4a4b77205ab76: "/dynamic/visit/proxy.phtml",

	_datecount : 0,

    _getJSON: function(r, j)
    {
        j = (j || r.headerJSON || r.responseJSON);
        if(j == null)
            return null;
        if(j.errors || j.failed)
        {
            var f = function(a, c)
            {
                var e = $(c);
                if(a.length > 0)
                {
                    var s = '<ul>';
                    $(a).each(function(e){ s += '<li>'+e+'</li>'; });
                    s += '</ul>';
                    if(e){ e.update(s); e.show(); }
                }
                else
                {
                    if(e){ e.hide(); }
                }
            }
            if(!j.errors)
            {
                j.errors = {error: [], warning: [], info: []}
                if(j.failed)
                    j.errors.error[0] = j.message;
            }

            f(j.errors.error, 'globuleError');
            f(j.errors.warning, 'globuleWarning');
            f(j.errors.info, 'globuleInfo');
        }
        return j;
    },

    _proxy: function(callbackurl, methodName, args, sync)
    {
        var targs = $H();
        for(var i = 0; i < args.length-1; i++){ targs.set('args[' + i + ']', args[i]); }
        callback = args[args.length-1];
        var ar = new Ajax.Request(callbackurl + '?method=' + methodName,
        {
            method: 'post',
            asynchronous: (!sync),
            parameters: targs,
            onComplete: (sync)?null:callback,
            onException: function(request, e){console.debug(request, e);}
        });
        if(sync)
            return new Ajax.Response(ar);
    },

    _getSmallCalendar: function(){ this._proxy(this._callback4a4b77205ab76, '_getSmallCalendar', arguments, false); },
    _getSmallCalendar_sync: function(){ return this._proxy(this._callback4a4b77205ab76, '_getSmallCalendar', arguments, true); },

    _t: ''
});
// End of generated code

if(typeof(VisitController) == 'undefined')
    VisitController = new Object();

Object.extend(VisitController,
{
	_maxReservation:3,
	_calendars: $H(),
    
    initReservationSmallCalendar: function(id, path, scalvar)
	{
        var c = 
        {
            path: path,
            scalvar: scalvar,
            listeners: $A()
        };
        
        
        var pl = $(id).getElementsByClassName('calendar-previous-month');
        if(pl[0].tagName.toUpperCase() == 'A')
            c.listeners.push($(pl[0]).observe('click', this.gotoDate.bindAsEventListener(this, id)));
        var nl = $(id).getElementsByClassName('calendar-next-month');
        if(nl[0].tagName.toUpperCase() == 'A')
            c.listeners.push($(nl[0]).observe('click', this.gotoDate.bindAsEventListener(this, id)));

        this._calendars.set(id, c);
	},
	
	gotoDate: function(e, id)
	{
	    e.stop();
	    var h = $H(e.element().href.toQueryParams());
	    var c = this._calendars.get(id);
	    c.listeners.invoke('stopObserving', 'click');
	    var d = h.get(c.scalvar);
	    this._getSmallCalendar(d, c.path, c.scalvar, function(id, c, r){
	        $(id).update(r.responseText);
	        this.initReservationSmallCalendar(id, c.path, c.scalvar);
	    }.bind(this, id, c));
	},	
	selectDate: function (date,time)
	{
		if($$('.date').length < this._maxReservation)
		{
			var visitDay=new Date();
			visitDay.setTime(date*1000);
			var template = new Template("<span class='date'><input type='hidden' name='formdata[date][]' value='#{date}' />#{date} <a href='#' onclick='VisitController.deleteDate(this);return false;' ><span>[X]</span></a></span>");  
			var hour='08:30 - 12:30';
			if(time=="PM")
				var hour='13:30 - 17:00';
				
				
			  if (Prototype.Browser.IE) 
		      {
		          var valeurs= {date: visitDay.getDate()+'-'+(visitDay.getMonth()+1)+'-'+(visitDay.getYear())+' '+hour};  
		      }
		      else
		      {
		          var valeurs= {date: visitDay.getDate()+'-'+(visitDay.getMonth()+1)+'-'+(visitDay.getYear()+1900)+' '+hour};  
		      }	
			
			this._datecount++;
			$('selectedDatesIntro').show();
			$('dateCount').value = this._datecount;
			$('selectedDates').insert(template .evaluate(valeurs));
		}			
	},
	deleteDate : function(elem)
	{
		$(elem).up(0).remove();
		this._datecount--;
		if(this._datecount == 0){
			$('selectedDatesIntro').hide();
			$('dateCount').value = '';
		}
	}
	
});
