/*
Script Dependencies: prototype.js, DOMhelp.js
*/
css_class = {
	show : 'show',
	hide : 'hide',
	input : 'text_input',
	check : 'checked'
	};

/* ADVANCED SEARCH PAGE */
advSearch = {
	
	init : function() {
		if (!document.getElementById) { return false; }
		// Get Advanced Search Form
		advSearch.form = $('advanced_search');
		if (!advSearch.form) { return; }
		
		// Get hidden elements if DOM is supported
		var showCheckbox = $('all_types');
		advSearch.currencyAbbrev = $('currency_abbrev');
		
		// Toggle Features
		var featureCheckbox = $('toggle_features');
		advSearch.features = $('optional_features');
		advSearch.features.hide();
		
		// Remove hide class
		DOMhelp.cssjs('remove', showCheckbox, css_class.hide, true );
		DOMhelp.cssjs('remove', advSearch.currencyAbbrev, css_class.hide, true );
		
		// Get Currency select element, populate selected value
		var currencyDropdown = $('currency_type');
//		advSearch.currencyAbbrev.innerHTML = currencyDropdown.value;
		advSearch.currencyAbbrev.innerHTML = currencyDropdown.options[currencyDropdown.selectedIndex].text.substr( 0, 3 )

		var allTypes = $('all_types_checkbox');
		if ( !advSearch.form || !allTypes ) {
			return false;
			}
		
		// Add events
		DOMhelp.addEvent( allTypes, 'click', advSearch.check_all_or_none, true);
		DOMhelp.addEvent( currencyDropdown, 'change', advSearch.currencyType, true);
		DOMhelp.addEvent( featureCheckbox, 'click', advSearch.toggleFeatures, true);
		
		// Show Currency Abbreviation
		
		},
	// Check or uncheck all types
	check_all_or_none : function( e ) {
		var t = DOMhelp.getTarget( e );
		var types = advSearch.form.getInputs('checkbox', 'type[]');
		for ( var i = 0; i < types.length; i++ ) {
			types[i].checked = t.checked == true ? true : false;
			}
		},
	// Display currency Abbreviation on change, then focus() on min price
	currencyType : function ( e ) {
		var t = DOMhelp.getTarget( e );
//		advSearch.currencyAbbrev.innerHTML = t.value;
		advSearch.currencyAbbrev.innerHTML = t.options[t.selectedIndex].text.substr( 0, 3 )
		if ( $('price_range_min') ) {
			$('price_range_min').focus();
			}
		},
	toggleFeatures : function ( e ) {
		var t = DOMhelp.getTarget( e );
		var label = $('show_features_label');
		var state = DOMhelp.cssjs('check', label, css_class.check, true);
		
		if ( state == false ) {
			new Effect.Appear('optional_features');
			label.innerHTML = "Hide Features";
			DOMhelp.cssjs('add', label, css_class.check, true);
			t.checked = false;
			}
		else {
			new Effect.Fade('optional_features');
			label.innerHTML = "Show Features";
			DOMhelp.cssjs('remove', label, css_class.check, true);
			t.checked = false;
			}
		}
	};

// Run the init method for the following
DOMhelp.addEvent( window, 'load', advSearch.init, false );

