function Variants() {

}

Variants.prototype.id = '';
Variants.prototype.parentId = '';
Variants.prototype.layers = {};
Variants.prototype.layersTypes = {color: 'colors', options: 'options'};
Variants.prototype.layerData = {};
Variants.prototype.ajaxUrl = '';
Variants.prototype.ajaxPriceUrl = '';
Variants.prototype.imageUrl = '';
Variants.prototype.colorUrl = '';
Variants.prototype.maxLayer = 0;
Variants.prototype.parentPrice = 0;
Variants.prototype.parentTPrice = 0;
Variants.prototype.useStock = 0;
Variants.prototype.stockWarningLimit = 0;
Variants.prototype.useStockOnDefaultMessage = 0;
Variants.prototype.useStockOffDefaultMessage = 0;
Variants.prototype.stockOnDefaultMessage = '';
Variants.prototype.stockOffDefaultMessage = '';
Variants.prototype.langId = 0;
Variants.prototype.group = null;
Variants.prototype.vat= 0;
Variants.prototype.b2b = 0;
Variants.prototype.fPrice = '';

/* SETTER METHODS BEGIN */

Variants.prototype.setId = function(id) {
	this.id = id;
}

Variants.prototype.setParentId = function(id) {
	this.parentId = id;
}

Variants.prototype.addLayer = function(layerName, layerObject) {
	this.layers[layerName] = layerObject;
}

Variants.prototype.setAjaxUrl = function(url) {
	this.ajaxUrl = url;
}

Variants.prototype.setAjaxPriceUrl = function(url) {
	this.ajaxPriceUrl = url;
}

Variants.prototype.setColorUrl = function(url) {
	this.colorUrl = url;
}

Variants.prototype.setImageUrl = function(url) {
	this.imageUrl = url;
}

Variants.prototype.setMaxLayer = function(maxLayer) {
	this.maxLayer = maxLayer;
}

Variants.prototype.setParentPrice = function(price) {
	this.parentPrice = price;
}

Variants.prototype.setParentTPrice = function(price) {
	this.parentTPrice = price;
}

Variants.prototype.setUseStock = function(value) {
	this.useStock = value;
}

Variants.prototype.setStockWarningLimit = function(value) {
	this.stockWarningLimit = value;
}

Variants.prototype.setUseStockOnDefaultMessage = function(value) {
	this.useStockOnDefaultMessage = value;
}

Variants.prototype.setUseStockOffDefaultMessage = function(value) {
	this.useStockOffDefaultMessage = value;
}

Variants.prototype.setStockOnDefaultMessage = function(value) {
	this.stockOnDefaultMessage = value;
}

Variants.prototype.setStockOffDefaultMessage = function(value) {
	this.stockOffDefaultMessage = value;
}

Variants.prototype.setLangId = function(value) {
	this.langId = value;
}

Variants.prototype.setGroup = function(value) {
	this.group = value;
}

Variants.prototype.setVat = function(value) {
	this.vat = value;
}

Variants.prototype.setB2b = function(value) {
	this.b2b = value;
}

/* SETTER METHODS END */


/* GETTER METHODS BEGIN */

Variants.prototype.getLayerCount = function() {
	return this.layers.length;
}

/* GETTER METHODS END */

Variants.prototype.setFirstSize = function() {
	$('#varsel0 option:eq(1)').attr('selected', 'selected');
}

Variants.prototype.getVarSelect = function(layerId) {
	
	var varSelect = '';
	var layer = this.layers[layerId].id;
	
	for(var i = 0; i <= layer; i++) {
		varSelect += $('#varsel' + i).val();
		
		if(i < layer) {
			varSelect += '|';
		}
	}
	
	return varSelect;
}

Variants.prototype.getLayerData = function(layerId, nextLayerId) {
	var postParams = {
		oxparentid: this.id,
		oxvarselect: this.getVarSelect(layerId),
		langid: this.langId
	};
	
	if(this.group != null) {
		postParams.group = this.group;
	}
	
	var me = this; // Workaround for using "this" in jQuery functions
	
	$.postJSON(this.ajaxUrl, postParams, function(data) {
		me.layerData = data;
		me.parseLayerData(nextLayerId, data);
	});
}

Variants.prototype.parseLayerData = function(layerId, data) {
	var layerType = this.layers[layerId].type;
	
	if(layerType == 'colors') {
		this.setLayerColors(layerId, data);
	} else if(layerType == 'options') {
		this.setLayerOptions(layerId, data);
	}
}

Variants.prototype.setLayerData = function(layerId, content) {
	$('#' + layerId).html(content);
}

Variants.prototype.setLayerColors = function(layerId, data) {
	
	var colors = '<tr>';
	var j = 1;
	var colorUrl = this.colorUrl;
		
	$.each(data, function(i) {
		colors += '<td><img onmouseover="variants.showBigColor(\'' + this.oxvarselect.replace(/\//, "_") + '\', \'' + capitalize(this.oxvarselect) + '\'); variants.setDeliveryInfo(\'' + this.oxid + '\');" onclick="variants.setActiveArticle(\'' + this.oxid + '\'); variants.setSelectedValue(\'' + layerId + '\', \'' + this.oxvarselect.replace(/\//, "_") + '\'); changeArtImage(\'' + encodeURI(this.oxvarselect) + '\');" src="' + colorUrl + encodeURI(this.oxvarselect.replace(/\//, "_")) + '.png" id="' + this.oxid + '" name="' + capitalize(this.oxvarselect) + '" alt="' + capitalize(this.oxvarselect) + '" width="21" height="21" class="color"/></td>';
		
		if(j % 10 == 0) {
			colors += '</tr><tr>';
		}
		
		j++;		
	});
	
	colors += '</tr>';
	
	this.setLayerData(layerId, colors);
}

Variants.prototype.showBigColor = function(color, name) {
	var me = this;
	$('#colorBig').attr('src', me.colorUrl + encodeURI(color) + '.png').show();

	$('#colorName').html(name);
}

Variants.prototype.setLayerOptions = function(layerId, data) {
	var options = '<option value="">Bitte w&auml;hlen Sie:</option>';
	
	$.each(data, function(i) {
		var variants = this.oxvarselect.split('|');
		options += '<option value="' + variants[0] + '" title="' + this.oxid + '">' + variants[0] + '</option>';		
	});
	
	this.setLayerData(layerId, options);
}

Variants.prototype.getDeliveryInfo = function(id) {
	var article = this.layerData[id];
	var delText = '';
	
	switch(this.getStockStatus(id)) {
		case 0:
		
			//delText = '<img src="' + this.imageUrl + 'status_green.gif" width="8" heigth="8" alt="" border="0">&nbsp;';
			
		/*	
			if(article.oxstocktext) {
				delText += article.oxstocktext;
			} else {
				delText += this.stockOnDefaultMessage;
			}
		*/
			break;

		case 1:
			//delText = '<img src="' + this.imageUrl + 'status_red.gif" width="8" heigth="8" alt="" border="0">&nbsp;';
			
		/*	
			if(article.oxnostocktext) {
				delText += article.oxnostocktext;
			} else if(this.useStockOffDefaultMessage) {
				delText += this.stockOffDefaultMessage;
			}	
		*/
			break;
	}
	
	return delText;
}

Variants.prototype.getStockStatus = function(id) {
	var stockstatus = 0;
	var article = this.layerData[id];

	if(this.useStock && article.oxstockflag != 4) {
		if(article.oxstock <= 0) {
			stockstatus = 1;
		}		
	}
	
	return stockstatus;
}

Variants.prototype.setDeliveryInfo = function(id) {
	$('#delivery').html(this.getDeliveryInfo(id));
}

Variants.prototype.getPrice = function(id) {
	
	var price = parseFloat(this.layerData[id].oxprice);
	var tprice = parseFloat(this.layerData[id].oxtprice);
	
	var mainPrice = (this.parentPrice > 0 && this.parentTPrice < this.parentPrice) ? this.parentTPrice : this.parentPrice ;
	var varPrice = (tprice > 0 && tprice < price) ? tprice : price;
	var finalPrice = (varPrice != mainPrice) ? varPrice  : mainPrice;
	
	if(finalPrice > 0) {				
		finalPrice = formatPrice(finalPrice);
	} 
	
	return finalPrice;
}

Variants.prototype.getTPrice = function(id) {
	
	var tprice = parseFloat(this.layerData[id].oxtprice);
	var finalPrice = 0;
	
	if(tprice > 0) {				
		finalPrice = formatPrice(tprice);
	} 
	
	return finalPrice;
}

Variants.prototype.setPrice = function(id) {
	var price = this.getPrice(id);
	var tprice = this.getTPrice(id);
	
	this.setFPrice(price);
	
	if(tprice > 0) {
		$('#bargainPrice').html(tprice.replace(/\./g, ","));
		$('p.bargain').show();
	} else {
		$('p.bargain').hide();
	}
}

Variants.prototype.setFPrice = function(price) {
	var postData = {oxprice: price, vat: this.vat, returnNetPrice: this.b2b};
	
	if(price > 0) {
		$.postJSON(this.ajaxPriceUrl, postData, function(data) {
			$('.details #price').html(data.fPrice);
		});
	}
}

Variants.prototype.getArticleNumber = function(id) {
	return this.layerData[id].oxartnum;
}

Variants.prototype.setArticleNumber = function(articleNumber) {
	$('#articleNumber').html(articleNumber);
}

Variants.prototype.setActiveArticle = function(id, value) {
	$('form[name="basket"] input[name="aid"]').val(id);
	this.setPrice(id);
	this.setArticleNumber(this.getArticleNumber(id));
	this.setDeliveryInfo(id);
}

Variants.prototype.setSelectedValue = function(layerId, value) {
	this.layers[layerId].selected = value;
	this.showSelectedValue(layerId, value);
}

Variants.prototype.showSelectedValue = function(layerId, value) {
	var colorUrl = this.colorUrl;
	
	$('.selectedValues .' + layerId).each(function(i) {
		if($(this).hasClass('color')) {
			$(this).children('.content').html('<img src="' + colorUrl + encodeURI(value) + '.png" name="' + capitalize(value) + '" alt="' + capitalize(value) + '" width="30" height="30" /> ' + capitalize(value));
		} else {
			$(this).children('.content').html(value);
		}
	});
}

Variants.prototype.checkValues = function() {
	var me = this;
	var returnValue = true;
	
	$.each(this.layers, function(i) {
		if(this.selected === false) {
			me.showErrorMessage(this.name);
			
			returnValue = false;
			return false;
		}
	});
	
	return returnValue;
}

Variants.prototype.showErrorMessage = function(name) {
	//alert('Bitt wählen Sie ein/eine ' + name + ' aus!');
	alert('Bitte Größe oder Farbe o. ä. auswählen!');
}

