// courseOutline.js

// Global variables (I hate using them but it's easier in this case)
var courseOutline = null;	// Array object to store the course outline
var arrow = null;			// HTML image tag of the arrow indicator for the menu outline
var MAX_SECTION_SIZE = 10;	// Maximum size of a subsection


/************************************************************************
 *	CreateOutline	-	constructs the course outline array object		*
 ************************************************************************/
CreateOutline = function()
{
	courseOutline = new Array
	(
	 	{// 0. Welcome message
			linkName	: "Welcome",
			fileName	: "0_welcome_message.html",
			mimeType	: "text/html",
			subSection	: null,
			resources   : null
		},
		{// 1. Introduction
			linkName	: "Introduction", 
			fileName	: "1-Introduction.asx",
			mimeType	: "video/x-ms-wmv",
			runtime		: "1:23",
			subSection	: null,
			resources   : null
		},
		
		{// 2. Video walk through process
			linkName	: "Video walk through process",			
			fileName	: "2-VideoWalkThroughProcess.asx",
			mimeType	: "video/x-ms-wmv",
			runtime		: "16:20",
			subSection	: null,
			resources   : null
		},
		
		{// 3. Permit process approval targets
			linkName	: "Permit process approval targets",
			fileName	: "3-PermitProcessApprovalTargets.asx",
			mimeType	: "video/x-ms-wmv",
			runtime		: "4:14",
			subSection	: null,
			resources   : null
		},
		
		{// 4. Permit guidelines
			linkName	: "Permit guidelines",
			fileName	: "4-PermitGuidelines.asx",
			mimeType	: "video/x-ms-wmv",			
			runtime		: "6:18",
			subSection	: null,
			resources   : null
		},
	
		{// 5. Step-by-step instructions for completing process
			linkName	: "Step-by-step instructions for completing process",
			mimeType	: null,
			fileName	: null,
			subSection	: new Array		/* subsection elements - structured the same way */
			(		
				{// 5a. from application to acceptance for processing
					linkName	: "application to acceptance for processing",
					fileName	: "5a-FromApplicationToAcceptanceForProcessing.asx",
					mimeType	: "video/x-ms-wmv",
					runtime		: "21:44",
					subSection	: null,
					resources   : new Array
					(
					 	{
							href : "http://www.calgary.ca/docgallery/bu/dba/building/commercial_interior_partitioning.pdf",
							name : "Interior Partitioning Checklist",
							type : "PDF"
						},
						{
							href : "http://www.calgary.ca/docgallery/bu/dba/building/tenancy_change_requirements_and_questionnaire.pdf",
							name : "Tenancy Change/Business Use Confirmation Form",
							type : "PDF"
						},
						{
							href : "http://www.calgary.ca/portal/server.pt/gateway/PTARGS_0_0_766_229_0_43/http;/content.calgary.ca/CCA/City+Business/Planning+and+Building/Permits/Development+Permits/Development+Permit+Application+Requirements.htm",
							name : "Development Permit – City of Calgary webpage",
							type : "HTML"
						},
						{
							href : "http://www.calgary.ca/DocGallery/BU/dba/development/development_permit_process.pdf",
							name : "Development Permit Process",
							type : "PDF"
						},
						{
							href : "http://www.calgary.ca/DocGallery/BU/dba/building/commercial_pre_screening.pdf",
							name : "Building Permit Information Form",
							type : "PDF"
						},
						{
							href : "http://www.calgary.ca/DocGallery/BU/dba/fees/building_permit_fee_schedule.pdf",
							name : "Fee Schedule",
							type : "PDF"
						},
						{
							href : "http://www.calgary.ca/docgallery/bu/dba/building/commercial.pdf",
							name : "Commercial New Projects and Additions Checklist",
							type : "PDF"
						},
						{
							href : "http://www.calgary.ca/DocGallery/BU/dba/brochures/commercial_building.pdf",
							name : "Commercial New Building Construction, Additions or Renovations Brochure",
							type : "PDF"
						}
					)
					
				},
				{// 5b. from acceptance fro processing to start of construction
					linkName	: "acceptance for processing to start of construction",
					fileName	: "5b-FromAcceptanceForProcessingToSartOfConstruction.asx",
					mimeType	: "video/x-ms-wmv",
					runtime		: "8:11",
					subSection	: null,
					resources   : null
				},
				{// 5c. from start of construction to occupancy
					linkName	: "start of construction to occupancy",
					fileName	: "5c-FromSartOfConstructionToOccupancy.asx",
					mimeType	: "video/x-ms-wmv",
					runtime		: "4:20",
					subSection	: null,
					resources   : new Array
					(
					 	{
							href : "http://www.calgary.ca/portal/server.pt/gateway/PTARGS_0_0_780_227_0_43/http;/content.calgary.ca/CCA/City+Business/Running+a+Business/Licences+and+Permits/Business+Licences/Business+Licences.htm",
							name : "Business Licence Application information",
							type : "HTML"
						},
						{
							href : "http://blt.calgary.ca/",
							name : "Business Licence Tool",
							type : "HTML"
						},
						{
							href : "http://www.calgary.ca/DocGallery/BU/dba/licence/business_licence_application.pdf",
							name : "Business Licence Application",
							type : "PDF"
						}
					)
					
				}			
			),
			resources   : null
		},
		
		{// 6. Downloadable copies of standard forms
			linkName	: "Downloadable copies of standard forms",
			fileName	: "6_downloadable_copies_of_standard_forms.html",
			mimeType	: "text/html",
			subSection	: null,
			resources   : null
		},
		
		{// 7. FAQ and common mistakes
			linkName   	: "Frequently asked questions (FAQs) and common mistakes",
			fileName   	: "7_faq_and_common_mistakes.html",
			mimeType	: "text/html",
			subSection 	: null,
			resources   : null
		},
		
		{// 8. Summary
			linkName   	: "Summary",
			fileName   	: "8-Summary.asx",
			mimeType	: "video/x-ms-wmv",
			runtime		: "2:17",
			subSection 	: null,
			resources   : null
		},
		
		{// 9. Contacts
			linkName	: "Contacts",
			fileName	: "9_contacts.html",
			mimeType	: "text/html",
			subSection	: null,
			resources   : null
		}
	);// END OF 'courseOutline = new Array'
	
	// arrow HTML image tag
	arrow = '<img src="images/arrow.png" width="9" height="9" alt="" title="Current selection" />';
}


/************************************************************************
 *	BuildOutline	-	fills the innerHTML of the course outline menu	*
 *						div with HTML text from the generated sections	*
 *	@var linkClicked	-	the link that was clicked in order to		*
 *							display the arrow next to it				*
 *	@var divID			-	id of the div element to return the HTML	*
 ************************************************************************/
BuildOutline = function ( linkClicked, divID )
{	
	// create the links if they have not already been created
	if(generalLinks == null && linkClicked > 0) {
		BuildGeneralLinks();
	}	

	// create the course outline if it has not already been done.
	if(courseOutline == null) {
		CreateOutline();
	}
	
	// get course ouline menu div element to write to
	outlineDiv = document.getElementById(divID);
	
	// verify that the div element was found
	if( outlineDiv == null ) {
		alert("BuildOutline :: Error locating document element '" + divID + "'.");
		return;
	}
	
	// write to the div
	outlineDiv.innerHTML = '<h3>Table of Contents</h3><div>'+						   
						   BuildRootOutline(courseOutline, linkClicked, 'course-outline') +
						   '</div>';
}


/************************************************************************
 *	BuildRootOutline	-	initial call to recursive function		 	*
 *	@var Outline		-	the inital course outline array object		*
 *	@var linkClicked	-	number of clicked link for arrow indicator	*
 *	@var id				-	id of the DIV element that contains the menu*
 *	@return	HTML		- 	course outline menu's HTML					*
 ************************************************************************/
BuildRootOutline = function(Outline, linkClicked, id) { return BuildSection(Outline, linkClicked, 0, 'courseOutline', id); }



/************************************************************************
 *	BuildSection	-	recursive function for building a menu section 	*
 *	@var Section		-	the array object with the section to build	*
 *	@var linkClicked	-	number of clicked link for arrow indicator	*
 *	@var parentLinkNumber	-	the link number of the parent section	*
 *	@var parentArrayName	-	array name for referencing subsections	*
 *	@return	HTML		- 	course outline menu's HTML					*
 ************************************************************************/
BuildSection = function(Section, linkClicked, parentLinkNumber, parentArrayName, divID, parentLinkName)
{	
	// verify section array bounds - stop processing this section if over the limit
	if(Section.length > MAX_SECTION_SIZE) {
		alert("BuildOutline :: Subsection is too large. A maximum of "+MAX_SECTION_SIZE+" sub elements has been set.");
		return;
	}
	
	// compensate for the root secection being "courseOutline[]" whereas the others are ".subSection[]"
	var methodRef = (parentLinkNumber>0?'.subSection':'')
	var ContentLoader = "";
	var alphaAscii = 0x61;		// letter representing a lowercase 'a'
	
	// write ordered list HTML tag
	var HTML = '<ol>';
	
	// Loop through each section
	for(var segment=0; segment<Section.length; segment++)
	{	
		var childLinkNumber = parentLinkNumber*MAX_SECTION_SIZE+segment;
		var childCourseIndex = parentArrayName+methodRef+'['+segment+']';
		var elementNumber = (segment==0 && parentLinkNumber==0 ? '&nbsp;&nbsp;' : segment+'.')
		
		// course outline contents (use 1-9 for parent list, and a-z for sublists)		
		HTML += '<li><p>' + (parentLinkNumber > 0 ? '&#'+(alphaAscii+segment)+';.' : elementNumber) + '</p>';
		
		// if the file name is null, then the link is not 'clickable'
		if(Section[segment].fileName == null ) {			
			HTML += wordWrap(Section[segment].linkName);
		
		// else 'clickable' link
		} else {
			
			// adjust for content type (default is the same as video)
			switch(Section[segment].mimeType) {
				case 'text/html':
					ContentLoader = "DisplayHtml("+childCourseIndex+"); ";
					break;
				case 'video/x-ms-wmv':
				default:
					ContentLoader = "DisplayVideo("+childCourseIndex+"); ";
			}
			
			// build menu html
			HTML += '<a'+(childLinkNumber == linkClicked ? ' class="selectedLink"' : '')+' href="javascript:;" ' +
				   'title="'+(parentLinkName!=null ? parentLinkName+' from ' : '')+Section[segment].linkName+'" ' +
				   'onmouseover="self.status=\''+Section[segment].linkName+'\'; return;" ' +
				   'onmouseout="self.status=\'\'; return;" ' +
				   'onclick="DisplayResources('+childCourseIndex+'); ' +
						    'BuildOutline('+childLinkNumber+', \''+divID+'\'); '+
							(linkClicked > 0 ? 'BuildGeneralLinks();' : '')+
							ContentLoader+'">'+
				  wordWrap(Section[segment].linkName) + '</a>';
		}
		
		// add the runtime to the end of the link
		if(Section[segment].runtime != null) HTML += "&nbsp;("+Section[segment].runtime+")";
		
		// add sub sections if there are any
		if(Section[segment].subSection != null)
			HTML += BuildSection(Section[segment].subSection, linkClicked, childLinkNumber, childCourseIndex, divID, Section[segment].linkName);
		
		
		// end list item	
		HTML += '</li>';		
	}
	
	// write div segment footer data
	HTML += '</ol>';
	
	return HTML;
}


// end of file