// hey look, I know its not great javascript but it works, mmkay...
function hide_option(element) {
	if(element.getSize().y > 0) {
		element.store('default-height', element.getSize().y);
		element.morph({'opacity': 0, 'height': 0});
	}
}
function show_option(element) {
	if(element.getSize().y == 0) {
		element.morph({'opacity': 1, 'height': element.retrieve('default-height')});
	}
}

window.addEvent('domready', function () {
	var options = $('product').getElements('div.options');
	if(options) Array.each(options, function (optiongroup, i) {
		var optionfilters = optiongroup.getElement('dl.optionfilters');
		if(optionfilters) {
			optionfilters.getElement('input[type=submit]').getParent().destroy();
			var optionselectors = optionfilters.getElements('select');
			var optionlist = optiongroup.getElement('div.optionlist');
			var usesForceSelect = false;
			if(optionlist.getElementById('forceselect')) usesForceSelect = true;

			Array.each(optionselectors, function ( selector, idx ) {
				selector.addEvent('change', function() {
					var remainingselectors = 0;
					Array.each(optionselectors, function ( otherselector, l ) {
						if(otherselector.getSelected().pick().get('value')=='' && otherselector.getSelected().pick().get('text')!='All') { // if we force the selection then the text part says 'Please select ...' rather than 'All'
							remainingselectors += 1;
						}
					});
					if(remainingselectors == 0) {
						if(usesForceSelect) hide_option(optionlist.getElementById('forceselect')); // hide the first one, containing summary information
						var show_something = false;
						Array.each(optionlist.getElements('div.option'), function (optionview, k) {
							if(optionview.get('id')!='forceselect') {
								var match = true;
								Array.each(optionselectors, function ( otherselector, l ) {
									if(otherselector.getSelected().pick().get('text')!='All') {
										var selectedvalue = otherselector.getSelected().pick().get('value');
										var attributedesc = optionview.getElement('div.attribute').getElement('dd[class~='+otherselector.get('name')+']');
										if(attributedesc && attributedesc.get('html').replace(/[^\w\d]/g, '')!=selectedvalue.replace(/[^\w\d]/g, '')) {
											match = false;
										}
									}
								});
								if(match) {
									show_something = true;
									show_option(optionview);
								} else {
									hide_option(optionview);
								}
							}
						});
						if(show_something) {
							if(document.getElementById('no_product_option_available')) {
								document.getElementById('no_product_option_available').destroy();
							}
						} else {
							if(!document.getElementById('no_product_option_available')) {
								options.grab(new Element('p', {
									'id': 'no_product_option_available',
									'html': '<small>Sorry, your selected item is currently unavailable. Please select an alternative.</small>'
								}));
							}
						}
					} else {
						Array.each(optionlist.getElements('div.option'), function (optionview, k) {
							if(optionview.get('id')!='forceselect' && optionselectors.length>1) { // if we have more than one selector, then hide some of the options if this selector is blank
								hide_option(optionview);
							} else {
								show_option(optionview);
							}
						});
					}
				});
			});
		}
	});
});

