
var preview_url = "http://rfp.sourcex.ca/images/previews/";//"http://192.168.253.11/Clients/srscan/SourceX/Development/images/previews/"; //"http://rfp.sourcex.ca/images/previews/";

RFPB.switchTree = {};
// Settings
RFPB.switchTree.projName      = "#projName";    // Where name is
RFPB.switchTree.projDesc      = "#projDesc";    // Where desc is
RFPB.switchTree.projTree      = "#projTree";    // Where tree is
RFPB.switchTree.projSelector  = "#selProj";     // Prefix of selection links
RFPB.switchTree.projStats     = "#projStats";   // Where stats are
RFPB.switchTree.projPrice     = "#projPrice";   // Where price is
RFPB.switchTree.projScroll    = "#rfp_content"; // Item to scroll on load
RFPB.switchTree.selectedClass = "current";      // Class of selected link
RFPB.switchTree.getURL        = "/ajax/tree/";  // Base URL
// Init
var projects = [];

/**
 * Loads a new tree.
 * @param int pid The project ID to load.
 */
RFPB.switchTree.switchTrees = function(pid)
{
	if (pid && (pid != 0))
	{
		$j("#rfp_content_inner").show();
		$j("#resource-content-inner").hide();
	
		//$j(RFPB.switchTree.projScroll).scrollTo( { top: 0, left: 0 }, 500 );
		//var formData = $j("#projSelect").serialize();
		//formData = formData + '&method=ajax&action=loadTree';
		var formData = '';
		var addURL = 'loadTree/'+pid;
		
		$j("#selectFirst").hide();
		$j('h3').show();
		$j('h4').show();
		//$j('#rfp_description .buyNow').hide();
		// CONST
		$j('#projBuy').empty();
		$j('#buyProj'+pid).clone().appendTo('#projBuy');
		//$j('#projBuy').show();
		// CONST
		
		$j('.'+RFPB.switchTree.selectedClass).removeClass(RFPB.switchTree.selectedClass);
		$j(RFPB.switchTree.projSelector+pid).parent().addClass(RFPB.switchTree.selectedClass);
		$j(RFPB.switchTree.projName).text(projects[pid]['name']);
		$j(RFPB.switchTree.projDesc).html(projects[pid]['description']).css('height','450px').css('height','');
		$j(RFPB.switchTree.projPrice).show().text('Price: $'+projects[pid]['price']);
		$j(RFPB.switchTree.projTree).text('Loading...');
		$j.get(RFPB.switchTree.getURL+addURL, formData, RFPB.switchTree.processTree);
		
		var previews = projects[pid]["previews"];
		var preview_html = "";
		
		for (i in previews) {
			preview_html += '<li><a href="' + preview_url + previews[i]["imageFilename"] + '">Sample ' + (parseInt(i) + 1) + '</a></li>';
		}		
		
		$j("#project-previews").html(preview_html);
		$j("#project-previews a").lbox();
		
		$j("#rfp-content").scrollTop(0);
		// Scroll the window to the top of the container (client request, ticket #74)
		window.scrollTo(0, document.getElementById('rfp-container').offsetTop);
	}
}

/**
 * Function which deals with received tree data received from server.
 */
RFPB.switchTree.processTree = function(data)
{
	eval('data='+data);
	$j(RFPB.switchTree.projTree).show().html(data['tree']);
	
	// Cats, reqs and mands
	/*var totalMands = 0;
	try // It might not exist, therefore...
	{
		// Case-sensitive check, since types are user-provided
		totalMands = parseInt(data['reqs']['mandatory']);
		if (isNaN(totalMands))
			totalMands = parseInt(data['reqs']['Mandatory']);
		if (isNaN(totalMands))
			totalMands = 0;
	}
	catch (ex) {} // We already set it to zero.
	*/
	$j(RFPB.switchTree.projStats).show().html('<div>'+data['cats']+
		'&nbsp;categories, '+data['reqs']['_total']+'&nbsp;requirements.</div>');
	/*// Cats, reqs and all types
	// Get requirement type counts
	var htmlString = '';
	var reqTotal = 0;
	
	$j.each(data['reqs'], function (index, value) {
		if (index != '_total')
			htmlString += '<li>' + index + ': ' + value + '</li>';
		else
			reqTotal = value;
		return true; // <-- Keep looping
	});
	
	$j(RFPB.switchTree.projStats).show().html('<div>' + data['cats'] +
		' categories, '+reqTotal+' requirements:</div><ul>'+htmlString+'</ul>');*/
	// Cats and reqs only
	//$j(RFPB.switchTree.projStats).show().html('Number of categories: ' + data['cats'] +
	//	'<br/>\nNumber of requirements: ' + data['reqs']);
	RFPB.switchTree.showTree();
}

/**
 * Shows the tree. Only call when tree has been replaced or on first show.
 */
RFPB.switchTree.showTree = function(wasNoSelection)
{
	idOfFolderTrees = ['projTree'];
	//idOfFolderTrees = ['dhtmlgoodies_tree'];
	useAjaxToLoadNodesDynamically = false;
	contextMenuActive = false;	// Set to false if you don't want to be able to delete and add new nodes dynamically
	initTree();
	// Initial hide of tree subnodes
	// This is done here so that non-JavaScript users will still see the whole
	// tree when they select it.
	// There ought to be a better way to do this, though...
	$j(".dhtmlgoodies_tree ul").hide();
	showHideNode(null, 'cat0');
	/*// Expand second-level cats
	// Why so many children calls: projTree div -> ul -> cat0 li -> ul -> catX lis
	$j("#projTree").children().children().children().children("li").each(
		function (index, elem) { showHideNode(null, elem.id.substr(21)); }
	);*/
	//if (!wasNoSelection)
		expandAll('projTree');
	// FIXME: This doesn't want to work inside folder-tree-static for some reason... Band-aid.
	$j("#projTree img[src*='folder']").click(showHideNode);
}


