// Generated from /dynamic/event/proxy.phtml, DO NOT EDIT!
if(typeof(SmallCalendarController) == 'undefined')
    SmallCalendarController = new Object();

Object.extend(SmallCalendarController,
{
    _callback49217a215263c: "/dynamic/event/proxy.phtml",

    _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._callback49217a215263c, '_getSmallCalendar', arguments, false); },
    _getSmallCalendar_sync: function(){ return this._proxy(this._callback49217a215263c, '_getSmallCalendar', arguments, true); },

    _getMonthEvent: function(){ this._proxy(this._callback49217a215263c, '_getMonthEvent', arguments, false); },
    _getMonthEvent_sync: function(){ return this._proxy(this._callback49217a215263c, '_getMonthEvent', arguments, true); },

    _getDayEvent: function(){ this._proxy(this._callback49217a215263c, '_getDayEvent', arguments, false); },
    _getDayEvent_sync: function(){ return this._proxy(this._callback49217a215263c, '_getDayEvent', arguments, true); },

    _getModalEvents: function(){ this._proxy(this._callback49217a215263c, '_getModalEvents', arguments, false); },
    _getModalEvents_sync: function(){ return this._proxy(this._callback49217a215263c, '_getModalEvents', arguments, true); },

    _getSmallDate: function(){ this._proxy(this._callback49217a215263c, '_getSmallDate', arguments, false); },
    _getSmallDate_sync: function(){ return this._proxy(this._callback49217a215263c, '_getSmallDate', arguments, true); },

    _t: ''
});
// End of generated code

Object.extend(SmallCalendarController,{
	
	_modal: null,
	_root: null,
	_date: null,
	_context: null,
	
	changeDate: function(date)
	{
		this._getSmallCalendar(date, function(r) {
        	$('calendar').update(r.responseText);
            initCalendarTooltips();
        }.bind(this));
	},
	
	getDayEvent: function(e)
	{
		td = e.findElement('td');
		arId = td.id.split('_');
		day = arId[1] < 10 ? '0'+arId[1] : arId[1];
		if(this._modal)
		{
			this._getModalEvents($F('_date')+'-'+day, function(r, j)
        	{
        		j = this._getJSON(r, j);
	            if(!j || j.failed)
	                return;
	            modal = '';
	            j.out.each(function(event)
	            {
	            	event.location = event.location == '' ? 'no location' : event.location;
	            	event.name = event.name == null ? 'no categorie' : event.name;
	            	event.description = event.description == '' ? 'no description' : event.description;
		        	var eventValue = {id: event.id, color: event.color, title: event.title, startDate: event.startDate, stopDate: event.stopDate, repeat: event.repeat, location: event.location, name: event.name, description: event.description};
					tmp = '<div id="detail">'+
								'<h3 style="color:##{color}"><a href="#" onclick="SmallCalendarController.slide(#{id});return false;">+</a>#{title}</h3>'+
								'<h4><span>Du #{startDate} au #{stopDate}</span></h4>'+
								'<div id="detail_#{id}" class="eventDetail" style="display:none">'+
									'<h4><span>Répétition: #{repeat}</span></h4>'+
									'<h4>Location: #{location}</h4>'+
									'<h4>Categorie: #{name}</h4>'+
									'<h5>Description:</h5>'+
									'<p>#{description}</p>'+
								'</div>'+
							'</div>';
					tmp = tmp.interpolate(eventValue);
					modal += tmp;
				});
				Modalbox.show(modal,
				{
					title: 'Detail of selected events'
				});
			}.bind(this));
					
		}
		else
		{
			var eventValue = {day: day};
			this._root = this._context+e.target.id;
			document.location = this._root.interpolate(eventValue);
		}
	},
	
	slide: function(id)
	{	
		detail = $('detail_'+id);
 		if(detail.visible())
 			return;
 		var lastHeight=0;
 		$$('.eventDetail').each(function(e){
	 		if(e.visible())
	 			lastHeight=e.getHeight();
	 			e.hide();
	 	});
 		var newHeight=0;
		newHeight = detail.getHeight()-lastHeight;
		Modalbox.options.afterResize = function(e){detail.show();Modalbox.options.afterResize=null};
		Modalbox.resize(0,newHeight);
	}
});
