$(document).ready(function(){
	// if a category is selected try to get the subcategory
	$("#shopping_cat").change(function(){
		var cat_id = $(this).attr('value');
		if(cat_id != '')
			var html = $.ajax({
				type: "POST",
				url: "ajax/get_shopping_sub_cat.php",
				data: "cat_id="+cat_id,
				async: false
			}).responseText;
		else{
			html = '';
		}
	$("#shopping_sub_cat_div").html(html);
	$("#error_message").html('');
	})
	
	// check and send the request
	$("#submit_shopping_form").click(function(){
		// choose beetween the category or the sub-cat
		var sub_cat_div = $("#shopping_sub_cat_div").html();
		var main_cat_id = $("#shopping_cat").attr('value');
		
		// no sub-cat --> take the cat_id
		var cat_id = '';
		if(sub_cat_div == ''){
			if(main_cat_id != ''){
				cat_id = main_cat_id;
			}
		}
		// sub-cat -->  take the subcat_id if one selected
		else{
			var sub_cat_id = $("#shopping_sub_cat").attr('value');
			if(sub_cat_id != ''){
				cat_id = sub_cat_id;
			}
			else if(main_cat_id != ''){
				cat_id = main_cat_id;
			}
		}
		
		// check errors
		var keyword = $("#keyword").attr('value');
		var html = '';
		if(cat_id == ''){
			var html = html+'no cat<br/>';
		}
		if(keyword == ''){
			var html = html+'no keyword';
		}
		
		if(html == ''){
			html = $.ajax({
					type: "POST",
					url: "ajax/get_shopping_products.php",
					data: "cat_id="+cat_id+"&keyword="+keyword,
					async: false
				}).responseText;
			$("#shopping_result").html(html);
			
			// add action on links
			$('#hot_product_details  div').hide();
			$('#prod-0').show(); 
			
			// add event if click on product
			$('#hot_shop_list ul li a').click(function(){
				var currentProduct = $(this).attr('href');
				$('#hot_product_details  div').hide();
				$('#prod-'+currentProduct).show(); 
				return false;
			})
			
		}
		else{
			$("#error_message").html(html);
		}
	})
});