jQuery(document).ready(function(){
	
	// Single page post share box
    
    if( jQuery('.shareitem').length > 0 && !(jQuery.browser.msie && parseInt(jQuery.browser.version) < 7) ) {
        var descripY = parseInt(jQuery('.shareitem').offset().top) - 20;
        descripY = 250;
        var $postShare = jQuery('#sharebox');
        var $postItems = jQuery('.shareitem');
        var pullX = $postShare.css('margin-left');
        
        jQuery(window).data('scrollBound', false);
        
        function positionShareScroll() {
            var scrollY = jQuery(window).scrollTop();
            var fixedShare = $postShare.css('position') == 'fixed';
            if ( scrollY > descripY ) {
                $postShare.stop().css({
                    position: 'fixed',
                    left: '50%',
                    top: 20,
                    marginLeft: -571
                });
                $postItems.css({
                	float: 'none',
                	padding: '0 0 5px 0'
                });
            } else if ( scrollY < descripY ) {
                $postShare.css({
                    position: 'absolute',
                    left: 0,
                    top: 0,
                    marginLeft: -92
                });
                $postItems.css({
                	float: 'none',
                	padding: '0 0 5px 0'
                });
            }
        }
        
		jQuery(window).resize(function(){
            var windowW = jQuery(window).width();
            var pulledOutside = $postShare.css('margin-left') == pullX;
            if ( windowW >= 1137 ) {
                if ( !jQuery(window).data('scrollBound') ) {
                    if ( !pulledOutside ) {
                        $postShare.animate({ marginLeft: pullX });
						//Make block horizontal
						$postShare.css('border-width', '1px');
						$postShare.css({
							marginRight: 7,
							marginTop: 0
						});
						$postItems.css({
							float: 'none',
							padding: '0 0 5px 0'
						});
                    }
                    jQuery(window)
                        .data('scrollBound', true)
                        .bind('scroll.positionShare', function(){
                            positionShareScroll()
                        })
                    ;
                    positionShareScroll();
                }
			} else {
				if ( pulledOutside || jQuery(window).data('scrollBound') ) {
					$postShare
					    .css({ position: 'relative', left: 0, top: 0 })
					    .animate({marginLeft: 0})
					;
					//Make block horizontal
					$postShare.css({
						marginTop: -16,
						marginBottom: 4
					});
					$postShare.css('border-width', 0);
					$postItems.css({
						float: 'left',
						padding: '10px'
					});
			
					jQuery(window)
						.data('scrollBound', false)
						.unbind('scroll.positionShare')
					;
                }
            }
        });
        jQuery(window).resize();
    }
	
});
