﻿if (typeof (window.XFXForce) == "undefined") {
    window.XFXForce = {};
}

(function(ShopProductSearch, $) {

XFXForce.ShopProductSearch = function(webServiceUrl, categoryDropdownID, seriesDropdownID, modelDropdownID, memoryDropdownID, busDropdownID, performanceDropdownID) {
    this.webServiceUrl = webServiceUrl;
    this.categoryDropdownID = categoryDropdownID;
    this.seriesDropdownID = seriesDropdownID;
    this.modelDropdownID = modelDropdownID;
    this.memoryDropdownID = memoryDropdownID;
    this.busDropdownID = busDropdownID;
    this.performanceDropdownID = performanceDropdownID;
};

XFXForce.ShopProductSearch.prototype.Init = function() {
    var $categories = $("#" + this.categoryDropdownID),
        $series = $("#" + this.seriesDropdownID),
        self = this;
    
    $categories.change(function() {
        _categoryChanged.call(self);
    });
    
    $series.change(function () {
        _seriesChanged.call(self);
    });
};

function _categoryChanged() {
    var newCategory = $("#" + this.categoryDropdownID).val(),
        self = this;
    $.ajax({
        type: "POST",
        url: this.webServiceUrl + "/GetProductSearchOptions",
        data: "{newCategory:\"" + newCategory + "\"}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(result) {
            if (result && result.d) {
                _updateDropdowns.call(self, result.d);
            }
        }
    });
}

function _seriesChanged() {
    var newSeries = $("#" + this.seriesDropdownID).val(),
        self = this;
    $.ajax({
        type: "POST",
        url: this.webServiceUrl + "/GetProductSearchOptionsBySeries",
        data: "{series:\"" + newSeries + "\"}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(result) {
            if (result && result.d) {
                _updateDropdowns.call(self, result.d);
            }
        }
    });
}

function _updateDropdowns(dropdownData) {
    var $graphicsDropdowns = $("#" + this.memoryDropdownID + ",#" + this.busDropdownID + ",#" + this.performanceDropdownID),
        selectedCategory = $("#" + this.categoryDropdownID).val();
        
    if (dropdownData.Series) {
        updateDropdownOptions($("#" + this.seriesDropdownID), dropdownData.Series);
    }
    
    if (dropdownData.Models) {
        updateDropdownOptions($("#" + this.modelDropdownID), dropdownData.Models);
    }
    
    if (selectedCategory == "Power Supply") {
        $graphicsDropdowns.val("").attr("disabled", "disabled");
    } else {
        $graphicsDropdowns.removeAttr("disabled");
    }
}

function updateDropdownOptions($dropdown, values) {
    var oldValue = $dropdown.val();
    $("option:not(:first)", $dropdown).remove();
    
    for(var i = 0; i < values.length; i++) {
        var option = $("<option></option");
        option.attr("value", values[i]);
        option.html(values[i]);
        $dropdown.append(option);
    }
    $dropdown.val(oldValue);
}

})(XFXForce, jQuery);
