(function($){
	
// The DOM is now ready
$(function() {
	
		// Form display rules
		$("#contact-info #form-wrapper").show();
		$("#contact-info").css("overflow", "");
		
		function formToggle() {
			formToggle.current.call(this);
			formToggle.current = (formToggle.current == formToggle.show) ? formToggle.hide : formToggle.show;
		}
		
		formToggle.show = function () {
			$("#contact-info #form-wrapper").show();
			this.blur();
			$("#start").text("Return to Viewing Images");
		};
		formToggle.hide = function () {
			$("#contact-info #form-wrapper").hide();
			this.blur();
			$("#start").text("Optimize Your Time Now");
		};
		formToggle.current = formToggle.show;
		
		$("#start").bind("click", function () {
			formToggle.call(this);
			return false;
		});
		
		$(".start-plan").bind("click", function () {
			formToggle.show.call(this);
			formToggle.current = formToggle.hide;
			return false;
		});
		
		
		// Allow non-US countries to have default state/province and remove zip field
		var state = document.getElementById("state");
		var	non_US = state.cloneNode(true),
			provinces = state.cloneNode(true),
			zipCodeField = $("input[name=zip]");

		non_US.options.length = 1;
		non_US.options[1] = new Option("N/A", "N/A", true, true);
		non_US.selectedIndex = 1;
		
		provinces.options.length = 1;

		$.each(["AB","BC","MB","NB","NL","NS","NT","NU","ON","PE","QC","SK","YT"], function (i, province) {
			provinces.options[(i+1)] = new Option(province, province, true, true);
		});
		
		provinces.selectedIndex = 0;
		
		$("#country").bind("change", function () {
			var stateDom = document.getElementById('state'),
				replace,
				zipLogic = function () {
					return (this.value == "N/A") ? "" : this.value;
				};

			switch (this.value) {
				case "United States":
					replace = state;
					break;
				case "Canada":
					replace = provinces;
					break;
				default:
					replace = non_US;
					zipLogic = "N/A";
					break;
			}
			
			stateDom.parentNode.replaceChild(replace, stateDom);
			zipCodeField.attr("value", zipLogic);
			
		}).triggerHandler("change");
		
		
		// Fix submit button for Mozilla
		if ($.browser.mozilla && $.browser.version.indexOf("1.8.0") == 0) {
			$("body").addClass("firefox_1_5");
		}


		
		// Contact Form Behavior
		var form = $("#data_capture"),
			contactBy = $("#contact_by"),
			submit = $("#submit"),
			formOne = $("#form_1"),
			contactInfo = $("#contact_info");
			
		// switch(domain[1]){
		// 			case "flexjet":
		// 				$("#contact-info form")[0]['00N300000013MYZ'].selectedIndex = 0;
		// 			break;
		// 			case "flexjet25":
		// 				$("#contact-info form")[0]['00N300000013MYZ'].selectedIndex = 1;
		// 			break;
		// 			case "flexjetone":
		// 				$("#contact-info form")[0]['00N300000013MYZ'].selectedIndex = 2;
		// 			break;
		// 		}

		// formHolder.appendChild(document.getElementById('contact_info'));

		var fields = {
			"Email": $("#formEmail"),
			"Mail": $("#formMail"),
			"Phone": $("#formPhone")
		};

		$("#formEmail").before('<div id="wrapContactBy"></div>');
		
		for (var i in fields) {
			fields[i].remove();
		}
		
		contactInfo.hide();

		contactBy.bind("change", function () {
			if (this.value == "") return;
			
			
			contactInfo.show();
			formOne.hide();	
				
			// Blur this field to prevent scrolling from firing more change events
			$("#salutation")[0].focus();
			
			// jQuery("#contact-info").css("overflow", "auto");
			
			// Add this form back into the document
			$("#wrapContactBy").empty().append(fields[this.value]);
		});

		$("#back-button").bind("click", function () {
			$("#contact-info form")[0]['00N300000012kyV'].selectedIndex = 0;
			contactInfo.hide();
			formOne.show();
			$("#contact-info").css("overflow", "");
			
			this.blur();
			return false;
		});
		
		//If page is reloaded, set contact by select list to default
		$('select#contact_by')[0].selectedIndex = 0;
		
		$("#contact-info form").bind("submit", function(event) {
		
			// Check everything for blank value
			
			$('select#contact_by, select#salutation, #contact_info input[type="text"]:not(#zip), #contact_info select:not(#salution), input#zip').each(function(){
				var $this = $(this);
				if (this.value == ""){
					alert("You have to enter your " + $this.parent('label').text().match(/^\s*(.*?):/)[1].toLowerCase() + ".");
			        this.focus();
					event.preventDefault();
			        return false;
				}
			});
			
			// try {
			// 	var salutation = jQuery("#contact-info form")[0]['salutation'];
			// 	if(salutation.value == ""){
			// 		this.selectedIndex = 0;
			// 	
			// 		alert("You have to enter your salutation");
			// 		return false;
			// 	}
			// 	jQuery("#contact-info form input, #contact-info form select").each(function () {
			// 		if (this.value == "" && this.name != "Campaign_ID" && this.name != "701600000009q7X") {
			// 			alert("You have to enter your " + jQuery(this).parent("label").text().match(/^\s*(.*?):/)[1].toLowerCase() + ".");
			// 			this.focus();
			// 			throw false;
			// 		}
			// 	});
			// } catch (e) {
			// 	return false;
			// }
			
						
			// Change the retURL field to pass through Spotlight variables
			var benefits = [];
			
			$.each(this['00N60000001hI6l'], function (i, benefit) {
				if (benefit.checked) benefits.push(benefit.value);
			});
			
			
			//"?benefits=", encodeURI(benefits.join(",")),
			this.retURL.value = this.retURL.value.replace(/\?.*/, "").concat(
				"?re=", encodeURI('request_info'),
				"&contact_by=", encodeURI(this["00N300000012kyV"].value),
				"&zip_code=", encodeURI(this.zip && this.zip.value || "")
			);
			
		});
		

		// Populate hidden field with the campaign id
		// if((window.location.search.match(/campaign_id=(.*?)(?:&|$)/) || ["",""])[1]){
		// 			var cid = (window.location.search.match(/campaign_id=(.*?)(?:&|$)/) || ["",""])[1];
		// 			$.cookie('campaign_id',cid, {expires: 0});
		// 			$("#Campaign_ID").attr("value", $.cookie('campaign_id'));
		// 		}
		
		if($.cookie('campaign_id') != null){
			var cid = $.cookie('campaign_id');
			var input_id = $('input#00N300000013xHu');
			$("#Campaign_ID").attr('value', cid);
			switch(cid){
			case '70160000000AHRM':
				input_id.attr('value', 'www.Flexjet25.com/olympicgold')
			break;
			case '70160000000AHRl':
				input_id.attr('value', 'www.Flexjet25.com/olympicemail')
			break;
			case '70160000000AHRN':
				input_id.attr('value', 'www.Flexjet25.com/olympicenews')
			break;
			case '70160000000AMwH':
				input_id.attr('value', 'www.Flexjet25.com/olympicHome')
			break;
			case '70160000000AHRH':
				input_id.attr('value', 'www.Flexjet25.com/olympicedition')
			break;
			case '70160000000AHRH':
				input_id.attr('value', 'www.Flexjet25.com/2010')
			break;
			case '70160000000AMvE':
				input_id.attr('value', 'www.flexjet25.com/olympicUE')
			break;
			case '70160000000AODH':
			case '70160000000AODR':
			case '70160000000AOD8':
			case '70160000000AOeB':
			case '70160000000AOD9':
			case '70160000000AOeF':
				input_id.attr('value', 'www.Flexjet25.com/olympicads')
			break;
			}
		}
			
			
		

			
		// Display form if we came from the thank-you page
		if (window.location.hash == "#start") {
			$("#start").triggerHandler("click");
		}
		
	});
	

})( jQuery.noConflict() ); // Pass in jQuery so we can safely use the $ alias within this block
