/**************************** CONFIGURABLE PRODUCT **************************/

/**
 * The element's id changed from attributeNN to attributeNN-PP
 * NN = the attribute Id
 * PP = the product id
 */
 /* If Product.Config isn't defined we have a problem, houston
if(typeof Product.Config=='undefined') {
    Product.Config = Class.create();
    Product.Config.prototype = {
    	initialize: function(){
    	}
	};
};
*/
Product.LBConfig = Class.create();
Product.LBConfig.prototype = Object.extend(new Product.Config({template:'', prices:1}), { // dummy values
	
	// refine the initialize method
	initialize: function(config){
		//alert([config].toJSON());
        this.config     = config;
		this.taxConfig  = this.config.taxConfig; // + 1.3update
        this.settings   = new Array(); // $$('.super-attribute-select');
        this.state      = new Hash();
        this.priceTemplate = new Template(this.config.template);
        this.prices     = config.prices;
        
        // filter settings by product id
        var tmpSettings = $$('.super-attribute-select');
        for(var i=0; i < tmpSettings.length; i++){
        	var element = tmpSettings[i];
        	if (element.id.replace(/^.+-/, '') == this.config.productId) {
        		this.settings.push(element);
        	}
        }

        this.settings.each(function(element){
            Event.observe(element, 'change', this.configure.bind(this))
        }.bind(this));

        // fill state
        this.settings.each(function(element){
	    	var attributeId = element.id.replace(/-[0-9]+$/, '');
	        attributeId = attributeId.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));
        }
    },
    
    fillSelect: function(element){
    	var attributeId = element.id.replace(/-[0-9]+$/, '');
        attributeId = attributeId.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) {
            var index = 1;
            for(var i=0;i<options.length;i++){
                var allowedProducts = [];
                if(prevConfig) {
                    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];
                    index++;
                }
            }
        }
    },
    
    reloadPrice: function(){
        var price = 0;
        for(var i=this.settings.length-1;i>=0;i--){
            var selected = this.settings[i].options[this.settings[i].selectedIndex];
            if(selected.config){
                price += parseFloat(selected.config.price);
            }
        }
		
		// add this.config.productId index to optionsPrice reference
        optionsPrice[this.config.productId].changePrice('config', price);
        optionsPrice[this.config.productId].reload();
    }
});

/**************************** BUNDLE PRODUCT **************************/

if(typeof Product.Bundle=='undefined') {
    Product.Bundle = Class.create();
    Product.Bundle.prototype = {
    	initialize: function(){
    	}
	};
};

Product.LBBundle = Class.create();
Product.LBBundle.prototype = Object.extend(new Product.Bundle({}), {
	
    initialize: function(config){
        this.config = config;
        this.reloadPrice();
    },

    reloadPrice: function() {
        var calculatedPrice = 0;
        for (var option in this.config.selected) {
            if (this.config.options[option]) {
                for (var i=0; i < this.config.selected[option].length; i++) {
                    calculatedPrice += Number(this.selectionPrice(option, this.config.selected[option][i]));
                }
            }
        }

        if (this.config.specialPrice) {
            var newPrice = (calculatedPrice*this.config.specialPrice)/100;
            calculatedPrice = Math.min(newPrice, calculatedPrice);
        }

        optionsPrice[this.config.productId].changePrice('bundle', calculatedPrice);
        optionsPrice[this.config.productId].reload();

        return calculatedPrice;
    }
});