function set_primary_process(process_id) {
    data = { process_id : process_id };
    $.getJSON("/change_process_idpage/", data, function(json) {
        if (json['content'] != '')
            $("#article").html(json['content']);
        else
            $("#article").html('<p>Coming soon.</p>');

        var div_id = "primary-selection";
//        var select_id = "primary-select";
//        $("#" + div_id + " .selection-wrap > input").remove();
//        $("#" + div_id + " .selection-wrap > div:first").remove();
//        $("#" + div_id + " .selection-wrap > select").remove();
        
//        var tag = '<select id="' + select_id  + '" name="' + select_id + '" tabindex="1">' +
//                  '<option value="">(Select a Process)</option>';

//        var i = 0;
//        while (json['process_id_' + i] != null) {
//            tag = tag + '<option value="' + json['process_id_' + i] + '"';
//            if (json['process_id_' + i] == json['process_id'])
//                tag = tag + " selected";
//            tag = tag + '>' + json['process_name_' + i] + '</option>';
//            i++;
//        }
        
//        tag = tag + '</select>';
//        $("#" + div_id + " p:first").before(tag);
//        $("#" + select_id).selectbox({debug: true});

        $("#" + div_id + " strong").html(json['group_name']);        
    }); 
}

//--------- Groups Box ----------------//

function openWindow(linky) {
	thisParent = $(linky).parent().attr("id");
	if(thisParent == "primary-group-select"){
		$("#primary-group-select").next("div.groups").show();
	}else {
		$("#secondary-group-select").next("div.groups").show();
	}
 	openStatus=1;
}
 
function closeWindow(linky) {
	$("div.groups").hide();
 	openStatus=0;
}
 
function setGroups() {
	openStatus=0; 	
 	$("div.groups").hide();
}

$(document).ready(function() {
    setGroups();

    $(".browse li > a").click(function() {
        var process_id = $(this).attr("href");
        set_primary_process(process_id);
        
        closeWindow();

        return false;
    });    
    
	$("a.groups-toggle").click(function() {

        if(openStatus==0){
			openWindow($(this));
		}else {
			closeWindow();
		}

		return false;
	});
	
	$("div.groups p > a").click(function() {
		closeWindow($(this));
		return false;
	});

	/*$("div.groups ul > li > a").click(function() {
        var group_id = $(this).attr("href");
        var _parent = $(this).parents().get(2);
        var parent_id = $(_parent).attr("id");

        var div_id = "secondary-selection";
        var select_id = "secondary-select";
        if (parent_id == "primary-group-select-box") {
            var div_id = "primary-selection";
            var select_id = "primary-select";
        }

        var data = { group_id:group_id };
        $.getJSON("/change_select_group/", data, function(json) {
            $("#" + div_id + " .selection-wrap > input").remove();
            $("#" + div_id + " .selection-wrap > div:first").remove();
            $("#" + div_id + " .selection-wrap > select").remove();

            var tag = '<select id="' + select_id  + '" name="' + select_id + '" tabindex="1">' +
                '<option value="">(Select a Process)</option>';

            var i = 0;
            while (json['process_id_' + i] != null) {
                tag = tag + '<option value="' + json['process_id_' + i]  + '">' +
                    json['process_name_' + i] + '</option>';
                i++;
            }

            tag = tag + '</select>';
            $("#" + div_id + " p:first").before(tag);
            $("#" + select_id).selectbox({debug: true});

            $("#" + div_id + " a.groups-toggle").html(json['group_name']);
        });
         
		closeWindow();
		return false;
    });*/

    //Browsing drop-downs
	$("div.cats ul").hide();
	$("div.cats h5").click(function(event) {
		$(this).next("ul").toggle();
		
		if($(this).children("span").hasClass("collapse")){
			$(this).children("span").removeClass();
		}else {
			$(this).children("span").addClass("collapse");
		}
	});
});
