AG.Brand = new function()
{
	this.selectedBrand = null,
	this.showBrandsByFirstLetter = function(letter) {
		
		var scriptUrl = 'http://' + document.location.host + '/ajax/get_brands_by_first_letter.php';
		
		// get the ID of the currently selected root category
		var rootCategoryUrl = $('#top_menu .active').attr('href');
		var rootCategory = rootCategoryUrl.split('/');
		
		$.ajax({
			type: 'POST',
			url: scriptUrl,
			data: 'CategoryID='+ parseInt(rootCategory[1]) +'&BrandNameFirstLetter=' + letter,
			success: function reloadTableSuccess(data) {
				$("#brands_" + letter).html(data);
				$("#brands_" + letter).show();
			}
		});
	}
}

$(function() {
	
	$().click(function(e){
		// hide all active popups
		$(".brands_index ul li div").each(function(){
			$(this).hide();
		});
		
		$(".brands_index ul li a").each(function(){
			if(AG.Brand.selectedBrand == null || $(this).attr("id") != AG.Brand.selectedBrand.attr("id")) {
				$(this).removeClass("active");
			}
		});
		AG.Brand.selectedBrand = null;
	});
	
	$(".brands_index ul li a").click(function (e) {
		// get the filter and strip any white spaces
		letter = $(this).text().replace(/\s+/g ,"");
		
		AG.Brand.selectedBrand = $(this);
		$(this).addClass("active");
		
		// load brands
		AG.Brand.showBrandsByFirstLetter(letter);
	});
});