var component_to_group = new Array();
var price_original;
var price = new Object();
var components_price = new Array();
var components_price_with_tax = new Array();
var components_tax = new Array();
var standard_group_price = new Array();
var standard_component_id = new Array();
var current_group_price = new Array();
var group_id;
var price_extra = 0;
var maingroup_price = new Array();
var subgroup_parent = new Array();

var tax_product = 0;
var tax_components = 0;
var tax_total = 0;

function setGroupId(group_id) {
    this.group_id = group_id;
}

function round_price(price){
    if (price == 0) return '0,00';
    var decimal_places = 2;
    var tmp_value = price;
    tmp_value = (tmp_value*100);
    tmp_value =  Math.round(tmp_value);
    tmp_value = tmp_value.toString()
    tmp_value_pre = tmp_value.substring(0,tmp_value.length-decimal_places);
    tmp_value_dez = tmp_value.substr(tmp_value.length-decimal_places,decimal_places);
    return tmp_value = tmp_value_pre + ',' + tmp_value_dez;
}

function changeConfig(component_id){
    if (component_id == null) {
    //first start

        //get price differences to default price and print
        jQuery(".component_box input").each(function() {
                var component_id = jQuery(this).attr('value');
                var group_id = component_to_group[component_id];

                current_component_price_with_tax = components_price_with_tax[component_id];
                if (current_group_price[group_id]) {
                    current_component_price = components_price[component_id]-current_group_price[group_id];
                } else {
                    current_component_price = components_price[component_id];
                }

                if (jQuery(this).attr('checked') && jQuery(this).attr('pre_config') != "true") {
                    jQuery("#tableRow_"+component_id+" .component_price").html(0)
                }
                else{
                	// Komponenten-Konfiguration durchführen (Preisberechnung, aktuellen Namen setzen usw.)
                	if(jQuery(this).attr('pre_config') == "true"){
        				changeConfig(component_id);
			        }

					// Preis formatieren
			        var current_component_price_formated = formatPrice(current_component_price_with_tax);
	                jQuery("#tableRow_"+component_id+" .component_price").html(current_component_price_formated)
                }
        });

    } else {
        //configuration changed
        var component_id = component_id;
        var component_name = jQuery('#component_name_'+component_id).html();
        var group_id = component_to_group[component_id];
        var group_name = jQuery('#subgroup_name_'+group_id).html();
        var component_name = jQuery('#component_name_'+component_id).html();
		var component_price = jQuery('#component_price_'+component_id).html();

        if (component_id == "nothing") {
            jQuery("#current_component_"+group_id).html("optional");
            jQuery("#current_component_price_"+group_id).html(0);
        } else {
            var current_component_price = components_price[component_id]-standard_group_price[group_id];
            var current_component_price_with_tax = components_price_with_tax[component_id];
            var current_component_price_formated = formatPrice(current_component_price_with_tax);

            // Komponenten Namen setzen
            var current_component_name = get_current_component_text(component_id, group_id);
			//alert(group_id);
        	jQuery("#current_component_"+group_id).html(current_component_name);
            jQuery("#current_component_"+group_id).attr("title", component_name);

        	// Komponenten-Preis setzen
            if (standard_component_id[group_id] == component_id) {
                jQuery("#current_component_price_"+group_id).html(0);
                jQuery("#current_component_"+group_id).html("-");	// Standard-Komponenten nicht anzeigen
            } else {
                jQuery("#current_component_price_"+group_id).html(current_component_price_formated);
            }
        }

        //change current group price
        if (component_id == "nothing") {
            price_extra -= current_group_price[group_id];
            current_group_price[group_id] = 0;

            maingroup_price[subgroup_parent[group_id]] -= current_group_price[group_id];
        } else {
            price_extra += components_price[component_id] - current_group_price[group_id];
            current_group_price[group_id] = components_price[component_id];
        }

        // Maingroup-Preis berechnen
        maingroup_price[subgroup_parent[group_id]][group_id] = components_price_with_tax[component_id];
        var current_maingroup_price = 0;
        var upgrade_counter = 0;
        jQuery.each(maingroup_price[subgroup_parent[group_id]], function(index, value) {
           	if(value){
		  		current_maingroup_price += value;
		  		upgrade_counter++;
		  	}
		});

		// Maingroup-Text erstellen
		var upgrade_text = "";
		if(upgrade_counter > 0){
			if(upgrade_counter == 1){upgrade_text = multi_upgrade_text_single;}
			else{upgrade_text = multi_upgrade_text.replace("%c", upgrade_counter);}

			maingroup_price[subgroup_parent[group_id]]['total'] = current_maingroup_price;
			jQuery("#current_maingroup_price_" + subgroup_parent[group_id]).html(formatPrice(maingroup_price[subgroup_parent[group_id]]['total']));
		}
		else{upgrade_text = "-"}
        jQuery("#current_maingroup_" + subgroup_parent[group_id]).html(upgrade_text);

        //adjust relative prices of other components
		if(show_relative_prices == "true"){
	        jQuery("#component_box_"+group_id+" input").each(function(index) {
	                var current_component_id = jQuery(this).attr('value');
					var current_component_price_with_tax = components_price_with_tax[component_id];

	                if (current_component_id == "nothing") {
	                    var current_component_price = -current_group_price[group_id];
	                    jQuery("#tableRow_"+current_component_id+"_"+group_id+" .component_price").html(formatPrice(current_component_price_with_tax));
	                    if (current_component_id == component_id) {
	                        jQuery("#tableRow_"+current_component_id+"_"+group_id+" .component_price").html(0)
	                    } else {
	                        jQuery("#tableRow_"+current_component_id+"_"+group_id+" .component_price").html(formatPrice(current_component_price_with_tax))
	                    }
	                } else {
	                    var current_component_price = components_price[current_component_id]-current_group_price[group_id];
	                    if (current_component_id == component_id) {
	                        jQuery("#tableRow_"+current_component_id+" .component_price").html(0);
	                    } else {
	                        jQuery("#tableRow_"+current_component_id+" .component_price").html(formatPrice(current_component_price_with_tax));
	                    }
	                }
	        });
        }

        //calculate extra price and total price
        if (price_extra != 0) {
        	// Preiszusammenfassung einblenden
        	if(show_calculate_box_permanent == "false"){
            	jQuery('#calculate_box').removeClass('hide');
            }

			// Steuern berechnen
            tax_components = (price_extra * tax_rate);
            tax_product = (price_original * tax_rate);
            tax_total = tax_product + tax_components;

            price_extra_brutto = price_extra + tax_components;

            // Preise aktualisieren
            jQuery("#price_components").html(formatPrice(price_extra_brutto));
            jQuery(".tax_total").html(formatPrice(tax_total));
            jQuery(".price_total").html(formatPrice(price_original + price_extra + tax_total));
        } else {
        	// Preiszusammenfassung ausblenden
        	if(show_calculate_box_permanent == "false"){
            	jQuery('#calculate_box').addClass('hide');
            }

            // Steuern berechnen
            tax_components = (price_extra * tax_rate);
            tax_product = (price_original * tax_rate);
            tax_total = tax_product + tax_components;

            // Preise aktualisieren
            jQuery("#price_components").html(formatPrice(price_extra));
            jQuery(".tax_total").html(formatPrice(tax_total));
            jQuery(".price_total").html(formatPrice(price_original + price_extra + tax_total));
        }
    }

    /**
     *
     * @access public
     * @return void
     **/
    function get_current_component_text(component_id, group_id){
		// Allgemeine Variablen
		var add_content = "...";
		var max_row_length = 66;

		// Komponenten-Variablen
		var current_component_name = jQuery('#component_name_'+ component_id).html();
		var current_component_price_with_tax = components_price_with_tax[component_id];
		var current_component_price_formated = formatPrice(current_component_price_with_tax);

		// Komponenten-Gruppen-Variable
		var group_name = jQuery('#subgroup_name_'+group_id).html();

		// maximale länge der Komponenten-Bezeichnung berechnen
		var price_length = (current_component_price_formated.length - 31);
       	var full_string_length = current_component_name.length + group_name.length + price_length;


		// Komponentennamen kürzen
       	if(full_string_length >= max_row_length){
       		var max_current_component_length = (max_row_length - (group_name.length + price_length) - add_content.length);
       		current_component_name = current_component_name.substring(0, max_current_component_length); // String kürzen
			current_component_name+= add_content; // ... An String setzen
       	}

		return current_component_name;
    }
}
