﻿function LikeBox(parentContainerID, positionTop, positionTopOffset, positionTopFixed, marginLeft, attachFBApp) {
    if (jQuery.browser.msie && parseInt(jQuery.browser.version) < 8)
        return;

    jQuery(document).ready(function ($) {
        var $floatingbox = $('#likebox');
        if ($floatingbox.length == 0) return;

        // Facebook
        $('#fb-root').append('<fb:like width="60" layout="box_count" send="false" action="like" font="tahoma" colorscheme="light" show_faces="false"></fb:like>');
        window.fbAsyncInit = function () {
            FB.init({
                appId: attachFBApp ? '142185925813839' : null,
                status: true,
                cookie: true,
                xfbml: true
            });
        };
        $.getScript(document.location.protocol + '//connect.facebook.net/en_US/all.js#xfbml=1');

        // Google like
        $.getScript('https://apis.google.com/js/plusone.js');
        $('#google-plus-box').append('<div class="g-plusone" data-size="tall" data-count="true"></div>');


        var $parentContainer = $('#' + parentContainerID);
        if ($parentContainer.length > 0) {
            var bodyY = parseInt($parentContainer.offset().top) + positionTopOffset;
            var originalX = $floatingbox.css('margin-left');

            $(window).scroll(function () {
                var scrollY = $(window).scrollTop();
                var isfixed = $floatingbox.css('position') == 'fixed';

                if ($floatingbox.length > 0) {
                    if (scrollY > bodyY && !isfixed) {
                        $floatingbox.stop().css({
                            position: 'fixed',
                            left: '50%',
                            top: positionTop,
                            marginLeft: marginLeft

                        });
                    } else if (scrollY < bodyY && isfixed) {
                        $floatingbox.css({
                            position: 'relative',
                            left: 0,
                            top: positionTopFixed,
                            marginLeft: originalX
                        });
                    }
                }
            });
        }
    });
}

