﻿function supplierListItemControl(supplierDetailsClientId, supplierRankLabelId, deletionWarningMessage, removeSupplierAjaxCommand,
                                    ajaxManagerId, supplierIntroductionPanelId, expandCollapsePanelId, unavailableReasonPanelId, unavailableReasonExpandCollapsePanelId) {
    this.loaded = false;
    this.expanded = false;
    this.expandCollapse = expandCollapse;
    this.showContactDetails = showContactDetails;
    this.hideContactDetails = hideContactDetails;
    this.unavailableReasonExpanded = false;
    this.expandCollapseUnavailableReason = expandCollapseUnavailableReason;
    this.showUnavailableReason = showUnavailableReason;
    this.hideUnavailableReason = hideUnavailableReason;
    this.endAJAXRequest = endAJAXRequest;
    this.raiseAJAXRequest = raiseAJAXRequest;
    this.supplierDetailsClientId = supplierDetailsClientId;
    this.supplierRankLabelId = supplierRankLabelId;
    this.deletionWarningMessage = deletionWarningMessage;
    this.removeSupplierAjaxCommand = removeSupplierAjaxCommand;
    this.ajaxManagerId = ajaxManagerId;
    this.supplierIntroductionPanelId = supplierIntroductionPanelId;
    this.expandCollapsePanelId = expandCollapsePanelId;
    this.unavailableReasonPanelId = unavailableReasonPanelId;
    this.unavailableReasonExpandCollapsePanelId = unavailableReasonExpandCollapsePanelId
}

function SupplierSelectedEventArgs(supplierId){
    this.supplierId = supplierId;
}

function SupplierQuestionnaireSelectedEventArgs(supplierId, questionnaireId){
    this.supplierId = supplierId;
    this.questionnaireId = questionnaireId;
}

function ProjectSubContractorSelectedEventArgs(projectSubContractorId){
    this.projectSubContractorId = projectSubContractorId;
}

function endAJAXRequest(request, args){
    if (args.get_error() != undefined){
       var errorMessage = args.get_error().message;
       args.set_errorHandled(true);
    }
    else{
        var newHeight = $('#' + this.supplierDetailsClientId + '').height();
        $('#' + this.supplierRankLabelId  + '').height(newHeight);
    }
}
            
function raiseAJAXRequest(args){
    var ajaxManager = $find(this.ajaxManagerId);
    ajaxManager.ajaxRequest(args);
}

function expandCollapse(){
    if(!this.expanded){
        this.showContactDetails();
    }
    else{
        this.hideContactDetails();
    }
    this.expanded = !this.expanded;
}

function showContactDetails(){
    var newHeight = $('#' + this.supplierIntroductionPanelId + '').height() + 
                    parseInt($('#' + this.supplierIntroductionPanelId + '').css("padding-top")) + 
                    parseInt($('#' + this.supplierIntroductionPanelId + '').css("padding-bottom")) + 
                    4 +
                    'px';
    $('#' + this.expandCollapsePanelId + '').animate({ height: newHeight }, { queue:false, duration: 300 } );
}

function hideContactDetails(){
    $('#' + this.expandCollapsePanelId + '').animate({ height: '0px' }, { queue:false, duration: 300 } );
}
            
function expandCollapseUnavailableReason(){
    if(!this.unavailableReasonExpanded){
        this.showUnavailableReason();
    }
    else{
        this.hideUnavailableReason();
    }
    this.unavailableReasonExpanded = !this.unavailableReasonExpanded;
}

function showUnavailableReason(){
    var newHeight = $('#' + this.unavailableReasonPanelId + '').height() +
                    parseInt($('#' + this.unavailableReasonPanelId + '').css("padding-top")) +
                    parseInt($('#' + this.unavailableReasonPanelId + '').css("padding-bottom")) + 
                    4 +
                    'px';
    $('#' + this.unavailableReasonExpandCollapsePanelId + '').animate({ height: newHeight }, { queue: false, duration: 300 });
}

function hideUnavailableReason(){
    $('#' + this.unavailableReasonExpandCollapsePanelId + '').animate({ height: '0px' }, { queue: false, duration: 300 });
}
            

