// JavaScript Document

$(document).ready(function()
{
	
	// set the default section and page
	defaultSection = 5;
	defaultPage = 23;
	
	idsLoad  = new Array();
	
	// see if there is any deep linking
	anchorTag = self.document.location.hash;
	anchorTagArr = anchorTag.split("/");
	if(anchorTagArr.length >= 3) {
		idsLoad = parseXmlName(anchorTagArr[1], anchorTagArr[2])
	}
	
	
	if(idsLoad.length != 2) {
		idsLoad[0] = defaultSection;
		idsLoad[1] = defaultPage;
	}
	
	// laod the template
	parseXml(idsLoad[0], idsLoad[1]);
	
});

function parseXml(sectionId, subsectionId)
{

	$("#right").css("visible","visible");

	xmlResponse =
	$.ajax({
	type: "GET",
	url: "_xml/content.xml",
	dataType: ($.browser.msie) ? "text" : "xml",
	cache: true,
	async: false,

	success: function(data){
		var xml;
		if (typeof data == "string") {
			xml = new ActiveXObject("Microsoft.XMLDOM");
			xml.async = false;
			xml.loadXML(data);
		} else {
			xml = data;
		}

		// start parsing for the requested section

			// load default page
			if(isNaN(sectionId)) {
				sectionId = defaultSection;
				subsectionId = defaultPage;
			}

			//alert(xmlResponse);

			// get the right data
			$(xml).find("section").each(function()
			{

			// section
			if($(this).attr("id") == sectionId) {
				
				sectionName = $(this).attr("name");

				//alert($(this).attr("id"));
				$(this).find("subsections").each(function()
				{

					$(this).find("subsection").each(function()
					{

						// subsection
						if($(this).attr("id") == subsectionId) {

							template = $(this).attr("template");
							templateName = $(this).attr("name");
							//alert(sectionName);
							
							$("#fullBodyContent").load("_templates/"+template, function() {
								
								// scroll to anchor if provided
								anchorTag = self.document.location.hash;
								if(anchorTag != "") {
									document.location.href = anchorTag;
								}
								
								// set deep linking structure
								document.location.href = "#/"+sectionName+"/"+templateName;
								
								// SCHEDULE PAGE SETUP
								if(sectionId == '3' && (subsectionId == '42' || subsectionId == '43'|| subsectionId == '44'|| subsectionId == '45')) { 								
									$(document).pngFix();
									tb_init('a.thickbox, area.thickbox, input.thickbox');																	
								}
								
							});

						}

					});

				});

			}

			});

		// end parsing
	}

	}, "xml").responseText;
	
}

function parseXmlName(sectionName, subsectionName)
{

	xmlResponse =
	$.ajax({
	type: "GET",
	url: "_xml/content.xml",
	dataType: ($.browser.msie) ? "text" : "xml",
	cache: true,
	async: false,

	success: function(data){
		var xml;
		if (typeof data == "string") {
			xml = new ActiveXObject("Microsoft.XMLDOM");
			xml.async = false;
			xml.loadXML(data);
		} else {
			xml = data;
		}


			// get the right data
			$(xml).find("section").each(function()
			{

			// section
			if($(this).attr("name") == sectionName) {
				
				sectionId = $(this).attr("id");

				//alert($(this).attr("id"));
				$(this).find("subsections").each(function()
				{

					$(this).find("subsection").each(function()
					{

						// subsection
						if($(this).attr("name") == subsectionName) {

							subsectionId = $(this).attr("id");
							//alert(sectionName);

						}

					});

				});

			}

			});

		// end parsing
	}

	}, "xml").responseText;
	
	ids = new Array(sectionId, subsectionId);	
	return ids;

}

function generateSiteMap()
{
	
	siteMapArr = new Array()

	xmlResponse =
	$.ajax({
	type: "GET",
	url: "_xml/content.xml",
	dataType: ($.browser.msie) ? "text" : "xml",
	cache: true,
	async: false,

	success: function(data){
		var xml;
		if (typeof data == "string") {
			xml = new ActiveXObject("Microsoft.XMLDOM");
			xml.async = false;
			xml.loadXML(data);
		} else {
			xml = data;
		}


			// get the right data
			$(xml).find("section").each(function()
			{

			// section
			//if($(this).attr("name") == sectionName) {
				
				sectionId = $(this).attr("id");

				//alert($(this).attr("id"));
				$(this).find("subsections").each(function()
				{

					$(this).find("subsection").each(function()
					{

						// subsection
						//if($(this).attr("name") == subsectionName) {

							subsectionId = $(this).attr("id");
							//alert(sectionName);

						//}

					});

				});

			//}

			});

		// end parsing
	}

	}, "xml").responseText;

	return siteMapArr;

}
