$(document).ready(function(){
// $.cookie('poll_id', null);
	$('div#vote-holder input:radio').click(function(){	 
		$.getJSON(_http_host + 'modules/_load_votes.php', {poll_option_id:$(this).val()}, function(data) {
   $('div#vote-holder').html(data.html);
			poll_ids = $.cookie('poll_ids');
			if (typeof(poll_ids) == 'undefined' || poll_ids == null)
			 poll_ids = new Array();
			poll_ids[data.poll_id] = 1;
			$.cookie('poll_ids', poll_ids, {expires: 365, path: '/', domain: 'autoexpert.in.ua'});
		});
	});																											
});

function setOptions(select, arr, leave) {
	select.length = leave ? leave : 0;
	for (var i in arr) {
		select.options[select.length] = new Option(arr[i], i, false, false);
	}
	select.selectedIndex = 0;
	select.disabled = false;
	return;
}

var req_choose_test = false;

function choose_test_field_onchange() {
 ch = {};
	$.each($('form#choose-test input:checkbox:checked'),
								function(key, value){
									ch[value.name] = 1;									
								}
							);
	$.each($('form#choose-test input:text[value>""]'),
								function(key, value){
									ch[value.name] = value.value; 
								}
							);	
	if (pm = $('form#choose-test input#price-min').val())
	 ch['price_min'] = pm;
	if (pm = $('form#choose-test input#price-max').val())
	 ch['price_max'] = pm;
		
	$.get(_http_host + 'modules/_load_choose_test_table.php',
							ch,
							function(data) {
								$('#div-container').html(data);
								$('#div-container .model a').click(
									function(){ 
										$('form#choose-test input#input-make-id').val($(this).attr('rel'));
										$('form#choose-test').submit();										
										return false;
									}
								);
							}
						);
}

function choose_make_onchange(make_select, model_select, trim_level_select) {
	$('#' + model_select + ' option:gt(0)').remove();
 $('#' + model_select).attr('disabled', 'disabled');
 $('#' + trim_level_select + ' option:gt(0)').remove();
 $('#' + trim_level_select).attr('disabled', 'disabled');	
	$.ajax({
  url: _http_host + 'modules/_load_choose_make.php',
  dataType: 'json',
  data: {make_id: $('#' + make_select).val()},
  success: function(data){
			$('#' + model_select).attr('disabled', '');
            $('#' + trim_level_select).attr('disabled', '');													
												$.each(data.model_options, 
																			function(key, value) {   
																				$('#' + model_select).append($(document.createElement("option")).val(key).text(value)); 
																			});
			},
		error: function(r, s, e) {alert(s + e)}
 });

	/*$.getJSON(_http_host + 'modules/_load_choose_make.php', {make_id: $('#' + make_select).val()}, 
											function(data, status) {
												alert(status);
											 $('#' + model_select).attr('disabled', '');
            $('#' + trim_level_select).attr('disabled', '');													
												$.each(data.model_options, 
																			function(key, value) {   
																				$('#' + model_select).append($(document.createElement("option")).val(key).text(value)); 
																			});
											});*/
}

function choose_model_onchange(model_select, trim_level_select) {
 $('#' + trim_level_select + ' option:gt(0)').remove();
 $('#' + trim_level_select).attr('disabled', 'disabled');	
	$.ajax({
  url: _http_host + 'modules/_load_choose_model.php',
  dataType: 'json',
  data: {model_id: $('#' + model_select).val()}, 
  success: function(data){
			$('#' + trim_level_select).attr('disabled', '');																							
											$.each(data.trim_level_options, 
																		function(key, value) {   
																			$('#' + trim_level_select).append($(document.createElement("option")).val(key).attr('title',value).text(value)); 
																		});
			},
		error: function(r, s, e) {alert(s + e)}
 });
 
	/*$.getJSON(_http_host + 'modules/_load_choose_model.php', {model_id: $('#' + model_select).val()}, 
										function(data) {
           $('#' + trim_level_select).attr('disabled', '');																							
											$.each(data.trim_level_options, 
																		function(key, value) {   
																			$('#' + trim_level_select).append($(document.createElement("option")).val(key).attr('title',value).text(value)); 
																		});
										});			*/								
}

function update_compare_rows(tbl) {
	$('#' + tbl + ' tr:gt(3):visible:odd').css('background-color', '#e9eef1');
	$('#' + tbl + ' tr:gt(3):visible:even').css('background-color', '#f6f9fb');
}

function compare_init() {
	for (i in rows_tree) {		
		$('#tr-' + i).addClass('fold');
		for (j in rows_tree[i])
		 $('#tr-' + rows_tree[i][j]).hide();
	}
}

function shr(r) {
	subrows = rows_tree[r];
	tr = $('#tr-' + r);
	tr.toggleClass('fold');
	tr.toggleClass('unfold');	
	for (i in subrows) {
	 e = $('#tr-' + subrows[i]);
		//alert(e.is(':hidden'));
		if (e.is(':hidden'))
		 e.show();
		else	
		 e.hide();
	}
	update_compare_rows('tbl-compare');
	return false;
}