
RegistrationLookUpDepartments = {

    init: function() {
        TFI.ClientUtils.initPopup(".pnlLookUpDepartmentsLD");
        RegistrationLookUpDepartments.setEventHandlers();
        RegistrationLookUpDepartments.hide();
    },
    
    setEventHandlers: function() {
        $(".btnValidateLD").unbind().click(RegistrationLookUpDepartments.onClickValidate);    
        $(".btnCancelLD").unbind().click(RegistrationLookUpDepartments.onClickCancel);
    },

    clear: function() {
        $(".pnlLookUpDepartmentsLD input").each(function(index, element) {
            $(element).removeAttr("checked");
        });
    },
    
    show: function(selectedDepartments, onSelectDepartments) {
        RegistrationLookUpDepartments.onSelectDepartments = onSelectDepartments;
        $.blockUI({ message: $(".pnlLookUpDepartmentsLD") });
        RegistrationLookUpDepartments.clear(); //Clear checkboxes
        RegistrationLookUpDepartments.SelectDepartments(selectedDepartments);
        return false;    
    },
    
    hide: function() {
        $(".pnlLookUpDepartmentsLD").css("display", "none"); //Hide panel
        $.unblockUI({ message: $(".pnlLookUpDepartmentsLD") });
        return false;
    },
    
    addEventHandlerDepartmentsSelected: function(eventHandler) { //Add custom event
        if(eventHandler != null)
            $(document).bind("departments:selected", eventHandler);
    },

    removeEventHandlerDepartmentsSelected: function(eventHandler) { //Remove custom event
        if(eventHandler != null)
            $(document).unbind("departments:selected", eventHandler);
    },
    
    triggerEventDepartmentsSelected: function(departments) { //Trigger custom event
        $(document).trigger("departments:selected", [departments] );
    },    
    
    onClickValidate: function(e) {
        if(RegistrationLookUpDepartments.onSelectDepartments != null) {
			var result = {};			
			result.mobilities = TFI.ClientUtils.getSelectedItems(".departmentsLD");
			RegistrationLookUpDepartments.onSelectDepartments(result);
		}
		RegistrationLookUpDepartments.hide();
        return false;
    },
    
    onClickCancel: function(e) {
        RegistrationLookUpDepartments.hide();
        return false;
    }, 
    
    SelectDepartments: function(selectedDepartments) {
        for (i=0; i<selectedDepartments.length; i++){
            $(".pnlLookUpDepartmentsLD input").each(function(index, element) {
                if (selectedDepartments[i] == element.value){
                    //element.checked = true;
                    $(element).attr("checked", "checked");
                }
            });
        }
    }
}

