function initialize_time_registration_viewlet($) {
    // Bindings for the widget.

    function refresh_callback() {
	// Shows the log list and makes it collapsible.
	$('#plonehrmTimeWidget .time_registration_log').show();
	$('#plonehrmTimeWidget .time_registration_log').collapsible({'min_elements': 0, 'elements': 'li'});
	
	// Makes the entries listing collapsible too.
	$('#plonehrmTimeWidget .time_registration_listing').collapsible({'min_elements': 5, 'elements': 'tbody tr'});

	// We provide the autocomplte values for the description.
	var autocomplete = $('#_autocomplete_time_description');
	if (autocomplete.length == 1) {
	    $('#time_description').autocomplete(eval(autocomplete.val()));
	}

	$(".date_input").datepicker(
	    {minDate: '-2Y',
	     maxDate: '+3Y',
	     closeText: 'X',
	     dateFormat: plonehrm_date_format
	    }
	);
    };

    function refresh_time_registration(e) {
	e.preventDefault();
	var data = url_to_dict($(this).attr('href'));
	$.pyproxy_call('jq_refresh_time_registration_viewlet',
		       data,
		       refresh_callback);
    };
    
    refresh_callback();

    $('#add_time_button, #credits_time_button, .time_registration_edit_link, .time_registration_delete_link').live('click',
														   refresh_time_registration);

    $('#submit-timeregistration').pyproxy('click',
					  'jq_save_time_registration_viewlet',
					  '#time_registration_form',
					  refresh_callback);
    
    $('#cancel-timeregistration').pyproxy('click',
					  'jq_cancel_time_registration_viewlet',
					  {},
					  refresh_callback);
}

function initialize_time_registration_overview($) {
    // Bindings for the worklocation overview

    function planning_updated() {
	$.tablefilter();
    }
    $('.time_registration_month_link').live('click',
					    function(e) {
						e.preventDefault();
						data = url_to_dict($(this).attr('href'));
						
						$.pyproxy_call('jq_refresh_time_overview_planning',
							       data,
							       planning_updated)
					    });
    $('#time_registration_month_switcher').pyproxy('click',
						   'jq_refresh_time_overview_planning',
						   '#time_registration_overview_planning_form',
						   planning_updated)
}

function initialize_time_registration_listing($) {
    // Binding for the employee's listing.
    var time_registration_overlay;

    function show_overlay(e) {
	// Makes a pyproxy_call to load the content of the overlay then shows it.
	var container = $('#time_registration_overlay');
	var data = url_to_dict($(this).attr('href'));
	e.preventDefault();
	overlay_opt = {
	    top: 272,
	    expose: { 
		color: '#FFFFFF', 
		loadSpeed: 200, 
		opacity: 0.4
	    },
	    
	    closeOnClick: false, 
	    api: true 
	};

	$.pyproxy_call('jq_refresh_time_registration_overview_viewlet',
		       data,
		       function() {
			   container.center(true);
			   $(".date_input").datepicker(
			       {minDate: '-2Y',
				maxDate: '+3Y',
				closeText: 'X',
				dateFormat: plonehrm_date_format
			       }
			   );
			   // Makes the entries listing collapsible too.
			   $('#plonehrmTimeWidget .time_registration_listing').collapsible({'min_elements': 5, 'elements': 'tbody tr'});

			   // We provide the autocomplete values for the description.
			   var autocomplete = $('#_autocomplete_time_description');
			   if (autocomplete.length == 1) {
			       $('#time_description').autocomplete(eval(autocomplete.val()));
			   }

			   time_registration_overlay = container.overlay(overlay_opt).load();
		       })
    }

    function check_saved() {
	// Checks if there is no error left before closing the overlay.
	if ($('.errormessage').length ==0 ) {
	    $('#employee_time_listing_content').load('time_listing_content');
	    hide_overlay();
	}
    }

    function hide_overlay() {
	time_registration_overlay.close();
    }

    $('.time_registration_edit_link, .time_registration_delete_link').live('click',
									   show_overlay);
    $('#submit-timeregistration').pyproxy('click',
					  'jq_save_time_registration_overview_viewlet',
					  '#time_registration_form',
					  check_saved);

    $('#cancel-timeregistration').live('click',
				       function(e) {
					   e.preventDefault();
					   hide_overlay();
				       });

}
