$(document).ready(function(){
	jQuery.validator.setDefaults({
		debug: false,
		success: "valid"
	});

	if($("#wizard").length > 0){
		var root = $("#wizard").scrollable();
		var api = root.scrollable();
		
		api.onBeforeSeek(function(event, i) {
			$("#status li").removeClass("active").eq(i).addClass("active");
			
			$("#status li").eq(i-1).hide();
	
			if(i>=this.getSize()){
				$("#wizard").hide('slide', {direction:'up'});
			}
		});
	
	}

	
/*	$('#oldData').validate({
		rules: {
		    pass1: {
		    	required:'true',
		    	minlength: 5
		    	},
		    pass2: {

		      equalTo: '#pass1'

		    }
		},
		
		messages: {	
			pass1: {
				required: "Please provide a password",
				minlength: "Atleast 5"
			},
			pass2: {
				
				equalTo: "Please enter the same password as above",
				
			}
		}

		
	});
*/	
/*	if($('#oldCarsDialog') != undefined){
		$('#oldCarsDialog').dialog({ autoOpen: true,
			resizable: false,
		    position: 'center',
		    modal: false,
		    height: 1020,
		    width: 500});
	}*/
	
	$('.sendCarForm').each(function(){
		
		$(this).click(function(){
		//	alert($('.'+$(this).prev('input').val()).serialize());
			$('.indicator').show();
			//
			 $.ajax({
				  url: "/cars/recover_car",
				  type:'POST',
				  data:$('.'+$(this).prev('input').val()).serialize(),
				  dataType: 'json',
				  timeout:5000,
				  success: function(msg){
					//  var current = $("#wizard").data("scrollable").getIndex();
					  $('.indicator').hide();
					  api.next();
					 
					  

					  /*
					  if(msg['saved']){
						  $('#carForm_'+msg['id']+' .msg').addClass('greenish');
						  $('#carForm_'+msg['id']+' .msg').text(msg['msg']);
						setTimeout(function(){
							$('#carForm_'+msg['id']).hide("slideUp");
							setTimeout(function(){
								if ( $("#oldCarsDialog div.cars:visible").length === 0){
									$('#oldCarsDialog').dialog('close');
								}
								
							 },700);
							
						}, 1500);
						
					  }*/
				  },
				  error: function(){
					  $('.indicator').hide();
					  alert('Ajoneuvon tietojen muuttaminen epäonnistui. Voit muuttaa tietoja Omat sivut - Omat ajoneuvot kohdassa.');
					  api.next();
				  }
				});
			return false;
		});
	});
	
	$('.carBrands').each(function(){
		if($(this).parent('div').next('div').children('select').val() == '13848'){
			var carBrand = $(this);
			
			$.getJSON('/cars/get_models_ajax',
					{carId: $(this).val()},
					function(carTypes) {
					if(carTypes !== null) {
					
				//	populateCarModelList(carTypes, $(carBrand).parent('div').next('div').children('select'));
					
					var options = '';
					$.each(carTypes, function(index, carType) {
						   
						options += '<option value="' + carType['optionValue'] + '">' + carType['optionDisplay'] + '</option>';
					   
					   });
					$(carBrand).parent('div').next('div').children('select').html(' ');
					$(carBrand).parent('div').next('div').children('select').html(options);
					
					}
					});
			
		}
		
		//populateStationList(stationNames);
	/*	var options = '';
		$.each(carTypes, function(index, carTypes) {
			   
			options += '<option value="' + carTypes['optionValue'] + '">' + carTypes['optionDisplay'] + '</option>';
		   
		   });
		$('#CarCarTypeId').html(' ');
		$('#CarCarTypeId').html(options);
		//alert(stationNames);
	}*/
		
		
	/*	$(this).change(function() {
			var carBrand = $(this);
			  if($(this).val().length != 0) {
			  $.getJSON('/cars/get_models_ajax',
						{carId: $(this).val()},
						function(carTypes) {
						if(carTypes !== null) {
						
						//populateCarModelList(carTypes, $(carBrand).parent('div').next('div').children('select'));
						}
						});
			  }
			  });*/
	});

	$('.more').each(function(){
		var ref = $(this);
		$(ref).click(function(){
			var el = $(ref).attr('class').substring(5);
			
			$("#wizard").css('height', 750);
			$('#'+el).show('slide',{direction:'up'});
			
			$(ref).hide();
			$(ref).prev('a').show();
			return false;
		});
	});
	
	$('.less').each(function(){
		var ref = $(this);
		$(ref).click(function(){
			
			var el = $(ref).attr('class').substring(10);
			
			$('#'+el).hide('slide',{direction:'up'}, function(){
				$("#wizard").css('height', 330);
			});
			
			$(ref).hide();
			$(ref).next('a').show();
			return false;
		});
	});
	
	  
});

function populateCarModelList(carTypes, selectObj) {
	//var options = '<option value="">Valitse malli</option><option value="0">Lisää uusi</option>'; 
	var options  = '<option value="13856">Valitse malli</option>';
	
	$.each(carTypes, function(index, carType) {
		   options += '<option value="' + index + '">' + carType + '</option>';
		   });
	
	$(selectObj).html(options);	
	
}
