var gst = {};

gst = {
	basePath: '',

	init: function() {
		// Search form
		if ( $('#keys').val() == 'Keywords' ) { $('#keys').addClass('blur'); }

		$('#search').submit(function() {
			if ( $('#keys').val() == 'Keywords' ) { $('#keys').val(''); }
		});

		$('#keys').focus(function() {
			if ( $(this).val() == 'Keywords' ) { $(this).val(''); }

			$(this).removeClass('blur').select();
		});

		$('#keys').blur(function() {
			if ( !$(this).val() || $(this).val() == 'Keywords' ) { $(this).addClass('blur').val('Keywords'); }
		});

		// Product search form
		$('#product-search-region').change(function() {
			if ( $(this).val() != '' )
			{
				$('#product-search-town optgroup').hide();

				$('#' + $(this).val()).show();
			}
			else
			{
				$('#product-search-town optgroup').show();
			}

			$('#product-search-town').val('');
		});

		$('#product-search-town').change(function() {
			if ( $(this).val() != '' )
			{
				var id = $(this).find('option[value="' + $(this).val() + '"]').parent().attr('id');

				$('#product-search-region option:selected').attr('selected', '');

				$('#product-search-region option[value="' + id + '"]').attr('selected', 'selected');

				$('#product-search-town optgroup').hide();

				$('#' + id).show();
			}
			else
			{
				$('#product-search-town optgroup').show();
			}
		});

		$('#product-search button').click(function() {
			if ( $('#product-search-type').val() ) {
				location = gst.basePath + 'list/' + $('#product-search-type').val() + '/' + ( $('#product-search-town').val() ? $('#product-search-town').val() : $('#product-search-region').val() );
			}
			else
			{
				alert('Please select what you want to search for.');
			}

			return false;
		});
	}
};

$(function() {
	gst.init();
});

