﻿$(document).ready(function () {

    $("#HSG2deviceSearch").autocomplete({
        source: function (request, response) {
            var requestThirdPartyProductData = new Object();
            requestThirdPartyProductData.searchString = $("#HSG2deviceSearch").val();

            GNscript.GnaxControls.Execute
            (
                "HeadsetGuide2.ThirdPartyProductsJSON",
                JSON.stringify(requestThirdPartyProductData),
                GNscript.CallbackChain
                (
                    {
                        onSuccess: function (serverResponse) {

                            var responseJSON = JSON.parse(serverResponse);
                            if (responseJSON.Data.length > 0) {
                                response($.map(responseJSON.Data, function (item) {
                                    return {
                                        label: item.name,
                                        value: item.id,
                                        manufacturer: item.manufacturer,
                                        ehsrccsupport: item.ehsrccsupport,
                                        alt: item.alt
                                    }
                                }));
                            }
                            else {
                                //No matches found, simulate a No Matching Products response
                                var emptyResponse = [{
                                    label: GNheadsetGuide2.NoMatchingProducts,
                                    value: "00000000-0000-0000-0000-000000000000",
                                    manufacturer: "",
                                    ehsrccsupport: "",
                                    alt: ""
                                }];

                                response($.map(emptyResponse, function (item) {
                                    return {
                                        label: item.label,
                                        value: item.value,
                                        manufacturer: item.manufacturer,
                                        ehsrccsupport: item.ehsrccsupport,
                                        alt: item.alt
                                    }
                                }));
                            }
                        }
                    }
                )
            );
        },
        minLength: 3,
        focus: function (event, ui) {
            if (ui.item.value != "00000000-0000-0000-0000-000000000000") {
                $("#HSG2deviceSearch").val(ui.item.label);
            }
            return false;
        },
        select: function (event, ui) {
            if (ui.item.value != "00000000-0000-0000-0000-000000000000") {
                $("#HSG2deviceSearch").val(ui.item.label);
                GNheadsetGuide2.ThirdPartyProductId = ui.item.value;
                GNheadsetGuide2.ManufacturerName = ui.item.manufacturer;
                GNheadsetGuide2.DeviceName = ui.item.label;
                GNheadsetGuide2.DeviceEhsRcc = ui.item.ehsrccsupport;
                GNheadsetGuide2.DeviceEhsRccAlt = ui.item.alt;
                $("#HeadsetGuide2Manufacturer option:eq(0)").attr("selected", "selected");
                $('#HeadsetGuide2Device option').each(function (i, option) { $(option).remove(); });
                $('#HeadsetGuide2Device').append('<option class="grey" value="-1">' + GNheadsetGuide2.FirstSelectManufacturer + '</option>');
                GNheadsetGuide2.ProductFamilySearch();
            }
            return false;

        }

    })
    .data("autocomplete")._renderItem = function (ul, item) {
        if (item.value != "00000000-0000-0000-0000-000000000000") {
            return $("<li></li>")
				    .data("item.autocomplete", item)
				    .append("<a>" + item.manufacturer + ' - ' + item.label + "</a>")
				    .appendTo(ul);
        }
        else {
            return $("<li></li>")
				    .data("item.autocomplete", item)
				    .append("<a>" + item.label + "</a>")
				    .appendTo(ul);
        }
    };

});



// *************************************************
// * Headset Guide 2.0
// *************************************************

GNheadsetGuide2 = new Object();
GNheadsetGuide2.ProductFamilyID = '';
GNheadsetGuide2.ThirdPartyProductId = '00000000-0000-0000-0000-000000000000';
GNheadsetGuide2.ManufacturerName = '';
GNheadsetGuide2.DeviceName = '';
GNheadsetGuide2.DeviceEhsRcc = '';
GNheadsetGuide2.DeviceEhsRccAlt = '';
GNheadsetGuide2.ThirdPartyProductsJSON = new Object();

//GNheadsetGuide2.UsageOnChange = function (selector) {
//    GNheadsetGuide2.Refresh3rdPartyProducts();
//    return false;
//}

GNheadsetGuide2.ManufacturerOnChange = function (selector) {
    GNheadsetGuide2.Refresh3rdPartyProducts();
    return false;
}

GNheadsetGuide2.DeviceOnChange = function (selector) {
    var deviceSelector = $('#HeadsetGuide2Device');
    if (deviceSelector.val() != null) {
        GNheadsetGuide2.ThirdPartyProductId = GNheadsetGuide2.ThirdPartyProductsJSON.Data[deviceSelector.val()].id;
        GNheadsetGuide2.DeviceEhsRcc = GNheadsetGuide2.ThirdPartyProductsJSON.Data[deviceSelector.val()].ehsrccsupport;
        GNheadsetGuide2.DeviceEhsRccAlt = GNheadsetGuide2.ThirdPartyProductsJSON.Data[deviceSelector.val()].alt;
    }
    else {
        GNheadsetGuide2.ThirdPartyProductId = '00000000-0000-0000-0000-000000000000';
    }

    GNheadsetGuide2.ManufacturerName = $("#HeadsetGuide2Manufacturer option:selected").text();
    GNheadsetGuide2.DeviceName = $("#HeadsetGuide2Device option:selected").text()
    $("#HSG2deviceSearch").val('');
    GNheadsetGuide2.ProductFamilySearch();
    return false;
}

GNheadsetGuide2.ShowProductFamilyOnClick = function (productFamilyId) {
    linkId = '#hsg2link' + productFamilyId;
    $(linkId).fadeOut(400);
    GNheadsetGuide2.ProductFamilyID = productFamilyId;
    GNheadsetGuide2.ProductSearch();
    return false;
}

GNheadsetGuide2.showfamiliesRadioOnClick = function () {
    GNheadsetGuide2.ProductFamilySearch();
}

GNheadsetGuide2.returnToProductFamiliesOnClick = function () {
    GNheadsetGuide2.HideProductsDivs();
    GNheadsetGuide2.ProductFamilySearch();
    return false;
}

GNheadsetGuide2.showproductsRadioOnClick = function () {
    GNheadsetGuide2.ProductSearch();
}

GNheadsetGuide2.Refresh3rdPartyProducts = function () {
    $('#HeadsetGuide2Device option').each(function (i, option) { $(option).remove(); });

    if ($('#HeadsetGuide2Manufacturer').val() == "00000000-0000-0000-0000-000000000000") {
        $('#HeadsetGuide2Device').append('<option class="grey" value="-1">' + GNheadsetGuide2.FirstSelectManufacturer + '</option>');
        return false;
    }

    var requestThirdPartyProductData = new Object();
    //requestThirdPartyProductData.usage = $('#HeadsetGuide2Usage').val();
    requestThirdPartyProductData.manufacturerId = $('#HeadsetGuide2Manufacturer').val();

    GNscript.GnaxControls.Execute
        (
            "HeadsetGuide2.ThirdPartyProductsJSON",
            JSON.stringify(requestThirdPartyProductData),
            GNscript.CallbackChain
            (
                {
                    onSuccess: function (serverResponse) {

                        GNheadsetGuide2.ThirdPartyProductsJSON = JSON.parse(serverResponse);

                        for (var j = 0; j < GNheadsetGuide2.ThirdPartyProductsJSON.Data.length; j++) {
                            if ($('#HeadsetGuide2Device option').length == 0) {
                                $('#HeadsetGuide2Device').append('<option value="-1">' + GNheadsetGuide2.Select + '</option>');
                            }
                            $('#HeadsetGuide2Device').append('<option value="' + j + '">' + GNheadsetGuide2.ThirdPartyProductsJSON.Data[j].name + '</option>');
                        }

                        if ($('#HeadsetGuide2Device option').length == 0) {
                            $('#HeadsetGuide2Device').append('<option value="-1">' + GNheadsetGuide2.NoMatchingProducts + '</option>');
                        }
                    }
                }
            )
        );
}

GNheadsetGuide2.ProductFamilySearch = function () {

    //Only do the Ajax call if the user has selected a 3rd party product
    if (GNheadsetGuide2.ThirdPartyProductId != '00000000-0000-0000-0000-000000000000') {
        var cordedWireless = 'all';
        var cordedWirelessSelector = $('#HSG2ProductFamilyRedux input:radio:checked');
        if (cordedWirelessSelector) {
            if (cordedWirelessSelector.val() != null) {
                cordedWireless = cordedWirelessSelector.val();
            }
        }

        var command = GNheadsetGuide2.ThirdPartyProductId + '|' + cordedWireless;

        $("#HSG2ManufacturerModelNames").empty();
        $('.EhsRccSupportIcon').remove();
        $("#HSG2YouSearchedFor").removeClass('mt5');

        $("#HSG2ManufacturerModelNames").append('&nbsp;' + GNheadsetGuide2.ManufacturerName + ' - ' + GNheadsetGuide2.DeviceName);

        switch (GNheadsetGuide2.DeviceEhsRcc) {
            case 'RCC':
                $("#HSG2YouSearchedFor").addClass('mt5');
                $("#HSG2ManufacturerModelNames").append('&nbsp;<img class="EhsRccSupportIcon" width="50px" height="15px" alt="' + GNheadsetGuide2.DeviceEhsRccAlt + '" src="/Style Library/Images/hsg2_supports_icon_rcc.png"  /><span class="EhsRccSupportDescription">&nbsp;' + GNheadsetGuide2.DeviceEhsRccAlt + '</span>');
                break;
            case 'EHS':
                $("#HSG2YouSearchedFor").addClass('mt5');
                $("#HSG2ManufacturerModelNames").append('&nbsp;<img class="EhsRccSupportIcon" width="50px" height="15px" alt="' + GNheadsetGuide2.DeviceEhsRccAlt + '" src="/Style Library/Images/hsg2_supports_icon_ehs.png"  /><span class="EhsRccSupportDescription">&nbsp;' + GNheadsetGuide2.DeviceEhsRccAlt + '</span>');
                break;
        }

        var callback = new Object();
        callback.Callback = function (result) {
            var sel = $('#HSG2ProductFamilies');
            if (sel) {
                if (result) {
                    sel.empty();
                    sel.append(result);
                }
                else {

                }
            }
            GNheadsetGuide2.HideProductFamiliesLoading();
            GNheadsetGuide2.ShowProductFamiliesDivs();
        }
        //Generally this method is responsible for showing and hiding the ProductFamilies div
        //It should not control other divs, although it makes sense to hide Solutions (Product)
        //GNheadsetGuide2.HideProductFamiliesDivs();
        $('#HSG2ProductFamilies').css('display', 'none');
        GNheadsetGuide2.HideProductsDivs();
        GNheadsetGuide2.HideProductFamiliesLoading();
        GNheadsetGuide2.ShowProductFamiliesLoading();
        GNscript.GnaxControls.Execute('HeadsetGuide2.ProductFamilies', command, callback);
    }
}

GNheadsetGuide2.ShowProductFamiliesDivs = function () {
    if ($('#HSG2SearchingProductFamilies').css('display') == 'none') {
        $('#HSG2SearchingProductFamilies').fadeIn(400);
    }
    $('#HSG2ProductFamilies').fadeIn(400);
}

GNheadsetGuide2.HideProductFamiliesDivs = function () {
    $('#HSG2SearchingProductFamilies').css('display', 'none');
    $('#HSG2ProductFamilies').css('display', 'none');
}

GNheadsetGuide2.ShowProductFamiliesLoading = function () {
    $('#HSG2ProductFamiliesLoading').fadeIn(400);
    var t = setTimeout("GNheadsetGuide2.ShowProductFamiliesLoadLongTime()", 5000);
}

GNheadsetGuide2.ShowProductFamiliesLoadLongTime = function () {
    $('#HSG2ProductFamiliesLongerThanUsual').css('display', 'inline');
}

GNheadsetGuide2.HideProductFamiliesLoading = function () {
    $('#HSG2ProductFamiliesLoading').css('display', 'none');
    $('#HSG2ProductFamiliesLongerThanUsual').css('display', 'none');
}

GNheadsetGuide2.ProductSearch = function () {

    // Only do the Ajax call if the user has selected a 3rd party product
    // and Product Family
    if (GNheadsetGuide2.ThirdPartyProductId != '00000000-0000-0000-0000-000000000000' &&
        GNheadsetGuide2.ProductFamilyID != '') {

        var cordedWireless = 'all';
        var cordedWirelessSelector = $('#HSG2ProductFamilyRedux input:radio:checked');
        if (cordedWirelessSelector) {
            if (cordedWirelessSelector.val() != null) {
                cordedWireless = cordedWirelessSelector.val();
            }
        }

        var productReduxSelection = '';
        var productReduxSelector = $('#HSG2ProductsRedux input:radio:checked');
        if (productReduxSelector && ($('#HSG2ProductsRedux').css('display') != 'none')) {
            if (productReduxSelector.val() != null) {
                productReduxSelection = productReduxSelector.val();
            }
        }

        var ampDirect = 'any';
        var monoDuo = 'any';
        switch (productReduxSelection) {
            case 'ampandduo':
                ampDirect = 'amplifier';
                monoDuo = 'duo';
                break;
            case 'ampandmono':
                ampDirect = 'amplifier';
                monoDuo = 'mono';
                break;
            case 'dcandduo':
                ampDirect = 'directconnect';
                monoDuo = 'duo';
                break;
            case 'dcandmono':
                ampDirect = 'directconnect';
                monoDuo = 'mono';
                break;
        }

        var command = GNheadsetGuide2.ThirdPartyProductId + '|' + cordedWireless + '|' + ampDirect + '|' + monoDuo + '|' + GNheadsetGuide2.ProductFamilyID;

        var callback = new Object();
        callback.Callback = function (result) {
            var sel = $('#HSG2ProductFamily');
            if (sel) {
                if (result) {
                    sel.empty();
                    sel.append(result);
                }
                else {

                }
            }
            GNheadsetGuide2.HideProductsLoading();
            GNheadsetGuide2.ShowProductsDivs();
        }
        $('#HSG2ProductFamilies div.listingitem').each(function (i, div) {
            if ($(div).attr("id") != GNheadsetGuide2.ProductFamilyID) {
                $(div).fadeOut(400);
            }
        });

        $('#HSG2ProductFamily').css('display', 'none');
        $('#HSG2ReturnToProductFamilies').css('display', 'none');
        GNheadsetGuide2.HideProductsLoading();
        GNheadsetGuide2.ShowProductsLoading();
        GNscript.GnaxControls.Execute('HeadsetGuide2.Products', command, callback);
    }
}

GNheadsetGuide2.ShowProductsDivs = function () {
    if ($('#HSG2ProductsRedux').css('display') == 'none') {
        $('#HSG2ProductsRedux').fadeIn(400);
    }

    $('#HSG2ProductFamily').fadeIn(400);

    if ($('#HSG2ReturnToProductFamilies').css('display') == 'none') {
        $('#HSG2ReturnToProductFamilies').fadeIn(400);
    }
}

GNheadsetGuide2.HideProductsDivs = function () {
    $('#HSG2ProductsRedux').css('display', 'none');
    $('#HSG2ProductFamily').css('display', 'none');
    $('#HSG2ReturnToProductFamilies').css('display', 'none');
}

GNheadsetGuide2.ShowProductsLoading = function () {
    $('#HSG2ProductsLoading').fadeIn(400);
    var t = setTimeout("GNheadsetGuide2.ShowProductLoadLongTime()", 5000);
}

GNheadsetGuide2.ShowProductLoadLongTime = function () {
    $('#HSG2ProductsLongerThanUsual').css('display', 'inline');
}

GNheadsetGuide2.HideProductsLoading = function () {
    $('#HSG2ProductsLoading').css('display','none');
    $('#HSG2ProductsLongerThanUsual').css('display', 'none');
}

