// Copyright 2002-2010 JP7

/**
 * Ajusta a posição do box ShareThis de acordo com o scroll e tamanho da janela.
 * 
 * @author Lucas Martins at JP7
 */
function positionShareBox() {
	if($('.box_sharethis').length > 0 && !($.browser.msie && parseInt($.browser.version) < 7)) {
        var descripY = parseInt($('.shareContent').offset().top) - 20;
        var $postShare = $('.box_sharethis');
        var pullX = $postShare.css('margin-left');
		
        $postShare.css('border', '2px solid #EEE');
		
		var windowW = $(window).width();
		if (windowW < 1137) {
			$postShare.css({
				borderWidth: 0,
				position: 'relative', 
				left: 0, 
				top: 0,
				marginLeft: 0,
				marginTop: -5,
				marginBottom: 6
			});
			$('.small-buttons').css('display', 'block');
			$('.large-buttons').css('display', 'none');
		}
		
        $(window).data('scrollBound', false);
        
        function positionShareScroll() {
            var scrollY = $(window).scrollTop();
            var fixedShare = $postShare.css('position') == 'fixed';
            if (scrollY > descripY && !fixedShare) {
                $postShare.stop().css({
                    position: 'fixed',
                    left: '50%',
                    top: 30,
                    marginLeft: -564
                });
            } else if (scrollY < descripY && fixedShare) {
                $postShare.css({
                    position: 'relative',
                    left: 0,
                    top: 0,
                    marginLeft: pullX
                });
            }
        }
		
		$(window).resize(function(){
            windowW = $(window).width();
            var pulledOutside = $postShare.css('margin-left') == pullX;
            if (windowW >= 1137) {
                if (!$(window).data('scrollBound')) {
                    if (!pulledOutside) {
						$('.small-buttons').hide();
                        $postShare.animate({marginLeft: pullX}, {
							complete: function() {
								if ($(window).width() < 1137) {
									$('.large-buttons').hide();
								}
							}
						}).css({
							borderWidth: 2,
							marginRight: 7,
							marginTop: 0
						});
						$('.large-buttons').show();
                    }
                    $(window).data('scrollBound', true).bind('scroll.positionShare', function() {
						positionShareScroll();
					});
                    positionShareScroll();
                }
            } else {
                if (pulledOutside || $(window).data('scrollBound')) {
					$('.large-buttons').hide();
					$('.small-buttons').show();
                    $postShare.css({ 
						position: 'relative', 
						left: 0, 
						top: 0,
						borderWidth: 0,
						marginTop: -5,
						marginBottom: 6
					}).animate({marginLeft: 0});
                    $(window).data('scrollBound', false).unbind('scroll.positionShare');
					if ($('.large-buttons').is(':visible')) {
						$('.large-buttons').css('display', 'none');
					}
                }
            }
        });
        $(window).resize();
    }
}

