if(typeof Product=='undefined') {
    var Product = {};
}
/**************************** BUNDLE PRODUCT **************************/
Product.Bundle = Class.create();
Product.Bundle.prototype = {
    initialize: function(config){
        this.config = config;
        this.reloadPrice();
    },
    changeSelection: function(selection){
        parts = selection.id.split('-');
        if (this.config['options'][parts[2]].isMulti) {
            selected = new Array();
            if (selection.tagName == 'SELECT') {
                for (var i = 0; i < selection.options.length; i++) {
                    if (selection.options[i].selected && selection.options[i].value != '') {
                        selected.push(selection.options[i].value);
                    }
                }
            } else if (selection.tagName == 'INPUT') {
                selector = parts[0]+'-'+parts[1]+'-'+parts[2];
                selections = $$('.'+selector);
                for (var i = 0; i < selections.length; i++) {
                    if (selections[i].checked && selections[i].value != '') {
                        selected.push(selections[i].value);
                    }
                }
            }
            this.config.selected[parts[2]] = selected;
        } else {
            if (selection.value != '') {
                this.config.selected[parts[2]] = new Array(selection.value);
            } else {
                this.config.selected[parts[2]] = new Array();
            }
            this.populateQty(parts[2], selection.value);
        }
       
        
        //modify Elements
        stepPic = document.getElementById('picSelectionStep'+parts[2]);
        newSto = document.getElementById('bundle-chair-image-'+parts[3]);
        stepPic.value = newSto.value;
        
        
        this.reloadPrice();
        this.highlightOption(parts,selection);
        
        notCompatible = document.getElementById('bundle-option-notwith-'+parts[3]);
        if(notCompatible){
        	value=notCompatible.value;
        	altoptions = value.split(';');
        	//0 ist nicht verfügbar
        	//1 ist verfügbar
        	
        	document.getElementById('bundle-option-'+altoptions[1]).click();
        	document.getElementById('bundleoptionlist-'+altoptions[0]).className="konfigurator-deactivate";
        	//alert('bundleoptionlist-'+altoptions[0]);
        	document.getElementById('bundleoptionreason-'+altoptions[0]).className="konfigreasonactive";
        	
        }
        
        compatible = document.getElementById('bundle-option-with-'+parts[3]);
        if(compatible){
        	document.getElementById('bundleoptionlist-'+compatible.value).className="";
        	document.getElementById('bundleoptionreason-'+compatible.value).className="konfigreasondeactive";
        	//alert(compatible.value);
        }
        
        
        this.reloadPicture();
        //this.reloadFinishPage();

    },

	reloadFinishPage: function(){
		tmpValue = "";
		for (var option in this.config.selected) {
            if (this.config.options[option]) {
                for (var i=0; i < this.config.selected[option].length; i++) {
                    elem = document.getElementById('konfig_option_name_' + this.config.selected[option][i]);
                    tmpValue += elem.value + "\n";
                }
            }
        }
        document.getElementById('konfigfinished_text').value = tmpValue;
	},


    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.changePrice('bundle', calculatedPrice);
        optionsPrice.reload();

        return calculatedPrice;
    },

	highlightOption: function(parts,selection){
		
		allelements = document.getElementById('options-list-'+parts[2]).getElementsByTagName('DIV')
		for (i=0; i < allelements.length; i++) {
			
			if(allelements[i].id.indexOf("bundle-option-back-"+parts[2]) != -1){
				allelements[i].className = 'konfigurator-inactive';
			}
		}
		
		backdiv = document.getElementById('bundle-option-back-'+parts[2]+ '-' + parts[3]);
        backdiv.className = 'konfigurator-active';
	
	},
	reloadPicture: function(){
	   
        var path = "/media/catalog/product/configurator/chair_f";
        for (var option in this.config.options) {
        	
        	valuefield = document.getElementById('picSelectionStep'+option);
                    if(valuefield){
                    	path += "_" + valuefield.value;
                    }else{
                    	path += "_x";
                    }
        }
        path += ".jpg";
        
        if(document.getElementById('configimage')){
        	document.getElementById('configimage').src= path;
        }
        
		
	},

	findactivated: function(){
		radiooptions = document.getElementsByTagName("input");
		
		for (i=0; i<radiooptions.length; i++) {
			if (radiooptions[i].type == 'radio')
				if(radiooptions[i].checked == true){
				
				//alert('This is radio ' + radiooptions[i].value);
				idname = radiooptions[i].id.substring(14);
				idnameparts = idname.split('-');
				this.config.selected[idnameparts[0]] = new Array(idnameparts[1]);
				
				//alert(radiooptions[i].id);
				}
		}
		
	},

    doClickfor: function(element,currentDiV){
    	if(document.getElementById("bundleoptionlist-" + element).className != "konfigurator-deactivate"){
    		document.getElementById("bundle-option-" + element).click();
    	}else{
    		//alert('Diese Option steht leider nicht');
    	}
    },


    selectionPrice: function(optionId, selectionId) {
        if (selectionId == '' || selectionId == 'none') {
            return 0;
        }

        if (this.config.options[optionId].selections[selectionId].customQty == 1 && !this.config['options'][optionId].isMulti) {
            if ($('bundle-option-' + optionId + '-qty-input')) {
                qty = $('bundle-option-' + optionId + '-qty-input').value;
            } else {
                qty = 1;
            }
        } else {
            qty = this.config.options[optionId].selections[selectionId].qty;
        }

        if (this.config.priceType == '0') {
            price = this.config.options[optionId].selections[selectionId].price;
            tierPrice = this.config.options[optionId].selections[selectionId].tierPrice;

            for (var i=0; i < tierPrice.length; i++) {
                if (Number(tierPrice[i].price_qty) <= qty && Number(tierPrice[i].price) <= price) {
                    price = tierPrice[i].price;
                }
            }
        } else {
            selection = this.config.options[optionId].selections[selectionId];
            if (selection.priceType == '0') {
                price = selection.priceValue;
            } else {
                price = (this.config.basePrice*selection.priceValue)/100;
            }
        }
        return price*qty;
    },

    populateQty: function(optionId, selectionId){
        if (selectionId == '' || selectionId == 'none') {
            this.showQtyInput(optionId, '0', false);
            return;
        }
        if (this.config.options[optionId].selections[selectionId].customQty == 1) {
            this.showQtyInput(optionId, this.config.options[optionId].selections[selectionId].qty, true);
        } else {
            this.showQtyInput(optionId, this.config.options[optionId].selections[selectionId].qty, false);
        }
    },

    showQtyInput: function(optionId, value, canEdit) {
        elem = $('bundle-option-' + optionId + '-qty-input');
        elem.value = value;
        elem.disabled = !canEdit;
        if (canEdit) {
            elem.removeClassName('qty-disabled');
        } else {
            elem.addClassName('qty-disabled');
        }
    },

    changeOptionQty: function (element) {
        if (Number(element.value) == 0) {
            element.value = 1;
        }
        parts = element.id.split('-');
        optionId = parts[2];
        if (!this.config['options'][optionId].isMulti) {
            selectionId = this.config.selected[optionId][0];
            this.config.options[optionId].selections[selectionId].qty = element.value*1;
            this.reloadPrice();
        }
    },

    validationCallback: function (elmId, result){
        if (typeof elmId == 'undefined') {
            return;
        }
        var container = $(elmId).up('ul.options-list');
        if (typeof container != 'undefined') {
            if (result == 'failed') {
                container.removeClassName('validation-passed');
                container.addClassName('validation-failed');
            } else {
                container.removeClassName('validation-failed');
                container.addClassName('validation-passed');
            }
        }
    }
}