/*
Some of these override earlier varien/product.js methods, therefore
varien/product.js must have been included prior to this file.
*/


Product.Config.prototype.initialize = function(config){
    this.config     = config;
    this.taxConfig  = this.config.taxConfig;
    this.settings   = $$('.super-attribute-select');
    this.state      = new Hash();
    this.priceTemplate = new Template(this.config.template);
    this.prices     = config.prices;

    this.settings.each(function(element){
        Event.observe(element, 'change', this.configure.bind(this));
        Event.observe(element, 'basis:change', this.configure.bind(this));
    }.bind(this));

    // fill state
    this.settings.each(function(element){
        var attributeId = element.id.replace(/[a-z]*/, '');
        if(attributeId && this.config.attributes[attributeId]) {
            element.config = this.config.attributes[attributeId];
            element.attributeId = attributeId;
            this.state[attributeId] = false;
        }
    }.bind(this))

    // Init settings dropdown
    var childSettings = [];
    for(var i=this.settings.length-1;i>=0;i--){
        var prevSetting = this.settings[i-1] ? this.settings[i-1] : false;
        var nextSetting = this.settings[i+1] ? this.settings[i+1] : false;
        if(i==0){
            this.fillSelect(this.settings[i])
        }
        else {
            this.settings[i].disabled=true;
        }
        $(this.settings[i]).childSettings = childSettings.clone();
        $(this.settings[i]).prevSetting   = prevSetting;
        $(this.settings[i]).nextSetting   = nextSetting;
        childSettings.push(this.settings[i]);
    }

    // try retireve options from url
    var separatorIndex = window.location.href.indexOf('#');
    if (separatorIndex!=-1) {
        var paramsStr = window.location.href.substr(separatorIndex+1);
        this.values = paramsStr.toQueryParams();
        this.settings.each(function(element){
            var attributeId = element.attributeId;
            element.value = this.values[attributeId];
            this.configureElement(element);
        }.bind(this));
    }
}

Product.Super.Configurable.prototype.registerObservers = function() {
    var elements = this.container.getElementsByClassName(this.observeCss);
    elements.each(function(element){
        Event.observe(element, 'change', this.update.bindAsEventListener(this));
        Event.observe(element, 'basis:change', this.update.bindAsEventListener(this));
    }.bind(this));
    return this;
}
