$(document).ready(function() {
	 $("div[id^='flavour'] > div.deliveryTime").show();								 
	 $("select[id^='selectSubOption']").each( function(i) {
			var deliveryTimeImage = $('#deliveryTimeImage_' + this.title + '_' + $(this).val() ).html();
			if (deliveryTimeImage == null) {
				var deliveryTimeImage = $('#deliveryTimeImage_' + this.title).html();
			}		
			$('.productLeft > .productDelivery > .right').html(deliveryTimeImage);
			$('.productLeft > .productDelivery > .right').show();

		});
		
	 fillTotalFields();
});

function fillTotalFields() {
	var colorLevel = 0;
	var totalDeliveryText = 0;
	var totalDeliveryTimeImageSrc = 0;
	var totalDeliveryTimeImageText = ''; 

	$("select[id^='selectSubOption']").each( function(i) {
			var tmpColorLvl = 0;
			var deliveryTimeText = 0;
			var deliveryTimeTextArray = 0;
			var tempDeliveryText = 0;
			var deliveryTimeImageSrc = '';

			deliveryTimeImageSrc = $('#deliveryTimeImage_' + $(this).attr('title') + '_' + $(this).val() + ' > img').attr('src');
			if (deliveryTimeImageSrc == null) {
				deliveryTimeImageSrc = $('#deliveryTimeImage_' + $(this).attr('title') + ' img').attr('src');
			}


			deliveryTimeText = $('#selectSubOption' + $(this).attr('title') + ' option:selected').attr('title');
			deliveryTimeTextArray = deliveryTimeText.split(" ");
			
			/* check time */
			if ( deliveryTimeTextArray[1] == 'Tag(en)') {
				tempDeliveryText = deliveryTimeTextArray[0] * 1;
			} else if ( deliveryTimeTextArray[1] == 'Woche(n)') {
				tempDeliveryText = deliveryTimeTextArray[0] * 7;
			} else if ( deliveryTimeTextArray[1] == 'Monat(en)') {
				tempDeliveryText = deliveryTimeTextArray[0] * 30;
			}

			/* check image */
			if (deliveryTimeImageSrc == '/themes/default/img/availabilityRed.gif') {
				tmpColorLvl = 4;
			} else {
				if (deliveryTimeImageSrc == '/themes/default/img/availabilityOrange.gif' && tmpColorLvl != 4 ) {
					tmpColorLvl = 3;
				} else {
					if (deliveryTimeImageSrc == '/themes/default/img/availabilityGreenOrange.gif' && tmpColorLvl != 3 ) {
						tmpColorLvl = 2;
					} else {
						if (deliveryTimeImageSrc == '/themes/default/img/availabilityGreen.gif' && tmpColorLvl != 2 ) {
							tmpColorLvl = 1;
						}
					}
				}
			}
			
			/* set vars */
			if ( tmpColorLvl > colorLevel ) {
				colorLevel = tmpColorLvl;
			}
			if ( tempDeliveryText > totalDeliveryText ) {
				totalDeliveryText = deliveryTimeTextArray[0];
			}
	 });

	if (colorLevel == 4) {
		totalDeliveryTimeImageSrc = '/themes/default/img/availabilityRed.gif';
		totalDeliveryTimeImageText = availabilityAmountRedText;
	} else {
		if (colorLevel == 3 ) {
			totalDeliveryTimeImageSrc = '/themes/default/img/availabilityOrange.gif';
			totalDeliveryTimeImageText = availabilityAmountOrangeText;
		} else {
			if (colorLevel == 2 ) {
				totalDeliveryTimeImageSrc = '/themes/default/img/availabilityGreenOrange.gif';
				totalDeliveryTimeImageText = availabilityAmountGreenOrangeText;
			} else {
				if (colorLevel == 1 ) {
					totalDeliveryTimeImageSrc = '/themes/default/img/availabilityGreen.gif';
					totalDeliveryTimeImageText = availabilityAmountGreenText;
				}
			}
		}
	}
	// Lieferbarkeit setzen
	var deliveryTimeTextOutput = 'Lieferzeit: Innerhalb von ' + totalDeliveryText + ' Tag(en) versandfertig.';
	$('div.productLeft > div.productDelivery > div.left').html(deliveryTimeTextOutput);

	// Verfügbarkeits image setzen
	$('.productLeft > .productDelivery > .right img').attr('src', totalDeliveryTimeImageSrc );
	$('.productLeft > .productDelivery > .right img').attr('alt', 'Verfügbarkeit: ' + totalDeliveryTimeImageText );
	$('.productLeft > .productDelivery > .right img').attr('title', 'Verfügbarkeit: ' + totalDeliveryTimeImageText );
}

function fillCartValues(article_id) {
	if ( $('#selectOption').length > 0 ) {
		var option_id = $('#selectOption :selected').attr('value');

	} else if ( $('#optionIdSelected').length > 0 ) {
		var option_id = $('#optionIdSelected').attr('value');
	}
	if ( $('#flavour' + option_id + ' #selectSubOption' + option_id) ) {
		var subOption_id = $('#selectSubOption'+ option_id +' :selected').attr('value');
	}

	var article_id = article_id;
			
	$("input[id^='optionSelected']").attr('name','option_selected[' + article_id + '][' + option_id + ']');
	$("input[id^='optionSelected']").val( $('#options-article-id-' + option_id).val() );
	$('#quantity').attr('name', 'quantity[' + article_id + '][' + option_id + ']');
	
	$("div[id^='price']").hide();
	$("#price" + option_id).show();
	$("div[id^='vpe']").hide();
	$("#vpe" + option_id).show();
	
	$("div[id^='flavour']").hide();
	$("#flavour" + option_id).show();

	fillTotalFields();	
}
