function initializeProgressiveDisclosure() {
	$('fieldset.progressiveDisclosure').each(function() {
		var $thisFieldset = $(this).hide();
		var fieldset = $thisFieldset.attr('id');
		// use the (semantic) legend element to build the progressive disclosure 'bar'
		var $thisLegend = $thisFieldset.children('legend').remove();
		$($thisFieldset).before('<div class="progressiveDisclosure"></div>');
		var $thisDiv = $thisFieldset.prev('div');
		$thisDiv.append('<span class="label">' + $thisLegend[0].firstChild.nodeValue + '</span>');
		$thisDiv.append('<span class="summary" id="' + fieldset + 'Summary">summary</span>');
		$thisDiv.append('<span class="control">Show</span>');
		// hide/reveal the fieldset upon clicking on the 'bar'
		$thisDiv.click(function() {
			$(this).toggleClass('expandedProgressiveDisclosure');
			var control = $(this).find('span.control')[0];
			if ($(this).hasClass('expandedProgressiveDisclosure')) {
				control.firstChild.nodeValue = 'Hide';
				$(this).next('fieldset').show();
			} else {
				control.firstChild.nodeValue = 'Show';
				$(this).next('fieldset').hide();
			}
		});
		// update progressive disclosure summaries at initialization and
		// upon change of any of the fieldset's input or select elements
		updateProgressiveDisclosureSummaryFor(fieldset);
		$thisFieldset.find('input').mouseup(function() {
			updateProgressiveDisclosureSummaryFor(fieldset);
		});
		$thisFieldset.find('input').click(function() {
			updateProgressiveDisclosureSummaryFor(fieldset);
		});
		$thisFieldset.find('input').keyup(function() {
			updateProgressiveDisclosureSummaryFor(fieldset);
		});
		$thisFieldset.find('select').change(function() {
			updateProgressiveDisclosureSummaryFor(fieldset);
		});
	});
}

function updateProgressiveDisclosureSummaryFor(fieldset) {
	var updateFunction = fieldset + 'SummaryComputation';
	if (typeof window[updateFunction] != 'undefined') {
		var summary = window[updateFunction]();
		changeSummaryText(fieldset,summary)
	}
}

function changeSummaryText(container,summary) {
	var summaryId = "#" + container + "Summary";
	$(summaryId)[0].firstChild.nodeValue = summary;
}

function deliveryLocationSummaryComputation() {
	var summary = 'Make a selection';
	var selectedRadioButton = $('input.locationRadioBox:radio:checked')[0].value;
	switch (selectedRadioButton) {
		case "pickup":
			var selected = $('select#pickupAddress option:selected');
			summary = 'Pickup at ' + selected.text();
			break;
		case "desk":
			var deskDeliveryLocation = $('input#deskDeliveryLocation')[0].value;
			summary = 'Desk ' + deskDeliveryLocation;
			break;
		case 'deliver':
			var selected = $('select#shippingAddress option:selected');
			summary = 'Delivered to ' + selected.text();
			break;
		default:
	}
	return summary
}
function deliveryScheduleSummaryComputation() {
	var summary = 'Make a selection';
	var selectedFrequency = $('input.frequencyRadioBox:radio:checked')[0].value;
	switch (selectedFrequency) {
		case "recommended":
			var recommendedDays = $('input#deliveryScheduleRecommendedDays')[0].value;
			summary = 'Every ' + recommendedDays + ' days';
			break;
		case "once":
			summary = 'Today by 3pm';
			break;
		case 'daily':
			summary = 'Daily';
			var weekdaysOnly = $('#deliveryScheduleWeekdaysOnly')[0].checked;
			if (weekdaysOnly == true) {
				summary = 'Weekdays only';
			}
			changeSummaryText('frequencyFieldsDaily',summary);
			break;
		case "weekly":
			var weeklyFrequency = $('input#deliveryScheduleWeeklyFrequency')[0].value;
			summary = 'Every ' + weeklyFrequency + ' weeks';
			changeSummaryText('frequencyFieldsWeekly',summary);
			break;
		case "monthly":
			summary = 'Monthly';
			break;
		default:
	}
	return summary;
}

function deliveryHoldSummaryComputation() {
	var summary = '(Optional)';
	var deliveryHoldEnabled = $('#deliveryHoldEnabled')[0].checked;
	if (deliveryHoldEnabled == true) {
		var selectedDeliveryHold = $('input.deliveryHoldRadioBox:radio:checked')[0].value;
		if (selectedDeliveryHold == 'indefinitely') {
			summary = 'Hold indefinitely';
		} else {
		var deliveryHoldEndDate = $('#deliveryHoldEndDate')[0].value;
			summary = 'Hold until ' + deliveryHoldEndDate;
		}
	}
	return summary;
}
function specialInstructionsSummaryComputation() {
	var summary = '(Optional)';
	return summary;
}

function billingInformationSummaryComputation() {
	var summary = 'TODO';
	return summary;
}
function paymentInformationSummaryComputation() {
	var summary = 'TODO';
	return summary;
}







function changeScheduleFrequency() {
	$('div.frequencyFields').hide();
	var selectedFrequency = $('input.frequencyRadioBox:radio:checked')[0].value;
	switch (selectedFrequency) {
		case "recommended":
			$('div#frequencyFieldsRecommended').show();
			$('div#frequencyFieldsEndDate').show();
			break;
		case "once":
			$('div#frequencyFieldsOnce').show();
			break;
		case "daily":
			$('div#frequencyFieldsDaily').show();
			$('div#frequencyFieldsEndDate').show();
			break;
		case "weekly":
			$('div#frequencyFieldsWeekly').show();
			$('div#frequencyFieldsEndDate').show();
			break;
		case "monthly":
			$('div#frequencyFieldsMonthly').show();
			$('div#frequencyFieldsEndDate').show();
			break;
		case "custom":
			$('div#frequencyFieldsCustom').show();
			break;
		default:
	}
}
function initializeChangeScheduleFrequency() {
	$('input.frequencyRadioBox').each(function() {
		var $thisElement = $(this);
		$thisElement.click(function() {
			changeScheduleFrequency();
		});
	});
}

function changeLocation() {
	$('div.locationFields').hide();
	$('div.storeHours').hide();
	var selectedLocation = $('input.locationRadioBox:radio:checked')[0].value;
	switch (selectedLocation) {
		case "pickup":
			updateStoreHours();
			$('div#locationFieldsPickup').show();
			break;
		case "desk":
			$('div#locationFieldsDesk').show();
			break;
		case "deliver":
			$('div#locationFieldsDeliver').show();
			break;
		default:
	}
}
function initializeChangeLocation() {
	$('#pickupAddress').change(function() {
		updateStoreHours();
	});

	$('input.locationRadioBox').each(function() {
		var $thisElement = $(this);
		$thisElement.click(function() {
			changeLocation();
		});
	});
}
function updateStoreHours() {
	$('div.storeHours').hide();
	var selected = $('select#pickupAddress')[0].value;
	var hoursDiv = "div#storeHours" + selected;
	$(hoursDiv).show();
}



function initializeDatePicker() {
	Date.firstDayOfWeek = 7;
	Date.format = 'mm/dd/yyyy';
	$(function() {
		$('input.datePicker').datePicker({clickInput:true})
		$('#datePickerCustomDates').datePicker({inline:true,selectMultiple:true});
	});
}

function initializeCharactersRemaining() {
	$(".countdown").each(function() {
		$(this).focus(function() {
			var c = $(this).attr("class");
			countdown.max = parseInt(c.match(/limit_[0-9]{1,}_/)[0].match(/[0-9]{1,}/)[0]);
			countdown.obj = this;
			iCount = setInterval(countdown.init,1000);
		}).keyup(function() {
			countdown.init();
			clearInterval(iCount);
		});
	});
}
var countdown = {
	init: function() {
		countdown.remaining = countdown.max - $(countdown.obj).val().length;
		if (countdown.remaining > countdown.max) {
			$(countdown.obj).val($(countdown.obj).val().substring(0,countdown.max));
		}
		$(countdown.obj).siblings(".remaining").html(countdown.remaining + " characters remaining.");
	},
	max: null,
	remaining: null,
	obj: null
};

function initializeDefaultData() {
	$(".defaultData").each(function() {
		$(this).keyup(function() {
			$(this).addClass('changedDefaultData');
		});
		$(this).blur(function() {
			$(this).addClass('changedDefaultData');
		});
	});
}



$(document).ready(function() {
	initializeDefaultData();
	initializeProgressiveDisclosure();

	changeScheduleFrequency();
	initializeChangeScheduleFrequency();

	changeLocation();
	initializeChangeLocation();

	updateStoreHours();

	initializeCharactersRemaining();
	initializeDatePicker();
});

