// helpers.js - common helper fucntions
// Copyright (c) 2003-2010 by Elijah Zarezky,
// All rights reserved

// greeting string
function getGreetingString()
{
	dateNow = new Date();
	nHours = dateNow.getHours();
	if (5 <= nHours && nHours <= 11)
	{
		return "Доброе утро!";
	}
	else if (12 <= nHours && nHours <= 16)
	{
		return "Добрый день!";
	}
	else if (17 <= nHours && nHours <= 23)
	{
		return "Добрый вечер!";
	}
	else {
		return "Добрая ночь!";
	}
}

// mirror information
function writeMirrorInfo()
{
	if (location.protocol == "http:") {
		// we're online
		if (location.hostname != "home.peterstar.ru") {
			// assume zarezky.spb.ru
			document.writeln("&#8222;Зеркало&#8220; этого сайта вы можете найти по адресу");
			document.write("<a href=\"http://home.peterstar.ru/zarezky/\" target=\"_blank\">");
			document.writeln("http://home.peterstar.ru/zarezky/</a>.");
			document.writeln("<font color=\"#FF0000\">Начиная с 11.06.2005, данное зеркало");
			document.writeln("не поддерживается в связи с недостатком свободного места на");
			document.write("сервере. Пожалуйста, обновите свои закладки, если таковые имеются.");
			document.writeln("</font>");
		}
		else {
			document.writeln("Канонический адрес этого сайта &mdash;");
			document.write("<a href=\"http://zarezky.spb.ru/\" target=\"_blank\">");
			document.writeln("http://zarezky.spb.ru</a>.");
		}
	}
}

// main menu
function writeMainMenu(strPagesPath)
{
	with (document) {
		writeln("<a href=\"" + strPagesPath + "/index.html\">О&nbsp;сайте</a><br/>");
		writeln("<a href=\"" + strPagesPath + "/site_news.html\">Новости</a><br/>");
		writeln("<a href=\"" + strPagesPath + "/lectures.html\">Лекции</a><br/>");
		writeln("<a href=\"" + strPagesPath + "/articles.html\">Статьи</a><br/>");
		writeln("<a href=\"" + strPagesPath + "/projects.html\">Проекты</a><br/>");
		writeln("<a href=\"" + strPagesPath + "/files.html\">Файлы</a><br/>");
		writeln("<a href=\"" + strPagesPath + "/favorites.html\">Избранное</a><br/>");
		writeln("<a href=\"" + strPagesPath + "/treasure.html\">Копилка</a><br/>");
		writeln("<a href=\"" + strPagesPath + "/sources.html\">Исходники</a><br/>");
		writeln("<a href=\"" + strPagesPath + "/music.html\">Музыка</a><br/>");
		writeln("<a href=\"" + strPagesPath + "/notepad.html\">Цитатник</a><br/>");
		writeln("<a href=\"" + strPagesPath + "/search.html\">Поиск</a>");
	}
}

// main menu (special cases)
function writeMainMenuEx(strPagesPath, strTarget, strNoLink)
{
	arrPages = new Array(
		"index.html",
		"site_news.html",
		"lectures.html",
		"articles.html",
		"projects.html",
		"files.html",
		"favorites.html",
		"treasure.html",
		"sources.html",
		"music.html",
		"notepad.html",
		"search.html"
	);

	arrNames = new Array(
		"О&nbsp;сайте",
		"Новости",
		"Лекции",
		"Статьи",
		"Проекты",
		"Файлы",
		"Избранное",
		"Копилка",
		"Исходники",
		"Музыка",
		"Цитатник",
		"Поиск"
	);

	for (var i = 0; i < arrPages.length; ++i) {
		if (arrPages[i] != strNoLink) {
			document.write("<a href=\"" + strPagesPath + "/" + arrPages[i] + "\"");
			if (strTarget != "") {
				document.write(" target=\"" + strTarget + "\"");
			}
			document.writeln(">" + arrNames[i] + "</a><br/>");
		}
		else {
			document.writeln(arrNames[i] + "<br/>");
		}
	}

	delete arrNames;
	delete arrPages;
}

// Rating@Mail.ru counter
function writeCounterRow(strImagesPath)
{
	document.write("<a href=\"https://mvp.support.microsoft.com/profile=621B230A-24C9-4349-B1C1-49630B7E7876\">");
	document.write("<img src=\"" + strImagesPath + "/MVPLogo-88x138.gif\" ");
	document.write("width=\"88\" height=\"138\" align=\"middle\" alt=\"MVP\" ");
	document.writeln("border=\"0\" vspace=\"8\"></a>");

	document.write("<a href=\"http://www.rsdn.ru/Users/Profile.aspx?uid=10117\">");
	document.write("<img src=\"" + strImagesPath + "/rsdn_member.jpg\" ");
	document.write("width=\"88\" height=\"31\" align=\"middle\" alt=\"RSDN\" ");
	document.writeln("border=\"0\"></a>");
}

// copyright line
function writeCopyrightNotice()
{
	with (document)
	{
		writeln("<p class=\"copyrightNotice\">");
		writeln("Авторские&nbsp;права&nbsp;&copy;&nbsp;2001&mdash;2010&nbsp;И.Зарецкий");
		writeln("</p>");
	}
}

// page footer
function writePageFooter(strImagesPath, fHTML, fCSS)
{
	with (document)
	{
		writeln("<table width=\"100%\" cellspacing=\"0\" cellpadding=\"0\" class=\"pageFooter\"><tr>");

		if (fHTML)
		{
			write("<td class=\"validatorW3C\">");
			write("<a href=\"http://validator.w3.org/check?uri=" + location + "\">");
			write("<img src=\"" + strImagesPath + "/valid-html401.png\" alt=\"\" ");
			writeln("width=\"88\" height=\"31\" border=\"0\"></a></td>");
		}

		if (fCSS)
		{
			write("<td class=\"validatorW3C\">");
			write("<a href=\"http://jigsaw.w3.org/css-validator/validator?uri=" + location + "\">");
			write("<img src=\"" + strImagesPath + "/vcss.gif\" alt=\"\" ");
			writeln("width=\"88\" height=\"31\" border=\"0\"></a></td>");
		}

		write("<td class=\"validatorW3C\">");
		write("<a href=\"http://www.anybrowser.org/campaign/\">");
		write("<img src=\"" + strImagesPath + "/w3c_ab.png\" alt=\"\" ");
		writeln("width=\"88\" height=\"31\" border=\"0\"></a></td>");

		write("<td class=\"validatorW3C\">");
		write("<a href=\"http://www.blumentals.net/webuilder/\" target=\"_blank\">");
		write("<img src=\"" + strImagesPath + "/webuilder.gif\" alt=\"WeBuilder\" ");
		writeln("width=\"88\" height=\"31\" border=\"0\"></a></td>");

		write("<td class=\"validatorW3C\">");
		write("<a href=\"http://softwaremaniacs.org/soft/highlight/\" target=\"_blank\">");
		write("<img src=\"" + strImagesPath + "/highlight_js.png\" alt=\"highlight.js\" ");
		writeln("width=\"88\" height=\"31\" border=\"0\"></a></td>");

		if (location.protocol == "http:")
		{
			// assume we're online - show actual counter from Mail.ru
			write("<td class=\"validatorW3C\">");
			write("<a target=\"_blank\" href=\"http://top.mail.ru/jump?from=415352\">");
			write("<img src=\"http://top.list.ru/counter?id=415352;t=216;js=10;");
			write("r=" + escape(document.referrer) + ";rand=" + Math.random() + "\" ");
			write("width=\"88\" height=\"31\" alt=\"Рейтинг@Mail.ru\" ");
			writeln("border=\"0\"></a></td>");

			write("<td class=\"validatorW3C\">");
			write("<a target=\"_blank\" href=\"http://stat.aport.ru/button.pl?f=hit&id=1663731\">");
			write("<img src=\"http://stat.aport.ru/button.pl?f=show&tp=3&id=1663731\" ");
			//write("<a target=\"_blank\" href=\"http://stat.aport.ru/hit.pl?id=1663731\">");
			//write("<img src=\"http://stat.aport.ru/show.pl?tp=41&id=1663731\" ");
			write("width=\"88\" height=\"31\" alt=\"Находится в каталоге Апорт\" ");
			writeln("border=\"0\"></a></td>");
		}
		else
		{
			// probably we're offline - display placeholder image
			write("<td class=\"validatorW3C\">");
			write("<img src=\"" + strImagesPath + "/top_mail_ru.gif\" ");
			write("width=\"88\" height=\"31\" align=\"middle\" alt=\"Рейтинг@Mail.ru\" ");
			writeln("border=\"0\"></td>");

			write("<td class=\"validatorW3C\">");
			write("<img src=\"" + strImagesPath + "/aport_catalog.png\" width=\"88\" ");
			write("height=\"31\" align=\"middle\" alt=\"Находится в каталоге Апорт\" ");
			writeln("border=\"0\"></td>");
		}

		write("<td class=\"copyrightNotice\">");
		write("Авторские&nbsp;права&nbsp;&copy;&nbsp;2001&mdash;2010<br/>");
		writeln("<a href=\"http://www.rsdn.ru/Users/Profile.aspx?uid=10117\"");
		writeln("target=\"_blank\">И.Зарецкий</a></td>");
		writeln("</tr></table>");
	}
}

function writePageFooterEx(strImagesPath, fXHTML, fCSS)
{
	with (document)
	{
		writeln("<table width=\"100%\" cellspacing=\"0\" cellpadding=\"0\" class=\"pageFooter\"><tr>");

		if (fXHTML)
		{
			write("<td class=\"validatorW3C\">");
			write("<a href=\"http://validator.w3.org/check?uri=" + location + "\">");
			write("<img src=\"" + strImagesPath + "/valid-xhtml10.png\" alt=\"\" ");
			writeln("width=\"88\" height=\"31\" border=\"0\"></a></td>");
		}

		if (fCSS)
		{
			write("<td class=\"validatorW3C\">");
			write("<a href=\"http://jigsaw.w3.org/css-validator/validator?uri=" + location + "\">");
			write("<img src=\"" + strImagesPath + "/vcss.gif\" alt=\"\" ");
			writeln("width=\"88\" height=\"31\" border=\"0\"></a></td>");
		}

		write("<td class=\"validatorW3C\">");
		write("<a href=\"http://www.anybrowser.org/campaign/\">");
		write("<img src=\"" + strImagesPath + "/w3c_ab.png\" alt=\"\" ");
		writeln("width=\"88\" height=\"31\" border=\"0\"></a></td>");

		write("<td class=\"validatorW3C\">");
		write("<a href=\"http://www.blumentals.net/webuilder/\" target=\"_blank\">");
		write("<img src=\"" + strImagesPath + "/webuilder.gif\" alt=\"WeBuilder\" ");
		writeln("width=\"88\" height=\"31\" border=\"0\"></a></td>");

		write("<td class=\"validatorW3C\">");
		write("<a href=\"http://softwaremaniacs.org/soft/highlight/\" target=\"_blank\">");
		write("<img src=\"" + strImagesPath + "/highlight_js.png\" alt=\"highlight.js\" ");
		writeln("width=\"88\" height=\"31\" border=\"0\"></a></td>");

		if (location.protocol == "http:")
		{
			// assume we're online - show actual counter from Mail.ru
			write("<td class=\"validatorW3C\">");
			write("<a target=\"_blank\" href=\"http://top.mail.ru/jump?from=415352\">");
			write("<img src=\"http://top.list.ru/counter?id=415352;t=216;js=10;");
			write("r=" + escape(document.referrer) + ";rand=" + Math.random() + "\" ");
			write("width=\"88\" height=\"31\" alt=\"Рейтинг@Mail.ru\" ");
			writeln("border=\"0\"></a></td>");

			write("<td class=\"validatorW3C\">");
			write("<a target=\"_blank\" href=\"http://stat.aport.ru/button.pl?f=hit&id=1663731\">");
			write("<img src=\"http://stat.aport.ru/button.pl?f=show&tp=3&id=1663731\" ");
			//write("<a target=\"_blank\" href=\"http://stat.aport.ru/hit.pl?id=1663731\">");
			//write("<img src=\"http://stat.aport.ru/show.pl?tp=41&id=1663731\" ");
			write("width=\"88\" height=\"31\" alt=\"Находится в каталоге Апорт\" ");
			writeln("border=\"0\"></a></td>");
		}
		else
		{
			// probably we're offline - display placeholder image
			write("<td class=\"validatorW3C\">");
			write("<img src=\"" + strImagesPath + "/top_mail_ru.gif\" ");
			write("width=\"88\" height=\"31\" align=\"middle\" alt=\"Рейтинг@Mail.ru\" ");
			writeln("border=\"0\"></td>");

			write("<td class=\"validatorW3C\">");
			write("<img src=\"" + strImagesPath + "/aport_catalog.png\" width=\"88\" ");
			write("height=\"31\" align=\"middle\" alt=\"Находится в каталоге Апорт\" ");
			writeln("border=\"0\"></td>");
		}

		write("<td class=\"copyrightNotice\">");
		write("Авторские&nbsp;права&nbsp;&copy;&nbsp;2001&mdash;2010<br/>");
		writeln("<a href=\"http://www.rsdn.ru/Users/Profile.aspx?uid=10117\"");
		writeln("target=\"_blank\">И.Зарецкий</a></td>");

		writeln("</tr></table>");
	}
}

function writePageFooterExFit(strImagesPath, fXHTML, fCSS)
{
	with (document)
	{
		writeln("<table width=\"100%\" cellspacing=\"0\" cellpadding=\"0\" class=\"pageFooterFit\"><tr>");

		if (fXHTML)
		{
			write("<td class=\"validatorW3C\">");
			write("<a href=\"http://validator.w3.org/check?uri=" + location + "\">");
			write("<img src=\"" + strImagesPath + "/valid-xhtml10.png\" alt=\"\" ");
			writeln("width=\"88\" height=\"31\" border=\"0\"></a></td>");
		}

		if (fCSS)
		{
			write("<td class=\"validatorW3C\">");
			write("<a href=\"http://jigsaw.w3.org/css-validator/validator?uri=" + location + "\">");
			write("<img src=\"" + strImagesPath + "/vcss.gif\" alt=\"\" ");
			writeln("width=\"88\" height=\"31\" border=\"0\"></a></td>");
		}

		write("<td class=\"validatorW3C\">");
		write("<a href=\"http://www.anybrowser.org/campaign/\">");
		write("<img src=\"" + strImagesPath + "/w3c_ab.png\" alt=\"\" ");
		writeln("width=\"88\" height=\"31\" border=\"0\"></a></td>");

		write("<td class=\"validatorW3C\">");
		write("<a href=\"http://www.blumentals.net/webuilder/\" target=\"_blank\">");
		write("<img src=\"" + strImagesPath + "/webuilder.gif\" alt=\"WeBuilder\" ");
		writeln("width=\"88\" height=\"31\" border=\"0\"></a></td>");

		write("<td class=\"validatorW3C\">");
		write("<a href=\"http://softwaremaniacs.org/soft/highlight/\" target=\"_blank\">");
		write("<img src=\"" + strImagesPath + "/highlight_js.png\" alt=\"highlight.js\" ");
		writeln("width=\"88\" height=\"31\" border=\"0\"></a></td>");

		if (location.protocol == "http:")
		{
			// assume we're online - show actual counter from Mail.ru
			write("<td class=\"validatorW3C\">");
			write("<a target=\"_blank\" href=\"http://top.mail.ru/jump?from=415352\">");
			write("<img src=\"http://top.list.ru/counter?id=415352;t=216;js=10;");
			write("r=" + escape(document.referrer) + ";rand=" + Math.random() + "\" ");
			write("width=\"88\" height=\"31\" alt=\"Рейтинг@Mail.ru\" ");
			writeln("border=\"0\"></a></td>");

			write("<td class=\"validatorW3C\">");
			write("<a target=\"_blank\" href=\"http://stat.aport.ru/button.pl?f=hit&id=1663731\">");
			write("<img src=\"http://stat.aport.ru/button.pl?f=show&tp=3&id=1663731\" ");
			//write("<a target=\"_blank\" href=\"http://stat.aport.ru/hit.pl?id=1663731\">");
			//write("<img src=\"http://stat.aport.ru/show.pl?tp=41&id=1663731\" ");
			write("width=\"88\" height=\"31\" alt=\"Находится в каталоге Апорт\" ");
			writeln("border=\"0\"></a></td>");
		}
		else
		{
			// probably we're offline - display placeholder image
			write("<td class=\"validatorW3C\">");
			write("<img src=\"" + strImagesPath + "/top_mail_ru.gif\" ");
			write("width=\"88\" height=\"31\" align=\"middle\" alt=\"Рейтинг@Mail.ru\" ");
			writeln("border=\"0\"></td>");

			write("<td class=\"validatorW3C\">");
			write("<img src=\"" + strImagesPath + "/aport_catalog.png\" width=\"88\" ");
			write("height=\"31\" align=\"middle\" alt=\"Находится в каталоге Апорт\" ");
			writeln("border=\"0\"></td>");
		}

		write("<td class=\"copyrightNoticeFit\">");
		write("Авторские&nbsp;права&nbsp;&copy;&nbsp;2001&mdash;2010<br/>");
		writeln("<a href=\"http://www.rsdn.ru/Users/Profile.aspx?uid=10117\"");
		writeln("target=\"_blank\">И.Зарецкий</a></td>");

		writeln("</tr></table>");
	}
}

// browser identification
function writeBrowserIdentity()
{
	with (document) {
		// begin of table
		writeln("<center><table border=\"1\" cellspacing=\"0\" cellpadding=\"4\" class=\"topicInner\">");
		writeln("<col width=\"1%\">");
		writeln("<col width=\"99%\">");
		// row #1
		writeln("<tr><td align=\"right\" bgcolor=\"#F0F0F0\"><strong>appName</strong></td>");
		writeln("<td bgcolor=\"#FAFAF5\">" + navigator.appName + "</td></tr>");
		// row #2
		writeln("<tr><td align=\"right\" bgcolor=\"#F0F0F0\"><strong>appCodeName</strong></td>");
		writeln("<td bgcolor=\"#FAFAF5\">" + navigator.appCodeName + "</td></tr>");
		// row #3
		writeln("<tr><td align=\"right\" bgcolor=\"#F0F0F0\"><strong>appVersion</strong></td>");
		writeln("<td bgcolor=\"#FAFAF5\">" + navigator.appVersion + "</td></tr>");
		// row #4
		writeln("<tr><td align=\"right\" bgcolor=\"#F0F0F0\"><strong>userAgent</strong></td>");
		writeln("<td bgcolor=\"#FAFAF5\">" + navigator.userAgent + "</td></tr>");
		// end of table
		writeln("</table></center>");
	}
}

// topic footer - stub
function writeTopicFooterStub(arrMSDN, strHREF, strLink)
{
	with (document) {
		// divider
		writeln("<hr size=\"1\" width=\"100%\" noshade=\"noshade\"/>");
		// table start
		writeln("<table border=\"0\" cellpadding=\"5\" class=\"topicFooter\">");
		// columns attributes
		writeln("<col align=\"left\" width=\"99%\">");
		writeln("<col align=\"right\" width=\"1%\">");
		// first row - "reference(s)" to MSDN
		write("<tr bgcolor=\"#F0F0F0\"><td style=\"border: 1px solid #C0C0C0;\">");
		writeln("<strong>Об этом в MSDN<strong></td>");
		write("<td style=\"white-space: nowrap; border: 1px solid #C0C0C0;\" align=\"right\">");
		for (var i = 0; i < arrMSDN.length; ++i) {
			write(arrMSDN[i] + "<br/>");
		}
		writeln("</td></tr>");
		// second row - sample project
		write("<tr bgcolor=\"#F0F9FF\"><td style=\"border: 1px solid #C0C9CF;\">");
		writeln("<strong>Пример к теме</strong></td>");
		write("<td style=\"white-space: nowrap; border: 1px solid #C0C9CF;\" align=\"right\">");
		writeln("будет</td></tr>");
		// table end
		writeln("</table>");
	}
}

// topic footer
function writeTopicFooter(arrMSDN, strPath, strFile, ckbSize)
{
	with (document) {
		// divider
		writeln("<hr size=\"1\" width=\"100%\" noshade=\"noshade\"/>");
		// table start
		writeln("<table border=\"0\" cellpadding=\"5\" class=\"topicFooter\">");
		// columns attributes
		writeln("<col align=\"left\" width=\"99%\">");
		writeln("<col align=\"right\" width=\"1%\">");
		// first row - "reference(s)" to MSDN
		write("<tr bgcolor=\"#F0F0F0\"><td style=\"border: 1px solid #C0C0C0;\">");
		writeln("<strong>Об этом в MSDN<strong></td>");
		write("<td style=\"white-space: nowrap; border: 1px solid #C0C0C0;\" align=\"right\">");
		for (var i = 0; i < arrMSDN.length; ++i) {
			write(arrMSDN[i] + "<br/>");
		}
		writeln("</td></tr>");
		// second row - sample project
		write("<tr bgcolor=\"#F0F9FF\"><td style=\"border: 1px solid #C0C9CF;\">");
		writeln("<strong>Пример к теме</strong></td>");
		write("<td style=\"white-space: nowrap; border: 1px solid #C0C9CF;\" align=\"right\">");
		write("<img src=\"../../images/common/download.gif\" width=\"16\" height=\"16\"");
		write(" border=\"0\" style=\"vertical-align: text-bottom;\">&nbsp;");
		write("<a href=\"" + strPath + strFile + "\">" + strFile + "</a>");
		writeln("&nbsp;&nbsp;(" + String(ckbSize) + "&nbsp;Кб)</td></tr>");
		// table end
		writeln("</table>");
	}
}

// article footer
function writeArticleFooter(arrMSDN, strPath, strFile, ckbSize, arrPublished)
{
	with (document) {
		// divider
		writeln("<hr size=\"1\" width=\"100%\" noshade=\"noshade\"/>");
		// table start
		writeln("<table border=\"0\" cellpadding=\"5\" class=\"topicFooter\">");
		// columns attributes
		writeln("<col align=\"left\" width=\"99%\">");
		writeln("<col align=\"right\" width=\"1%\">");
		// first row - "reference(s)" to MSDN
		if (arrMSDN.length > 0) {
			write("<tr bgcolor=\"#F0F0F0\"><td style=\"border: 1px solid #C0C0C0;\">");
			writeln("<strong>Об этом в MSDN<strong></td>");
			write("<td style=\"white-space: nowrap; border: 1px solid #C0C0C0;\" align=\"right\">");
			for (var i = 0; i < arrMSDN.length; ++i) {
				write(arrMSDN[i] + "<br/>");
			}
			writeln("</td></tr>");
		}
		// second row - demo project
		if (strFile != null) {
			write("<tr bgcolor=\"#F0F9FF\"><td style=\"border: 1px solid #C0C9CF;\">");
			writeln("<strong>Демонстрационный проект</strong></td>");
			write("<td style=\"white-space: nowrap; border: 1px solid #C0C9CF;\" align=\"right\">");
			write("<img src=\"../../images/common/download.gif\" width=\"16\" height=\"16\"");
			write(" border=\"0\" style=\"vertical-align: text-bottom;\">&nbsp;");
			write("<a href=\"" + strPath + strFile + "\">" + strFile + "</a>");
			writeln("&nbsp;&nbsp;(" + String(ckbSize) + "&nbsp;Кб)</td></tr>");
		}
		// third row - published
		write("<tr bgcolor=\"#FAFAF5\"><td style=\"border: 1px solid #CACAC5;\">");
		writeln("<strong>Опубликовано<strong></td>");
		write("<td style=\"white-space: nowrap; border: 1px solid #CACAC5;\" align=\"right\">");
		for (var j = 0; j < arrPublished.length; ++j) {
			write(arrPublished[j] + "<br/>");
		}
		writeln("</td></tr>");
		// table end
		writeln("</table>");
	}
}

// absolute position of page element
function getAbsPosition(pageElem, strOrigin)
{
	var iPos = 0;
	while(pageElem != null)
	{
		iPos += pageElem["offset" + strOrigin];
		pageElem = pageElem.offsetParent;
	}
	return iPos;
}

// fucking IE warning
function warnAboutIE()
{
	var strTemp = new String();
	var strIeMsg = strTemp.concat
	(
		"Используемый Вами браузер подозрительно похож на Internet Explorer,\n",
		"который не в состоянии правильно отобразить данную страницу.\n",
		"Рекомендуется использовать Firefox, Opera или Safari, адекватно\n",
		"воспринимающие стандартные (X)HTML, CSS и JavaScxript."
	)
	alert(strIeMsg);
}

// end of file
