
var newsObjectPosition = 0;
var bottomBlockHeight = 160;
var newsObjectHeight = 0;
var scrollTop = 0;
var documentHeight = 0;

$(document).ready(function(){
	/*Start menu settings*/
	$(".menuList li").bind("mouseleave" , function(){
		$('#' + this.id + ' ul.menuListLevel2').css('display', 'none');
	})
	$(".menuList li").bind("mouseenter" , function(){
		$('#' + this.id + ' ul.menuListLevel2').css('display', 'block');
	})
	$(".menuListLevel2 li").bind("mouseleave" , function(){
		$('#' + this.id + ' ul.menuListLevel3').css('display', 'none');
	})
	$(".menuListLevel2 li").bind("mouseenter" , function(){
		$('#' + this.id + ' ul.menuListLevel3').css('display', 'block');
	})
	/*End menu settings*/
	
	/*Start search object settings*/
	$("#searchText").bind("focus" , function(){
		if ($(this).val() == $(this).attr('startText'))
		{
			$(this).val("");
		}
	});
	$("#searchText").bind("focusout" , function(){
		if ($(this).val() == '')
		{
			$(this).val($(this).attr('startText'));
		}
	});
	/*End search object settings*/
	
	/*Start signin object settings*/
	$("#signEnterLink").click(function(){
		var offset = jQuery('#sighInContainer').offset();
		var x = offset.left;
		var y = offset.top;

		$("#slideSignInBlock").css('left', x - 10 + 'px');
		$("#slideSignInBlock").css('top', y + 20 + 'px');
		$("#slideSignInBlock").slideToggle();
	});
	$("#signExitLink").click(function(){
		$("#slideSignInBlock").submit();
	});
	$("#linkSignInSubmit").click(function(){
		$("#slideSignInBlock").submit();
	});
	/*End signin object settings*/
	
	//var fixedBlockPosition
	
	if ($("#newsObject").offset()!=null)
	{	
		newsObjectHeight = $("#newsObject").height();
		newsObjectPosition = $("#newsObject").offset().top;
		newsObjectPosition = (newsObjectPosition < 300)?570:newsObjectPosition;
		
		scrollAcions();
		
		$(window).scroll(function(){
			scrollAcions();
		});
		
		$(window).resize(function(){
			scrollAcions();
		});
	}
	
	$(".poolAnswerVariant").bind("change", function(){
		$("#poolAction_"+$(this).attr("poolid")).attr("answerid", $(this).attr("answerid"));
	});
	
	$(".poolAction").click(function(){
		poolId = $(this).attr("poolid");
		answerId = $(this).attr("answerid");
		
		if (answerId=='')
		{
			alert($(this).attr("errortext"));
		}
		else
		{
			$("#poolAnswer_"+poolId).addClass("poolLoader");
			var data = {
			poolId:			poolId,
			answerId:		answerId,
			action:			'voting',
			handler:		'PoolAjaxController'
			};
			$.post(
				ajaxHandlerUrl,
				data,
				function (response)
				{
					$("#poolAnswer_"+poolId).removeClass("poolLoader");
					if (response.success)
					{
						$("#poolAnswer_"+poolId).html(response.html);
					}
				},
				"json"
			);
			return true;
		}
	});
});

function scrollAcions()
{
	scrollTop = $(window).scrollTop();
	documentHeight = $(document).height();
	
	//alert("scrollLimit = " +  scrollLimit + "\nscroll position = " + $(window).scrollTop() + "\nwindow height = " + $(window).height() + "\ndocument height = " + $(document).height() + "\nelement height = " + $(".newsObject").height());
	//$(".developers").html("newsObjectPosition = " + newsObjectPosition + "\nscroll position = " + $(window).scrollTop() + "\nwindow height = " + $(window).height() + "\ndocument height = " + $(document).height() + "\nelement height = " + $(".newsObject").height());
	if ((scrollTop + newsObjectHeight) > (documentHeight - bottomBlockHeight))
	{
		$("#newsObject").css('position', 'relative');
		var topCoord = ((newsObjectPosition + newsObjectHeight + bottomBlockHeight) >= documentHeight)?0:(documentHeight - newsObjectPosition - bottomBlockHeight - newsObjectHeight);
		$("#newsObject").css('top', topCoord);
	}
	else
	{
		if (scrollTop >= newsObjectPosition && (scrollTop + newsObjectPosition + bottomBlockHeight) < documentHeight)
		{
			$("#newsObject").css('top', '0');
			$("#newsObject").css('position', 'fixed');
		}
		else if (scrollTop >= newsObjectPosition && (scrollTop + newsObjectPosition + bottomBlockHeight) >= documentHeight)
		{
			$("#newsObject").css('position', 'relative');
			$("#newsObject").css('top', (scrollTop - newsObjectPosition));
		}
		else
		{
			$("#newsObject").css('position', 'relative');
			$("#newsObject").css('top', '0');
		}
	}
}

function replaceSelectedText(obj, text, param1, param2)
{
	obj.focus();

	if (document.selection)
	{
		var s = document.selection.createRange();
		s.text = param1 + text + param2;
		//eval("s.text="+cbFunc+"(s.text);");
		s.select();
		return true;
	}
	else if (typeof(obj.selectionStart)=="number")
	{
		var start = obj.selectionStart;
		var end = obj.selectionEnd;
		
		//eval("var rs = "+cbFunc+"(obj.value.substr(start,end-start));");
		var rs = param1 + text + param2;
		obj.value = obj.value.substr(0,start)+rs+obj.value.substr(end);
		obj.setSelectionRange(end,end);
		return true;
	}
	
	return false;
}

function goPool(poolId, answerId)
{
	
	attachCommentId = $("#attachCommentId").val();
	var data = {
	poolId:			poolId,
	answerId:		getPageByHash(hash),
	action:			'voting',
	handler:		'PoolAjaxController'
	};
	$.post(
		ajaxHandlerUrl,
		data,
		function (response)
		{
			$("#activeLoader").css('display', 'none');
			isCommentProcessFlag = false;
			
			if (response.success)
			{
				$("#viewComment").html(response.html);
				updateComRate();
				updateUserLinks();
			}
		},
		"json"
	);
	return true;
}
