var imgs = [];
(function($) {

	$(document).ready(function() {
		var img = new Image;
		img.src = "/application/includes/images/select_list_up.gif";
		imgs.push(img);
		
		img = new Image;
		img.src = "/application/includes/images/select_list.gif";
		imgs.push(img);
		
		img = new Image;
		img.src = "/application/includes/images/select_list_bottom.gif";
		imgs.push(img);
		
		img = new Image;
		img.src = "/application/includes/images/select_single.gif";
		imgs.push(img);
	
		// var resolution = 1;
		// if($(window).width() <= 1220) {
			// resolution = 0;
			// $('body').addClass('site17');
		// }
		// $(window).resize(function() {
			// if($(this).width() <= 1220) {
				// $('body').addClass('site17');
				// resolution = 0;
			// } else {
				// $('body').removeClass('site17');
				// resolution = 1;
			// }
		// });
		
		//-------------------------------------------
		
		// $('#podbor_block .active, #podbor_block .select_button').live('click', function() {
			// if ($(this).parent().find('.option').length < 2)  return true;
			// $('#podbor_block .list').hide();
			// $(this).parent().find('.list').show();
			// $('#podbor_shadow').show();
		// });

		// $('#auto_mark .active, #auto_mark .select_button').live('click', function() {
			// $('#podbor_block .list').hide();
			// $(this).parent().find('.list').show();
			// $('#podbor_shadow').show();
		// });
		
		
		// $('#auto_mark .option').live('click', function() {
			// var id = $(this).attr('rel');
			// $('#auto_mark .active').attr('rel', id).html($(this).html());
			// $('#auto_mark .list').hide();
			
			// $('#auto_model .active').attr('rel','0').html('МОДЕЛЬ АВТОМОБИЛЯ');
			// $('#auto_model .list').html('');
			// $('#auto_year .active').attr('rel','0').html('ГОД ВЫПУСКА АВТОМОБИЛЯ');
			// $('#auto_year .list').html('');
			// $('#auto_type .active').attr('rel','0').html('МОДИФИКАЦИЯ АВТОМОБИЛЯ');
			// $('#auto_type .list').html('');
			
			// $('#podbor_shadow').hide();
			// $.ajax({
				// url:      '/podbor/getModels/'+id,
				// dataType: 'json',
				// cache:    false,
				// success:  function(res) {
					// $('#auto_model .list').html(res.result);
					// if (res.count == 1) {
						// var obj = $('#auto_model .option');
						// $('#auto_model .active').attr('rel', obj.attr('rel')).html(obj.html());
						// $('#auto_model .list').hide();
						// $('#auto_model').css('background-image','url("/application/includes/images/select_single.gif")');
						
						// $('#auto_year .list').html(res.nextresult);
						// if (res.nextcount == 1) {
							// obj = $('#auto_year .option');
							// $('#auto_year .active').attr('rel', obj.attr('rel')).html(obj.html());
							// $('#auto_year .list').hide();
							// $('#auto_year').css('background-image','url("/application/includes/images/select_single.gif")');
							
							// $('#auto_type .list').html(res.nextnextresult);
							// if (res.nextnextcount == 1) {
								// obj = $('#auto_type .option');
								// $('#auto_type .active').attr('rel', obj.attr('rel')).html(obj.html());
								// $('#auto_type .list').hide();
								// $('#auto_type').css('background-image','url("/application/includes/images/select_single.gif")');
							// } else $('#auto_type').css('background-image','url("/application/includes/images/select.gif")');
							
						// } else $('#auto_year').css('background-image','url("/application/includes/images/select.gif")');
					// } else $('#auto_model').css('background-image','url("/application/includes/images/select.gif")'); 
				// }
			// });
		// });
		
		// $('#auto_model .option').live('click', function() {
			// var id = $(this).attr('rel');
			// $('#auto_model .active').attr('rel', id).html($(this).html());
			// $('#auto_model .list').hide();
			
			// $('#auto_year .active').attr('rel','0').html('ГОД ВЫПУСКА АВТОМОБИЛЯ');
			// $('#auto_year .list').html('');
			// $('#auto_type .active').attr('rel','0').html('МОДИФИКАЦИЯ АВТОМОБИЛЯ');
			// $('#auto_type .list').html('');
			
			// $('#podbor_shadow').hide();
			// $.ajax({
				// url:      '/podbor/getYears/'+id,
				// dataType: 'json',
				// cache:    false,
				// success:  function(res) {
					// $('#auto_year .list').html(res.result);
					// if (res.count == 1) {
						// obj = $('#auto_year .option');
						// $('#auto_year .active').attr('rel', obj.attr('rel')).html(obj.html());
						// $('#auto_year .list').hide();
						// $('#auto_year').css('background-image','url("/application/includes/images/select_single.gif")');
						
						// $('#auto_type .list').html(res.nextresult);
						// if (res.nextcount == 1) {
							// obj = $('#auto_type .option');
							// $('#auto_type .active').attr('rel', obj.attr('rel')).html(obj.html());
							// $('#auto_type .list').hide();
							// $('#auto_type').css('background-image','url("/application/includes/images/select_single.gif")');
						// } else $('#auto_type').css('background-image','url("/application/includes/images/select.gif")');
					// } else $('#auto_year').css('background-image','url("/application/includes/images/select.gif")');
				// }
			// });
		// });
		
		
		// $('#auto_year .option').live('click', function() {
			// var id = $(this).attr('rel');
			// $('#auto_year .active').attr('rel', id).html($(this).html());
			// $('#auto_year .list').hide();
			
			// $('#auto_type .active').attr('rel','0').html('МОДИФИКАЦИЯ АВТОМОБИЛЯ');
			// $('#auto_type .list').html('');
			
			// $('#podbor_shadow').hide();
			// $.ajax({
				// url:      '/podbor/getTypes/'+id,
				// dataType: 'json',
				// cache:    false,
				// success:  function(res) {
					// $('#auto_type .list').html(res.result);
					// if (res.count == 1) {
						// obj = $('#auto_type .option');
						// $('#auto_type .active').attr('rel', obj.attr('rel')).html(obj.html());
						// $('#auto_type .list').hide();
						// $('#auto_type').css('background-image','url("/application/includes/images/select_single.gif")');
					// } else $('#auto_type').css('background-image','url("/application/includes/images/select.gif")');
				// }
			// });
		// });
		
		
		// $('#auto_type .option').live('click', function() {
			// var id = $(this).attr('rel');
			// $('#auto_type .active').attr('rel', id).html($(this).html());
			// $('#auto_type .list').hide();
			// $('#podbor_shadow').hide();
		// });
		
		// $('#podbor_block .podbor').click(function() {
			// var mark_id = $('#auto_mark .active').attr('rel');
			// var model_id = $('#auto_model .active').attr('rel');
			// var type_id = $('#auto_type .active').attr('rel');
			
			// if ( (mark_id == 0) || (model_id == 0) || (type_id == 0) ) {
				// alert('Заполнены не все поля формы');
				// return;
			// }
			
			// window.location = '/podbor/podbor/'+type_id;
		// });
		
		var model_type = '';
		var marks = '';
		var model = '';
		var spec = '';
		
		// -- определяем высоту #shadow
		$('#podbor_shadow').css('height', $('body').height()+'px');
		
		$('#podbor_shadow').click(function() {  
			$('#podbor_block .list').hide();
			$('.jScrollPaneWrap').hide();
			$(this).hide();
		});
		  
		$('#podbor_block .select').click(function() {		
			if ($(this).attr('rel') == 'scroll' && $(this).find('.option').length > 7) { 
				if (typeof($(this).data('scroll')) == 'undefined') {
					$(this).data('scroll', true).find('.list').css({  
						'display': 'block',
						'width': '224px',
						'height': '170px'					
					}).jScrollPane({scrollbarWidth:18, showArrows:true}).find('.option').css({ 
						'width': '93%',
						'background': 'none'
					}); 
					$('.jScrollPaneContainer').wrap('<div class="jScrollPaneWrap"></div>');  
					$('#podbor_shadow').show(); 
				} else {
					$(this).find('.jScrollPaneWrap').show();
					$(this).find('.list').show();
					$('#podbor_shadow').show(); 
				}				
			} else {  				
				var el = $(this).find('.list');	
				if (el.find('.option').length == 1) el.find('.option').addClass('single');
				if (el.css('display') == 'none') {
					el.show();
					$('#podbor_shadow').show();
				}  
				else {
					el.hide();
					$('#podbor_shadow').hide();
				}
			}
		});
		
		//определяемся с типом модели
		$('#model_type .option').live('click', function() {
			$(this).parent().parent().find('.active').html($(this).html());			
			$('#model_type_h').val($(this).attr('rel'));
			$.ajax({
				url: '/podbor/getMarks_ajax/' + $('#model_type_h').val() + '/',  
				dataType: 'html',
				success: function(data) {					
					$('#mark .list').html(data);
					$('#mark .active').html('Выбрать');
					$('#mark_h').val('');					
					$('#model_h').val('');
					$('#spec_h').val('');
					$('#model .list').html('');
					$('#model .active').html('Модель автомобиля');
					$('#spec .list').html('');
					$('#spec .active').html('Спецификация автомобиля');					
					$('#podbor_shadow').click();
				}
			});	
		});
		
		//определяемся с маркой
		$('#mark .option').live('click', function() {			
			$('#mark .active').html($(this).html());
			$('#mark_h').val($(this).attr('rel'));
			$.ajax({
				url: '/podbor/getModels_ajax/' + $(this).attr('rel') + '/' + $('#model_type_h').val() + '/',  
				dataType: 'html',
				success: function(data) {
					$('#model .list').html(data);
					$('#model .active').html('Выбрать');
					$('#model_h').val('');
					$('#spec_h').val('');
					$('#spec .list').html('');
					$('#spec .active').html('Спецификация автомобиля');
					$('#podbor_shadow').click();
				}
			});
		});
		
		//определяемся с моделью   
		$('#model .option').live('click', function() {
			$('#model .active').html($(this).html()); 
			$('#model_h').val($(this).attr('rel'));
			$.ajax({  
				url: '/podbor/getSpec_ajax/' + $(this).attr('rel') + '/',
				dataType: 'html',
				success: function(data) {
					$('#spec .list').html(data);
					$('#spec_h').val('');
					$('#spec .active').html('Выбрать');
					$('#podbor_shadow').click();
				}
			}) 
		});
		
		//определяемся со спецификацией
		$('#spec .option').live('click', function() {
			$('#spec_h').val($(this).attr('rel'));
			$('#spec .active').html($(this).html()); 	 
			$('#spec_h').val($(this).attr('rel'));
			$('#podbor_shadow').click();
		});
		
		$('.podbor').click(function() {
			if ($('#model_type_h').val() == '') {
				alert('Выберите тип автомобиля');
				return false;
			} else if ($('#mark_h').val() == '') {
				alert('Выберите марку автомобиля'); 
				return false;
			} else if ($('#model_h').val() == '') {
				alert('Выберите модель автомобиля');
				return false;  
			} else if ($('#spec_h').val() == '') {
				alert('Выберите спецификацию автомобиля');
				return false;
			} else location.href = '/podbor/getCatalogList/' + 
				$('#model_type_h').val() + '/' + 
				$('#mark_h').val() + '/' + 
				$('#model_h').val() + '/' +  
				$('#spec_h').val() + '/';
		});
		
	});	
})(jQuery)

