var H = false;

function activate_category(e) {
		
	if ( H == false ) {
		H = 'cat';
	}
	
	$('h-category').value = e;
	
	// If there are no active products, we update the product liset
	if (!get_active_product() || H == 'cat') {
		update_product_list(e);
	}
	
	// Deactivate all active links
	$$('ul#search-categories a').each( function(e2) {
		e2.removeClassName('active');
	});
	
	// Activate the clicked link
	$('c' + e).addClassName('active');
		
	
}	

function set_query(c, p) {
	
	
	$('product-loader').show();
	$('category-loader').show();
	
	$('search-products').update('');
	
	new Ajax.Updater('search-products', '/search/ajax/updateProductList', {
	  parameters: { category: c, product: p },
	 onSuccess: function(transport) {
	  	$('product-loader').hide();
	  	$('category-loader').hide();
	  	$('c' + c).addClassName('active');
	  }
	});
	
	$('h-product').value = p;
	$('h-category').value = c;
	return;
	
}

function activate_product(e) {
	
	if ( H == false ) {
		H = 'prod';
	}
	
	$('h-product').value = e;
	
	// If there are no active products, we update the product liset
	if ( !get_active_category() || H == 'prod') {
		update_category_list(e);
	}
	
	// Deactivate all active links
	$$('ul#search-products a').each( function(e2) {
		e2.removeClassName('active');
	});
	
	// Activate the clicked link
	$('p' + e).addClassName('active');

}

function get_active_product() {
	if ( $F('h-product') != '0' && $F('h-product') != '' ) {
		return $F('h-product');
	} else {
		return false;
	}
}

function get_active_category() {
	if ( $F('h-category') != '0' && $F('h-category') != '' ) {
		return $F('h-category');
	} else {
		return false;
	}
}

function update_category_list(product) {
	$('category-loader').show();
	$('search-categories').update('');
	new Ajax.Updater('search-categories', '/search/ajax/updateCategoryList', {
	  parameters: { product: product },
	  onSuccess: function(transport) {
	  	$('category-loader').hide();
	  }
	});
	return;
}

function update_product_list(category) {
	$('product-loader').show();
	$('search-products').update('');
	new Ajax.Updater('search-products', '/search/ajax/updateProductList', {
	  parameters: { category: category },
	 onSuccess: function(transport) {
	  	$('product-loader').hide();
	  }
	});
	
	return;
}

shortcut.add("Enter", function() {
	window.setTimeout('submit_search()', 100);
}, {
	'type':'keydown',
	'propagate':false,
	'target': $('location'),
	'keycode':65
	}
);

function submit_search() {
	
	var success = true;
	
	
	if (false == ( active_category = get_active_category() )) {
		success = false;
		$('search-category-box').addClassName('error');
	}
	if (false == ( active_product = get_active_product() )) {
		success = false;
		$('search-product-box').addClassName('error');
	}
	if ( $F('location') == '' ) {
		success = false;
		$('search-location-box').addClassName('error');
	}
	
	if ( !success ) {
		alert('controleer alle rood gemarkeerde velden');
	} else {
		
		// build query string
		//window.location = '/search/search?_s=1&categorie[]=' + active_category + '&product[]=' + active_product + '&loc=' + $F('location');
		
		var product_name 	= $('p' + active_product).readAttribute('rel');
		var category_name 	= $('c' + active_category).readAttribute('rel');
		
		window.location = '/search/' + $F('location') + '/' + category_name + '/' + product_name + '';
		
	}
	
}