$('.objednavka_form').ready(
	function(){
		//selects
		$('.select_current').click(
			function(){
				var _this = $(this);
			
				//hide all selects_values from other selects
				$(document).find('.select_current').each(
					function(){
						if( $(this).parent().get(0) != _this.parent().get(0) ){
							$(this).parent().find('.select_values').hide();
						}
					}
				)
			
				$(this).parent().find('.select_values').slideToggle();		
			}
		);
		$('.select_value').click(
			function(){
				//hide select_values
				$(this).parent().parent().parent().find('.select_values').slideUp();					
				
				//find and update select_current
				var select_current = $(this).parent().parent().parent().find('.select_current');
				select_current.html( $(this).html() );
				
				//show price
				$('.'+select_current.attr('handle')+"_type").html( $(this).html() );
				$('.'+select_current.attr('handle')+"_price").html( $(this).attr('price') );
				
				//update price
				var service_price = 0;
				if($('.additional_service_price').html() != '' && $('.additional_service_price').html() != '&nbsp;'){
					service_price = parseInt( $('.additional_service_price').html() );
				}
				
				var wash_price = 0;
				if( $('.wash_program_price').html().length != 0){
					wash_price = parseInt( $('.wash_program_price').html() );
				}
				
				var sum_price =  wash_price + service_price;
				
				//20% already in price
				if( $('#dodavka').is(':checked') ||  $('#suv').is(':checked') ){
					sum_price = sum_price + sum_price / 100 * 20;
				}
				
				$('.cena_celkem').html( sum_price );
				
				//update input
				$( 'input[name="'+select_current.attr('handle')+'"]' ).val( $(this).html() + " - " + $(this).attr('price') );
			}
		)
		
		$('#dodavka').click(
			function(){
				if( $('input[name="price_percent"]').val() == '0'){
					var cena = parseInt( $('.cena_celkem').html() );
					cena_celkem = cena + cena / 100 * 20;
					$('.cena_celkem').html( cena_celkem );
					$('input[name="price_percent"]').val('1');			
					
					$('.priplatek').show();
				}
			}
		)
		
		$('#suv').click(
			function(){
				if( $('input[name="price_percent"]').val() == '0'){
					var cena = parseInt( $('.cena_celkem').html() );
					cena_celkem = cena + cena / 100 * 20;
					$('.cena_celkem').html( cena_celkem );
					$('input[name="price_percent"]').val('1');			
					
					$('.priplatek').show();
				}
			}
		)
		
		$('#osobni').click(
			function(){
				if( $('input[name="price_percent"]').val() == '1'){
					var cena = parseInt( $('.cena_celkem').html() );
					cena_celkem = cena / 120 * 100;
					$('.cena_celkem').html( cena_celkem );
					$('input[name="price_percent"]').val('0');			

					$('.priplatek').hide();
				}
			}
		)
		
		
		$('.objednavka_button').click(
			function(){
				var _parent = $(this).parent();
				
				var errors = 0;
				
				$(document).find('.required_input_check').each(
					function(){
						if( $(this).val().length == 0){
							errors++;
							$('.'+$(this).attr('handle') ).addClass('red');
						}else{
							$( '.'+$(this).attr('handle') ).removeClass('red');
						} 
					}
				)
				$(document).find('.required_select_check').each(
					function(){
						if( $(this).html().length == 0){
							errors++;
							$('.'+$(this).attr('handle') ).addClass('red');
						}else{
							$( '.'+$(this).attr('handle') ).removeClass('red');
						} 
					}
				)
				
				//send
				if(errors == 0){
					_parent.html( 'aaa '+$('#loading').html() );
					
					//create msg
					var msg = "";
					msg += 'Typ vozu: '+$('input[name="car_type"]').val()+"\n\r";
					msg += 'Mycí program: '+$('input[name="wash_program"]').val()+"\n\r";
					if( $('input[name="additional_service"]').val().length != 0 && $('input[name="additional_service"]').val() != '&nbsp;' ){
						msg += 'Doplňkové služby:: '+$('input[name="additional_service"]').val()+"\n\r";
					}
					msg += 'Termín mytí: '+$('input[name="time"]').val()+"\n\r";
					if( $('input[name="name"]').val().length != 0 ){
						msg += 'Jméno a příjmení: '+$('input[name="name"]').val()+"\n\r";
					}
					msg += 'Telefon: '+$('input[name="phone"]').val()+"\n\r";
					if( $('input[name="email"]').val().length != 0 ){
						msg += 'E-mail: '+$('input[name="email"]').val()+"\n\r";
					}	
					
					var processUrl = $(document).find('form').attr('action');
					$.ajax({ 
            		    type: "POST", 
		                url : processUrl, 
		                cache: false, 
		                processData: false, 
		                data: "msg=" + encodeURIComponent(msg), 
		                success: sendSuccess(_parent)
			        }); 	
				}else{
					$('.required_error').show();
				}
			}
		)
	}
);

function sendSuccess(_parent)  {
	_parent.html( '<span class="blue">Vaše objednávka <br>byla úspěšně odeslána.</span>' );
}

