$.fn.tooltip = function(word, defn) {
	function innerHighlight(node, word, defn) {
		var skip = 0;
		if (node.nodeType == 3) {
			var pos = node.data.toUpperCase().indexOf(word);
			if (pos >= 0) {
				var spannode = document.createElement('span');
				spannode.className = 'ttText';
				spannode.setAttribute('alt', defn);
				var middlebit = node.splitText(pos);
				var endbit = middlebit.splitText(word.length);
				var middleclone = middlebit.cloneNode(true);
				spannode.appendChild(middleclone);
				middlebit.parentNode.replaceChild(spannode, middlebit);
				skip = 1;
			}
		} else if (node.nodeType == 1 && node.childNodes && !/(script|style)/i.test(node.tagName) && (node.className.indexOf('noTT') == -1)) {
			for (var i = 0; i < node.childNodes.length; ++i) {
				i += innerHighlight(node.childNodes[i], word, defn);
			}
		}
		return skip;
	}
	return this.each(function() {
		innerHighlight(this, word.toUpperCase(), defn);
	});
};

$(function() {
	$("#container").tooltip('ORWL','Outlined Raised White Letters');
	$("#container").tooltip('RWL','Raised White Letters');
	$("#container").tooltip('BSW','Black Sidewall');
	$("#container").tooltip('WSW','White Sidewall');
	$("#container").tooltip('tire size','Tire type, section width, aspect ratio and rim size');
	$("#container").tooltip('Type','Tire type (i.e., P-Metric, E-Metric, Light Truck/Flotation, PAX-continued mobility tires)');
	$("#container").tooltip('Section Width','The distance between the outside of a tire’s sidewalls, not including any lettering or designs');
	$("#container").tooltip('Aspect Ratio','The relationship of a tire’s sidewall height to its section width, expressed as a percentage');
	$("#container").tooltip('Rim Size','The diameter of the rim in inches');
	$("#container").tooltip('Tire Name','Brand name of the tire model');
	$("#container").tooltip('Vehicle Category','The general category of the vehicle for which a tire is designed, although not necessarily required');
	$("#container").tooltip('Original Equipment Size Match','Tires matching the exact size and speed rating as the original tires placed on the vehicle during manufacture.');
	$("#container").tooltip('Versions','The variable features a tire may offer, such as raised white lettering or run-flat capabilities. These features do not affect the tire’s vehicle size or application.');
	$("#container").tooltip('Options Available','The variable features a tire may offer, such as raised white lettering or run-flat capabilities. These features do not affect the tire’s vehicle size or application.');
	$("#container").tooltip('Standard Limited Warranty','All Uniroyal® tires have one of these warranties. It covers defects in workmanship and materials for the life of the original usable tread, or for 6 years from date of purchase, whichever occurs first. See the warranty for details.');
	$("#container").tooltip('Mileage Limited Warranty','Additional limited warranty that covers tire tread life (mileage)');
	$("#container").tooltip('Possible Substitutions','Tires that can be substituted for the original tires placed on the vehicle during manufacture.');
	$("#container").tooltip('All Season Tire','For year-round use in moderate weather and road conditions');
	$("#container").tooltip('Winter Tire','For use only during the winter season');
	$("#container").tooltip('Summer Tire','For use in dry and very mildly wet conditions');
	$("#container").tooltip('Manufacturer Performance Rating','How this tire performs (braking, wear-life, fuel efficiency, comfort, and off-road performance) when compared against other Michelin/Uniroyal/BFG tire lines');
	$("#container").tooltip('Wear Life','The usable life of the tread');
	$("#container").tooltip('Comfort','The tire’s ability to provide a cushioned ride by absorbing shocks and bumps in the road');
	$("#container").tooltip('Fuel Efficiency','The lack of resistance, or the ease with which the tire rolls on pavement, resulting in better fuel efficiency');
	$("#container").tooltip('Braking','The tire’s ability to grip pavement when stopping, bringing the vehicle to a halt more quickly.');
	$("#container").tooltip('Off Road','The tire’s overall ability to perform when driven on dirt or other loose and natural surfaces');
	$("#container").tooltip('Handling','Vehicle responsiveness');
	$("#container").tooltip('Tire Category','Passenger, Light Truck, High Performance, SUV, Winter, etc');
	$("#container").tooltip('Sidewall','Portion of tire between the tread and the bead. This area also contains the manufacturer’s markings (i.e., tire description, serial number, etc.)');
	$("#container").tooltip('Rim Width Range','Allowable rim widths for a specified tire size');
	$("#container").tooltip('Overall Diameter','Tire’s diameter in inches, without any load');
	$("#container").tooltip('Tread Depth','The depth of tread rubber, measured in 32nds of an inch. If a new tire has 10/32nds of rubber, you have 8/32nds of usable rubber. Tires must be replaced when the wear bars are visible at 2/32nds.');
	$("#container").tooltip('Revolutions per Mile','Also called RPM. This is the number of times a tire turns traveling one mile. RPM varies based on speed, load, and inflation pressure.');
	$("#container").tooltip('Speed Rating','An alphabetical code (A-Z) assigned to a tire that indicates the maximum speed at which the tire can carry a load under specified service conditions.');
	$("#container").tooltip('Load Index','An assigned number ranging from 0 to 279 that corresponds to the load-carrying capacity of a tire.');
	$("#container").tooltip('Load&#160;Index','An assigned number ranging from 0 to 279 that corresponds to the load-carrying capacity of a tire.');
	$("#container").tooltip('Maximum Load','The maximum amount of weight the tire can safely carry');

	$(".ttText").hover(function(e) {
		ttText = $(this).attr('alt');
		$tooltip = $("div.tooltip");
		$tooltip.html(ttText).css('visibility','hidden').show().css({
			position:'absolute',
			top: (((e.pageY+40+$tooltip.height()) < $(window).height()) ? (e.pageY+15) : (e.pageY-15-$tooltip.height()))+'px',
			left:(((e.pageX+50+$tooltip.width() ) < $(window).width() ) ? (e.pageX+25) : (e.pageX-25-$tooltip.width() ))+'px',
			visibility:'visible'
		});
	}, function() {
		$("div.tooltip").hide();
	});
});


var data_dir= "/uniroyal/tire-selector/data";

function filter(v) {
	if (v) v=v.toLowerCase().replace(/[^a-zA-Z0-9]/g, "_");
	return v;
}

function sizeTypeFilter(v) {
	if (v) v=v.toLowerCase().replace(/^[\r\n]+|[\r\n]+$/g, "").substring(0,(v.indexOf("(")-1));
	return v;
}

function urlEncode(v) {
	if(v) v=encodeURIComponent(v).replace(/%20/g,"+").replace(/%2F/g,"|");
	return v;
}

function trim(v) {
	if(v) v=v.replace(/^\s+|\s+$/g,"");
	return v;
}

$(function() {
	var submittable = false;
	var submitForm;
	$("div#note_winter_available p.button, div#note_only_winter_available p.button").addClass('clickable').click(function() {
		if($("input[name=winter_radio]:checked").val() == 'yes') {
			submittable = true;
			$(submitForm).attr('action', $(submitForm).attr('action')+'winter-tires');
			submitForm.submit();
		} else if($("input[name=winter_radio]:checked").val() == 'no') {
			submittable = true;
			$(submitForm).attr('action', $(submitForm).attr('action')+'tires');
			submitForm.submit();
		}
	});
	
	$("form#search_dropdowns, form#find_vehicle, form#find_size, form#find_name, form#find_category").submit(function() {
		homepage = false;
		if($(this).attr('id').substring(0,5) == 'find_') homepage = true;
		year	= $("#year option:selected").val();
		make	= $("#make option:selected").val();
		model	= $("#model option:selected").val();
		option	= $("#options option:selected").val();
	
		tireSizeType		= $("#tireSizeType option:selected").val();
		tireSectionWidth	= $("#tireSectionWidth option:selected").val();
		tireAspectRatio		= $("#tireAspectRatio option:selected").val();
		tireRimDiameter		= $("#tireRimDiameter option:selected").val();
		
		tireName			= $('#tireName option:selected').val();

		tireCategory		= $('#tireCategory option:selected').val();
	
		if (typeof(year) != "undefined" && year.length>0 && filter(year) != 'please_select____' && typeof(make) != "undefined" && make.length>0 && filter(make) != 'please_select____' && typeof(model) != "undefined" && model.length>0 && filter(model) != 'please_select____' && typeof(option) != "undefined" && option.length>0 && filter(option) != 'please_select____') {
			if(submittable) return true;
			$(this).attr('action','/tire-selector/vehicle/'+urlEncode(year)+'/'+urlEncode(make)+'/'+urlEncode(model)+'/'+urlEncode(option)+'/');
			submitForm = this;
			if(homepage) {
				$.getJSON("/tire-selector/tireselector/testVehicleWinter?IS_MULTIBRAND=true&year="+urlEncode(year)+"&make="+urlEncode(make)+"&model="+urlEncode(model)+"&option="+urlEncode(option), function(data) {
					if(data.winter == "true" && data.nonWinter == "true") {
						// BOTH
						$("#note_winter_available").show();
						$("ul.dropdowns").hide();
					} else if(data.winter == "true") {
						// WINTER ONLY
						$(submitForm).attr('action', $(submitForm).attr('action')+'winter-tires');
						submittable = true;
						$(submitForm).submit();
					} else {
						$(submitForm).attr('action', $(submitForm).attr('action')+'tires');
						submittable = true;
						$(submitForm).submit();
					}
				});
				return submittable;
			} else {
				if(location.href.indexOf('/winter-tires')!=-1 || location.href.indexOf('/compare-winter-tires') !=-1) $(submitForm).attr('action', $(submitForm).attr('action')+'winter-tires');
				else $(submitForm).attr('action', $(submitForm).attr('action')+'tires');
			}
			return true; 
		} else if (typeof(tireSizeType) != "undefined" && tireSizeType.length>0 && filter(tireSizeType) != 'please_select____' && typeof(tireSectionWidth) != "undefined" && tireSectionWidth.length>0 && filter(tireSectionWidth) != 'please_select____' && typeof(tireAspectRatio) != "undefined" && tireAspectRatio.length>0 && filter(tireAspectRatio) != 'please_select____' && typeof(tireRimDiameter) != "undefined" && tireRimDiameter.length>0 && filter(tireRimDiameter) != 'please_select____') {
			if(submittable) return true;
			$(this).attr('action','/tire-selector/size/'+urlEncode(tireSizeType)+'/'+urlEncode(tireSectionWidth)+'/'+urlEncode(tireAspectRatio)+'/'+urlEncode(tireRimDiameter)+'/tires');
			submitForm = this;
			if(homepage) {
				$.getJSON("/tire-selector/tireselector/testVehicleWinter?IS_MULTIBRAND=true&tireSizeType="+urlEncode(tireSizeType)+'&tireSectionWidth='+urlEncode(tireSectionWidth)+'&tireAspectRatio='+urlEncode(tireAspectRatio)+'&tireRimDiameter='+urlEncode(tireRimDiameter), function(data) {
					if(data.winter == "true" && data.nonWinter == "true") {
						// BOTH
						$("#note_winter_available").show();
					} else if(data.winter == "true") {
						// WINTER ONLY
						$(submitForm).attr('action', $(submitForm).attr('action')+'winter-tires');
						submittable = true;
						$(submitForm).submit();
					} else {
						$(submitForm).attr('action', $(submitForm).attr('action')+'tires');
						submittable = true;
						$(submitForm).submit();
					}
				});
				return submittable;
			}
			return true;
		} else if (typeof(tireName) != "undefined" && tireName.length>0 && filter(tireName) != 'please_select____') {
			submittable = true;
			$(this).attr('action','/tire-selector/name/'+tireName+'-tires');
			return submittable;
		} else if (typeof(tireCategory) != "undefined" && tireCategory.length>0 && filter(tireCategory) != 'please_select____') {
			submittable = true;
			$(this).attr('action','/tire-selector/category/'+tireCategory+'-tires');
			return submittable;
		} else {
			submittable = false;
			return submittable;
		}
	});

	$("form#search_dropdowns #search_dropdowns_submit").addClass('clickable').click(function() {
		$("form#search_dropdowns").submit();
	}).bind('keyup', function(e) {
		if(e.keyCode == 13) {
			$("form#search_dropdowns").submit();
		}
	});

	$("form[id^=find_] img[id$=submit]").addClass('clickable').click(function() {
		$(this).parents("form").submit();
		// $("form#search_dropdowns").submit();
	}).bind('keyup', function(e) {
		if(e.keyCode == 13) {
			// $("form#search_dropdowns").submit();
		}
	});

	// Tire Names
	var tireName			= $('#tireName option').val();
	if(typeof(tireName) != "undefined" && tireName.length>0) {
		$.ajax({
			type: "GET",
			url: '/tire-selector/tireselector/getBrandLinesForDropdown',
			success: function(data) {
					$("#tireName").html('');
					var r=data;
					var r=r.split("||");
					if (r.length>1) {
						$("#tireName").append('<option value="Please select ...">Choose a tire name ...</option>');
						$(r).each(function() {
							var s=this.split('~');
							if(tireName.toLowerCase().replace(/[ \/]/g,'-').replace(/[^a-z0-9-]/g,'') == s[0]) {
								$("#tireName").append('<option selected="selected" value="'+s[0]+'">'+s[1]+'</option>');
							} else {
								$("#tireName").append('<option value="'+s[0]+'">'+s[1]+'</option>');
							}
						});
					} else {
						$("#tireName").html('').append('<option value="'+r+'">'+r+'</option>');
					}
			},
			error: function(a,b,c) {
				$("#tireName").html('');
			}
		});
	}
	$("#tireName").change(function() {
		if($("#tireName option:selected").val() != 'Please select ...') {
			dcsMultiTrack("DCS.dcsuri", "/funnel_action/tsn/step_1", "WT.ti", "Funnel Action > TSN > Step 1", "WT.si_n", "TSN", "WT.si_x", "1");
		}
	});


	var tireCategory		= $('#tireCategory option').val();
	if(typeof(tireCategory) != "undefined" && tireCategory.length>0) {
		$.ajax({
			type: "GET",
			url: '/tire-selector/tireselector/getCategories',
			success: function(data) {
					$("#tireCategory").html('');
					var r=data;
					var r=r.split("||");
					if (r.length>1) {
						$("#tireCategory").append('<option value="Please select ...">Choose a category ...</option>');
						$(r).each(function() {
							var s=this.split('~');
							// if(tireCategory.toLowerCase().replace(/[^a-z0-9 -]/g,'').replace(/[ \/]+/g,'-') == s[0]) {
							if(tireCategory.toLowerCase().replace(/[^a-z0-9 -]/g,'').replace(/[ \/]+/g,'-') == s[0]) {
								$("#tireCategory").append('<option selected="selected" value="'+s[0]+'">'+s[1]+'</option>');
							} else {
								$("#tireCategory").append('<option value="'+s[0]+'">'+s[1]+'</option>');
							}
						});
					} else {
						$("#tireCategory").html('').append('<option value="'+r.toLowerCase().replace(/[ \/]/g,'-').replace(/[^a-z0-9-]/g,'')+'">'+r+'</option>');
					}
			},
			error: function(a,b,c) {
				$("#tireCategory").html('');
			}
		});
	}
});


// 	|	BY_VEHICLE		|
var prefillVehicle = false;
var year, make, model, option;

$(function() {
	year	= $("#year option").val();
	make	= $("#make option").val();
	model	= $("#model option").val();
	option	= $("#options option").val();

	if (typeof(year) != "undefined" && year.length>0 && typeof(make) != "undefined" && make.length>0 && typeof(model) != "undefined" && model.length>0 && typeof(option) != "undefined" && option.length>0) {
		prefillVehicle = true;
	}

	$.ajax({
		type: "GET",
		url: data_dir+'/ymmo/years.txt',
		success: function(data) {
				$("#year").html('');
				var r=data.replace(/^[\r\n]+|[\r\n]+$/g, "");
				if(r.substring(r.length-2,r.length)=="||") r=r.substring(0,r.length-2);
				var r=r.split("||");
				r = r.reverse();
				if (r.length>1) {
					if(!prefillVehicle) {
						$("#year").append('<option value="Please select ..." selected="selected">Choose a year ...</option>');
					} else {
						$("#year").append('<option value="Please select ...">Choose a year ...</option>');
					}
					$(r).each(function() {
						if(prefillVehicle && (year == this)) {
							$("#year").append('<option selected="selected" value="'+this+'">'+this+'</option>');
						} else {
							$("#year").append('<option value="'+this+'">'+this+'</option>');
						}
					});
				} else {
					$("#year").html('').append('<option value="'+r+'">'+r+'</option>').change();
				}
				$("#year").change();
				$("#tire_module_loading").hide();
				$("#tire_module select").show();
		},
		error: function(a,b,c) {
			$("#year").html('');
		}
	});

	$("#year").change(function() {
		submittable = false;
		if(prefillVehicle && year != $(this).val()) prefillVehicle = false;
		var my_year = filter($(this).val());
		$("#make, #model, #options, #by-vehicle-btn").html('');

		if(my_year != 'please_select____') {
			$.ajax({
				type: "GET",
				url: data_dir+'/ymmo/'+my_year+'.makes.txt',
				success: function(data) {
					dcsMultiTrack("DCS.dcsuri", "/funnel_action/tsv/step_1", "WT.ti", "Funnel Action > TSV > Step 1", "WT.si_n", "TSV", "WT.si_x", "1");
					var r=data.replace(/^[\r\n]+|[\r\n]+$/g, "");
					if(r.substring(r.length-2,r.length)=="||") r=r.substring(0,r.length-2);
					var r=r.split("||");
					if (r.length>1) {
						$("#make").html('').append('<option value="Please select ...">Choose a Make ...</option>');
						$(r).each(function() {
							if(prefillVehicle && (make == this)) {
								$("#make").append('<option selected="selected" value="'+this+'">'+this+'</option>');
							} else {
								$("#make").append('<option value="'+this+'">'+this+'</option>');
							}
						});
						$("#make").change();
					} else {
						$("#make").html('').append('<option value="'+r+'">'+r+'</option>').change();
					}
				},
				error: function(a,b,c) {
					$("#make").html('');
				}
			});
		} else {
			$("#make").html('');
		}
	});

	$("#make").change(function() {
		submittable = false;
		if(prefillVehicle && make != $(this).val()) prefillVehicle = false;
		var my_year = filter($("#year").val());
		var my_make = filter($(this).val());
		$("#model, #options, #by-vehicle-btn").html('');
		if(my_make != 'please_select____') {
			$.ajax({
				   type: "GET",
				   url: data_dir+'/ymmo/'+my_year+"."+my_make+'.models.txt',
				   success: function(data) {
					dcsMultiTrack("DCS.dcsuri", "/funnel_action/tsv/step_2", "WT.ti", "Funnel Action > TSV > Step 2", "WT.si_n", "TSV", "WT.si_x", "2");
					var r=data.replace(/^[\r\n]+|[\r\n]+$/g, "");
						if(r.substring(r.length-2,r.length)=="||") r=r.substring(0,r.length-2);
						var r=r.split("||");
						if (r.length>1) {
							$("#model").html('').append('<option value="Please select ...">Choose a model ...</option>');
							$(r).each(function() {
								if(prefillVehicle && (model == this)) {
									$("#model").append('<option selected="selected" value="'+this+'">'+this+'</option>');
								} else {
									$("#model").append('<option value="'+this+'">'+this+'</option>');
								}
							});
							$("#model").change();
						} else {
							$("#model").html('').append('<option value="'+r+'">'+r+'</option>').change();
						}
				   },
					error: function(a,b,c) {
						$("#model").html('');
					}
			});
		} else {
			$("#model").html('');
		}
	});

	$("#model").change(function() {
		submittable = false;
		if(prefillVehicle && model != $(this).val()) prefillVehicle = false;
		var my_year = filter($("#year").val());
		var my_make = filter($("#make").val());
		var my_model = filter($(this).val());
		$("#options, #by-vehicle-btn").html('');
		if(my_model != 'please_select____') {
			$.ajax({
				type: "GET",
				url: data_dir+'/ymmo/'+my_year+'.'+my_make+'.'+my_model+'.options.txt',
				success: function(data) {
					 dcsMultiTrack("DCS.dcsuri", "/funnel_action/tsv/step_3", "WT.ti", "Funnel Action > TSV > Step 3", "WT.si_n", "TSV", "WT.si_x", "3");
					var r=data.replace(/^[\r\n]+|[\r\n]+$/g, "");
					if(r.substring(r.length-2,r.length)=="||") r=r.substring(0,r.length-2);
					var r=r.split("||");
					if (r.length>1) {
						$("#options").html('').append('<option value="Please select ...">Choose an option ...</option>').change();
						$(r).each(function() {
							if(prefillVehicle && (trim(option) == this)) {
								$("#options").append('<option selected="selected" value="'+this+'">'+this+'</option>');
							} else {
								$("#options").append('<option value="'+this+'">'+this+'</option>');
							}
						});
						$("#options").change();
					} else {
						$("#options").html('').append('<option value="'+r+'">'+r+'</option>').change();
					}
				},
				error: function(a,b,c) {
					$("#options").html('');
				}
			});
		} else {
			$("#options").html('');
		}
	});
	
	$("#options").change(function() {
		if($("#options option:selected").val() != 'Please select ...') {
			dcsMultiTrack("DCS.dcsuri", "/funnel_action/tsv/step_4", "WT.ti", "Funnel Action > TSV > Step 4", "WT.si_n", "TSV", "WT.si_x", "4");
		}
		submittable = false;
	});
});

// BY TIRE SIZE
var prefillSize = false;
var tireSizeType, tireSectionWidth, tireAspectRatio, tireRimDiameter;

$(function() {
	tireSizeType		= $("#tireSizeType option").val();
	tireSectionWidth	= $("#tireSectionWidth option").val();
	tireAspectRatio		= $("#tireAspectRatio option").val();
	tireRimDiameter		= $("#tireRimDiameter option").val();

	if (typeof(tireSizeType) != "undefined" && tireSizeType.length>0 && typeof(tireSectionWidth) != "undefined" && tireSectionWidth.length>0 && typeof(tireAspectRatio) != "undefined" && tireAspectRatio.length>0 && typeof(tireRimDiameter) != "undefined" && tireRimDiameter.length>0) {
		prefillSize = true;
	}

	$.ajax({
		type: 'GET',
		url: data_dir+'/tires/types.txt',
		success: function(data) {
			var r=data.replace(/^[\r\n]+|[\r\n]+$/g, "");
			if(r.substring(r.length-2,r.length)=="||") r=r.substring(0,r.length-2);
			var r=r.split("||");
			if (r.length>1) {
				if(!prefillSize) {
					$("#tireSizeType").append('<option selected="selected" value="Please select ...">Choose a type ...</option>');
				} else {
					$("#tireSizeType").html('<option value="Please select ...">Choose a type ...</option>');
				}
				$(r).each(function() {
					
					if(prefillSize && (sizeTypeFilter(tireSizeType+' (') == sizeTypeFilter(this))) {
						$("#tireSizeType").append('<option selected="selected" value="'+sizeTypeFilter(this)+'">'+this.replace('(','(Ex: ')+'</option>');
					} else {
						$("#tireSizeType").append('<option value="'+sizeTypeFilter(this)+'">'+this.replace('(','(Ex: ')+'</option>');
					}
				});
			}
			$("#tireSizeType").change();
		},
		error: function(a,b,c) {
			$("#tireSizeType").html('');
		}
	});

	$("#tireSizeType").change(function() {
		submittable = false;
		if(prefillSize && sizeTypeFilter(tireSizeType+' (') != $(this).val()) prefillSize = false;
		var my_type = filter($(this).val());
		$("#tireSectionWidth, #tireAspectRatio, #tireRimDiameter").html('');

		if(my_type != 'please_select____') {
			$.ajax({
				type: 'GET',
				url: data_dir+'/tires/'+my_type+'/widths.txt',
				success: function(data) {
					dcsMultiTrack("DCS.dcsuri", "/funnel_action/tss/step_1", "WT.ti", "Funnel Action > TSS > Step 1", "WT.si_n", "TSS", "WT.si_x", "1");
					var r=data.replace(/^[\r\n]+|[\r\n]+$/g, "");
					if(r.substring(r.length-2,r.length)=="||") r=r.substring(0,r.length-2);
					var r=r.split("||");
					if (r.length>1) {
						if(prefillSize) {
							$("#tireSectionWidth").html('<option value="Please select ...">Choose a width ...</option>');
						} else {
							$("#tireSectionWidth").append('<option value="Please select ..." selected="selected">Choose a width ...</option>');
						}
						$(r).each(function() {
							if(prefillSize && (tireSectionWidth == this)) {
								$("#tireSectionWidth").append('<option selected="selected">'+this+'</option>');
							} else {
								$("#tireSectionWidth").append('<option>'+this+'</option>');
							}
						});
						$("#tireSectionWidth").change();
					} else if (r.length == 1) {
						$(r).each(function() {
							$("#tireSectionWidth").append('<option>'+this+'</option>');
						});
						$("#tireSectionWidth").change();
					}
				},
				error: function(a,b,c) {
					$("#tireSectionWidth").html('');
				}
			});
		}
	});

	$("#tireSectionWidth").change(function() {
		submittable = false;
		if(prefillSize && (tireSectionWidth != $(this).val())) prefillSize = false;
		var my_type = filter($("#tireSizeType").val());
		var my_width = filter($(this).val());
		$("#tireAspectRatio, #tireRimDiameter").html('');
		if(my_width != 'please_select____') {
			$.ajax({
				type: 'GET',
				url: data_dir+'/tires/'+my_type+'/'+my_width+'.aspects.txt',
				success: function(data) {
					dcsMultiTrack("DCS.dcsuri", "/funnel_action/tss/step_2", "WT.ti", "Funnel Action > TSS > Step 2", "WT.si_n", "TSS", "WT.si_x", "2");
					var r=data.replace(/^[\r\n]+|[\r\n]+$/g, "");
					if(r.substring(r.length-2,r.length)=="||") r=r.substring(0,r.length-2);
					var r=r.split("||");
					$("#tireAspectRatio").html('');
					if (r.length>1) {
						if(prefillSize) {
							$("#tireAspectRatio").html('<option value="Please select ...">Choose an aspect ratio ...</option>');
						} else {
							$("#tireAspectRatio").append('<option value="Please select ..." selected="selected">Choose an aspect ratio ...</option>');
						}
						$(r).each(function() {
							if(prefillSize && (tireAspectRatio == this)) {
								$("#tireAspectRatio").append('<option selected="selected">'+this+'</option>');
							} else {
								$("#tireAspectRatio").append('<option>'+this+'</option>');
							}
						});
						$("#tireAspectRatio").change();
					} else if (r.length == 1) {
						$(r).each(function() {
							$("#tireAspectRatio").append('<option>'+this+'</option>');
						});
						$("#tireAspectRatio").change();
					}
				},
				error: function(a,b,c) {
					$("#tireAspectRatio").html('');
				}
			});
		}
	});

	$("#tireAspectRatio").change(function() {
		submittable = false;
		if(prefillSize && (tireAspectRatio != $(this).val())) prefillSize = false;
		var my_type = filter($("#tireSizeType").val());
		var my_width = filter($("#tireSectionWidth").val());
		var my_aspect = filter($(this).val());
		$("#tireRimDiameter").html('');
		if(my_aspect != 'please_select____') {
			$.ajax({
				type: 'GET',
				url: data_dir+'/tires/'+my_type+'/'+my_width+'.'+my_aspect+'.sizes.txt',
				success: function(data) {
					dcsMultiTrack("DCS.dcsuri", "/funnel_action/tss/step_3", "WT.ti", "Funnel Action > TSS > Step 3", "WT.si_n", "TSS", "WT.si_x", "3");
					var r=data.replace(/^[\r\n]+|[\r\n]+$/g, "");
					if(r.substring(r.length-2,r.length)=="||") r=r.substring(0,r.length-2);
					var r=r.split("||");
					$("#tireRimDiameter").html('');
					if (r.length>1) {
						if(prefillSize) {
							$("#tireRimDiameter").append('<option value="Please select ...">Choose a rim size ...</option>');
						} else {
							$("#tireRimDiameter").append('<option selected="selected" value="Please select ...">Choose a rim size ...</option>');
						}
						$(r).each(function() {
							if(prefillSize && (tireRimDiameter == this)) {
								$("#tireRimDiameter").append('<option selected="selected">'+this+'</option>');
							} else {
								$("#tireRimDiameter").append('<option>'+this+'</option>');
							}
						});
						$("#tireRimDiameter").change();
					} else if (r.length == 1) {
						$(r).each(function() {
							$("#tireRimDiameter").append('<option>'+this+'</option>');
						});
						$("#tireRimDiameter").change();
					}
				},
				error: function(a,b,c) {
					$("#tireRimDiameter").html('');
				}
			});
		}
	});
	
	$("#tireRimDiameter").change(function() {
		submittable = false;
		if($("#tireRimDiameter option:selected").val() != 'Please select ...') {
			dcsMultiTrack("DCS.dcsuri", "/funnel_action/tss/step_4", "WT.ti", "Funnel Action > TSS > Step 4", "WT.si_n", "TSS", "WT.si_x", "4");
		}
	});
	
/*
	$("a.print").click(function() {
		dcsMultiTrack("DCS.dcsuri", "/funnel_action/tsn/step_1", "WT.ti", "Funnel Action > TSN > Step 1", "WT.si_n", "TSN", "WT.si_x", "1");
	});
*/
	
	$("#tab_tiresize_content li:has(select) img, #find_size li:has(select) img").addClass('clickable').click(function(e) {
		$("div.tooltip_widget").show().css({
			position:'absolute',
			top: (e.pageY-75)+'px',
			left:(e.pageX+25)+'px'
		}).children('span').eq(0).text($(this).attr('alt'));
	});

	$("div.tooltip_widget img").addClass('clickable').click(function() {
		$("div.tooltip_widget").hide();
	});
	
	// $("table[id^=tab_performanceratings], table[id^=tab_techspecs]").tablesorter({debug:true});
	
	$("p#dealer_link a").addClass('clickable').click(function() {
		$("div#dealer_expanded").show().css('z-index',100);
		$("ul.dropdowns").hide();
		return false;
	});
	
	$("div#dealer_expanded p.back a").click(function() {
		$("div#dealer_expanded").hide();
		$("ul.dropdowns").show();
		return false;
	});
	
	$("table.tire_results tr :has([class*=showhide_])").addClass('clickable').click(function() {
		if($(this).find('.showhide_closed').size() != 0) {
			$(this).find('.showhide_closed').removeClass('showhide_closed').addClass('showhide_open').html('Hide').closest("table.tire_results").find("tr:gt(0)").show();
		} else {
			$(this).find('.showhide_open').removeClass('showhide_open').addClass('showhide_closed').html('Show').closest("table.tire_results").find("tr:gt(0)").hide();	
		}
	});
	
	$("img#tire-name-btn").addClass('clickable').click(function() {
		$("form#locate_a_dealer").submit();
	});
	
/*
	if($("div.tire_ratings p.testimonial").height() > 112) {
		$("div.tire_ratings span.showhide").show();
		$("div.tire_ratings p.testimonial").height(106).css({'overflow':'hidden', 'margin-bottom':'14px'});
	}
*/
	
	if($("div.testimonial").height() > 100) {
		$("div.testimonial").append('<p><a class="more collapsed">More</a></p>').children('p.text').height('100px');
	}
	
	$("div.testimonial .more").addClass('clickable').click(function() {
		if($(this).hasClass('collapsed')) {
			$("div.testimonial p.text").height('auto');
			$(this).html('Less').removeClass('collapsed').addClass('expanded');
		} else {
			$(this).html('More').removeClass('expanded').addClass('collapsed');
			$("div.testimonial p.text").height('100px');
		}
	});

	if($.tablesorter) {
		$("div[id^=tab_performanceratings_] table").tablesorter({widgets:['zebra']});

/*
	$("table[id^=tab_techspecs_]").tablesorter({debug:true, widgets:['zebra'],
		headers: {
			4: {sorter:false},
			5: {sorter:false},
			6: {sorter:false},
			7: {sorter:false},
			8: {sorter:false},
			9: {sorter:false},
			10: {sorter:false},
			11: {sorter:false},
			12: {sorter:false},
			13: {sorter:false},
			14: {sorter:false},
			15: {sorter:false},
			16: {sorter:false},
			17: {sorter:false},
			18: {sorter:false},
			19: {sorter:false},
			20: {sorter:false},
			21: {sorter:false},
			22: {sorter:false},
			23: {sorter:false},
			24: {sorter:false}
		}
	});
*/
	}
});