var FBSM=window.FBSM;
if(typeof FBSM=='undefined'){FBSM=new Object();FBSM.C=new Object();FBSM.PD=new Object();FBSM.U=new Object();FBSM.SC=new Object();FBSM.W=new Object();FBSM.WC=new Object();FBSM.F=new Object();FBSM.TFG=new Object();FBSM.R=new Object();FBSM.TF=new Object();}
FBSM.TF=new Object();

FBSM.TF.YearList = new Object();
FBSM.TF.MakeList = new Object();
FBSM.TF.ModelList = new Object();
FBSM.TF.StyleList = new Object();
FBSM.TF.SizeList = new Object();

FBSM.TF.RightWidgetSelection = new Object();
FBSM.TF.MainWidgetSelection = new Object();

$(document).ready(function (){
	if($(".tireFitment_right").length > 0){
		if($.cookies.get("selectedTireFitmentRight"))
			FBSM.TF.InitExistingRightTireFitmentWidget($.cookies.get("selectedTireFitmentRight"));
		else
			FBSM.TF.InitRightTireFitmentWidget();

		//Right Content Event Handlers----------
		$(".tireFitment_right #tireFitmentYearSelectRight").change(function(){
			$(".tireFitment_right #tireFitmentMakeSelectRight,.tireFitment_right #tireFitmentStyleSelectRight,.tireFitment_right #tireFitmentSizeSelectRight,.tireFitment_right #tireFitmentModelSelectRight").html('');
			$(".tireFitment_right #tireFitmentMakeSelectRight,.tireFitment_right #tireFitmentStyleSelectRight,.tireFitment_right #tireFitmentSizeSelectRight,.tireFitment_right #tireFitmentModelSelectRight,.tireFitment_right #tireFitmentSubmitRight").attr("disabled", "disabled");
			if($(".tireFitment_right #tireFitmentYearSelectRight").val() != -1)
				FBSM.TF.RightTireFitmentYearSelected($(".tireFitment_right #tireFitmentYearSelectRight").val());
		});
		$(".tireFitment_right #tireFitmentMakeSelectRight").change(function(){
			$(".tireFitment_right #tireFitmentStyleSelectRight,.tireFitment_right #tireFitmentSizeSelectRight,.tireFitment_right #tireFitmentModelSelectRight").html('');
			$(".tireFitment_right #tireFitmentStyleSelectRight,.tireFitment_right #tireFitmentSizeSelectRight,.tireFitment_right #tireFitmentModelSelectRight,.tireFitment_right #tireFitmentSubmitRight").attr("disabled", "disabled");
			if($(".tireFitment_right #tireFitmentMakeSelectRight").val() != -1)
				FBSM.TF.RightTireFitmentMakeSelected($(".tireFitment_right #tireFitmentMakeSelectRight").val());
		});
		$(".tireFitment_right #tireFitmentModelSelectRight").change(function(){
			$(".tireFitment_right #tireFitmentStyleSelectRight,.tireFitment_right #tireFitmentSizeSelectRight").html('');
			$(".tireFitment_right #tireFitmentStyleSelectRight,.tireFitment_right #tireFitmentSizeSelectRight,.tireFitment_right #tireFitmentSubmitRight").attr("disabled", "disabled");
			if($(".tireFitment_right #tireFitmentModelSelectRight").val() != -1)
				FBSM.TF.RightTireFitmentModelSelected($(".tireFitment_right #tireFitmentModelSelectRight").val());
		});
		$(".tireFitment_right #tireFitmentStyleSelectRight").change(function(){
			$(".tireFitment_right #tireFitmentSizeSelectRight").html('');
			$(".tireFitment_right #tireFitmentSizeSelectRight,.tireFitment_right #tireFitmentSubmitRight").attr("disabled", "disabled");
			if($(".tireFitment_right #tireFitmentStyleSelectRight").val() != -1)
				FBSM.TF.RightTireFitmentStyleSelected($(".tireFitment_right #tireFitmentStyleSelectRight").val());
		});
		$(".tireFitment_right #tireFitmentSizeSelectRight").change(function(){
			$(".tireFitment_right #tireFitmentSubmitRight").attr("disabled", "disabled");
			if($(".tireFitment_right #tireFitmentSizeSelectRight").val() != -1)
				FBSM.TF.RightTireFitmentSizeSelected($(".tireFitment_right #tireFitmentSizeSelectRight").val());
		});
		
		$(".tireFitment_right #tireFitmentSubmitRight").click(function(){
			var widgetFitmentSelection = new String();
			for(i = 0; i < 5; i++){
				widgetFitmentSelection += FBSM.TF.RightWidgetSelection[i] + '~'
			}
			$.cookies.set("selectedTireFitmentRight", widgetFitmentSelection);

			var sizeId = FBSM.TF.SizeList.list[FBSM.TF.RightWidgetSelection[4]].SizeId;
			
			var tfgDescription = "";
			$("#miniTireFitmentRight select option:selected").each(function(){
				if(tfgDescription!="") tfgDescription += "|";
				tfgDescription += $(this).html();
			});
			$.cookies.set("selectedTFGDescription", tfgDescription);
			
			window.location = 'TireFitmentProductList.htm?sizeId=' + sizeId;
		});
		//-------------------------------
	}
	if($(".tireFitment_main").length > 0){
		FBSM.TF.InitMainTireFitmentWidget();
		
		$(".tireFitment_main #tireFitmentYearSelect").change(function(){
			$(".tireFitment_main #tireFitmentMakeSelect,.tireFitment_main #tireFitmentStyleSelect,.tireFitment_main #tireFitmentSizeSelect,.tireFitment_main #tireFitmentModelSelect").html('');
			$(".tireFitment_main #tireFitmentMakeSelect,.tireFitment_main #tireFitmentStyleSelect,.tireFitment_main #tireFitmentSizeSelect,.tireFitment_main #tireFitmentModelSelect,.tireFitment_main #tireFitmentSubmit").attr("disabled", "disabled");
			if($(".tireFitment_main #tireFitmentYearSelect").val() != -1)
				FBSM.TF.MainTireFitmentYearSelected($(".tireFitment_main #tireFitmentYearSelect").val());
		});
		$(".tireFitment_main #tireFitmentMakeSelect").change(function(){
			$(".tireFitment_main #tireFitmentStyleSelect,.tireFitment_main #tireFitmentSizeSelect,.tireFitment_main #tireFitmentModelSelect").html('');
			$(".tireFitment_main #tireFitmentStyleSelect,.tireFitment_main #tireFitmentSizeSelect,.tireFitment_main #tireFitmentModelSelect,.tireFitment_main #tireFitmentSubmit").attr("disabled", "disabled");
			if($(".tireFitment_main #tireFitmentMakeSelect").val() != -1)
				FBSM.TF.MainTireFitmentMakeSelected($(".tireFitment_main #tireFitmentMakeSelect").val());
		});
		$(".tireFitment_main #tireFitmentModelSelect").change(function(){
			$(".tireFitment_main #tireFitmentStyleSelect,.tireFitment_main #tireFitmentSizeSelect").html('');
			$(".tireFitment_main #tireFitmentStyleSelect,.tireFitment_main #tireFitmentSizeSelect,.tireFitment_main #tireFitmentSubmit").attr("disabled", "disabled");
			if($(".tireFitment_main #tireFitmentModelSelect").val() != -1)
				FBSM.TF.MainTireFitmentModelSelected($(".tireFitment_main #tireFitmentModelSelect").val());
		});
		$(".tireFitment_main #tireFitmentStyleSelect").change(function(){
			$(".tireFitment_main #tireFitmentSizeSelect").html('');
			$(".tireFitment_main #tireFitmentSizeSelect,.tireFitment_main #tireFitmentSubmit").attr("disabled", "disabled");
			if($(".tireFitment_main #tireFitmentStyleSelect").val() != -1)
				FBSM.TF.MainTireFitmentStyleSelected($(".tireFitment_main #tireFitmentStyleSelect").val());
		});
		$(".tireFitment_main #tireFitmentSizeSelect").change(function(){
			$(".tireFitment_main #tireFitmentSubmit").attr("disabled", "disabled");
			if($(".tireFitment_main #tireFitmentSizeSelect").val() != -1)
				FBSM.TF.MainTireFitmentSizeSelected($(".tireFitment_main #tireFitmentSizeSelect").val());
		});
		
		$(".tireFitment_main #tireFitmentSubmit").click(function(){
			var widgetFitmentSelection = new String();
			for(i = 0; i < 5; i++){
				widgetFitmentSelection += FBSM.TF.MainWidgetSelection[i] + '~'
			}
			$.cookies.set("selectedTireFitmentRight", widgetFitmentSelection);
			
			var tfgDescription = "";
			$("#miniTireFitment select option:selected").each(function(){
				if(tfgDescription!="") tfgDescription += "|";
				tfgDescription += $(this).html();
			});
			$.cookies.set("selectedTFGDescription", tfgDescription);

			var sizeId = FBSM.TF.SizeList.list[FBSM.TF.MainWidgetSelection[4]].SizeId;
			window.location = 'TireFitmentProductList.htm?sizeId=' + sizeId;
		});
	}
});

FBSM.TF.InitRightTireFitmentWidget = function(){
	$(".tireFitment_right #tireFitmentYearSelectRight").attr("disabled", "");
	$(".tireFitment_right #tireFitmentYearSelectRight").html("<option value=''>Loading...</option");
	$.getJSON("/Services/TireFitmentGuide.asmx/GetYearsJScriptJSON", function(json){
		FBSM.TF.YearList = json;
		$(".tireFitment_right #tireFitmentYearSelectRight").html("<option value='-1'>Select Year</option");
		for(i = 0; i < FBSM.TF.YearList.list.length; i++) $(".tireFitment_right #tireFitmentYearSelectRight").append("<option value='" + i + "'>" + FBSM.TF.YearList.list[i] + "</option>");
	});
}
FBSM.TF.RightTireFitmentYearSelected = function(SelectedYear){
	FBSM.TF.RightWidgetSelection[0] = SelectedYear;
	$(".tireFitment_right #tireFitmentMakeSelectRight").attr("disabled", "");
	$(".tireFitment_right #tireFitmentMakeSelectRight").html("<option value=''>Loading...</option");
	$.getJSON("/Services/TireFitmentGuide.asmx/GetMakesJScriptJSON?year=" + FBSM.TF.YearList.list[SelectedYear], function(json){
		FBSM.TF.MakeList = json;
		$(".tireFitment_right #tireFitmentMakeSelectRight").html("<option value='-1'>Select Make</option");
		for(i = 0; i < FBSM.TF.MakeList.list.length; i++) $(".tireFitment_right #tireFitmentMakeSelectRight").append("<option value='" + i + "'>" + FBSM.TF.MakeList.list[i].Name + "</option>");
	});
}
FBSM.TF.RightTireFitmentMakeSelected = function(SelectedMake){
	FBSM.TF.RightWidgetSelection[1] = SelectedMake;
	$(".tireFitment_right #tireFitmentModelSelectRight").attr("disabled", "");
	$(".tireFitment_right #tireFitmentModelSelectRight").html("<option value=''>Loading...</option");
	$.getJSON("/Services/TireFitmentGuide.asmx/GetModelJScriptJSON?makeId=" + FBSM.TF.MakeList.list[SelectedMake].Value, function(json){
		FBSM.TF.ModelList = json;
		$(".tireFitment_right #tireFitmentModelSelectRight").html("<option value='-1'>Select Model</option");
		for(i = 0; i < FBSM.TF.ModelList.list.length; i++) $(".tireFitment_right #tireFitmentModelSelectRight").append("<option value='" + i + "'>" + FBSM.TF.ModelList.list[i].Name + "</option>");
	});
}
FBSM.TF.RightTireFitmentModelSelected = function(SelectedModel){
	FBSM.TF.RightWidgetSelection[2] = SelectedModel;
	$(".tireFitment_right #tireFitmentStyleSelectRight").attr("disabled", "");
	$(".tireFitment_right #tireFitmentStyleSelectRight").html("<option value=''>Loading...</option");
	$.getJSON("/Services/TireFitmentGuide.asmx/GetStylesJScriptJSON?modelId=" + FBSM.TF.ModelList.list[SelectedModel].Value, function(json){
		FBSM.TF.StyleList = json;
		$(".tireFitment_right #tireFitmentStyleSelectRight").html("<option value='-1'>Select Style</option");
		for(i = 0; i < FBSM.TF.StyleList.list.length; i++) $(".tireFitment_right #tireFitmentStyleSelectRight").append("<option value='" + i + "'>" + FBSM.TF.StyleList.list[i].Name + "</option>");
	});
}
FBSM.TF.RightTireFitmentStyleSelected = function(SelectedStyle){
	FBSM.TF.RightWidgetSelection[3] = SelectedStyle;
	$(".tireFitment_right #tireFitmentSizeSelectRight").attr("disabled", "");
	$(".tireFitment_right #tireFitmentSizeSelectRight").html("<option value=''>Loading...</option");
	$.getJSON("/Services/TireFitmentGuide.asmx/GetSizesJScriptJSON?styleId=" + FBSM.TF.StyleList.list[SelectedStyle].Value, function(json){
		FBSM.TF.SizeList = json;
		$(".tireFitment_right #tireFitmentSizeSelectRight").html("<option value='-1'>Select Size</option");
		for(i = 0; i < FBSM.TF.SizeList.list.length; i++) $(".tireFitment_right #tireFitmentSizeSelectRight").append("<option value='" + i + "'>" + FBSM.TF.SizeList.list[i].Name + "</option>");
	});
}
FBSM.TF.RightTireFitmentSizeSelected = function(SelectedSize){
	FBSM.TF.RightWidgetSelection[4] = SelectedSize;
	$(".tireFitment_right #tireFitmentSubmitRight").attr("disabled", "");
}

FBSM.TF.InitExistingRightTireFitmentWidget = function(PreviousFitmentSelection){
	$(".tireFitment_right .loadingIndicator").show();
	
	var previousFitmentArray = PreviousFitmentSelection.split('~');
	for(i = 0; i < 5; i++) FBSM.TF.RightWidgetSelection[i] = previousFitmentArray[i];
	
	$.getJSON("/Services/TireFitmentGuide.asmx/GetYearsJScriptJSON", function(json){
		FBSM.TF.YearList = json;
		var selectedValue = FBSM.TF.YearList.list[FBSM.TF.RightWidgetSelection[0]];
		$(".tireFitment_right #tireFitmentYearSelectRight").html("<option value='-1'>Select Year</option");
		for(i = 0; i < FBSM.TF.YearList.list.length; i++) $(".tireFitment_right #tireFitmentYearSelectRight").append("<option value='" + i + "'" + (FBSM.TF.YearList.list[i] == selectedValue ? " selected" : "") + ">" + FBSM.TF.YearList.list[i] + "</option>");
		
		$.getJSON("/Services/TireFitmentGuide.asmx/GetMakesJScriptJSON?year=" + selectedValue, function(json){
			FBSM.TF.MakeList = json;
			selectedValue = FBSM.TF.MakeList.list[FBSM.TF.RightWidgetSelection[1]].Value;
			$(".tireFitment_right #tireFitmentMakeSelectRight").html("<option value='-1'>Select Make</option");
			for(i = 0; i < FBSM.TF.MakeList.list.length; i++) $(".tireFitment_right #tireFitmentMakeSelectRight").append("<option value='" + i + "'" + (FBSM.TF.MakeList.list[i].Value == selectedValue ? " selected" : "") + ">" + FBSM.TF.MakeList.list[i].Name + "</option>");
			
			$.getJSON("/Services/TireFitmentGuide.asmx/GetModelJScriptJSON?makeId=" + selectedValue, function(json){
				FBSM.TF.ModelList = json;
				selectedValue = FBSM.TF.ModelList.list[FBSM.TF.RightWidgetSelection[2]].Value;
				$(".tireFitment_right #tireFitmentModelSelectRight").html("<option value='-1'>Select Model</option");
				for(i = 0; i < FBSM.TF.ModelList.list.length; i++) $(".tireFitment_right #tireFitmentModelSelectRight").append("<option value='" + i + "'" + (FBSM.TF.ModelList.list[i].Value == selectedValue ? " selected" : "") + ">" + FBSM.TF.ModelList.list[i].Name + "</option>");

				$.getJSON("/Services/TireFitmentGuide.asmx/GetStylesJScriptJSON?modelId=" + selectedValue, function(json){
					FBSM.TF.StyleList = json;
					selectedValue = FBSM.TF.StyleList.list[FBSM.TF.RightWidgetSelection[3]].Value;
					$(".tireFitment_right #tireFitmentStyleSelectRight").html("<option value='-1'>Select Style</option");
					for(i = 0; i < FBSM.TF.StyleList.list.length; i++) $(".tireFitment_right #tireFitmentStyleSelectRight").append("<option value='" + i + "'" + (FBSM.TF.StyleList.list[i].Value == selectedValue ? " selected" : "") + ">" + FBSM.TF.StyleList.list[i].Name + "</option>");

					$.getJSON("/Services/TireFitmentGuide.asmx/GetSizesJScriptJSON?styleId=" + selectedValue, function(json){
						FBSM.TF.SizeList = json;
						selectedValue = FBSM.TF.SizeList.list[FBSM.TF.RightWidgetSelection[4]].Value;
						$(".tireFitment_right #tireFitmentSizeSelectRight").html("<option value='-1'>Select Size</option");
						for(i = 0; i < FBSM.TF.SizeList.list.length; i++) $(".tireFitment_right #tireFitmentSizeSelectRight").append("<option value='" + i + "'" + (FBSM.TF.SizeList.list[i].Value == selectedValue ? " selected" : "") + ">" + FBSM.TF.SizeList.list[i].Name + "</option>");
						
						$(".tireFitment_right #tireFitmentYearSelectRight,.tireFitment_right #tireFitmentMakeSelectRight,.tireFitment_right #tireFitmentStyleSelectRight,.tireFitment_right #tireFitmentSizeSelectRight,.tireFitment_right #tireFitmentModelSelectRight,.tireFitment_right #tireFitmentSubmitRight").attr("disabled", "");
					});
				});
			});
		});
	});
}

FBSM.TF.InitMainTireFitmentWidget = function(){
	$(".tireFitment_main #tireFitmentYearSelect").attr("disabled", "");
	$(".tireFitment_main #tireFitmentYearSelect").html("<option value=''>Loading...</option");
	$.getJSON("/Services/TireFitmentGuide.asmx/GetYearsJScriptJSON", function(json){
		FBSM.TF.YearList = json;
		$(".tireFitment_main #tireFitmentYearSelect").html("<option value='-1'>Select Year</option");
		for(i = 0; i < FBSM.TF.YearList.list.length; i++) $(".tireFitment_main #tireFitmentYearSelect").append("<option value='" + i + "'>" + FBSM.TF.YearList.list[i] + "</option>");
	});
}
FBSM.TF.MainTireFitmentYearSelected = function(SelectedYear){
	FBSM.TF.MainWidgetSelection[0] = SelectedYear;
	$(".tireFitment_main #tireFitmentMakeSelect").attr("disabled", "");
	$(".tireFitment_main #tireFitmentMakeSelect").html("<option value=''>Loading...</option");
	$.getJSON("/Services/TireFitmentGuide.asmx/GetMakesJScriptJSON?year=" + FBSM.TF.YearList.list[SelectedYear], function(json){
		FBSM.TF.MakeList = json;
		$(".tireFitment_main #tireFitmentMakeSelect").html("<option value='-1'>Select Make</option");
		for(i = 0; i < FBSM.TF.MakeList.list.length; i++) $(".tireFitment_main #tireFitmentMakeSelect").append("<option value='" + i + "'>" + FBSM.TF.MakeList.list[i].Name + "</option>");
	});
}
FBSM.TF.MainTireFitmentMakeSelected = function(SelectedMake){
	FBSM.TF.MainWidgetSelection[1] = SelectedMake;
	$(".tireFitment_main #tireFitmentModelSelect").attr("disabled", "");
	$(".tireFitment_main #tireFitmentModelSelect").html("<option value=''>Loading...</option");
	$.getJSON("/Services/TireFitmentGuide.asmx/GetModelJScriptJSON?makeId=" + FBSM.TF.MakeList.list[SelectedMake].Value, function(json){
		FBSM.TF.ModelList = json;
		$(".tireFitment_main #tireFitmentModelSelect").html("<option value='-1'>Select Model</option");
		for(i = 0; i < FBSM.TF.ModelList.list.length; i++) $(".tireFitment_main #tireFitmentModelSelect").append("<option value='" + i + "'>" + FBSM.TF.ModelList.list[i].Name + "</option>");
	});
}
FBSM.TF.MainTireFitmentModelSelected = function(SelectedModel){
	FBSM.TF.MainWidgetSelection[2] = SelectedModel;
	$(".tireFitment_main #tireFitmentStyleSelect").attr("disabled", "");
	$(".tireFitment_main #tireFitmentStyleSelect").html("<option value=''>Loading...</option");
	$.getJSON("/Services/TireFitmentGuide.asmx/GetStylesJScriptJSON?modelId=" + FBSM.TF.ModelList.list[SelectedModel].Value, function(json){
		FBSM.TF.StyleList = json;
		$(".tireFitment_main #tireFitmentStyleSelect").html("<option value='-1'>Select Style</option");
		for(i = 0; i < FBSM.TF.StyleList.list.length; i++) $(".tireFitment_main #tireFitmentStyleSelect").append("<option value='" + i + "'>" + FBSM.TF.StyleList.list[i].Name + "</option>");
	});
}
FBSM.TF.MainTireFitmentStyleSelected = function(SelectedStyle){
	FBSM.TF.MainWidgetSelection[3] = SelectedStyle;
	$(".tireFitment_main #tireFitmentSizeSelect").attr("disabled", "");
	$(".tireFitment_main #tireFitmentSizeSelect").html("<option value=''>Loading...</option");
	$.getJSON("/Services/TireFitmentGuide.asmx/GetSizesJScriptJSON?styleId=" + FBSM.TF.StyleList.list[SelectedStyle].Value, function(json){
		FBSM.TF.SizeList = json;
		$(".tireFitment_main #tireFitmentSizeSelect").html("<option value='-1'>Select Size</option");
		for(i = 0; i < FBSM.TF.SizeList.list.length; i++) $(".tireFitment_main #tireFitmentSizeSelect").append("<option value='" + i + "'>" + FBSM.TF.SizeList.list[i].Name + "</option>");
	});
}
FBSM.TF.MainTireFitmentSizeSelected = function(SelectedSize){
	FBSM.TF.MainWidgetSelection[4] = SelectedSize;
	$(".tireFitment_main #tireFitmentSubmit").attr("disabled", "");
}
