function resetSearch() 
{

	// Resest all forms to closed state
	$('.colorList').slideUp('normal');
	$('.colorList').removeClass('focused');
	$('#detailedSearches .subFields .subFieldsHeader a span').css('background-image', 'url(/img/layout/search_form_arrow2.png)');

	$('.subOptions').slideUp('normal');
	$('.subOptions').removeClass('focused');
	$('#detailedSearches > form > fieldset > ol > li > div.fieldLabelContainer > a.expandable span').css('background-image', 'url(/img/layout/search_form_arrow.png)');


	$('.subFieldHeaderLink').css('width', '100%');
	$('.selectedColor').show();
	$('.searchAllColorsLink').hide();

}

function clearAllValues3434()
{

	try
	{
		document.getElementById('productDetailedSearch_productType').value = "";
		document.getElementById('productDetailedSearch_primaryColor').value = "";
		document.getElementById('productDetailedSearch_accentColor').value = "";
		document.getElementById('productDetailedSearch_theme').value = "";
		document.getElementById('productPatternSearch_pattern').value = "";
		document.getElementById('productKeywordSearch_keyword').value = "";
	}
	catch(e){}
	
	blnDebugging = false; // set to true if you wish to debug values...
	if(blnDebugging)
	{
		alert('Product Type: ' + document.getElementById('productDetailedSearch_productType').value);
		alert('Primary Color: ' + document.getElementById('productDetailedSearch_primaryColor').value);
		alert('Accent Color: ' + document.getElementById('productDetailedSearch_accentColor').value);
		alert('Theme: ' + document.getElementById('productDetailedSearch_theme').value);
		alert('Pattern Name:  ' + document.getElementById('productPatternSearch_pattern').value);
		alert('Keyword: ' + document.getElementById('productKeywordSearch_keyword').value);
	}
}

$().ready(function() 
{
	// VS - July 12, 2011
	clearAllValues3434();

	$( "#secondaryNav .subOptions" ).hide();

	$( "#detailedSearches > form > fieldset > ol > li > div > a" ).click(function() {
		var field = $(this).closest('.field');
		var subMenu = $(field).find('.subOptions');
		var header = $(this).closest('.fieldLabelContainer');

		if(subMenu.length > 0) {
			if (subMenu.is(':visible') || subMenu.hasClass('justSearchedFor')) {
				subMenu.slideUp('normal');
				header.removeClass('focused');
				header.find('span').css('background-image', 'url(/img/layout/search_form_arrow.png)');
			}
			else {
				resetSearch();
				// Open selected menu
				subMenu.slideDown('normal');
				header.addClass('focused');
				header.find('span').css('background-image', 'url(/img/layout/search_form_arrow_focused.png)');
			}
		}
	});




	$( "#secondaryNav .colorList" ).hide();
	$( "#secondaryNav .searchAllColorsLink" ).hide();

	$( ".subFieldsHeader > a.subFieldHeaderLink" ).click(function() {
		var field = $(this).closest('.field');
		var subMenu = $(field).find('.colorList');
		var header = $(this).closest('.subFieldsHeader');

		if(subMenu.length > 0) {
			if (subMenu.is(':visible') || subMenu.hasClass('justSearchedFor')) {				
				subMenu.slideUp('normal');
				header.removeClass('focused');
				header.find('span').css('background-image', 'url(/img/layout/search_form_arrow2.png)');
				$(field).find('.subFieldHeaderLink').css('width', '100%');
				$(field).find('.selectedColor').show();
				$(field).find('.searchAllColorsLink').hide();
			}
			else {
				resetSearch();
				// Open selected menu
				subMenu.slideDown('normal');
				header.addClass('focused');
				header.find('span').css('background-image', 'url(/img/layout/search_form_arrow2_focused.png)');
				// Hide selected color of opening color list
				$(field).find('.selectedColor').hide();
				$(field).find('.subFieldHeaderLink').css('width', '70px');
				$(field).find('.searchAllColorsLink').show();
			}
		}
	});










	$( ".field.singleOption .option" ).click(function() {
		var field = $(this).closest('.field');
		var newValue = $(this).html();

		$(field).find('a').removeClass('selected');
		$(this).addClass('selected');
		$(field).find('.value').val(newValue);
		$(field).find('.expandable').trigger('click');
	});


	$( ".field.multipleOptions .option" ).click(function() {
		var field = $(this).closest('.field');
		var optionInput = $(field).find('.value');
		var optionDisplayInput = $(field).find('.displayValue');
		var optionInputValue = optionInput.val();
		var newValue = $(this).html();	

		if ($(this).is('.selected')) {
			$(this).removeClass('selected');
			optionInputValue = optionInputValue.replace(newValue, '');
			optionInputValue = optionInputValue.replace(', , ', ', ');
			optionInputValue = optionInputValue.replace(/^, /, '');
			optionInputValue = optionInputValue.replace(/, $/, '');

		} else {
			$(this).addClass('selected');
			optionInputValue = optionInputValue + ', ' + newValue;
			optionInputValue = optionInputValue.replace(/^, /, '');
		}

		optionInput.val(optionInputValue);
		
		if (optionInputValue.length > 25) {
			optionInputValue = optionInputValue.substr(0, 25) + '...';
			optionDisplayInput.val(optionInputValue);
		}
		else {
			optionDisplayInput.val(optionInputValue);
		}
	});


	$( ".field.singleColorSwatch .option" ).click(function() {
		var field = $(this).closest('.field');
		var bgColorName = $(this).find('.colorName').html();
		var jpgName = bgColorName.replace(' ', '_').replace('/', '_').toLowerCase();

		$(field).find('li').removeClass('checkedColor');
		$(this).closest('li').addClass('checkedColor');

		
		// Just changing the src doesn't work in IE6
		//$(field).find('.selectedColor').attr('src', 'img/layout/swatches/'+jpgName+'.jpg');
		var id = $(field).find('.selectedColor').attr('id');
		var classes = $(field).find('.selectedColor').attr('class');
		$(field).find('.selectedColor').remove();
		$(field).find('.subFieldsHeader a.subFieldHeaderLink').prepend('<img id="'+id+'" class="'+classes+'" src="/img/layout/swatches/'+jpgName+'.jpg" />');


		$(field).find('.value').val(bgColorName);
		$(field).find('.subFieldsHeader a.subFieldHeaderLink').trigger('click');
	});

	$( ".field.singleColorSwatch .searchAllColorsLink" ).click(function() {
		var field = $(this).closest('.field');

		$(field).find('li').removeClass('checkedColor');
		$(this).closest('li').addClass('checkedColor');


		
		// Just changing the src doesn't work in IE6
		//$(field).find('.selectedColor').attr('src', 'img/layout/swatches/'+jpgName+'.jpg');
		var id = $(field).find('.selectedColor').attr('id');
		var classes = $(field).find('.selectedColor').attr('class');
		$(field).find('.selectedColor').remove();
		$(field).find('.subFieldsHeader a.subFieldHeaderLink').prepend('<img id="'+id+'" class="'+classes+'" src="/img/layout/swatches/all_colors.png" />');



		$(field).find('.value').val('');
		$(field).find('.subFieldsHeader a.subFieldHeaderLink').trigger('click');
	});









	// Special Effects Checkbox
	$('#special_effects_dimensional_option').click(function() {
		$('.subOptions').addClass('justSearchedFor');

		if ($(this).hasClass('selected')) {
			$('.special_effects_dimensional_suboption.selected').trigger('click');
			$(this).removeClass('selected');
		}
		else 
		{
			$('.special_effects_dimensional_suboption:not(.selected)').trigger('click');
			$(this).addClass('selected');
		}

		$('.subOptions').removeClass('justSearchedFor');
	});




	//////////////////////////////////
	// Default Options
	/////////////////////////////////
	// Wallpaper is the default theme
	$('#productTypeField .subOptions').addClass('justSearchedFor');
	$('#productTypeField .subOptions li:nth-child(1) a.option').trigger('click');
	$('#productTypeField .subOptions').removeClass('justSearchedFor');


});
