/* All Jquery actions called by the vacation viewlet or vacation overview */

var vacation_overlay;

// Fetches the list of vacation types and activates auto-complete.
make_auto_complete = function(){
    jq.getJSON(window.location + '/vacation_type_autocompletion',
 	       function(data) {
		   jq('#vacation_description').autocomplete(data);
 	       });
};

/* Callback function used when updating the worklocation overview.
   If just process data and then hides again the departments that were
   not delected.*/
update_vacation_overview_callback = function() {
    jq.tablefilter();
};

/* Removes the overlay onces the user clicked on a button
   in vacaton history.
*/
update_history_callback = function() {
    vacation_overlay.close();
};

// Bindings for the vacation widget.
initialize_vacation_viewlet = function($) {
    $('#manage_vacation_units_button').pyproxy('click',
					       'jq_units_vacation_form');
    $('#cancel_vacation_form').pyproxy('click',
				       'jq_list_vacations');

    $('#add_vacation_submit').pyproxy('click',
				      'jq_add_vacation',
				      '#vacation_add_form');

    $('#edit_vacation_submit').pyproxy('click',
				       'jq_edit_vacation',
				       '#vacation_add_form');

    $('delete_vacation_submit').pyproxy('click',
					'jq_delete_vacation',
					'#vacation_delete_form');

    $('#save_units_submit').pyproxy('click',
				    'jq_save_vacation_units',
				    '#vacation_units_form');

    $('#add_vacation_button').pyproxy('click',
				      'jq_add_vacation_form',
				      '',
				      make_auto_complete);

    $('.vacation_edit_link').live('click',
				  function() {
				      vac_id = $(this).attr('rel');
				      $.pyproxy_call('jq_edit_vacation_form',
						     {'vacation_id': vac_id});
				      return false;
				  }
				  );

    $('.vacation_delete_link').live('click',
				     function() {
					 vac_id = $(this).attr('rel');
					 $.pyproxy_call('jq_delete_vacation_form',
							{'vacation_id': vac_id});
					 return false;
				    });
};

// Bindings for the vacation view of the worklocation.
initialize_wl_vacation_view = function($) {
    $('#save_vacation_unit').pyproxy('click',
				     'jq_update_vacation_units',
				     '#vacation_pass_update');

    $('.vacation_month_button').live('click',
				     function(e) {
					 e.preventDefault();
					 date = $(this).attr('rel');
					 $.pyproxy_call('jq_update_vacation_overview',
							{'date': date},
							update_vacation_overview_callback);
				       });

    $('.vacation_month_switcher').live('click',
				       function(e) {
					   e.preventDefault();
					   form = $(this).parent();
					   date = form.find('[name=month]').val() + '-' + form.find('[name=year]').val();
					   $.pyproxy_call('jq_update_vacation_overview',
							  {'date': date},
							  update_vacation_overview_callback);
				       });
};

// Bindings for the vacation history view.
initialize_emp_vacation_view = function($){
    overlay_opt = {
	top: 272, 
	expose: { 
	    color: '#000000', 
	    loadSpeed: 200, 
	    opacity: 0.5 
	}, 
	
	closeOnClick: false, 
	api: true 
    };

    /* Move the forms to add/delete in history view at the body level */
    delete_form = $('#history_vacation_delete_form');
    edit_form = $('#history_vacation_edit_form');

    $('body').append(delete_form);
    $('body').append(edit_form);

    delete_form.center(true);
    edit_form.center(true);

    // Now the pyproxy bindings

    $('#delete_vacation_submit_history').pyproxy('click',
						 'jq_delete_vacation_history',
						 '#history_vacation_delete_form',
						 update_history_callback);

    $('#edit_vacation_submit_history').pyproxy('click',
					       'jq_edit_vacation_history',
					       '#history_vacation_edit_form',
					       function() {
						   if ($('.errormessage').length ==0 ) {
						       /* We only remove the overlay if there is
							  no error message left.*/
						       vacation_overlay.close();
						   }
					       });

    $('.vacation_history_edit_link').live('click',
					  function() {
					      vac_id = $(this).attr('rel');
					      $.pyproxy_call('jq_load_vacation_form',
							     {'vacation_id': vac_id},
							     function() {
								 form = $('#history_vacation_edit_form');
								 vacation_overlay = form.overlay(overlay_opt).load();
								 make_auto_complete();
							     });
					      return false;
					  });

    $('.vacation_history_delete_link').live('click',
					    function() {
						form = $('#history_vacation_delete_form');
						form.find('input[name=vacation_id]').attr('value',
											  $(this).attr('rel'));
						vacation_overlay = form.overlay(overlay_opt).load();
						return false;
					    });
};
