/*
 * This file populates empty divs with category navigation
 *
 * Requires: jQuery 1.2+ (jsonp support)
 *
 * Include in the <head> of your page:
 *
 * <script type="text/javascript" src="http://www.google.com/jsapi"></script>
 * <script type="text/javascript">
 *   google.load("jquery", "1");
 * </script>
 * <script src="path_to_this_script/ajax_categories.js"></script>
 *
 * In your <body> somewhere:
 *
 * <div id="category-cultural"></div>
 *
 * ...becomes...
 *
 * <div id="category-cultural">...inserted html...</div>
 */

var vis_jsonp_category_endpoint = "http://visions.ab.ca/ajax_categories.php";

var vis_cat_url = "http://visions.ab.ca/content.php?parent=";

var vis_parent_ids = {
    // Format: "div name" : parent_id,
    "category-cultural" : 1,
    "category-physical" : 3,
    "category-mental"   : 4,
    "category-emotional": 2
}

var vis_header_class_names = {
    "category-cultural" : "cultural_heading",
    "category-physical" : "physical_heading",
    "category-mental"   : "mental_heading",
    "category-emotional": "emotional_heading"
}

function renderHeader(key,json){
	var pdata = json["parent"];
	var name = pdata["cat_name"];
	var cat_id = pdata["id"];
	var records = pdata["count"];
	var desc = pdata["cat_description"];
	var vheader = '<div class="'+vis_header_class_names[key]+'"><h2 style="color:#000000"><a class="blackTitleLinks" href="'+vis_cat_url+cat_id+'" title="'+desc+'">'+name+' ('+records+')</a></h2></div>';
	return vheader;
}

function renderList(key,json){
	var children = json["children"];
	var vlist = '<ul>';
	for (var c in children){
		vlist += renderListItem(children[c]);
	}
	vlist += '</ul>';
	return vlist;
}

function renderListItem(c){
	var vitem = '<li>';
	vitem += '<a title="'+c["cat_description"]+'" class="menuLinks" href="'+vis_cat_url+c["id"]+'">'+c["cat_name"]+' ('+c["resource_count"]+')</a>';
	vitem += '</li>';
	return vitem;
}

function renderJSON(json,status){
	$('#'+this.cat).html(renderHeader(this.cat,json)+renderList(this.cat,json));
}

$(document).ready(function(){
    
    for (var category in vis_parent_ids){
        $.ajax({
            cat:category,
            dataType: "jsonp",
            data: {"parent_id":vis_parent_ids[category]},
            url: vis_jsonp_category_endpoint,
            type : "GET",
            success : renderJSON
        });
    }
});

