/* clxShop JS Library, 2008 by Nico Puhlmann <nico@puhlmann.com>
 * requires Prototype JavaScript framework, version 1.6.0
 */
/* Prototype extensions */
Event.fireEvent = function(eventType, element) {
  if (document.createEvent) {
  	var evt = document.createEvent("Events");
    evt.initEvent(eventType, true, true);
 	$(element).dispatchEvent(evt);
  } else if (document.createEventObject) {
  	var evt = document.createEventObject();
	$(element).fireEvent('on' + eventType, evt);
  }
}

/* Attribut Konfigurator für Produkte */
var AttributProduktKonfig = Class.create(); 
AttributProduktKonfig.prototype = {
	initialize: function(config) {
		reloadprice = (config.typ == 2) ? true : false;
		config.conf.each(function(c) {
			select_rule = (config.typ == 3) ? '.attribut-select-' + c.produktid : '.attribut-select';
			c.reloadprice = reloadprice;
			new AttributObserver(select_rule, c);
		});
	}
}
var AttributObserver = Class.create(); 
AttributObserver.prototype = {
	initialize: function(select_rule, config) {
		this.attributes = config.attributes;
		if(this.attributes.length == 0)	return false;
		this.masterproduktid = config.produktid;
		this.reloadprice = config.reloadprice;
		this.prices = config.prices;
		this.price = this.prices[this.masterproduktid];
		this.produktid = 0;
		this.selects   = $$(select_rule);
		this.possible_products = [];
		eindex = 1;
		this.selects.each(function(element) {
			element.index = eindex;
			element.disabled = (element.selectedIndex==0 && eindex > 1) ? true : false;
			if(eindex == this.selects.length){
				Event.observe(window, 'load', function() { Event.fireEvent("change", element); });
			}
			eindex++;
			Event.observe(element, 'change', this.build.bindAsEventListener(this));
			Event.observe(element, 'keyup', this.build.bindAsEventListener(this));
		}.bind(this));  
	},
	
	build: function(event) { 
		var element = Event.element(event);
		var attributid = element.id.replace(/attribut_/, '');
		this.possible_products = [];
		this.selects.each(function(subelement) {
			var subattributid = subelement.id.replace(/attribut_/, '');
			if(subattributid != attributid && (subelement.index-element.index)==1) {
				this.reset(subelement);
				this.fill(subelement, this.attributes[subattributid], attributid, element);
				subelement.disabled = (subelement.index > element.index && (subelement.index-element.index)==1) ? false : true;
			}else if(subelement.index > element.index){
				this.reset(subelement);
				subelement.disabled = true;
			}
			for(i=0;i<this.attributes[subattributid].values.length;i++) {
				value = this.attributes[subattributid].values[i];
				if(value.id == $F(subelement)){
					for(j=0;j<value.products.length;j++){
						if(subelement.index == 1 && this.possible_products.indexOf(value.products[j]) == -1) {
							this.possible_products.push(value.products[j]);
						}
					}
					if(subelement.index > 1) {
						possible_products = this.possible_products;
						for(k=0;k<possible_products.length;k++){
							if(value.products.indexOf(possible_products[k]) == -1) {
								this.possible_products = this.possible_products.without(possible_products[k]);
							}
						}
					}
				}
			}
		}.bind(this));
		if(this.possible_products.length == 1 && this.possible_products[0] && this.possible_products[0] > 0){
			this.produktid = this.possible_products[0];
		} else {
			this.produktid = 0;
		}
		if(this.reloadprice){
			this.setprice(element, attributid);
		}
		this.callback();
	},
	
	setprice: function(element, attributid) {
		if($('produkt-price')) {
			$('produkt-price').innerHTML = (this.produktid > 0) ? this.prices[this.produktid] : this.price;
		}
	},
	
	callback: function(){
		var cbproduktid = (this.produktid > 0) ? this.produktid : this.masterproduktid;
		if(this.produktid > 0 || this.possible_products.length == 0) {
			AttributProduktKonfigCallback(cbproduktid);
		}
    },
    
    reset: function(element){
        for(var i=element.options.length-1;i>=0;i--){
            element.remove(i);
        }
    },  
	
    fill: function(element, attribute, masterattributid, selectedelement){
    	selected = $F(selectedelement);
    	Choosetext = (typeof(Choosetext) != "undefined") ? Choosetext : unescape("Bitte w%E4hlen");
    	element.options[0] = new Option(Choosetext, '');
    	index = 1;
    	add_option = true;
    	have_values = [];
        for(var i=0; i < attribute.values.length; i++){
        	value = attribute.values[i];
	        for(var x=0; x < value.products.length; x++){
	        	add_option = false;
	        	products = value.products;
	        	for(var y=0; y < this.attributes[masterattributid].values.length; y++){
	        		mastervalue = this.attributes[masterattributid].values[y];
	        		if(mastervalue.id == selected && mastervalue.products.indexOf(products[x]) > -1 && this.possible_products.indexOf(products[x]) > -1){
	                    add_option = true;
	                    break;
	        		}
        		}
        		if(add_option == true) break;
	        }
	        if(add_option && have_values.indexOf(value.id) == -1) {
	        	element.options[index] = new Option(value.value, value.id);
	        	have_values[index] = value.id;
                index++;
	        }
        }
    }
}; 
function AttributProduktKonfigCallback(produktid){}

/* Warenkorb hinzufuegen */
function BasketAdd(formid) {
 var selects = $$('.attribut-select');
 var errors = 0;
 selects.each(function(element) 
 {
	eid = element.id + '_error';
	if($(eid)){
		$(eid).remove();
 	}
 	if(element.selectedIndex == 0){
 		Choosetext = (typeof(Choosetext) != "undefined") ? Choosetext : "Bitte w&auml;hlen";
 		html = '&nbsp;&nbsp;<span class="error" id="' + eid + '">' + Choosetext + '</span>';
		new Insertion.After(element.id, html);
 		errors++;
 	}
 });
 if(errors == 0){
 	$(formid).submit();
 }
 return false;
}; 

var ClxShopProdukt = Class.create(); 
ClxShopProdukt.prototype = {
	initialize: function(produktid) {
		this.produktid = produktid;
	},	
	AttributwertByKennung: function(kennung, callback) {
		url = '/aapi/produkt_attributwert/' + this.produktid + '/' + kennung;
		new Ajax.Request(url, { method: 'get', onComplete: callback }); 
	},
	Grundpreis: function(callback) {
		url = '/aapi/produkt_grundpreis/' + this.produktid;
		new Ajax.Request(url, { method: 'get', onComplete: callback }); 
	},
	Alterpreis: function(callback) {
		url = '/aapi/produkt_alterpreis/' + this.produktid;
		new Ajax.Request(url, { method: 'get', onComplete: callback }); 
	},
	Versandgewicht: function(callback) {
		url = '/aapi/produkt_versandgewicht/' + this.produktid;
		new Ajax.Request(url, { method: 'get', onComplete: callback }); 
	},
	Lieferstatus: function(callback) {
		url = '/aapi/produkt_lieferstatus/' + this.produktid;
		new Ajax.Request(url, { method: 'get', onComplete: callback }); 
	},
	Artikelnummer: function(callback) {
		url = '/aapi/produkt_artikelnummer/' + this.produktid;
		new Ajax.Request(url, { method: 'get', onComplete: callback }); 
	}
}