

function getElementPosition(elem) {
    var w = elem.offsetWidth;
    var h = elem.offsetHeight;

    var l = 0;
    var t = 0;

    while (elem) {
        l += elem.offsetLeft;
        t += elem.offsetTop;
        elem = elem.offsetParent;
    }

    return { "left": l, "top": t, "width": w, "height": h };
}

function getClientWidth() {
	return document.compatMode == 'CSS1Compat' || window.opera ? document.documentElement.clientWidth : document.body.clientWidth;
}



var ua = navigator.userAgent.toLowerCase();
var isOpera = (ua.indexOf('opera') > -1);
var isIE = (!isOpera && ua.indexOf('msie') > -1);

function getViewportHeight() {
	return ((document.compatMode || isIE) && !isOpera) ? (document.compatMode == 'CSS1Compat') ? document.documentElement.clientHeight : document.body.clientHeight : (document.parentWindow || document.defaultView).innerHeight;
}

function getDocumentHeight() {
	return Math.max(document.compatMode != 'CSS1Compat' ? document.body.scrollHeight : document.documentElement.scrollHeight, getViewportHeight());
}

function getBodyScrollTop() {
  return self.pageYOffset || (document.documentElement && document.documentElement.scrollTop) || (document.body && document.body.scrollTop);
}





$(function() {

	
				window.onscroll = function() {
			var op = document.getElementById("sidebar-panel");

			
			//var opHeight = $("#sidebar-panel").height();
			var opHeight=640;
			
			var winHeight = getViewportHeight();
			var pos = getElementPosition(op);
			
			var op2 = document.getElementById("center");
			var centerHeight = $(op2).height();

			var posfrom = 455; // От чего начинать ездить
			var p170 = 60;
			var p95 = 0;
			var posend = 410; // расстояние для низа

			
			var minwinheight = opHeight-150; //мин высота окна
			var posend2 = (posfrom + centerHeight - opHeight-32); // где заканчивать

				if ((getDocumentHeight() - winHeight - getBodyScrollTop() <= p170) || (winHeight < minwinheight))
				{
		
					return;

				}
				else if (getBodyScrollTop() > posfrom)
				{
				 $("#sidebar-panel").stop().animate({marginTop: getBodyScrollTop() - posend + "px"}, 600);
					
					
				}
				
				else
				{
					$("#sidebar-panel").stop().animate({marginTop: p95 + "px"}, 600);
			
				}

		}



	$("#lnk-more").click(function() {
		if ($(this).text() == "Читать все") {
			$(this).text("Свернуть").hide();
			$("#tb-more").slideDown();
		}
		else {
			$(this).text("Читать все");
			$("#tb-more").slideUp();
		}
	});

	$(".c-fotos a").fancybox({
		'overlayShow': false
	});

	$('#mi-thumbs').jcarousel({
		wrap: "circular",
		visible: 3,
		scroll: 1
	});

	$("#mi-thumbs a").click(function() {
		$("#main-image img").attr("src", $(this).attr("rel"));
		$("#zoom").attr("href", $(this).attr("ref"));
	})

	$("#zoom").fancybox({
		'overlayShow': false
	});
})

