
//URL = new String(Request.ServerVariables('URL'));

URL = new String(location.href);
URL = URL.substring(URL.indexOf('/',7), URL.lastIndexOf('/') + 1);

var RootSection = '';
var root = null;

// template functions ==========================================================

function title(full_mode) {
	var struct = Structure.element(URL);
	var title = struct.title;
	while (struct) {
		struct = struct.parent;
		if (struct && (struct.parent || full_mode))
			title = struct.short_title + ((title) ? ' | ' + title : '');
	}
	return title;
}

// navigation classes ==========================================================

function SectionItem(code, title, title2, childs) {
	this.code = code;
	this.title = title.replace(/[\[\]]/g, '');
	this.short_title = title.replace(/\[(.*)\]/, '');
	this.sub_title = '';
	if (this.short_title != this.title)
		this.sub_title = title.replace(/.*\[(\s\/\s)?(.*)\]/, '$2');
	this.childs = childs;
	this.title2 = title2;
	this.onclick = null;
	this.init = SectionItemInit;
}

function SectionItemInit(parent, root) {
	this.parent = parent;
	this.url = this.code.match(/^http:/) ? this.code : (parent.url + this.code + '/');
	root.index[this.url] = this;
	if (this.childs) {
		for (var i = 0; i < this.childs.length; i++) {
			this.childs[i].init(this, root);
		}
	}
}

function SiteStructure(args) {
	this.title = '';
	this.short_title = '';
	this.parent = null;
	this.url = '/';
	this.sections = new Array();
	this.index = new Array();
	for (var i = 0; i < arguments.length; i++) {
		this.sections[i] = arguments[i];
		this.sections[i].init(this, this);
	}
	this.path = SiteStructurePath;
	this.element = SiteStructureElement;
}

function SiteStructurePath(url) {
	var point = this.element(url).parent;
	var path_str = '';
	while (point && point != this) {
		path_str = ' / ' + PathElement(point.short_title, point.url) + path_str;
		point = point.parent;
	}
	return '<a href="/">Home</a>' + path_str;
}

function SiteStructureElement(url) {
	var element = this.index[url];
	return (element) ? element : this;
}

function PathElement(title, url) {
	return (url == URL) ? '<nobr>' + title + '</nobr>' : '<a href="' + url + '"><nobr>' + title + '</nobr></a>';
}

// main menu classes ===========================================================

function BuildMainMenu()
{
	for (var i = 1; i < MainMenuItemsNumber + 1; i++)
	{
		if(URL.indexOf(Structure.sections[i].url)==0)
		    // If it is Order menu add https link
		    if (URL == '/book/order/'){
		        document.write('						<li class="act"><span><a href="http://www.persistencepays.ca' + Structure.sections[i].url + '">' + Structure.sections[i].title2 + '</a></span></li>\n');
		        }
		    else {
		        document.write('						<li class="act"><span><a href="http://www.persistencepays.ca' + Structure.sections[i].url + '">' + Structure.sections[i].title2 + '</a></span></li>\n');
		        }
			
		else	
		    // If it is Order menu add https link
		    if (URL == '/book/order/'){
		        document.write('						<li><span><a href="http://www.persistencepays.ca' + Structure.sections[i].url + '">' + Structure.sections[i].title2 + '</a></span></li>\n');
		    }
		    else {
    		    document.write('						<li><span><a href="http://www.persistencepays.ca' + Structure.sections[i].url + '">' + Structure.sections[i].title2 + '</a></span></li>\n');
		    }
			
	}
}

function BuildBookMenu()
{
	var book = Structure.element('/book/');
	if(URL.indexOf(book.url)==0){
	        // If it is Order menu add https link
		    if (URL == '/book/order/'){
		        document.write('			<div class="book"><div class="act"><a href="http://www.persistencepays.ca' + book.url + '">' + book.title2 + '</a></div></div>\n');
		    }
		    else {
		        document.write('			<div class="book"><div class="act"><a href="' + book.url + '">' + book.title2 + '</a></div></div>\n');
		    }
	}
	else {
	    // If it is Order menu add https link
		if (URL == '/book/order/'){
		    document.write('			<div class="book"><div><a href="http://www.persistencepays.ca' + book.url + '">' + book.title2 + '</a></div></div>\n');
		}
		else
		{
		    document.write('			<div class="book"><div><a href="' + book.url + '">' + book.title2 + '</a></div></div>\n');
		}
	}
	
	if(book.childs)
	{
		document.write('			<ul>\n');
		
		if (URL == '/book/order/'){
		
		        for(var i = 0; i < book.childs.length; i++)
		        {
			        if(URL.indexOf(book.childs[i].url)==0){
			            // If it is Order menu add https link
			            if (book.childs[i].url == '/book/order/'){
			                document.write('				<li' + (i == (book.childs.length - 1) ? ' class="act last"' : ' class="act"') + '><span><a href="https://www.persistencepays.ca:4433/book/order/">' + book.childs[i].title + '</a></span></li>\n');
			                }
			            else
			                {
			                document.write('				<li' + (i == (book.childs.length - 1) ? ' class="act last"' : ' class="act"') + '><span><a href="http://www.persistencepays.ca' + book.childs[i].url + '">' + book.childs[i].title + '</a></span></li>\n');
			                }
				        }
			        else
			            // If it is Order menu add https link
			            if (book.childs[i].url == '/book/order/'){
				            document.write('				<li' + (i == (book.childs.length - 1) ? ' class="last"' : '') + '><span><a href="https://www.persistencepays.ca:4433/book/order/">' + book.childs[i].title + '</a></span></li>\n');
				            }
				            else
				            {
				            document.write('				<li' + (i == (book.childs.length - 1) ? ' class="last"' : '') + '><span><a href="http://www.persistencepays.ca' + book.childs[i].url + '">' + book.childs[i].title + '</a></span></li>\n');
				            };
		        }
		}
		else {
		
		        for(var i = 0; i < book.childs.length; i++)
		        {
			        if(URL.indexOf(book.childs[i].url)==0){
			            // If it is Order menu add https link
			            if (book.childs[i].url == '/book/order/'){
			                document.write('				<li' + (i == (book.childs.length - 1) ? ' class="act last"' : ' class="act"') + '><span><a href="https://www.persistencepays.ca:4433/book/order/">' + book.childs[i].title + '</a></span></li>\n');
			                }
			            else
			                {
			                document.write('				<li' + (i == (book.childs.length - 1) ? ' class="act last"' : ' class="act"') + '><span><a href="http://www.persistencepays.ca' + book.childs[i].url + '">' + book.childs[i].title + '</a></span></li>\n');
			                }
				        }
			        else
			            // If it is Order menu add https link
			            if (book.childs[i].url == '/book/order/'){
				            document.write('				<li' + (i == (book.childs.length - 1) ? ' class="last"' : '') + '><span><a href="https://www.persistencepays.ca:4433/book/order/">' + book.childs[i].title + '</a></span></li>\n');
				            }
				            else
				            {
				            document.write('				<li' + (i == (book.childs.length - 1) ? ' class="last"' : '') + '><span><a href="http://www.persistencepays.ca' + book.childs[i].url + '">' + book.childs[i].title + '</a></span></li>\n');
				            };
		        }
		        
		}
		
		
		
		
		document.write('			</ul>\n');
	}
}

function BuildSiteMap()
{
	document.write('				<ul class="sitemap">\n');
	for (var i = 0; i < Structure.sections.length - 1; i++)
	{
		document.write('					<li><a href="' + Structure.sections[i].url + '">' + Structure.sections[i].title + '</a></li>\n');
		if(Structure.sections[i].childs)
		{
			subsec = Structure.sections[i];
			document.write('					<ul>\n');
			for (var j = 0; j < subsec.childs.length; j++)
				document.write('					<li><a href="' + subsec.childs[j].url + '">' + subsec.childs[j].title + '</a></li>\n');
			document.write('					</ul>\n');
		}
	}
	document.write('				</ul>\n');
}
