// helpers.js - common helper fucntions
// Copyright (c) 2003-2012 by Elijah Zarezky,
// All rights reserved

// returns greeting string for the "About" page
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 "Добрая ночь!";
	}
}

// returns greeting string for the "About" page in English
function getGreetingStringInEnglish()
{
	dateNow = new Date();
	nHours = dateNow.getHours();

	if (5 <= nHours && nHours <= 11)
	{
		return "Good morning!";
	}
	else if (12 <= nHours && nHours <= 16)
	{
		return "Good afternoon!";
	}
	else if (17 <= nHours && nHours <= 23)
	{
		return "Good evening!";
	}
	else
	{
		return "Hello everyone who doesn't sleep!";
	}
}

// writes mirror information to the "About" page
function writeMirrorInfo()
{
	if (location.protocol == "http:" && location.hostname != "localhost")
	{
		// assume we're online
		if (location.hostname != "home.peterstar.ru")
		{
			// assume zarezky.spb.ru
			document.writeln("&laquo;Зеркало&raquo; этого сайта вы можете найти по адресу");
			document.write("<a href=\"http://home.peterstar.ru/zarezky/\" target=\"_blank\">");
			document.writeln("http://home.peterstar.ru/zarezky/</a>.");
			document.writeln("<font color=\"#C00000\">Начиная с 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>.");
		}
	}
}

// creates Google "+1" button
function createGooglePlusoneButton()
{
	if (location.protocol == "http:" && location.hostname != "localhost")
	{
		gapi.plusone.render("googlePlusone", {size: "standard", count: "true"});
	}
}

// creates VKontakte "Like" widget
function createVKLikeWidget()
{
	if (location.protocol == "http:" && location.hostname != "localhost")
	{
		VK.Widgets.Like("vk_like", {type: "mini", verb: 1});
	}
}

// creates VKontakte "Recommended" widget
function createVKRecommendedWidget()
{
	if (location.protocol == "http:" && location.hostname != "localhost")
	{
		VK.Widgets.Recommended("vk_recommended", {limit: 10, period: 'month', verb: 1});
	}
}

// creates VKontakte "Comments" widget
function createVKCommentsWidget()
{
	if (location.protocol == "http:" && location.hostname != "localhost")
	{
		var widgetWidth = document.getElementById("vk_comments_container_2309915").offsetWidth;
		VK.Widgets.Comments("vk_comments", {limit: 10, width: widgetWidth.toString(), attach: false});
	}
}

// adjusts VKontakte "Comments" widget
function adjustVKCommentsWidget()
{
	if (location.protocol == "http:" && location.hostname != "localhost")
	{
		var vkComments = document.getElementById("vk_comments");
		if (vkComments != null)
		{
			var vkCommentsFrames = vkComments.getElementsByTagName("iframe");
			if (vkCommentsFrames != null)
			{
				vkComments.style.display = "none";
				var vkCommentsContainer = document.getElementById("vk_comments_container_2309915");
				vkComments.style.width = vkCommentsContainer.offsetWidth + "px";
				vkCommentsFrames[0].style.width = vkCommentsContainer.offsetWidth + "px";
				vkCommentsFrames[0].src = vkCommentsFrames[0].src;
				vkComments.style.display = "block";
			}
		}
	}
}

// writes social networks buttons
function writeShareButtons(strImagesPath, fWithRussian)
{
	with (document)
	{
		if (fWithRussian)
		{
			// VKontakte
			write("<a target=\"_blank\" href=\"http://vkontakte.ru/share.php?url=" + location.href + "\" ");
			write("title=\"Поделиться ВКонтакте\" rel=\"nofollow\">");
			write("<img src=\"" + strImagesPath + "/vkontakte.png\" class=\"socNetwork\" alt=\"ВКонтакте\"/></a>");

			// Yandex
			write("<a target=\"_blank\" href=\"http://zakladki.yandex.ru/userarea/links/addfromfav.asp?bAddLink_x=1&lurl=");
			write(location.href + "&lname=" + title + "\" title=\"Добавить в Яндекс.Закладки\" rel=\"nofollow\">");
			write("<img src=\"" + strImagesPath + "/yandex.png\" class=\"socNetwork\" alt=\"Yandex\"/></a>");
		}

		// Facebook
		write("<a target=\"_blank\" href=\"http://www.facebook.com/sharer.php?u=" + location.href);
		write("&t=" + title + "\" title=\"Поделиться в Facebook\" rel=\"nofollow\">");
		write("<img src=\"" + strImagesPath + "/facebook.png\" class=\"socNetwork\" alt=\"Facebook\"/></a>");

		// LinkedIn
		write("<a target=\"_blank\" href=\"http://www.linkedin.com/shareArticle?mini=true&url=" + location.href);
		write("&title=" + title + "&source=zarezky.spb.ru\" title=\"Поделиться в LinkedIn\" rel=\"nofollow\">");
		write("<img src=\"" + strImagesPath + "/linkedin.png\" class=\"socNetwork\" alt=\"LinkedIn\"/></a>");

		// LiveJournal
		write("<a target=\"_blank\" href=\"http://www.livejournal.com/update.bml?event=" + location.href);
		write("&subject=" + title + "\" title=\"Опубликовать в LiveJournal\" rel=\"nofollow\">");
		write("<img src=\"" + strImagesPath + "/livejournal.png\" class=\"socNetwork\" alt=\"LiveJournal\"/></a>");

		// Twitter
		write("<a target=\"_blank\" href=\"http://twitter.com/home?status=Читаю: " + location.href + "\" ");
		write("title=\"Опубликовать в Twitter\" rel=\"nofollow\">");
		write("<img src=\"" + strImagesPath + "/twitter.png\" class=\"socNetwork\" alt=\"Twitter\"/></a>");
	}
}

// writes main menu to the sidebar's cell
function writeMainMenu(strPagesPath)
{
	with (document)
	{
		writeln("<a href=\"" + strPagesPath + "/index.html\">О&nbsp;сайте</a><br/>");
		writeln("<a href=\"" + strPagesPath + "/news.html\">Новости</a><br/>");
		writeln("<a href=\"" + strPagesPath + "/blog.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 + "/quotationary.html\">Цитатник</a>");
	}
}

// writes main menu to the sidebar's cell (special cases)
function writeMainMenuEx(strPagesPath, strTarget, strNoLink)
{
	arrPages = new Array
	(
		"index.html",
		"news.html",
		"blog.html",
		"lectures.html",
		"articles.html",
		"projects.html",
		"files.html",
		"favorites.html",
		"treasure.html",
		"sources.html",
		"quotationary.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;
}

// writes Mail.ru counter to the sidebar's cell
function writeMailRuCounter(strImagesPath)
{
	with (document)
	{
		if (location.protocol == "http:" && location.hostname != "localhost")
		{
			// assume we're online - show actual counters from HotLog, LiveInternet, Yandex & Mail.ru
			write("<a href=\"http://click.hotlog.ru/?2165329\" target=\"_blank\">");
			writeln("<img src=\"http://hit39.hotlog.ru/cgi-bin/hotlog/count?" + Math.random() +
				"&s=2165329&im=502&r=" + escape(document.referrer) + "&pg=" + escape(window.location.href) +
				"&j=" + (navigator.javaEnabled() ? "Y" : "N") + "&wh=" + screen.width + "x" + screen.height +
				"&px=" + (((navigator.appName.substring(0, 3) == "Mic")) ? screen.colorDepth : screen.pixelDepth) +
				"&js=1.3" + "\" border=\"0\" width=\"88\" height=\"31\" alt=\"HotLog\" title=\"HotLog\"/><\/a>");
			writeln("<div style=\"height: 8px;\">&nbsp;</div>");
			write("<a href=\"http://www.liveinternet.ru/click\" target=\"_blank\">");
			write("<img src=\"//counter.yadro.ru/hit?t15.4;r" + escape(document.referrer) +
				((typeof(screen) == "undefined") ? "" : ";s" + screen.width + "*" + screen.height + "*" +
				(screen.colorDepth ? screen.colorDepth : screen.pixelDepth)) + ";u" + escape(document.URL) +
				";h" + escape(document.title.substring(0, 80))+ ";" + Math.random() + "\" ");
			writeln("width=\"88\" height=\"31\" border=\"0\" alt=\"LiveInternet\" title=\"LiveInternet\"/></a>");
			writeln("<div style=\"height: 8px;\">&nbsp;</div>");
			write("<a href=\"http://metrika.yandex.ru/stat/?id=4442326&amp;from=informer\" target=\"_blank\" ");
			write("rel=\"nofollow\"><img src=\"//bs.yandex.ru/informer/4442326/3_0_C5C5C5FF_A5A5A5FF_0_pageviews\" ");
			writeln("width=\"88\" height=\"31\" border=\"0\" alt=\"Яндекс.Метрика\" title=\"Яндекс.Метрика\"/></a>");
			writeln("<div style=\"height: 8px;\">&nbsp;</div>");
			write("<a href=\"http://top.mail.ru/jump?from=415352\" target=\"_blank\">");
			write("<img src=\"http://top.list.ru/counter?id=415352;t=216;js=10;");
			write("r=" + escape(document.referrer) + ";rand=" + Math.random() + "\" ");
			writeln("width=\"88\" height=\"31\" border=\"0\" alt=\"Рейтинг@Mail.ru\" title=\"Рейтинг@Mail.ru\"/></a>");
		}
		else
		{
			// probably we're offline - display placeholder images
			write("<img src=\"" + strImagesPath + "/openstat-ru.gif\" ");
			write("width=\"88\" height=\"31\" align=\"middle\" alt=\"Openstat Рейтинг\" title=\"Openstat Рейтинг\" ");
			writeln("border=\"0\"/>");
			writeln("<div style=\"height: 8px;\">&nbsp;</div>");
			write("<img src=\"" + strImagesPath + "/hotlog-ru.gif\" ");
			write("width=\"88\" height=\"31\" align=\"middle\" alt=\"HotLog\" title=\"HotLog\" ");
			writeln("border=\"0\"/>");
			writeln("<div style=\"height: 8px;\">&nbsp;</div>");
			write("<img src=\"" + strImagesPath + "/live-internet-ru.gif\" ");
			write("width=\"88\" height=\"31\" align=\"middle\" alt=\"LiveInternet\" title=\"LiveInternet\" ");
			writeln("border=\"0\"/>");
			writeln("<div style=\"height: 8px;\">&nbsp;</div>");
			write("<img src=\"" + strImagesPath + "/yandex-metrika.png\" ");
			write("width=\"88\" height=\"31\" align=\"middle\" alt=\"Яндекс.Метрика\" title=\"Яндекс.Метрика\" ");
			writeln("border=\"0\"/>");
			writeln("<div style=\"height: 8px;\">&nbsp;</div>");
			write("<img src=\"" + strImagesPath + "/top-mail-ru.gif\" ");
			write("width=\"88\" height=\"31\" align=\"middle\" alt=\"Рейтинг@Mail.ru\" title=\"Рейтинг@Mail.ru\" ");
			writeln("border=\"0\"/>");
		}
	}
}

// writes page footer buttons
function writePageFooterButtons(strImagesPath, fXHTML, fHTML, fCSS)
{
	with (document)
	{
		if (fXHTML)
		{
			write("<td class=\"validatorW3C\">");
			write("<a href=\"http://validator.w3.org/check?uri=" + location + "\" target=\"_blank\">");
			write("<img src=\"" + strImagesPath + "/valid-xhtml10-blue.png\" alt=\"\" ");
			writeln("width=\"88\" height=\"31\" border=\"0\"/></a></td>");
		}
		else if (fHTML)
		{
			write("<td class=\"validatorW3C\">");
			write("<a href=\"http://validator.w3.org/check?uri=" + location + "\" target=\"_blank\">");
			write("<img src=\"" + strImagesPath + "/valid-html401-blue.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 + "\" target=\"_blank\">");
			write("<img src=\"" + strImagesPath + "/valid-css2-blue.png\" alt=\"\" ");
			writeln("width=\"88\" height=\"31\" border=\"0\"/></a></td>");
		}

		write("<td class=\"validatorW3C\">");
		write("<a href=\"http://www.anybrowser.org/campaign/\" target=\"_blank\">");
		write("<img src=\"" + strImagesPath + "/w3c-ab-blue.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>");

		write("<td class=\"validatorW3C\">");
		write("<a href=\"http://www.shadowbox-js.com/\" target=\"_blank\">");
		write("<img src=\"" + strImagesPath + "/shadowbox-js.png\" alt=\"shadowbox.js\" ");
		writeln("width=\"88\" height=\"31\" border=\"0\"/></a></td>");
	}
}

// writes page footer
function writePageFooter(strImagesPath, fXHTML, fHTML, fCSS)
{
	with (document)
	{
		writeln("<hr width=\"100%\" size=\"1\" noshade=\"noshade\"/>");
		writeln("<table width=\"100%\" cellspacing=\"0\" cellpadding=\"0\" class=\"pageFooter\"><tr>");
	}

	writePageFooterButtons(strImagesPath, fXHTML, fHTML, fCSS);

	with (document)
	{
		write("<td class=\"copyrightNotice\">");
		write("Авторские&nbsp;права&nbsp;&copy;&nbsp;2001&mdash;2012<br/>");
		writeln("И.Зарецкий&nbsp;a.k.a.&nbsp;<a href=\"http://www.rsdn.ru/Users/Profile.aspx?uid=10117\"");
		writeln("target=\"_blank\">SchweinDeBurg</a></td>");

		writeln("</tr></table>");
	}
}

// writes page footer in English
function writePageFooterInEnglish(strImagesPath, fXHTML, fHTML, fCSS)
{
	with (document)
	{
		writeln("<hr width=\"100%\" size=\"1\" noshade=\"noshade\"/>");
		writeln("<table width=\"100%\" cellspacing=\"0\" cellpadding=\"0\" class=\"pageFooter\"><tr>");
	}

	writePageFooterButtons(strImagesPath, fXHTML, fHTML, fCSS);

	with (document)
	{
		write("<td class=\"copyrightNotice\">");
		write("Copyright&nbsp;&copy;&nbsp;2001&mdash;2012<br/>");
		writeln("Elijah Zarezky&nbsp;a.k.a.&nbsp;<a href=\"http://www.rsdn.ru/Users/Profile.aspx?uid=10117\"");
		writeln("target=\"_blank\">SchweinDeBurg</a></td>");

		writeln("</tr></table>");
	}
}

// writes article footer
function writeArticleFooter(arrMSDN, strPath, strFile, ckbSize, arrPublished)
{
	with (document)
	{
		// table start
		writeln("<table border=\"0\" cellspacing=\"0\" cellpadding=\"6\" 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)
		{
			writeln("<tr><td><strong>Об этом в MSDN<strong></td>");
			write("<td style=\"white-space: nowrap; text-align: right;\">");
			for (var i = 0; i < arrMSDN.length; ++i)
			{
				write(arrMSDN[i] + "<br/>");
			}
			writeln("</td></tr>");
		}

		// second row - demo project
		if (strFile != null)
		{
			writeln("<tr><td><strong>Демонстрационный проект</strong></td>");
			write("<td style=\"white-space: nowrap; text-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
		if (arrPublished.length > 0)
		{
			writeln("<tr><td><strong>Опубликовано<strong></td>");
			write("<td style=\"white-space: nowrap; text-align: right;\">");
			for (var j = 0; j < arrPublished.length; ++j)
			{
				write(arrPublished[j] + "<br/>");
			}
			writeln("</td></tr>");
		}

		// table end
		writeln("</table>");
	}
}

// writes RSDN article footer
function writeRsdnArticleFooter(arrMSDN, strPath, strFile, ckbSize, nMagNum, nMagYear, nArticleNum)
{
	var arrPublished = new Array
	(
		"журнал <a href=\"http://www.rsdn.ru/mag/main.htm\" target=\"_blank\">RSDN Magazine</a> #" + nMagNum + "\'" + nMagYear,
		"<a href=\"http://www.rsdn.ru/article/?" + nArticleNum + "\" target=\"_blank\">http://www.rsdn.ru/article/?" + nArticleNum + "</a>"
	);
	writeArticleFooter(arrMSDN, strPath, strFile, ckbSize, arrPublished);
}

// end of file

