Product.Config.prototype.currentAttributeOption = new Array();
Product.Config.prototype.fillSelect = function(element) {
        if (this.config.fillSelect===false) {
            return;
        }
        var attributeId = element.id.replace(/[a-z]*/, '');
        var options = this.getAttributeOptions(attributeId);
        this.clearSelect(element);
        element.options[0] = new Option(this.config.chooseText, '');

        var prevConfig = false;
        if(element.prevSetting){
            prevConfig = element.prevSetting.options[element.prevSetting.selectedIndex];
        }

        if(options) {
            optionsPrices = new Array();
            options.each(function(option) {
                if (option.price) {
                    optionsPrices[option.id] = option.price;
                }
            });
            var index = 1;
            for(var i=0;i<options.length;i++){
                var allowedProducts = [];
                if(prevConfig && prevConfig.config) {
                    for(var j=0;j<options[i].products.length;j++){
                        if(prevConfig.config.allowedProducts
                            && prevConfig.config.allowedProducts.indexOf(options[i].products[j])>-1){
                            allowedProducts.push(options[i].products[j]);
                        }
                    }
                } else {
                    allowedProducts = options[i].products.clone();
                }

                if(allowedProducts.size()>0){
                    options[i].allowedProducts = allowedProducts;
                    element.options[index] = new Option(this.getOptionLabel(options[i], options[i].price), options[i].id);
                    element.options[index].config = options[i];

                    // the only custom line added in this method:
                    if ($('attribute' + attributeId + '-option' + options[i].id)) {
                        $('attribute' + attributeId + '-option' + options[i].id).show();
                    }
                    
                    index++;
                }

                config = this.config;
                var pConfig = this;
                if ($('attribute' + attributeId + '-option' + options[i].id)) {
                    if(this.config.outOfStockOptions[attributeId] && this.config.outOfStockOptions[attributeId].indexOf(options[i].id) != -1 ) {
                        $('attribute' + attributeId + '-option' + options[i].id).addClassName('disabled').observe('click', function(event) {   
                            event.preventDefault();
                        }); 
                    }  
                    else {
                        $('attribute' + attributeId + '-option' + options[i].id).observe('click', function(event) {
                            optionId = this.id.split('-')[1].replace('option', '');
                            pConfig.setAttributeOption(attributeId, optionId);
                            if (optionsPrices[optionId]) {
                                price = new Number(optionsPrices[optionId]).toFixed(2).replace('.', ',');
                            } else {
                                price = new Number(0).toFixed(2).replace('.', ',');    
                            }
                            price = config.template.replace('#{price}', price);

                            //apply discount set in configurable for current simple
                            var newSpan = '';
                            newSpan = '<span class="price">'+price+'</span>';
                            if (config.basePrice != config.oldPrice) {
                                var specialPrice,priceAddon = "0";
                                options.each(function(a){
                                    if (a.id == optionId) priceAddon = a.priceAddon;
                                });
                                specialPrice = (new Number(config.basePrice) + new Number(priceAddon)).toFixed(2).replace('.', ',');
                                specialPrice = config.template.replace('#{price}', specialPrice);
                                newSpan = '<span class="special-price"><span class="price"> '+specialPrice+' </span></span>\n\
<span class="old-price"><span class="price"> ' + price + ' </span></span>';
                            }

                            if ($$('div.product-options-container div.price-box').length > 0) {
                                $$('div.product-options-container div.price-box')[0].update(newSpan);
                                if ($$('div.product-selected-options div.price-box').length > 0) {
                                    $$('div.product-selected-options div.price-box')[0].update(newSpan);
                                }
                            }
                            event.preventDefault();
                        });            
                    } 
                }        
            }
            /*if (this.config.outOfStockOptions[attributeId] && $$('#attribute' + attributeId + '-option' + this.config.outOfStockOptions[attributeId][0]).length > 0) {
                $$('#attribute' + attributeId + '-option' + this.config.outOfStockOptions[attributeId][0]).each(function(elem) {
                    elem.addClassName('disabled');
                });
            }*/
//            price = new Number(0).toFixed(2).replace('.', ',');
//            price = config.template.replace('#{price}', price);
        }
    }
//Product.Config.prototype.initializeCustomSelector = function() {
//    var x;
//    for (x in this.config.attributes) {
//        this.config.attributes[x].options.each(function(option) {
//                $('attribute' + x + '-option' + option.id).show();
//                this.currentAttributeOption[x] = 0;
//            }.bind(this)
//        );
//    }
//}
Product.Config.prototype.setAttributeOption = function(attribute_id, option_id) {
    if (this.currentAttributeOption[attribute_id]) {
        var previousOption = $('attribute' + attribute_id + '-option' + this.currentAttributeOption[attribute_id]);
        if (previousOption && previousOption.hasClassName('active')) {
            previousOption.removeClassName('active');
        }
    }

    this.currentAttributeOption[attribute_id] = option_id;

    var currentOption =  $('attribute' + attribute_id + '-option' + option_id);
    if (currentOption) {
        currentOption.addClassName('active');
    }
    $('attribute' + attribute_id).setValue(option_id);

    return false;
};

Product.Config.prototype.setAttributeOptionMultiple = function(attribute_id, option_id, product_id) {
    if (this.currentAttributeOption[attribute_id]) {
        var previousOption = $('attribute' + attribute_id + '-option' + this.currentAttributeOption[attribute_id]);
        if (previousOption && previousOption.hasClassName('active')) {
            previousOption.removeClassName('active');
        }
    }

    this.currentAttributeOption[attribute_id] = option_id;

    var currentOption =  $('attribute' + attribute_id + '-option' + option_id); 
    if (currentOption) {
        currentOption.addClassName('active');
    }
    $$('#product_addtocart_form-'+product_id+' .attribute' + attribute_id)[0].setValue(option_id);

    return false;
};

