$(function(){
	$("#a_ap_form_box, #a_query_form_box, #a_colors").fancybox({
		'transitionIn'		: 'none',
		'transitionOut'		: 'none'
	});
	
	$('#ap_types li a').click(function(){
		var $id = $(this).attr('rel');
		if($id<6){
			
			$('#ap_width select').html('');
			$.ajax({
			    dataType: 'json',
		    	type: 'POST',
		    	url:  '/appraisal/getWidths',
		    	data: { id: $id },
				beforeSend: function(){
		    		$('#ap_loader').show();
				},
		    	success: function(data) {
					$('#ap_loader').hide();
					$.each(data, function(){
						$('#ap_width select').append('<option value="'+this.id+'">'+this.value+' m</option>');
					});
		    	}
			});
			
			$('#ap_step_2').show();
			$('#ap_step_3, #ap_height').hide();
			$('#ap_types li a').removeClass('selected');
			
			$('#ap_send input[name="type"]').val($(this).attr('title'));
			
			$(this).addClass('selected');
		}else if($id == 6){
			$("#ap_step_2, #ap_step_3").hide();
			$("#a_query_form_box").click();
		}
	});
	
	$('#ap_width select').change(function(){
		$('#ap_height select').html('');
		$id = $(this).val();
		
		
		$('#ap_height select').html('');
		$.ajax({
		    dataType: 'json',
	    	type: 'POST',
	    	url:  '/appraisal/getHeights',
	    	data: { id: $id },
			beforeSend: function(){
	    		$('#ap_loader').show();
			},
	    	success: function(data) {
				$('#ap_loader').hide();
				$.each(data, function(){
					$('#ap_height select').append('<option value="'+this.id+'">'+this.value+' m</option>');
				});
	    	}
		});
		
		$('#ap_height').show();
		$('#ap_step_3').hide();
		
	});
	
	$('#ap_height select').change(function(){
		$id = $(this).val();
		
		$('#extra_1, #extra_6').html('');
		$('#extra_6').append('<option value="0">Wybierz</option>');
		
		$.ajax({
		    dataType: 'json',
	    	type: 'POST',
	    	url:  '/appraisal/getRelationData',
	    	data: { id: $id, type_id: $('#ap_types li a.selected').attr('rel') },
			beforeSend: function(){
	    		$('#ap_loader').show();
	    		$('#ap_step_3').hide();
			},
	    	success: function(data) {
				$('#extra_acrylic').val(data.price_extra_acrylic);
				for(i=0;i<=10;i++){
					$('#extra_1').append('<option value="'+(i*data.price_extra_width)+'">'+(i*10)+' cm</option>');
				}
				var $regions = {
					'dolnośląskie': 4,
					'kujawsko-pomorskie': 4,
					'lubelskie': 3,
					'lubuskie': 5,
					'mazowieckie': 4,
					'małopolska': 1,
					'opolskie': 3,
					'podkarpackie': 2,
					'podlaskie': 5,
					'pomorskie': 5,
					'śląskie': 2,
					'świętokrzyskie': 3,
					'warmińsko-mazurskie': 5,
					'wielkopolskie': 4,
					'zachodnio-pomorskie': 5,
					'łódzkie': 4
				};
				
				$.each($regions, function(name, val){
					$('#extra_6').append('<option value="'+data.price_region[val]+'">'+name+'</option>');
				});
				
				$('#ap_colors_box a').click(function(){
					$('select#color option').removeAttr('selected');
					$('select#color option[rel="'+$(this).attr('rel')+'"]').attr('selected', 'selected');
					$('select#color').change();
					$('#fancybox-close').click();
				});
				
				if($('#ap_types li a.selected').attr('rel')==2)
					$('#color option').val(parseInt(data.price_point)+100);
				else
					$('#color option').val(data.price_point);
				$('#color option:eq(0)').val('0');
				
				$('#ap_price span').text(data.price);
				$('#ap_send input[name="price"]').val(data.price);
				
				$('#ap_loader').hide();

				$('#ap_step_3 select option').removeAttr('selected');
				$('#ap_step_3 select option:eq(0)').attr('selected', 'selected');
				
				$('#ap_step_3').show();
				
	    	}
		});
		
	});
	
	
	$('select[id^="extra_"]').change(function(){
		if($(this).attr('id')=='extra_1'){
			
			$cm = parseFloat($('#extra_1 option[value="'+$(this).val()+'"]').text());
			
			if($cm<=10){
				$price = $gate_price[1];
			}else if($cm<=30){
				$price = $gate_price[2];
			}else if($cm<=50){
				$price = $gate_price[3];
			}
			
			var $i = 0;
			$('#extra_4 option').each(function(){
				$(this).val($price*$i);
				$i++;
			});
			
		}
		
		$('select#color').change();
	});
	
	$('select#color').change(function(){
		
		$all_price = parseInt($('#ap_price .ap_org').text());
		
		$('select[id^="extra_"]').each(function(){
			$price = parseInt($(this).val());
			if($(this).val()==null) $price = 0;
			$all_price += $price;
		});
		if($('select#color').val()!='0')
			$all_price += (parseFloat($('#extra_1 option:selected').text())/10)*$('#extra_acrylic').val();

		$all_price += ($(this).val()==null)?0:parseFloat($(this).val());

		changePrice($all_price);
		
	});
	
	$('#ap_form').submit(function(){
		var $error = false;
		$('select[name="region"]').parent('li').removeClass('ap_required_error');
		
		if($('select[name="region"] option:selected').text()=='Wybierz'){
			$error = true;
			$('select[name="region"]').parent('li').addClass('ap_required_error');
		}
		/*if($('select[name="color"]').val()=='0'){
			$error = true;
			$('select[name="color"]').parent('li').addClass('ap_required_error');
		}*/
		
		if( !$error ){
			
			$('#ap_form_box input[name="form_width"]').val($('select[name="width"] option:selected').text());
			$('#ap_form_box input[name="form_height"]').val($('select[name="height"] option:selected').text());
			$('#ap_form_box input[name="form_extra_door"]').val($('select[name="extra_door"] option:selected').text());
			$('#ap_form_box input[name="form_extra_window"]').val($('select[name="extra_window"] option:selected').text());
			$('#ap_form_box input[name="form_extra_window_2"]').val($('select[name="extra_window_2"] option:selected').text());
			$('#ap_form_box input[name="form_extra_gate"]').val($('select[name="extra_gate"] option:selected').text());
			$('#ap_form_box input[name="form_extra_height"]').val($('select[name="extra_height"] option:selected').text());
			$('#ap_form_box input[name="form_color"]').val($('select[name="color"] option:selected').text());
			$('#ap_form_box input[name="form_type"]').val($('#ap_send input[name="type"]').val());
			$('#ap_form_box input[name="form_region"]').val($('select[name="region"] option:selected').text());
			$('#ap_form_box input[name="form_price"]').val($('#ap_send input[name="price"]').val());
			
			$("#a_ap_form_box").click();
		}
		
		return false;
	});
	
	function checkForm(formName){
		var $return = false;
		
		$(formName+' input, '+formName+' select, '+formName+' textarea').parent('li').removeClass('ap_required_error');
		
		$(formName+' .ap_required').each(function(){
			if($(this).children('input').val()=='' || 
				$(this).children('textarea').val()=='' || 
				$(this).children('select').val()=='0'){
				
					$(this).addClass('ap_required_error');
					$return = true;
			}
		});
		
		return !$return;
	}
	
	
	$('#ap_form_box form').submit(function(){
		
		if( checkForm('#ap_form_box') ){
			
			var $data = {};
			$.each($('#ap_form_box form').serializeArray(), function(i, field){
				$data[field.name] = field.value;
			});
			
			$.ajax({
			    dataType: 'json',
		    	type: 'POST',
		    	url:  '/appraisal/sentAppraisal',
		    	data: $data,
				beforeSend: function(){
					$('#ap_form_box form ul, #ap_form_box form input[type="submit"]').hide();
					$('#ap_form_box .ap_alert').show().text('Wysyłanie...');
				},
		    	success: function(data) {
					$('#ap_form_box .ap_alert').text('Wysłano!');
		    	},
		    	error: function(XMLHttpRequest, textStatus, errorThrown){
		    		$('#ap_form_box .ap_alert').text('Wystąpił błąd, proszę spróbować póżniej');
		    	}
			});
		}
		
		return false;
	});
	
	
	$('#ap_query_form_box form').submit(function(){
		
		if( checkForm('#ap_query_form_box') ){
			var $data = {};
			
			$.each($('#ap_query_form_box form').serializeArray(), function(i, field){
				$data[field.name] = field.value;
			});
			
			$.ajax({
			    dataType: 'json',
		    	type: 'POST',
		    	url:  '/appraisal/sentQuery',
		    	data: $data,
				beforeSend: function(){
					$('#ap_query_form_box form ul, #ap_query_form_box form input[type="submit"]').hide();
					$('#ap_query_form_box .ap_alert').show().text('Wysyłanie...');
				},
		    	success: function(data) {
					$('#ap_query_form_box .ap_alert').text('Wysłano!');
		    	},
		    	error: function(XMLHttpRequest, textStatus, errorThrown){
		    		$('#ap_query_form_box .ap_alert').text('Wystąpił błąd, proszę spróbować póżniej');
		    	}
			});
		}
		
		return false;
	});
	
	function changePrice(price){
		$('#ap_price span:eq(0)').text(price);
		$('#ap_send input[name="price"]').val(price);
	}
});
