function changeImage(id, direction) {
	if (direction == 'next') {
		imageNum[id]++;
	} else {
		imageNum[id]--;
	}

	$.ajax({
		type: "get",
		processData: true,
		url: "functions/load_image.php",
		data: "id=" + id + "&pg=" + imageNum[id],
		cache: false,
		success: function(data) {
			$("#placeImage").hide().html(data).fadeIn("slow");
		}
	});
	return false;
}

function clearCursor() {
	document.body.style.cursor = "default";
}

function highlight(field, is_highlight) {
	var val = (is_highlight == 1) ? "elementError" : "element";
	document.getElementById(field).className = val;
}

function loadAspectRatio(sw) {
	if (parseInt(sw) > 0) {
		waitCursor();
		$.ajax({
			type: "get",
			processData: true,
			url: "functions/load_aspect_ratio.php",
			data: "sw=" + sw,
			success: function(data) {
				$("#aspectRatioDiv").html(data);
			}
		});
	}
}

function loadMakes(year) {
	if (parseInt(year) > 0) {
		waitCursor();
		$.ajax({
			type: "get",
			processData: true,
			url: "functions/load_makes.php",
			data: "year=" + year,
			success: function(data) {
				$("#makeDiv").html(data);
			}
		});
	}
}

function loadModels(year, make_id) {
	if (parseInt(year) > 0 && parseInt(make_id) > 0) {
		waitCursor();
		$.ajax({
			type: "get",
			processData: true,
			url: "functions/load_models.php",
			data: "year=" + year + "&make_id=" + make_id,
			success: function(data) {
				$("#modelDiv").html(data);
			}
		});
	}
}

function loadOptions(year, make_id, model) {
	if (parseInt(year) > 0 && parseInt(make_id) > 0 && model != "") {
		waitCursor();
		$.ajax({
			type: "get",
			processData: true,
			url: "functions/load_options.php",
			data: "year=" + year + "&make_id=" + make_id + "&model=" + model,
			success: function(data) {
				$("#optionDiv").html(data);
			}
		});
	}
}

function loadPlusSize(plus_size, accepted) {
	var vehicle_id = document.getElementById('vehicle_id').value;
	if (vehicle_id > 0) {
		//all hover and click logic for buttons
		loadResults(vehicle_id, plus_size, accepted);
	} else {
		alert('No vehicle selected.');
	}
}

function loadResults(vehicle_id, plus_size, accepted) {
	if (parseInt(vehicle_id) > 0 && plus_size >= -2 && plus_size <= 9) {
		waitCursor();
		$.ajax({
			type: "get",
			processData: true,
			url: "functions/load_results.php",
			data: "vehicle_id=" + vehicle_id + "&plus_size=" + plus_size + "&acc=" + ((accepted) ? 1 : 0),
			success: function(data) {
				$("#resultsDiv").html(data);
			}
		});
	} else {
		openWarningDialog();
	}
}

function loadRimDiameters(sw, ar) {
	if (parseInt(sw) > 0 && parseInt(ar) > 0) {
		waitCursor();
		$.ajax({
			type: "get",
			processData: true,
			url: "functions/load_rim_diameter.php",
			data: "sw=" + sw + "&ar=" + ar,
			success: function(data) {
				$("#rimDiameterDiv").html(data);
			}
		});
	}
}

function loadSizeResults(sw, ar, rd) {
	if (parseInt(sw) > 0 && parseInt(ar) > 0 && parseInt(rd) > 0) {
		waitCursor();
		$.ajax({
			type: "get",
			processData: true,
			url: "functions/load_size_results.php",
			data: "sw=" + sw + "&ar=" + ar + "&rd=" + rd,
			success: function(data) {
				$("#resultsDiv").html(data);
			}
		});
	}
}

function loadWarning(plus_size, file) {
	if (accepted) {
		loadPlusSize(plus_size, true);
	} else {
		var vehicle_id = $("#vehicle_id").value;
		openAcceptDialog(vehicle_id, plus_size, file);
	}
}

function loadWarningPlus(plus_size) {
	if (accepted) {
		dia.dialog('close');
		loadPlusSize(plus_size, true);
	}
}

function openDialog() {
	dia = $("#dialog").dialog({
		closeOnEscape: true,
		bgiframe: true,
		modal: true,
		width: 400,
		buttons: {
			Close: function() {
				$(this).dialog('close');
			}
		}
	});
	if (!dia.dialog('isOpen')) {
		dia.dialog('open');
	}
}

function openAcceptDialog(vehicle_id, plus_size, file) {
	var content = '<div>Before accessing plus size fitments, <a href="#" onclick="setAccepted(true, \'' + file + '\'); return false;" style=" color:#FB9D00;">please download</a> our plus size disclosure statement.<br /><br />You agree to use plus sizes at your own risk and indemify and hold harmess Continental Tires from any incidents that occur from using the tire selector.</div><div style="text-align:center; margin-top:10px;"><a href="#" class="fg-button ui-state-default ui-corner-all ui-state-disabled" id="plus_agree" disabled="disabled" onclick="loadWarningPlus(\'' + plus_size +  '\'); return false;">I Agree</a></div><div style="font-size:10px; padding-top:10px;"><b>NOTE</b>: You must click the download link to activate the agree button.</div>';
	$("#dialog").removeAttr("title").attr("title", "CTA Waiver of Liability");
	$("#warningContent").html(content);
	openDialog();
}

function openWarningDialog() {
	$("#resultsDiv").html("");
	$('#sizeNotice').css("display", "block");
	$('#sizeChoice').css("display", "none");

	var diaW = $("#dialogWarning").dialog({
		closeOnEscape: true,
		bgiframe: true,
		modal: true,
		width: 400,
		buttons: {
			Close: function() {
				$(this).dialog('close');
			}
		}
	});
	if (!diaW.dialog('isOpen')) {
		diaW.dialog('open');
	}
}

function setAccepted(val, file) {
	accepted = true;
	$("#plus_agree").removeAttr("disabled");
	$("#plus_agree").removeClass("ui-state-disabled");
	$("#plus_agree")
	.hover(
		function(){
			$(this).addClass("ui-state-hover");
		},
		function(){
			$(this).removeClass("ui-state-hover");
		}
	)
	.mousedown(function(){
			$(this).parents('.fg-buttonset-single:first').find(".fg-button.ui-state-active").removeClass("ui-state-active");
			if( $(this).is('.ui-state-active.fg-button-toggleable, .fg-buttonset-multi .ui-state-active') ){ $(this).removeClass("ui-state-active"); }
			else { $(this).addClass("ui-state-active"); }
	})
	.mouseup(function(){
		if(! $(this).is('.fg-button-toggleable, .fg-buttonset-single .fg-button,  .fg-buttonset-multi .fg-button') ){
			$(this).removeClass("ui-state-active");
		}
	});
	window.location = 'download.php?file=' + file;
}

function showTire(vid, id, plus_size) {
	var url = (vid > 0) ? "tirepressure.php" : "tirespecs.php";
	$.ajax({
		type: "get",
		processData: true,
		url: url,
		data: "vid=" + vid + "&pid=" + id + "&ps=" + plus_size,
		cache: false,
		success: function(data) {
			$("#dialogTire").html(data);
		}
	});

	var diaT = $("#dialogTire").dialog({
		closeOnEscape: true,
		bgiframe: true,
		modal: true,
		width: 700,
		height: 480,
		resizable: true,
		draggable: true
	});

	if (!diaT.dialog('isOpen')) {
		diaT.dialog('open');
	}
}

function waitCursor() {
	document.body.style.cursor = "wait";
}
