
Cufon.replace('.box h2', { fontFamily: 'Kozuka Gothic Pro OpenType' });

;(function($) {
	function initNavigation() {
		var origSelected = $('#main-navigation li.selected');
		var items = $('#main-navigation li').hover(
			function() {
				items.removeClass('selected');
				$(this).addClass('selected');
			},
			function() {
				items.removeClass('selected');
				origSelected.addClass('selected');
			}
		);
	}
	function initDropMenus() {
		$('dl.dropmenu.togglable:not(.dropmenu-active) dd').slideUp(0);
		$('dl.dropmenu.togglable > dt > a').live('click', function(e) {
			e.preventDefault();
			var sub = $(this).closest('dl').children('dd');
			if (sub.is(':visible')) {
				sub.slideUp(150, function() {
					$(this).parent('dl').removeClass('dropmenu-active');
				});
			}
			else {
				$(this).closest('dl').addClass('dropmenu-active');
				sub.slideDown(150);
			}
			this.blur();
		});
	}
	function initSelects() {
		$('select').each(function() {
			function update() {
				$(this).closest('.custom-select').toggleClass('custom-select-disabled', this.disabled);
				if (this.selectedIndex < 0) return;
				val.text(this.options[this.selectedIndex].text);
			}
			var val = $(document.createElement('span'));
			val.addClass('custom-select-value');
			var wrapper = $(document.createElement('span'));
			var body = $(document.createElement('span'));
			body.addClass('custom-select-body');
			var arrow = $(document.createElement('span'));
			arrow.addClass('custom-select-arrow');
			wrapper.addClass('custom-select')
				.append(body.append(val))
				.append(arrow)
				.insertBefore(this)
				.append(this)
				.attr('id', 'custom-select-' + this.name);
			$(this).click(update).change(update);
			$(this).bind('stateChange', update);
			update.call(this);
		});
	}
	$(function() {
		initNavigation();
		initDropMenus();
		initSelects();
	});
})(jQuery);

jQuery.clean.convert = function(html) {
  var n = document.createElement('div');
  n.innerHTML = html;
  return n;
};

$(document).ready(function() {

  function setEnabledState(el, enabled) {
    el.attr('disabled', !enabled);
    el.trigger('stateChange');
  }

  $('#brand').change(function() {
		$('#production_year, #model, #version, #tyre_size').html('<option></option>');

		setEnabledState($('#production_year, #model, #version, #tyre_size'), false);
    setEnabledState($('#search_button'), false);

		$.get('/tyre-selector-years', {
      make: $(this).val()
    }, function(xml) {
			$('#production_year, #model, #version, #tyre_size').empty();

      $('year', xml).each(function() {
        $('#production_year').append('<option value="'+$(this).text()+'">'+$(this).text()+'</option>');
      });

			$('#production_year, #model, #version, #tyre_size').prepend('<option disabled="true" selected="true">' + localeStrings.tyre_selector_select + '</option>');
      $('#production_year').get(0).selectedIndex = 0;
			$('#model').get(0).selectedIndex = 0;
			$('#version').get(0).selectedIndex = 0;
      $('#tyre_size').get(0).selectedIndex = 0;

			setEnabledState($('#production_year'), true);
    });
  });
	
	$('#production_year').change(function() {
		$('#model, #version, #tyre_size').html('<option></option>');
		
		setEnabledState($('#model, #version, #tyre_size'), false);
		setEnabledState($('#search_button'), false);
		
		$.get('/tyre-selector-models', {
			make: $("#brand").val(),
			year: $(this).val()
		}, function(xml) {
			$('#model, #version, #tyre_size').empty();
			
			$('model', xml).each(function() {
				$('#model').append('<option value="'+$(this).attr('id')+'">'+$(this).text()+'</option>');
			});

			$('#model, #version, #tyre_size').prepend('<option disabled="true" selected="true">' + localeStrings.tyre_selector_select + '</option>');
			$('#model').get(0).selectedIndex = 0;
			$('#version').get(0).selectedIndex = 0;
			$('#tyre_size').get(0).selectedIndex = 0;
			
			setEnabledState($('#model'), true);
		});
	});


	$('#model').change(function() {
		$('#version, #tyre_size').html('<option></option>');
		
		setEnabledState($('#version, #tyre_size'), false);
		setEnabledState($('#search_button'), false);
		
		$.get('/tyre-selector-versions', {
			make: $("#brand").val(),
			year: $("#production_year").val(),
			model: $(this).val()
		}, function(xml) {
			$('#version, #tyre_size').empty();
			$('version', xml).each(function() {
				$('#version').append('<option value="'+$(this).attr('tyres')+'">'+$(this).text()+'</option>');
			});

			$('#version, #tyre_size').prepend('<option disabled="true" selected="true">' + localeStrings.tyre_selector_select + '</option>');
			$('#version').get(0).selectedIndex = 0;
			$('#tyre_size').get(0).selectedIndex = 0;
			
			setEnabledState($('#version'), true);
		});
	});
	
	$('#version').change(function() {
		$('#tyre_size').html('<option></option>');
		
		setEnabledState($('#tyre_size'), false);
		setEnabledState($('#search_button'), false);

		$.get('/tyre-selector-tyres', {
			tyres: $(this).val()
		}, function(xml) {
			$('#tyre_size').empty();
			
			var tyresizes = {};
			$('tyre', xml).each(function() {
				// $('#tyre_size').append('<option value="'+$(this).attr('tyres')+'">'+$(this).text()+'</option>');
				var tmp = $(this).text().split("\\");
				tyresizes[ tmp[0] ]++;
				tyresizes[ tmp[1] ]++;
			});
			
			$.each(tyresizes, function (size, n) {
				var tmp = size.split("/");
				var sizestring = parseInt(tmp[0]) +"/"+ tmp[1] + tmp[2] + tmp[3];
				var sizestring2 = parseInt(tmp[0]) +":"+ tmp[1] +":"+ tmp[2] +":"+ tmp[3];
				$("#tyre_size").append('<option value="'+ sizestring2 +'">'+ sizestring +'</option>');
			});

			$('#tyre_size').prepend('<option disabled="true" selected="true">' + localeStrings.tyre_selector_select + '</option>');
			$('#tyre_size').get(0).selectedIndex = 0;

			setEnabledState($('#tyre_size'), true);
			setEnabledState($('#search_button'), true);
		});
	});

});

try {
  document.execCommand('BackgroundImageCache', false, true);
}
catch (e) {}
