//### HELPER ###//

function addEvent(obj, evType, fn) {
	
	if (obj.addEventListener) {
		obj.addEventListener(evType, fn, true);
		return true;
	}
	else if (obj.attachEvent) {
		var r = obj.attachEvent('on'+evType, fn);
		return r;
	}
	else {
		return false;
	}
}

function preload(img){
	if(document.images) { 
		if(!document.pr) document.pr=new Array();
		j= document.pr.length;
		document.pr[j]=new Image;
		document.pr[j].src=img;
	}
}

function doc(element) {
	return document.getElementById(element);
}



//### FUNCTIONS ###

productOptions = new Array();
var productOption = function (id, image, price, quant, value, model, valueicon, input) {
	this.id = id;
	this.image = image;
	preload(this.image);
	this.price = price;
	this.quant = quant;
	this.value = value;
	this.model = model;
	this.icon = valueicon;
	this.input = input;
}

var initiateProductAttributes = function () {

	prodimg = doc('productimg').innerHTML;
	prodmodel = doc('model').innerHTML;
	prodprice = doc('price').innerHTML;

	for (var option in articles) { 
		var icon = articles[option][8].split('|')[1];
		var value = articles[option][8].split('|')[0];
		productOptions[option] = new productOption(articles[option][0], articles[option][7], articles[option][4], articles[option][5], value, articles[option][3], icon, articles[option][6]);
		var basketPid = articles[option][2];
		var catUid = articles[option][1];
	}

	for (var option in productOptions) {

		if (productOptions[option].icon != '' && productOptions[option].icon != undefined && productOptions[option].icon != ' ') {
			var img = document.createElement("img");
			img.src = productOptions[option].icon;	
			img.setAttribute('title', productOptions[option].value);	
			img.id = 'icon_'+option;
			img.onclick = function () {
				changeOption(this.id.replace(/icon_/, ''));
			}
			doc('thumbs').appendChild(img);
		}
	}

	var form = document.createElement("form");
	form.setAttribute('name', 'basket');
	form.setAttribute('action', 'index.php?id='+basketPid);
	form.setAttribute('method', 'post');
	form.id = 'basket_form';
	
	var input = document.createElement("input");
	input.setAttribute('type', 'hidden'); 
	input.setAttribute('name', 'tx_commerce_pi1[catUid]');
	input.setAttribute('value', catUid);
	form.appendChild(input);

	doc('prodoptions').appendChild(form);

	doc('model').style.display = 'none';
	doc('price').style.display = 'none';
}

var changeOption = function (which) {
	
	var imgtitle = doc('prodtitle').firstChild.data+': '+productOptions[which].value;
	if (productOptions[which].image != '') doc('productimg').innerHTML = '<img src="'+productOptions[which].image+'" alt="'+imgtitle+'" title="'+imgtitle+'" />';
	else if (doc('productimg').innerHTML != prodimg) doc('productimg').innerHTML = prodimg;

	/*if (productOptions[which].model != '') doc('model').firstChild.data = prodmodel+productOptions[which].model;
	else doc('model').firstChild.data = '';

	if (productOptions[which].price != '') doc('price').firstChild.data = prodprice+productOptions[which].price;
	else doc('price').firstChild.data = '';

	var input = doc('qty_input') ? doc('qty_input') : document.createElement("input");
	input.setAttribute('type', 'text'); 
	input.setAttribute('size', '3');
	input.setAttribute('name', productOptions[which].input);
	input.setAttribute('value', productOptions[which].quant);
	input.className = 'qtyInput';
	input.id = 'qty_input';

	if (!doc('qty_input')) doc('basket_form').appendChild(input);

	if(!doc('submit')) {
		var input = document.createElement("input");
		input.setAttribute('type', 'submit');
		input.setAttribute('value', 'In den Warenkorb');
		input.id = 'submit';
		doc('basket_form').appendChild(input);
	}*/

}



// set Variables


// start everything off on window load
//addEvent(window, 'load', setProducts);


