/**
*	Interactive Poster Background Processes 1.0
*
*	Mark Lindros, Oracle Corporation
*
*/

/*
 * Initialize all JS for the Background Processes Interactive
 * This function initializes all the categories of bg processes rendered by the poster.
 */
function init_bgprocesses() {
    // Does element exist?
    if (!$('ul.bgcategories').length) {
        //If not, exit.
        return;
    }

    // Listen for click on categories
    $('ul.bgcategories a').click(function() {
        if (!$(this).hasClass('current')) {
            // Change the current indicator.
            $(this).addClass('current').parent('li').siblings('li').find('a.current').removeClass('current');
        }

        writeBackgroundProcesses($(this).text());
        this.blur();
        return false;
    });

    //Initialize the window with the first category's processes
    writeBackgroundProcesses($('ul.bgcategories a:first').text());
    $('ul.bgcategories a:first').addClass('current');
}

var bginteractive = null;

function writeBackgroundProcesses(category) {
    //Write background processes information
    $('.bgwindow').scrollTop(0);
    $('.bgwindow').html(''); //Clear the window for new content

    //$.ajax({
    //    type:"GET",
    //    url:"data/poster.xml",
    //    dataType:"xml",
    //    async: false,
    //    success:function (xml) {

	if (bginteractive == null)
		//bginteractive = $(xml).find('interactive[style="background_processes"]');
		bginteractive = $.grep(posterData.interactives, function(e){ return e['-style'] == "background_processes"; })[0];
		
	//Set title bar
	$('.bgtitleleft').html('<p align="" style="font-size:14pt;"><strong>&nbsp;' + category + '</strong></p>');
	$('.bgtitleright').html('<p style="font-size:16pt;"><strong>' + $(bginteractive).attr("-name") + ' Background Processes&nbsp;</strong></p>');

	//$(bginteractive).find('process[category="' + category + '"]').each(function() {
	$($.grep(bginteractive.processes, function(e){ return e['-category'] == category; })).each(function() {
		var jRow = $(document.createElement("tr"));
		jRow.append('    <td class="bgacronym">' + $(this).attr("-acronym") + '</td>');
		jRow.append('    <td class="bgname">' + $(this).attr("-name") + '</td>');
		jRow.append('    <td class="bgdesc">' + $(this).attr("-desc") + '</td>');
		jRow.append('    <td class="bgrequired" align="center">' + $(this).attr("-required") + '</td>');
		jRow.append('    <td class="bgstart_by_default" align="center">' + $(this).attr("-start_by_default") + '</td>');
		jRow.append('    <td class="bgnew_this_release" align="center">' + $(this).attr("-new_this_release") + '</td>');
		$('.bgwindow').append(jRow);
	});

	$('.bgwindow').parent().fixedHeaderTable({altClass: 'odd'});
    //    }
    //});
}

function writeBackgroundProcess(pid) {
    var category = null;

    //Write background processes information
    $('.bgwindow').scrollTop(0);
    $('.bgwindow').html(''); //Clear the window for new content

    //Make suggestions go away
    $('#bgprocssearchform').trigger('focusout');

    
	if (bginteractive == null)
		//bginteractive = $(xml).find('interactive[style="background_processes"]');
		bginteractive = $.grep(posterData.interactives, function(e){ return e['-style'] == "background_processes"; })[0];
	//var process = $(bginteractive).find('process[name="' + pid + '"]');
	var process = $.grep(bginteractive.processes, function(e){ return e['-name'] == pid; })[0];
	category = $(process).attr("-category");

	//Set title bar
	$('.bgtitleleft').html('<p align="" style="font-size:14pt;"><strong>&nbsp;' + $(process).attr("-category") + '</strong></p>');
	$('.bgtitleright').html('<p style="font-size:16pt;"><strong>' + $(bginteractive).attr("-name") + ' Background Processes&nbsp;</strong></p>');

	var jRow = $(document.createElement("tr"));
	jRow.append('    <td class="bgacronym">' + $(process).attr("-acronym") + '</td>');
	jRow.append('    <td class="bgname">' + $(process).attr("-name") + '</td>');
	jRow.append('    <td class="bgdesc">' + $(process).attr("-desc") + '</td>');
	jRow.append('    <td class="bgrequired" align="center">' + $(process).attr("-required") + '</td>');
	jRow.append('    <td class="bgstart_by_default" align="center">' + $(process).attr("-start_by_default") + '</td>');
	jRow.append('    <td class="bgnew_this_release" align="center">' + $(process).attr("-new_this_release") + '</td>');
	$('.bgwindow').append(jRow);
	$('.bgwindow').parent().fixedHeaderTable({});
   
    //Set the current category for this searched ID
    $('ul.bgcategories a').each(function() {
        if ($(this).text() == category) {
            // Change the current indicator.
            $(this).addClass('current').parent('li').siblings('li').find('a.current').removeClass('current');
        }
    });
}

//re-build the fixedHeaderTable on resize
function resetBGProcsTables() {
    var bgwin = $('.bgwindow').parent();
    if ($(bgwin).is(':hidden')) return;
    $(bgwin).fixedHeaderTable('destroy');
    $(bgwin).fixedHeaderTable({altClass: 'odd'});
}

