
//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, 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.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) {
	if(URL == '/')
		return 'HOME';
	var point = this.element(url);
	var path_str = '';
	while (point && point != this) {
		if(path_str != '')
			path_str = ' &raquo; ' + path_str;
		path_str = PathElement(point.short_title, point.url) + path_str;
		RootSection = point.code;
		point = point.parent;
	}
	return '<a href="/">HOME</a> &raquo; ' + path_str;
}

function SiteStructureElement(url) {
	var element = this.index[url];
	return (element) ? element : this;
}

function PathElement(title, url) {
	return (url == URL) ? title : '<a href="' + url + '">' + title + '</a>';
}

// main menu classes ===========================================================

function BuildMainMenu()
{
	for (var i = 0; i < Structure.sections.length; i++)
	{
		if(Structure.sections[i].url == '/sitemap/')
			continue;
		if(URL.indexOf(Structure.sections[i].url)==0)
			document.write('	<a href="' + Structure.sections[i].url + '" class="current">' + Structure.sections[i].title + '</a>\n');
		else		
			document.write('	<a href="' + Structure.sections[i].url + '">' + Structure.sections[i].title + '</a>\n');
	}
}

function BuildLeftMenu()
{
	var leftRoot=Structure.sections[0];
	document.write('		<ul class="left-menu">\n');
	if(leftRoot.childs)
	{
		for (var i = 0; i < leftRoot.childs.length; i++)
		{
			var subItem = leftRoot.childs[i];
			if(URL.indexOf(subItem.url)==0)
				document.write('			<li class="current"><a href="' + subItem.url + '"><span>' + subItem.short_title + '</span></a>\n');
			else		
				document.write('			<li><a href="' + subItem.url + '"><span>' + subItem.short_title + '</span></a>\n');
				
			if(subItem.childs)
			{
				if(subItem.childs.length > 0)
					document.write('				<ul id="left-submenu" name="left-submenu">\n');
				for (var si = 0; si < subItem.childs.length; si++)
				{
					var subSubItem = subItem.childs[si];
		
					if(URL.indexOf(subSubItem.url)==0)
//						document.write('					<li><a href="' + subSubItem.url + '" class="current"><span>' + subSubItem.short_title + '</span></a>\n');
						document.write('					<li><a href="' + subSubItem.url + '" class="current"><img src="/img/b1.gif"><span>' + subSubItem.short_title + '</span></a>\n');
					else		
//						document.write('					<li><a href="' + subSubItem.url + '"><span>' + subSubItem.short_title + '</span></a>\n');
						document.write('					<li><a href="' + subSubItem.url + '"><img src="/img/b1.gif"><span>' + subSubItem.short_title + '</span></a>\n');

					if(subSubItem.sub_title || (subSubItem.childs && (subSubItem.childs.length > 0)))
						document.write('						<ul>\n');
					if(subSubItem.sub_title)
						document.write('							<li><a href="' + subSubItem.url + '"><img src="/img/b.gif">' + subSubItem.sub_title + '</a></li>\n');
					if(subSubItem.childs)
						for (var ssi = 0; ssi < subSubItem.childs.length; ssi++)
							document.write('							<li><a href="' + subSubItem.childs[ssi].url + '"><img src="/img/b.gif">' + subSubItem.childs[ssi].short_title + '</a></li>\n');
					if(subSubItem.sub_title || (subSubItem.childs && (subSubItem.childs.length > 0)))
						document.write('						</ul>\n');

					document.write('					</li>\n');
				}
				if(subItem.childs.length > 0)
					document.write('					<div class="spacer"><!-- --></div>\n				</ul>\n');
			}
			document.write('			</li>\n');
		}
	}
	document.write('		</ul>\n');
}

function BuildTitleAndSubMenuType1(url)
{
	var section = Structure.element(url);
	document.write(' 					<b>' + section.short_title + '</b>\n');
	document.write(' 					<ul>\n');
	document.write(' 						<li><a href="' + section.url + '">' + section.sub_title + '</a></li>\n');
	if(section.childs)
		for (var i = 0; i < section.childs.length; i++)
			document.write(' 						<li><a href="' + section.childs[i].url + '">' + section.childs[i].short_title + '</a></li>\n');
	document.write(' 					</ul>');
}

function BuildTitleAndSubMenuType2()
{
	var section = Structure.element(URL);
	if(section.sub_title == '')
		section = section.parent
	document.write('	<h1>' + section.short_title + '</h1>\n	<div class="inner-menu"><div>');
	if(URL == section.url)
		document.write('<b>' + section.sub_title + '</b>');
	else
		document.write('<a href="' + section.url + '">' + section.sub_title + '</a>');
	if(section.childs)
	{
		for (var i = 0; i < section.childs.length; i++)
			if(URL == section.childs[i].url)
				document.write(' | <b>' + section.childs[i].short_title + '</b>');
			else
				document.write(' | <a href="' + section.childs[i].url + '">' + section.childs[i].short_title + '</a>');
	}
	document.write('</div></div>\n');
}

function BuildPageTitle()
{
	var section = Structure.element(URL);
	document.write('	<h1>' + section.short_title + '</h1>\n');
}

function BuildSiteMap()
{
	document.write('<ul>\n');
	for (var ti = 0; ti < Structure.sections.length; ti++)
	{
		if(Structure.sections[ti].url == '/sitemap/')
			continue;
		document.write('	<li><a href="' + Structure.sections[ti].url + '"><b>' + Structure.sections[ti].title + '</b></a>\n');
		var leftRoot=Structure.sections[ti];
		if(leftRoot.childs)
		{
			document.write('	<ul>\n');
			for (var i = 0; i < leftRoot.childs.length; i++)
			{
				var subItem = leftRoot.childs[i];
				document.write('		<li><a href="' + subItem.url + '">' + subItem.short_title + '</a>\n');
					
				if(subItem.childs)
				{
					document.write('			<ul>\n');
					for (var si = 0; si < subItem.childs.length; si++)
					{
						var subSubItem = subItem.childs[si];
						document.write('				<li><a href="' + subSubItem.url + '">' + subSubItem.short_title + '</a>\n');
	
						if(subSubItem.sub_title || subSubItem.childs)
							document.write('					<ul>\n');
						if(subSubItem.sub_title)
							document.write('						<li><a href="' + subSubItem.url + '">' + subSubItem.sub_title + '</a></li>\n');
						if(subSubItem.childs)
							for (var ssi = 0; ssi < subSubItem.childs.length; ssi++)
								document.write('						<li><a href="' + subSubItem.childs[ssi].url + '">' + subSubItem.childs[ssi].short_title + '</a></li>\n');
						if(subSubItem.sub_title || (subSubItem.childs && (subSubItem.childs.length > 0)))
							document.write('					</ul>\n');
	
						document.write('				</li>\n');
					}
					document.write('			</ul>\n');
				}
				document.write('		</li>\n');
			}
			document.write('	</ul>\n');
		}
		document.write('	</li>\n');
	}
	document.write('</ul>\n');
}

