$(document).ready(function() {
	window.active_row = new Object();
	window.previous_delivery_time = new Array();
	window.active_row.id = null;
	window.active_row.type = null;
	window.init = true;
	initConfigurator = function() {
		window.previous_delivery_time[1] = 15;
		window.previous_delivery_time[3] = 4;
		$('input:radio').checkbox();
		updatePreview(null,null);
		validateQuantity();
		window.init = false;
	}
	updatePrice = function() {
		var price_url = window.price_url;
		cb = function(data) {
			data = data.split('||');
			$('#net_price').val(data[0]);
			$('#vat').val(data[1]);
			$('#total_price').val(data[2]);
			$('#weight').val(data[6]);
			$('#single_shipping').val(data[7]);
			$('#net_price_1').text(data[3]);
			$('#vat_1').text(data[4]);
			$('#single_price_1').text(data[9]);
			$('#total_price_1').text(data[5]);
			$('#total_price_2').text(data[5]);
			$('#single_shipping_1').text(data[8]);
			//alert(data[8]);
			if (data[1]==0) {
				$('#vat_1').parents('tr').hide();
				$('#vat_2').text('');
			} else {
				$('#vat_1').parents('tr').show();
				$('#vat_2').text(window.lang['incl_vat']);
			}
			$('#msg_price_calc').slideUp('normal');		
		}
		$('#net_price_1, #vat_1, #total_price_1, #single_price_1, #total_price_2, #vat_2').text('');
		$('#msg_price_calc').remove();	
		$('.overview_notice').after('<p id="msg_price_calc" class="hidden">'+window.lang.msg_price_calc+'</p>');
		$('#msg_price_calc').slideDown('fast');
		$.post(price_url,$('#form_configurator').serialize(),cb);
	}
	switchConfigurator = function(id) {
	
		window.switchActive = true;
		
		var valid = true;
		if (window.active_row.id!=null) {
			if (updateStaticValue(window.active_row.id,window.active_row.type,false,false)===false) {
				var valid = false;
			}
		}
		
		if (valid===true) {
			
		$('ul.tab_nav li').removeClass('selected');
		$('ul.tab_nav li#tab_'+id).addClass('selected');
		$('#overview_type').text($('ul.tab_nav li#tab_'+id).find('span').text());
		
		if (window.active_configurator != 3 && id == 3) {
			window.previous_delivery_time[1] = $('input[name="delivery_time"]:checked').val();
			$('#row_delivery_time input[value="'+window.previous_delivery_time[3]+'"]').click();		
			$('#hint_delivery_time_1').hide();
			$('#hint_delivery_time_3').show();
			$('#hint_quantity_1').hide();
			$('#hint_quantity_3').show();
			$('#hint_layers_3').show();
		} else if (window.active_configurator == 3 && id != 3) {
			window.previous_delivery_time[3] = $('input[name="delivery_time"]:checked').val();
			$('#row_delivery_time input[value="'+window.previous_delivery_time[1]+'"]').click();
			$('#hint_delivery_time_3').hide();
			$('#hint_delivery_time_1').show();
			$('#hint_quantity_3').hide();
			$('#hint_quantity_1').show();
			$('#hint_layers_3').hide();
		}		
		
		if (id==1) {
			// hide additional fields for configurator 2
			
			toggleFixed('row_layers',false);
			
			// thickness
			updateStaticValue('row_thickness','radio',true,true);
			toggleFixed('row_thickness',true);
			
			// surface
			updateStaticValue('row_surface','radio',true,true);
			toggleFixed('row_surface',true,1);
			
			// overlay
			updateStaticValue('row_overlay','multiple_radio',true,true);
			toggleFixed('row_overlay',true);
			
			// contour
			$('#subrow_contour .fixed_value').parent().show();
			$('#multiple_online_contour').parent().hide();
			
			$('#subrow_ridge_width').show();
						
			// layers
			$('#layers_additional').slideUp('normal');
			if ($('input[name="layers"]:checked').val()!='2'&&$('input[name="layers"]:checked').val()!='4') {
				$('#row_layers input[value="2"]').click();
				updateStaticValue('row_layers','radio',true,false);
			}
			
			$('#delivery_time_group_2').hide();			
			$('#delivery_time_group_5').show();
			updateStaticValue('row_delivery_time','radio',true,false);
			
			window.active_configurator = 1;
			$('#type').val('1');
			updateDependentFields('layers',$('input[name="layers"]:checked').val());
			
		} else if (id==2) {
					
			// show additional fields for configurator 2
			toggleFixed('row_thickness',false);
			toggleFixed('row_surface',false);
			toggleFixed('row_overlay',false);
			toggleFixed('row_layers',false);
			$('#subrow_contour .fixed_value').parent().hide();
			$('#multiple_online_contour').parent().show();
			if ($('#multiple_online_contour option:selected').val() == '2') {
				$('#subrow_ridge_width').hide();
			}
			$('#layers_additional').slideDown('normal');
			
			$('#delivery_time_group_2').hide();			
			$('#delivery_time_group_5').show();
			updateStaticValue('row_delivery_time','radio',true,false);
			
			window.active_configurator = 2;
			$('#type').val('2');
			updateDependentFields('layers',$('input[name="layers"]:checked').val());	
			
		} else if (id==3) {
			
			// show additional fields for configurator 3
			
			// surface
			updateStaticValue('row_surface','radio',true,true);
			toggleFixed('row_surface',true,3);
			flashRow('#row_surface');
			toggleFixed('row_thickness',false);
			toggleFixed('row_overlay',false);
			$('#subrow_contour .fixed_value').parent().hide();
			$('#multiple_online_contour').parent().show();
			if ($('#multiple_online_contour option:selected').val() == '2') {
				$('#subrow_ridge_width').hide();
			}
			$('#layers_additional').slideDown('normal');
			
			if ($('input[name="delivery_time"]:checked').val()==2) {
				//toggleFixed('row_layers',true);
				if ($('input[name="layers"]:checked').val()!='2') {
					$('#row_layers input[value="2"]').click();
					updateStaticValue('row_layers','radio',true,false);
				}
				updateDependentFields('layers',2);
			} else {
				toggleFixed('row_layers',false);
			}
			
			$('#delivery_type option').removeAttr('selected');
			$('#delivery_type option[value="1"]').attr('selected','selected');
			updateStaticValue('row_delivery_options','delivery_options',true,false);
			
			window.active_configurator = 3;
			$('#type').val('3');
						
			updateDependentFields('layers',$('input[name="layers"]:checked').val());				
			
			$('#delivery_time_group_5').hide();			
			$('#delivery_time_group_2').show();
			updateStaticValue('row_delivery_time','radio',true,false);
			
			if (validateQuantity()===false) {
				var target = $('.suboptions:visible').attr('id').replace(/_suboptions/,'');
				var dim = calculateMultipleArea(target);
				var q = Math.floor(1205300/((parseInt(dim[0])+6)*(parseInt(dim[1])+6)));
				$('input[name="quantity"]').val(q);
				updateStaticValue('row_quantity','text',false,false);
				$('#current_error_msg').slideUp('normal');
			}
			
			
		}
		validateQuantity();
		updatePrice();
		updatePreview(null,null);
		
		window.switchActive = false;
		
		}
	}
	showError = function(target,msg) {
		$('#current_error_msg').remove();
		$('#'+target+' .hint').before('<div id="current_error_msg" class="hint hint_error hidden"><p>'+msg+'</p></div>');
		$('#current_error_msg').slideDown('normal');
	}
	validateArea = function() {
		var target = $('input[name="multiple_options"]:checked').parents('.input_combi').next().attr('id').replace(/_suboptions/,'');
		var min_w = 30;
		var min_h = 30;
		var max_w = 426;
		var max_h = 271;
		var dim = calculateMultipleArea(target);
		if (dim[0]<min_w||dim[0]>max_w||dim[1]<min_h||dim[1]>max_h||isNaN(dim[0])||isNaN(dim[1])) {
			var msg = window.lang.error_check_values;
			if (dim[0]<min_w||dim[1]<min_h) {
				msg += window.lang.error_area_too_small;
			} else if (dim[0]>max_w||dim[1]>max_h) {
				msg += window.lang.error_area_too_big;
			}
			showError(target+'_suboptions',msg);
			return false;			
		} else {
			$('#current_error_msg').slideUp('normal');
			validateQuantity();
			return true;
		}
	}
	validateQuantity = function() {
		var target = $('.suboptions:visible').attr('id').replace(/_suboptions/,'');
		var dim = calculateMultipleArea(target);
		var q = $('#quantity').val();
		var max_area = 4000000;
		if (window.active_configurator == 3 || $('#row_delivery_time input:checked').val()=='5') {
			max_area = 1205300;
		}
		var max_q = Math.floor(max_area/((parseInt(dim[0])+6)*(parseInt(dim[1])+6)));
		$('#available_quantity').html(max_q);
		if (parseInt(q)>parseInt(max_q)) {
			$('#quantity').val(max_q);
			var u = $('#row_quantity').find('label').text().substr(1);
			$('#row_quantity .static').text(max_q+u);
			$('#overview_quantity').text(max_q+u);
		}
		if (isNaN(q)||(parseInt(q)!=parseFloat(q))) {
			showError('row_quantity',window.lang.error_check_values);
			return false;
		} else if (((parseInt(dim[0])+6)*(parseInt(dim[1])+6))*(parseInt(q))>max_area){
			/*
			if (window.active_configurator == 3) {
				showError('row_quantity',window.lang.error_max_area_3);
			} else {
				showError('row_quantity',window.lang.error_max_area_1);
			}
			*/
			return false;
		} else {
			$('#current_error_msg').slideUp('normal');
			return true;
		}
	}
	calculateMultipleArea = function(target) {
		var w = $('#'+target+'_dimensions_w').val();
		var h = $('#'+target+'_dimensions_h').val();
		var total_w = w;
		var total_h = h;
		var r = new Array(w,h);
		if (target=='multiple_online') {
			total_w = w * $('#'+target+'_count_x').val();
			total_h = h * $('#'+target+'_count_y').val();
			if ($('#'+target+'_suboptions input[name="'+target+'_border"]:checked').val()!='1') {
				var border_w = $('#multiple_online_border_width option:selected').text().split(' ');
				border_w = border_w[0];
				var border_type = $('#'+target+'_suboptions input[name="'+target+'_border"]:checked').val();
				if (border_type=='2') {
					total_h += border_w*2;
				} else if (border_type=='3') {
					total_w += border_w*2;
				} else if (border_type=='4') {
					total_h += border_w*2;
					total_w += border_w*2;
				}
			}
			if ($('#multiple_online_contour option:selected').val()!='2') {
				var ridge_w = $('#multiple_online_ridge_width option:selected').text().substr(0,3);
				total_w += ($('#'+target+'_count_x').val()-1)*ridge_w;
				total_h += ($('#'+target+'_count_y').val()-1)*ridge_w;
			}
			r[0] = total_w.toFixed(2);
			r[1] = total_h.toFixed(2);
			$('#multiple_total_w').text(r[0]);
			$('#multiple_total_h').text(r[1]);
			$('#multiple_online_total_w').val(r[0]);
			$('#multiple_online_total_h').val(r[1]);
		}
		window.current_multiple_area = r;
		return r;	
	}
	updateStaticValue = function(target,type,resetActiveRow,fixedValue) {
		var valid = true;
		var txt = '';
		if (type=='radio') {
			txt = $('#'+target).find(':checked').parent().parent().text().substr(2);
			$('#'+target).find('.static').text($.trim(txt));
			if ($('#'+target).find('input[name="delivery_time"]').length>0) {
				var tmp = $('#'+target).find(':checked').val();
				$('#overview_delivery').text(tmp);
				if (tmp==2) {
					//toggleFixed('row_layers',true);
					$('#row_layers input[value="2"]').click();
					$('#row_layers').find('.static').text('2');
					toggleFixed('row_service_print',true);
					$('#row_service_print input[value="1"]').click();
					var txt = $('#row_service_print input[value="1"]').parents('label').find('.label_value').text();
					$('#row_service_print').find('.static').text(txt);
					//flashRow('#row_layers');
					//flashRow('#row_service_print');
					//updateDependentFields('layers',2);
				} else {
					//toggleFixed('row_layers',false);
					toggleFixed('row_service_print',false);
				}
				validateQuantity();
			}
		} else if (type=='multiple_radio') {
			txt = $('#'+target+' .subrow:eq(0)').find(':checked').parents('label').find('.label_value').text();
			$('#'+target).find('.static_value1').text(txt);
			txt = $('#'+target+' .subrow:eq(1)').find(':checked').parents('label').find('.label_value').text();
			$('#'+target).find('.static_value2').text(txt);			
		} else if (type=='text') {
			if (target=='row_quantity') {
				if (validateQuantity()===false) {
					valid = false;
				} 
			} 
			if (valid===true) {
				txt0 = $('#'+target).find('input').attr('value');
				if (txt0==undefined) txt0 = '';
				txt = txt0 + $('#'+target).find('label').text().substr(1);
				$('#'+target).find('.static').text($.trim(txt));
				if ($('#'+target).find('#quantity').length>0) {
					$('#overview_quantity').text($.trim(txt0));
				}
			}
		} else if (type=='multiple_options') {
			if (validateArea()===true) {
				valid = true;
				var i = $('input[name="multiple_options"]:checked');
				txt = i.parents('label').find('span').text().split(' ');
				$('#'+target).find('.static .static_value').text(txt[0]);
				$('#'+target).find('.static .value1').text(window.current_multiple_area[0]);
				$('#'+target).find('.static .value2').text(window.current_multiple_area[1]);
			} else {
				valid = false;
			}
		} else if (type=='delivery_options') {
			var t1 = $('select[name="delivery_country"] option:selected').text();
			var t2 = $('select[name="delivery_type"] option:selected').text();
			if ($('select[name="delivery_type"]:hidden').length>0) {
				t2 = $('.fixed_delivery').text();
			}
			$('#'+target).find('.static .value_country').text(t1);
			$('#'+target).find('.static .value_type').text(t2);
		}
		if (valid===true) {
			$('#'+target).find('.row_button').find('a span').text(lang.edit);
			
			var cb = function(){
					if (fixedValue!==true) {
						$(this).parent().children('.static').show();
					}
					if (resetActiveRow===true) {
						window.active_row.id = null;
					}
					$(this).parent().parent().removeClass('active_row');
			}
			$('#'+target).find('.row_value').children('.edit').slideUp('fast',cb);
			$('#'+target).find('.row_icon').addClass('row_icon_done');
			var i = $('#'+target).find('input:first').attr('name');
			updateDependentFields(i,txt);
			updatePreview(i,txt);
			if (window.switchActive!==true) updatePrice();
			return true;
		} else {
			return false;
		}
	}
	updateDependentFields = function(id,value) {
		if (id=='layers') {
			$('#row_thickness .input_combi').show();
			if (value=='6') {
				$('#row_thickness input[value=1]').parents('.input_combi').hide();
				if ($('#row_thickness input:checked').val()=='1') {
					$('#row_thickness input[value=2]').click();
					updateStaticValue('row_thickness','radio',true,false);
					flashRow('#row_thickness');
				}
			} else if (value=='8') {
				$('#row_thickness input[value="1"]').parents('.input_combi').hide();
				$('#row_thickness input[value="2"]').parents('.input_combi').hide();
				if ($('#row_thickness input:checked').val()=='1'||$('#row_thickness input:checked').val()=='2') {
					$('#row_thickness input[value=3]').click();
					updateStaticValue('row_thickness','radio',true,false);
					flashRow('#row_thickness');
				}
			}
			if (value!='2') {
				toggleFixed('row_e_test',true);
				if (window.active_configurator == 1) {
					$('#row_overlay .fixed_inward').show();
				} else {
					$('#row_overlay .static_inward').show();	
					$('#subrow_inward_overlay').show();		
				}
				if (window.active_configurator == 3) {
					//toggleFixed('row_delivery_time',true);
					//$('#row_delivery_time input[value="4"]').click();
					//flashRow('#row_delivery_time');
					if ($('#row_delivery_time input:checked').val()=='2') {
						$('#row_delivery_time input[value="3"]').click();
					}
					$('#row_delivery_time input[value="2"]').parents('.input_combi').hide();
					updateStaticValue('row_delivery_time','radio',true,false);
				} else {
					toggleFixed('row_delivery_time',false);
					updateStaticValue('row_delivery_time','radio',true,false);
				}
			} else {
				toggleFixed('row_e_test',false);
				if (window.active_configurator == 1) {
					$('#row_overlay .fixed_inward').hide();
				} else {
					$('#row_overlay .static_inward').hide();	
					$('#subrow_inward_overlay').hide();		
				}
				$('#row_delivery_time input[value="2"]').parents('.input_combi').show();
				//toggleFixed('row_delivery_time',false);
				updateStaticValue('row_delivery_time','radio',true,false);
			}
		} 
	}
	updatePreview = function(id,value) {
		var materials = new Array('','lf','sn','ag','au','hal');
		$('.configurator_preview img').removeClass('front');
		$('.configurator_preview img').removeClass('front_text');
		$('.configurator_preview img').removeClass('front_layers');
		$('.configurator_preview img').hide();
		var material = materials[$('#row_surface input:checked').val()];
		if (window.active_configurator == 1) material = 'lf';
		if (window.active_configurator == 3) material = 'au';
		$('#'+material+'_basic').addClass('front');
		$('#'+material+'_basic').show();
		var text = $('#row_service_print input:checked').val();
		if (text!='1') {
			$('#'+material+'_text').addClass('front_text');
			$('#'+material+'_text').show();
		}
		var layers = $('#row_layers input:checked').val();
		if (layers!='2') {
			$('#'+material+layers).addClass('front_layers');
			$('#'+material+layers).show();
		}
		
	}
	toggleFixed = function(id,fixedBoolean,fixedType)  {
		id = '#'+id;
		if (fixedBoolean === true) {
			if (arguments.length>2) {
				$(id+' .row_value .fixed span').hide();
				$(id+' .row_value .fixed span#fixed_value_'+fixedType).show();
			}
			$(id+' .row_value .fixed').show();
			$(id+' .row_value .static').hide();
			$(id+' .row_button .disabled').show();
			$(id+' .row_button a').hide();
			$(id).addClass('fixed_value');
			$(id).attr('title',lang.fixed_feature);	
		} else {
			$(id+' .row_value .fixed').hide();
			$(id+' .row_value .static').show();
			$(id+' .row_button .disabled').hide();
			$(id+' .row_button a').show();
			$(id).removeClass('fixed_value');	
			$(id).removeAttr('title');
		}
	}
	flashRow = function(row_id) {
		/*
		$(row_id+' .flash_bar').remove();
		$(row_id).append('<div class="flash_bar"></div>');
		$(row_id+' .flash_bar').css('opacity','.5');
		$(row_id+' .flash_bar').fadeOut(2000);
		*/
	}
	var c = 1;
	$('#form_configurator .row').each(function(){
		if (!$(this).attr('id')) {											 
			$(this).attr('id','row'+c);
		}
		$(this).find('.row_icon').addClass('icon_no'+c);
		c++;
	}); 
	
	$('.configurator_body .row').click(function(){
													
		if ($(this).hasClass('fixed_value')===false) {
		
		var id = $(this).attr('id');
		var active_row = window.active_row.id;						
		var type = '';
		
		if ($(this).hasClass('simple_radio')) {
			type = 'radio';
		} else if ($(this).hasClass('simple_text')) {
			type = 'text';
		} else if ($(this).hasClass('multiple_text')) {
			type = 'multiple_text';
		} else if ($(this).hasClass('multiple_options')) {
			type = 'multiple_options';
		} else if ($(this).hasClass('multiple_radio')) {
			type = 'multiple_radio';
		} else if ($(this).hasClass('delivery_options')) {
			type = 'delivery_options';
		}
		
		if (active_row==id) {
			updateStaticValue(id,type,true,false);
		} else {
			var valid = true;
			if (window.active_row.id!=null) {
				if (updateStaticValue(window.active_row.id,window.active_row.type,false,false)===false) {
					valid = false;
				}
			}
			if (valid===true) {
				$(this).addClass('active_row');
				$(this).children('.row_value').children('.static').hide();
				$(this).children('.row_value').children('.edit').slideDown('fast', function() {
					if ($(this).parent().parent().hasClass('simple_text')) {
						var i = $(this).find('input');
						i.focus();
						i.select();
					}
				});
				$(this).find('.row_button').find('a span').text(lang.save);
				window.active_row.id = id;
				window.active_row.type = type;
			}
		}
		}
	}); 
	$('input:text').focus(function(){this.select();});
	$('.configurator_body .row .edit').click(function(){return false;});
	$('.configurator_body .fixed_value').attr('title',lang.fixed_feature);
	$('.configurator_body .suboptions input').blur(function() {
		var t = $(this).parents('.suboptions').attr('id').replace(/_suboptions/,'');
		calculateMultipleArea(t);
	});
	$('.configurator_body .suboptions select').change(function() {
		if ($(this).attr('id')=='multiple_online_contour') {
			if ($(this).find('option:selected').val()=='2') {
				$('#subrow_ridge_width').slideUp('normal');
			} else {
				$('#subrow_ridge_width').slideDown('normal');
			}
		}
		var t = $(this).parents('.suboptions').attr('id').replace(/_suboptions/,'');
		calculateMultipleArea(t);
	});
	$('.configurator_body select#delivery_country').change(function() {
		var c = $(this).find('option:selected').val();
		if (window.available_shipping[c].length>1) {
			$('.fixed_delivery').hide();
			$('#delivery_type').show();
		} else {
			$('.fixed_delivery').show();
			$('#delivery_type').hide();
		}
		if (window.available_shipping[c].length>2) {
			if ($('#delivery_type option').length<3) {
				$('#delivery_type option:last').after('<option value="80">'+window.lang.delivery_pickup+'</option>');
			}
		} else {
			$('#delivery_type option[value="80"]').remove();
		}
	});
	$('.configurator .tab_nav a').click(function() {
		switchConfigurator($(this).parent().attr('id').replace(/tab_/,''));
	});
	$('a.print_version').click(function() {
		var url = window.print_url;
		var orig_url = $('#form_configurator').attr('action');
		$('#form_configurator').attr('target','_blank');
		$('#form_configurator').attr('action',url);
		$('#form_configurator').submit();
		$('#form_configurator').attr('action',orig_url);
		$('#form_configurator').removeAttr('target');
	});
	$('#form_configurator_submit').click(function() {
		if ($('#net_price').val()!=0) {
			if (window.active_row.id==null) {
				$('#form_configurator').submit();
			}
		} 
		if (updateStaticValue(window.active_row.id,window.active_row.type,true,false)===true) {
			$('#form_configurator').submit();
		}
	});
	setTimeout(initConfigurator,500);
});