/*
 *	Sitewide functions must be included first. 
 */
$(function(){
	//Create a site namespace to include all other site object under
	site = {};
	
	site.category_list = function(){
		var active_category = '';
		var category_click_callback;
		
		var obj = { 
			load:function(filter, callback){
				var html = "";
				var active_class = "";
				var item = "";
				
				$.post("/index/get_categories",
						{ 
							content_type_id:filter 
						},
						function(data){
					$(".category_nav").html("");
			        for(i=0;i<data.items.length;i++){
			        	item = data.items[i];
						if(item.id == active_category)
							active_class = "nav_item_active";
						else
							active_class = "";
						
						html = "<div class='nav_item " + active_class + "'>" + 
								"<a href='' id='category_" + item.id  + "'>" +  
								item.name.toUpperCase()  +"</a></div>";
								
						$(".category_nav").append(html);
						
						$(".category_nav .nav_item a").click(category_click);
						
						category_click_callback = callback;
			        }							
				},"json");
			},
			active_category:function() { return active_category; }
		}
		
		var category_click = function(e){
			var id = $(this).attr("id");
			id = id.substring(9);
			
			$(".category_nav .nav_item").removeClass("nav_item_active");
			
			if (active_category == id) {
				active_category = '';
			}
			else {
				active_category = id;
				$(this).parent().addClass("nav_item_active");
			}
			
			$(this).blur();
			category_click_callback();
			e.preventDefault();			
		}
		
		return obj;
	}();
})