﻿function DashManager() {

    this.Init = function(NbVehicle) {
        Dash.NbItem = NbVehicle;
        Dash.UpdateApi();
        Dash.UpdateCount();
        Dash.AddRemoveStatus = false;
    }


    this.AddOne = function() {
        var Calc = Dash.NbItem;
        if (typeof (Calc) == "string") {
            var TempCalc = parseInt(Calc);
            Calc = TempCalc + 1;
        }
        else
            Calc = Calc + 1;
        Dash.NbItem = Calc;
        Dash.AddRemoveStatus = false;
    }


    this.RemoveOne = function() {
    var Calc = Dash.NbItem;
    if (typeof (Calc) == "string") {
        var TempCalc = parseInt(Calc);
        Calc = TempCalc - 1;
    }
    else
        Calc = Calc - 1;
        Dash.NbItem = Calc;
        Dash.AddRemoveStatus = true;
    }

    this.RemoveAll = function() {
        Dash.NbItem = 0;
    }


    this.AddVehicle = function(ProdId, FamiId) {
        j$.ajax({
            url: '/Ajax/Selection/AddProduct.aspx?FamiId=' + FamiId + '&ProdId=' + ProdId,
            async: false,

            success: function(response) {
                j$('#update').html(response);
                //Update(response);
            }
        });

        Dash.UpdateDisplay(FamiId);
        Dash.UpdateCount();

    }

    this.RemoveVehicle = function(ProdId, FamiId) {
        if (j$('#Chk' + ProdId).length > 0) {
            j$('#Chk' + ProdId)[0].checked = false;
        }
        j$.ajax({
            url: '/Ajax/Selection/RemoveProduct.aspx?FamiId=' + FamiId + '&ProdId=' + ProdId,
            async: false,
            success: function(response) {
                j$('#update').html(response);
                //Update(response);
            }
        });
        Dash.UpdateDisplay(FamiId);
        Dash.UpdateCount();

    }

    this.UpdateCount = function() {
        j$('.SIKCountDash').each(function(index, Item) {
            Item.innerHTML = Dash.NbItem;
        });
        j$('#BtnComparateur').hide();
        if (j$('#hidDash').length > 0) {
            if (Dash.NbItem > 1) {
                if (j$('#alnkComparateur').length > 0)
                    j$('#alnkComparateur')[0].href = j$('#hidLink')[0].value;
                if (j$('#HrefComparHeader').length > 0)
                    j$('#HrefComparHeader')[0].href = j$('#hidLink')[0].value;
                j$('#BtnComparateur').show();
            }
            else {
                if (j$('#alnkComparateur').length > 0)
                    j$('#alnkComparateur')[0].href = '#';
                if (j$('#HrefComparHeader').length > 0)
                    j$('#HrefComparHeader')[0].href = '#';
            }
        }
        else {
            if (j$('#DashItems').length > 0) {
                j$('#DashItems').html('');
            }
            if (j$('#HrefComparHeader').length > 0)
                j$('#HrefComparHeader')[0].href = '#';
        }
        if (Dash.NbItem == 0) {
            if (j$('#DashItems').length > 0) {
                j$('#DashItems').html('');
            }
        }
    }

    this.ClearAllSelection = function() {
        j$('.SIKChkProduct').each(function(index, Item) {
            if (Item.checked) Item.checked = false;
        });
        j$('#clear-selection-confirm').hide();
        j$.ajax({
            url: '/Ajax/Selection/RemoveAllProducts.aspx?FamiId=1001',
            async: false,
            success: function(response) {
                j$('#update').html(response);
                //Update(response);
            }
        });
        if (j$('#DashItems').length > 0) {
            j$('#DashItems').html('');
        }
        Dash.NbItem = 0;
        Dash.UpdateCount();
    }

    this.UpdateDisplay = function(FamiId) {
        j$.ajax({
            url: '/Ajax/Selection/FamilyProducts.aspx?FamiId=' + FamiId,
            async: false,
            success: function(response) {
                j$('#UpdateDash').html(response);
            }
        });
        Dash.UpdateApi();
    }


    this.UpdateApi = function() {
        Dash.api = jQuery("#dash-vehicles").scrollable({ hoverClass: 'hover', size: 1, api: true });
        if (Dash.AddRemoveStatus) Dash.api.prevPage();
        Dash.api.reload();

    }
}



