$(document).ready(function() {
	$('a.lightbox').lightbox();
	
	$('#search_mark, #search_model, #search_season, #search_spikes, #search_city, #search_condition').each(function() {
		if ($(this).val())
			$('#search_ext').show();
	});
	
	if (!$('#search_r').val()) {
		$('#other_basic_params').hide();
		$('#search_btn').attr('disabled', 'disabled');
	}
	
	// Включение\выключение расширенного поиска 
	$('#search_switch a').click(function() {
		if ($('#search_r').val()) {
			$('#search_ext').toggle();
			$(this).toggleText('Скрыть расширенный поиск', 'Расширенный поиск');
		}
		return false;
	});
	
	// Активация\деактивация возможностей поиска 
	$('#search_r').change(function() {
		var other_basic_params = $('#other_basic_params');
		var search_btn = $('#search_btn');
		if ($(this).val() > 0) {
			other_basic_params.show();
			search_btn.attr('disabled', '');
		}
		else {
			other_basic_params.hide();
			search_btn.attr('disabled', 'disabled');
			$('#search_ext').hide();
			$('#search_switch a').text('Расширенный поиск');
		}
	});
	
	$('#search_mark').change(function() {
		$('#search_model option:gt(0)').remove();
		if (!$(this).val()) return;

		$.getJSON('index.php', {module: 'ajax', func: 'getModels', mark_id: $(this).val()}, function(data) {
			if (!data) return;
			$.each(data, function(i, item) {
				$('<option></option>').text(item.name).val(item.id).appendTo('#search_model');
			});
		});
	});
	
	$('#search_model').change(function() {
		if (!$(this).val()) {
			$('#search_season').val('').attr('disabled', '');
			return;
		}
		
		$.get('index.php', {module: 'ajax', func: 'getSeason', model_id: $(this).val()}, function(data) {
			$('#search_season').val(data).attr('disabled', 'disabled');
		});
	});
	
	// Информация о компании 
	$('#about_link').click(function() {
		if (location.href.indexOf('results') > 0 || location.href.indexOf('filter') > 0)
			location.href = '?module=search#about';
		else
			$('#company_info').toggle();
		return false;
	});
	
	// Выбор в результатах поиска 
	$('#results_table .flag').attr('title', 'Выбрать').click(function() {
		var id = $(this).attr('id').replace('flag_', '');
		var flag = 'images/flag' + (($(this).attr('src').indexOf('flag_a') > 0) ? '' : '_a') + '.gif';
		
		$(this).attr('src', flag);
		
		$('#selected_results').text($.get('index.php', {module: 'ajax', func: $(this).attr('src').indexOf('flag_a') > 0 ? 'addToSelected' : 'removeFromSelected', id: id}, function(data) {
			$('#selected_results').text(data || 0);
			if (data > 0)
				$('#clear_selected a').show();
		}));
		
		return false;
	});
	
	$('#clear_selected a').click(function() {
		$.get('index.php', {module: 'ajax', func: 'clearSelected'});
		$('#selected_results').text(0);
		$('.flag').attr('src', 'images/flag.gif');
		$(this).hide();
		
		return false;
	});
	
	$('#selected_counter_link').click(function() {
		if (parseInt($('#selected_results').text()) < 1)
			return false;
	});
	
	// Отображение\скрытие детальной информации в результатах поиска 
	$('#results_table tbody tr.data').click(function() {
		var table_row = $(this);
		var details_row = table_row.next('tr.details');
		var mid = details_row.attr('id').replace(/[^\d]+/g, '');
		details_row.toggle();
		
		if (table_row.hasClass('viewed'))
			return;

		$.get('index.php', {module: 'ajax', func: 'setViewed', mid: mid}, function(data) {
			table_row.addClass('viewed');
		});
	});
	
	$('#search_season').change(function() {
		$('#search_spikes').attr('disabled', $(this).val() > 2 ? '' : 'disabled');
	});
	
	$.get('index.php', {module: 'ajax', func: 'getSeason', model_id: $('#search_model').val()}, function(data) {
		if (!data) return;
		$('#search_season').val(data).attr('disabled', 'disabled');
	});
	
	if (parseInt($('#selected_results').text()) < 1)
		$('#clear_selected a').hide();
	
	if (location.href.indexOf('#about') > 0)
		$('#company_info').show();
	
	//$('#search_form select').styleSelect({styleClass: "selectMedved"});
});
