function getScrollTop()
{
	var scrollTop = 0;
	if (window.pageYOffset)
		scrollTop = window.pageYOffset;
	else if (document.documentElement.scrollTop)
		scrollTop = document.documentElement.scrollTop;
	else if (document.body.scrollTop)
		scrollTop = document.body.scrollTop;
	return scrollTop;
}

$(document).ready(function() {
	var commentBusy = advertBusy = false;

	if (location.hash)
	{
		$('#comments-content').show();
	}

	$('.share').css('opacity', 0.55);
	$('.share').hover(
		function() {
			$(this).stop().animate({
				opacity: 1.0,
			});
		},
		function() {
			$(this).stop().animate({
				opacity: 0.55,
			});
		}
	);

	$('#comments').click(function() {
		$('#comments-content').toggle();
		return false;
	});

	$('#comment-close').click(function() {
		$('#comment-form').hide();
		return false;
	});

	$('#comment-captcha-refresh').click(function() {
		var randomString = Math.random();
		randomString = parseInt(randomString*10000000000);
		$('#comment-img').attr('src', projectPath+'captcha.php?'+randomString);
		return false;
	});

	$('#leave-comment').click(function() {
		$('#advert-frame').hide();
		$('#comment-form').toggle();
		$('#Done').hide();
		$('#Author').val('');
		$('#Comment').val('');
		$('#CaptchaComment').val('');
		$('#ErrorAuthor').hide();
		$('#ErrorComment').hide();
		$('#ErrorCaptcha').hide();
		return false;
	});

	$('#add-comment').click(function() {
		if (commentBusy || advertBusy) return false;

		var a = new Object;
		for (var i = 0; i < this.form.elements.length; i++)
		{
			if (this.form.elements[i].value == '') this.form.elements[i].value = this.form.elements[i].defaultValue;
			a[this.form.elements[i].name] = this.form.elements[i].value;
		}

		$('#ErrorAuthor').hide();
		$('#ErrorComment').hide();
		$('#ErrorCaptcha').hide();

		$('#comment-loading').show();
		commentBusy = true;

		JsHttpRequest.query(
			projectPath+'infoblock/ajax.php',
			a,
			function(result, errors)
			{
				$('#comment-loading').hide();
				commentBusy = false;
				if (result.Errors)
				{
					for (var i = 0; i < result.Errors.length; i++)
					{
						$('#Error'+result.Errors[i].Field).html(result.Errors[i].Message);
						$('#Error'+result.Errors[i].Field).fadeIn();
					}
					$('#CaptchaComment').val('');
					var randomString = Math.random();
					randomString = parseInt(randomString*10000000000);
					$('#comment-img').attr('src', projectPath+'captcha.php?'+randomString);
				}
				else
				{
					$('#comment-form').hide();
					$('#Done').html(result.Message);
					$('#Done').fadeIn();
					$('#Author').val('');
					$('#Comment').val('');
					$('#CaptchaComment').val('');
					$('#comments-content').show();

					var oddClass = '';
					if (!$('#comments-content div:first').hasClass('odd')) oddClass = ' odd';
					$('#comments-content').prepend('<div class="comm'+oddClass+'"><p class="name">'+result.Comment.CommentDateFmt+' - '+result.Comment.Author+': <em><a href="#c'+result.Comment.CommentID+'" name="c'+result.Comment.CommentID+'" title="'+GetTranslation('comment-link', 'infoblock')+'">#'+result.Comment.CommentID+'</a></em></p><p>'+result.Comment.Comment+'</p></div>');
				}
			}
		);

		return false;
	});

	$('#advert').click(function() {
		if (commentBusy || advertBusy) return false;

		$('#comment-form').hide();
		$('#advert-content').html('<img src="'+projectPath+'website/gold.ru/template/images/preloader_grey.gif" class="advert-preloader" />');
		$('#advert-frame').show();

		advertBusy = true;

		JsHttpRequest.query(
			projectPath+'form/ajax.php',
			{'PageID': 4},
			function(result, errors)
			{
				advertBusy = false;

				$('#advert-content').hide();
				$('#advert-content').html(result.Content);
				var height = $('#advert-content').height();
				$('#advert-content').css('height', '32px');
				$('#advert-content').show();

				$('#advert-content').stop().animate({
					height: height
				}, 300, function() {
					$('#advert-content').css('height', 'auto');
				});

				var randomString = Math.random();
				randomString = parseInt(randomString*10000000000);
				$('#advert-img').attr('src', projectPath+'captcha.php?'+randomString);
			}
		);
		return false;
	});

	$('#advert-submit').live('click', function() {
		if (commentBusy || advertBusy) return false;

		var form = document.getElementById('advert-form');

		var a = new Object;
		for (var i = 0; i < form.elements.length; i++)
		{
			a[form.elements[i].name] = form.elements[i].value;
		}

		$('#advert-loading').show();
		advertBusy = true;

		JsHttpRequest.query(
			projectPath+'form/ajax.php',
			a,
			function(result, errors)
			{
				advertBusy = false;

				$('#advert-content').html(result.Content);
			}
		);
		return false;
	});

	$('#advert-close').live('click', function() {
		$('#advert-frame').hide();
		return false;
	});

	$('#advert-captcha').live('click', function() {
		var randomString = Math.random();
		randomString = parseInt(randomString*10000000000);
		$('#advert-img').attr('src', projectPath+'captcha.php?'+randomString);
		$('#advert-code').focus();
		return false;
	});

	$('#contact-captcha').live('click', function() {
		var randomString = Math.random();
		randomString = parseInt(randomString*10000000000);
		$('#contact-img').attr('src', projectPath+'captcha.php?'+randomString);
		$('#contact-code').focus();
		return false;
	});

	$(function () {
		$(".main_col, .right_col").addtocopy(
			{
				htmlcopytxt: '<br>Подробнее: <a href="' + window.location.href + '">' + window.location.href + '</a>', minlen: 50, addcopyfirst: false
			}
		);
	});

});

