jQuery(function(){

	/* --- form --- */

	var step = {

		"disable":function(element){

			element.css({"opacity":0.33});

			element.find("input").attr("disabled", "disabled");
			element.find("select").attr("disabled", "disabled");

			if (element.hasClass("step3")) {

				element.find("select[name='CountryCode']").trigger("change");

			}

		},

		"enable":function(element){

			element.css({"opacity":1.00});

			element.find("input").removeAttr("disabled");
			element.find("select").removeAttr("disabled");

		}

	};

	jQuery(".dealer-locator-module-search-form").each(function(index,element){

		var form = jQuery(element);

		var step1 = form.find("fieldset.step1");
		var step1_ProductLineID = step1.find("select[name='ProductLineID']");
	
		var step2 = form.find("fieldset.step2");
		var step2_DealerTypeIDs = step2.find("input[name='DealerTypeIDs']");
		var step2_SelectAll = step2.find(".DealerTypeIDsSelectAll");

		var step3 = form.find("fieldset.step3");
		var step3_Radius = step3.find("select[name='Radius']");
		var step3_ZIPCode = step3.find("input[name='ZIPCode']");
		var step3_CountryCode = step3.find("select[name='CountryCode']");
		var step3_StateCode = step3.find("select[name='StateCode']");
		var step3_CountyID = step3.find("select[name='CountyID']");
		var step3_Keywords = step3.find("input[name='Keywords']");

		var step4_Submit = form.find(".actions button[type='submit']");
		var step4_Reset = form.find(".actions a");

		if (step1_ProductLineID.val() == "") step.disable(step2);
		if (step2.find("input[name='DealerTypeIDs']:checked").length == 0) step.disable(step3);

		step1_ProductLineID.bind("change", function(){

			var value = parseInt(step1_ProductLineID.val(), 10);
			var sections = step2.find(".section");

			step.disable(step2);
			step.disable(step3);

			sections.hide().find("input").removeAttr("checked");

			if (value) {

				step.enable(step2);

				sections.each(function(index,element){

					var section = jQuery(element);

					if (section.data("productlineid") == value) {

						section.show();

						section.find("input").attr("checked", "checked");
						
						step.enable(step3);

					}
	
				});
	
			}
	
		});

		step1.find("img").bind("click",function(){

			var source = jQuery(this);

			step1.find(".image").addClass("disabled");

			source.parent().removeClass("disabled");
			step1_ProductLineID.val(source.data("productlineid")).trigger("change");

		});

		step2_DealerTypeIDs.bind("click", function(){

			var source = jQuery(this);
			var count = source.parents(".section").find("input[name='DealerTypeIDs']:checked").length;

			if (count == source.parents(".section").find("input[name='DealerTypeIDs']").length) {

				step2_SelectAll.attr("checked", "checked");

			} else {

				step2_SelectAll.removeAttr("checked");

			}

			source.parents(".section").siblings().find("input[name='DealerTypeIDs']").removeAttr("checked");

			if (count > 0) {

				step.enable(step3);

			} else {

				step.disable(step3);

			}

		});

		step2_SelectAll.bind("click", function(){

			var source = $(this);

			if (source.is(":checked")) {

				source.parents(".section").find("input").attr("checked", "checked");

				step.enable(step3);

			} else {

				source.parents(".section").find("input").removeAttr("checked");

				step.disable(step3);

			}

			source.parents(".section").siblings().find("input[name='DealerTypeIDs']").removeAttr("checked");

		});

		step3_CountryCode.bind("change", function(){

			step3_StateCode.empty();

			step3_ZIPCode.attr("maxlength", (step3_CountryCode.val() == "US")?"5":"7");
			step3_ZIPCode.attr("placeholder", step3_ZIPCode.data(step3_CountryCode.val()));
			step3_ZIPCode.val("");

			placeholder_fallback();

			jQuery.ajax({
	
				dataType: "json",
				success: function(json) {
	
					if (json) {
	
						step3_StateCode.append("<option value=''>Select " + step3_StateCode.data(step3_CountryCode.val()) + "</option>");
	
						jQuery.each(json, function(index, state){
	
							step3_StateCode.append("<option value='" + state.code + "'>" + state.name + "</option>");
	
						});
	
					}
	
				},
				url: step3_CountryCode.data("url") + step3_CountryCode.val() + "/"
	
			});

			if (step3_CountryCode.val() == "US") {
	
				step3_CountyID.parent().show();
	
			} else {
	
				step3_CountyID.parent().hide();
	
			}

		});

		step3_ZIPCode.bind("focus", function(){

			step3_StateCode.val("");

		});

		step3_StateCode.bind("change", function(){

			step3_Radius.val("");
			step3_ZIPCode.val("");

			placeholder_fallback();

			if (step3_CountryCode.val() != "US") return;
	
			step3_CountyID.find("option").not(":first-child").remove();
	
			jQuery.ajax({
	
				dataType: "json",
				success: function(json) {
	
					if (json) {

						jQuery.each(json, function(index, county){
	
							step3_CountyID.append("<option value='" + county.id + "'>" + county.name + "</option>");
	
						});
	
					}
	
				},
				url: step3_StateCode.data("url") + step3_StateCode.val() + "/"
	
			});
	
		});

		step4_Submit.bind("click",function(){

			if ((step3_ZIPCode.val() == "" || step3_ZIPCode.val() == step3_ZIPCode.attr("placeholder")) && step3_StateCode.val() == "") {

				alert("Please enter a ZIP code or select a state.");
				
				return false;

			}

		});

		step4_Reset.bind("click",function(){

			step1_ProductLineID.val("");
			step1.find(".image").removeClass("disabled");

			step2_DealerTypeIDs.removeAttr("checked");
			step2.find(".section").hide();

			step.disable(step2);

			step3_Radius.val("50");
			step3_ZIPCode.val("");
			step3_CountryCode.val("US");
			step3_StateCode.val("");
			step3_CountyID.val("");
			step3_Keywords.val("");

			placeholder_fallback();

			step.disable(step3);

			return false;

		});

	});

	/* --- modal --- */

	var modal = {

		"element": $("#modal"),

		"hide": function() {

			this.element.hide();

			$("body").css({"overflow":"scroll"});

		},

		"init": function() {

			sIFR.replace(gillsansregular,{
				css:".sIFR-root { color: #ffffff; font-size: 32px; }",
				offsetLeft:-1,
				offsetTop:-7,
				selector:"#modal h2 span",
				wmode:"transparent"
			});

			var self = this.element;

			this.element.bind("click", function(event){

				if (event.target === event.currentTarget) {

					self.hide();

				}

			});

			placeholder_fallback();

			this.resize();

		},

		"resize": function() {

			var h = Math.max($(document).height(), $(window).height());
			var w = Math.max($(document).width(), $(window).width());

			this.element.css({

				"height":h,
				"width":w

			});

			var box = $("#dealer-locator-module-modal");

			var marginTop = ($(window).height()-box.height())/2;
			var marginLeft = ($(window).width()-box.width())/2;

			box.css({"margin-top":marginTop, "margin-left":marginLeft});

			return this;

		},

		"show": function() {

			$("body").css({"overflow":"hidden"});

			this.resize();

			this.element.show();

			var box = $("#dealer-locator-module-modal");

			var marginTop = ($(window).height()-box.height())/2;
			var marginLeft = ($(window).width()-box.width())/2;

			box.css({"margin-top":marginTop, "margin-left":marginLeft});

			sIFR.replace(gillsansregular,{
				css:".sIFR-root { color: #ffffff; font-size: 32px; }",
				offsetLeft:-1,
				offsetTop:-7,
				selector:"#modal h2 span",
				wmode:"transparent"
			});

			return this;

		}

	};

	function placeholder_fallback() {

		$("[placeholder]").focus(function() {
		  var input = $(this);
		  if (input.val() == input.attr("placeholder")) {
			 input.val("");
			 input.removeClass("placeholder");
		  }
		}).blur(function() {
		  var input = $(this);
		  if (input.val() == "" || input.val() == input.attr("placeholder")) {
			 input.addClass("placeholder");
			 input.val(input.attr("placeholder"));
		  }
		}).blur();

		$("[placeholder]").parents("form").submit(function() {
		  $(this).find("[placeholder]").each(function() {
			 var input = $(this);
			 if (input.val() == input.attr("placeholder")) {
				input.val("");
			 }
		  })
		});

	}

	modal.init();

	$(window).bind("resize", function(){

		modal.resize();

	});
	
	jQuery("a[href^='/dealers/']").bind("click", function(){

		modal.show();

		return false;

	});

});
