/*
Techdoc Builder 

Last revised: 6/8/05: Jason Bonthron
*/


// ============================================================== techdoc_Builder()

function techdoc_Builder()
{
	this.jumpLinks       = false;
	this.sortAlpha       = false;
	this.categoriesArr   = [];
	
	this.add_Jump_Links  = function(){ this.jumpLinks = true; };
	this.sort_Alpha      = function(){ this.sortAlpha = true; };
	
	this.add_Category    = add_Cat;
	this.add_Doc         = add_Doc;
	this.add_Sub_Heading = add_Sub;
	
	this.create_page     = create_page;
	this.alphaSorter     = alphaSorter;
	this.createJumpLinks = createJumpLinks;
};

// ------------------------------------------------------- add_Cat
function add_Cat(catName, jumpName)
{
	this.categoriesArr[this.categoriesArr.length] = new Category(catName, jumpName);
};

// ------------------------------------------------------- add_Doc
function add_Doc(category, title, URL, fileFormat, fileSize)
{
	for(var i=0; i < this.categoriesArr.length; i++)
	{
		var loopingCat = this.categoriesArr[i];
		
		if(loopingCat.name == category)
		{
			loopingCat.docs[loopingCat.docs.length] = new techDoc(title, URL, fileFormat, fileSize);
		};
	};	
};

// ------------------------------------------------------- add_Sub
// !? SUB_HEADING is treated like a special type of Doc
function add_Sub(category, subName)
{ 
	this.add_Doc(category, "SUB_HEADING", subName, "", ""); 
};

// ------------------------------------------------------- create_page
function create_page()
{
	var returnHTML = new String();

	if( this.sortAlpha ){ this.alphaSorter(); };
	if( this.jumpLinks ){ returnHTML += this.createJumpLinks(); };
	
	for(var i=0; i < this.categoriesArr.length; i++)
	{
		var loopingCat = this.categoriesArr[i];
			
		// category title
		returnHTML += category_title_HTML;
  		returnHTML = returnHTML.replace( "%__catName__%",   loopingCat.name     );
  		returnHTML = returnHTML.replace( "%__jumpName__%",  loopingCat.jumpName );

		// start table
		returnHTML += category_table_HTML1; 
		
		// doc listings
		for(var j=0; j < loopingCat.docs.length; j++)
		{
			var loopingDoc = loopingCat.docs[j];
			
			if(loopingDoc.title == "SUB_HEADING")
			{
				returnHTML += subheading(loopingDoc); // subhead
			}
			else
			{
				returnHTML += docListing(loopingDoc); // ordinary doc			
			};			
		};	
			
		// end table
		returnHTML += category_table_HTML2; 
	};	
	
	document.write(returnHTML);
	return;
};

// -------------------------------------------------------  createJumpLinks
function createJumpLinks()
{
	var returnHTML = jump_start_HTML;	
	
	// math: calculate # full rows <tr> needed
	var fullRows = Math.floor( this.categoriesArr.length / linksPerRow ); 
	var remainder = this.categoriesArr.length % linksPerRow  
	if(remainder != 0 ){ fullRows += 1 }; 
	
	var categoryCounter = 0; 
	
	for(var i = 0; i < fullRows; i++ )
	{
		returnHTML += jump_row_start; // --------- Row start
	
		for(var jab=0; jab < linksPerRow; jab++)
		{
			currentCat = this.categoriesArr[categoryCounter];
			
			if(currentCat != null)
			{	
				// if no jump link name was provided, I create one
				if(currentCat.jumpName == ""){ createJumpName(currentCat); };
				
				var tempHTML = jump_row_link;
				tempHTML = tempHTML.replace( "%__name__%",     currentCat.name    );
				tempHTML = tempHTML.replace( "%__jumpName__%", currentCat.jumpName);
				returnHTML += tempHTML;
				categoryCounter++;	
			}
			else // currentCat == null, add spacers to finish off <tr> 
			{
				returnHTML += jump_row_blank;	
			};
		};
	
		returnHTML += jump_row_end; // --------- Row end
	};
	
	returnHTML += jump_end_HTML;
	return returnHTML;
};

// -------------------------------------------------------  alphaSorter
function alphaSorter()
{
	this.categoriesArr = this.categoriesArr.sort(function (x,y){	
		x1 = x.name.toLowerCase();
		y1 = y.name.toLowerCase();
		x2 = x1.replace(/\s/g,"");
		y2 = y1.replace(/\s/g,"");
		
		if (x2 < y2) { return -1; }
		else if (x2 > y2) { return 1; }
		else { return 0; };	
	}); 
};



// ============================================================== Category()

function Category(catName, jumpName)
{
	this.name     = catName;
	this.jumpName = jumpName;
	this.docs     = [];
};


// ============================================================== techDoc()

function techDoc(title, URL, fileFormat, fileSize)
{
	this.title      = title;
	this.URL        = URL;
	this.fileFormat = fileFormat;
	this.fileSize   = fileSize;
};


// ============================================================== docListing()

function docListing(docObject)
{
	var returnHTML = doc_HTML;	
	returnHTML = returnHTML.replace( "%__URL__%",        docObject.URL        );
	returnHTML = returnHTML.replace( "%__title__%",      docObject.title      );
	returnHTML = returnHTML.replace( "%__fileFormat__%", docObject.fileFormat );
	returnHTML = returnHTML.replace( "%__fileSize__%",   docObject.fileSize   );
	return returnHTML;
};


// ============================================================== subheading()

function subheading(docObject)
{
	var returnHTML = sub_HTML;	
	// not really adding a URL !, see add_sub method
	returnHTML = returnHTML.replace( "%__subhead__%", docObject.URL ); 
	return returnHTML;	
};


// ============================================================== createJumpName

function createJumpName(category)
{
	var tempName = category.name.toLowerCase();
	tempName = tempName.replace(/\s/g,""); //remove whitespace
	category.jumpName = tempName;
};
 


// ============================================================== HTML templates
// variables in this format:  %__something__%
// are replaced out by the script

// category title HTML
var category_title_HTML = "<!--[generic-title]--><table border='0' cellspacing='0' cellpadding='0' width='568'><tr><td width='568' height='10'><a name='%__jumpName__%'></a><img src='/sites/templates/pix/spacer.gif' width='10' height='10' border='0' alt=''/></td></tr><tr><td width='568' class='sectionheader'>%__catName__%</td></tr></table><!--[/generic-title]--><table border='0' cellspacing='0' cellpadding='0' width='568'><tr><td width='568' height='4'><img src='/sites/templates/pix/spacer.gif' width='568' height='4' border='0' alt=''/></td></tr><tr><td width='568' bgcolor='#a6a6a6'><img src='/sites/templates/pix/oline.gif' width='568' height='1' alt=''/></td></tr><tr><td width='568' height='10'><img src='/sites/templates/pix/spacer.gif' width='568' height='10' border='0' alt=''/></td></tr></table><!--[generic-body]-->"

// category start table
var category_table_HTML1 = "<table border='0' cellspacing='0' cellpadding='0' width='568'>";

// category end table
var category_table_HTML2 = "<tr><td width='568' colspan='2'><img src='/sites/templates/pix/spacer.gif' width='568' height='10' border='0' alt=''/></td></tr></table>";

// doc listing rows
var doc_HTML = "<tr><td width='12' valign='top'><img src='/sites/templates/pix/bullet.gif' width='12' height='10' border='0' alt=''/></td><td width='556'><a class='bodylink2' href='%__URL__%'>%__title__%</a> <nobr class='filesize'>%__fileFormat__% %__fileSize__%</nobr></td></tr><tr><td width='568' colspan='2'><img src='/sites/templates/pix/spacer.gif' width='568' height='3' border='0' alt=''/></td></tr>";

// subheading rows
var sub_HTML = "<tr><td colspan='2' height='8'><img src='/sites/templates/pix/spacer.gif' width='568' height='8' border='0' alt=''/></td></tr><tr><td colspan='2' class='sectiontext'>%__subhead__%</td></tr><tr><td colspan='2' height='8'><img src='/sites/templates/pix/spacer.gif' width='568' height='8' border='0' alt=''/></td></tr>";

// jump links start table
var jump_start_HTML = "<!-- *** Jump Links *** --><!--[table-of-content-section]--><table cellpadding='0' cellspacing='0' width='568' border='0'>";

// jump links end table
var jump_end_HTML = "</table><table border='0' cellpadding='0' cellspacing='0' width='583'><tr><td width='583'><img src='/sites/templates/pix/spacer.gif' width='583' height='15' border='0' alt=''/></td></tr></table><!--[/table-of-content-section]--><!-- *** / Jump Links *** -->";

// jump link rows
var linksPerRow = 4; 
// if you change this value, consider modifying the HTML templates below so 
// your table widths add up correctly ;-)

var jump_row_start = "<tr>";

var jump_row_link = "<td width='12' valign='top'><img src='/sites/templates/pix/bullet.gif' width='12' height='10' alt=''/></td><td width='130' class='jumplink' valign='top'><a class='jumplink' href='#%__jumpName__%'>%__name__%</a></td>";

var jump_row_blank = "<td width='12' valign='top'><img src='/sites/templates/pix/spacer.gif' width='12' height='10' alt=''/></td><td width='130' class='jumplink' valign='top'>&nbsp;</td>";

var jump_row_end = "</tr>";

