﻿
/**
* msOnlineShare
* @requires  jQuery 1.3.2
* @author    v-spsund 
* @history   adapted from dynPageTools.js/dynSocialBookmarks v-dacam 2010/07/22
*
*/

/* Test for jQuery */
if (typeof jQuery != 'undefined') {

    var msOnlineShare = {

        /**
        * params
        * Can customize during call to the init method.
        * @example
        * 	msOnlineShare.init({
        *		holder: '#your-div-id',
        *		delay: 500
        *	});
        */
        params: {
            delay: 1000,
            hideClass: 'hide',
            parentDiv: '#site-share-link',
            holder: '#share-this-page',
            toggler: 'a.share'
        },
        offLeft: '-999999', /* "hider" left offset, in pixels */

        /* holders */
        pageWrapper: false,
        target: false,
        timer: false,
        left: 0,
        top: 0,
        boxHeight: 0,
        boxTop: 0,

        parseArgs: function (args) {
            if (!args) { return false; }
            if (args['delay']) {
                this.params.delay = args['delay'];
            }
            if (args['hideClass']) {
                this.params.hideClass = args['hideClass'];
            }
            if (args['holder']) {
                this.params.holder = args['holder'];
            }
            if (args['toggler']) {
                this.params.toggler = args['toggler'];
            }
            if (args['parentDiv']) {
                this.params.parentDiv = args['parentDiv'];
            }
        },

        init: function (args) {

            /* Immediately attach unload routines */
            $(window).unload(function () {
                msOnlineShare.unload();
            });


            this.parseArgs(args); /* process incoming parameters */
            /* Now run init routines. */

            this.target = $(this.params.holder);
            /* if ($.browser.msie) { this.target.css({ 'marginTop': '1.5em' }); } */
            $toggler = $(this.params.parentDiv + ' ' + this.params.toggler);

            /* get position of the link */
            var position = $toggler.position();
            this.top = position.top + 28;
            this.left = position.left + 18;
            position = null;
            /* get info about the flying div */
            this.boxHeight = parseInt(this.target.height());
            /* this.boxTop = parseInt(this.top - this.boxHeight); */
            this.boxTop = parseInt(this.top) - 3;

            /* Attach hide/show events */
            $toggler.click(function () {
                msOnlineShare.toggleIt();
                msOnlineShare.timer = setTimeout(
						function () { msOnlineShare.toggleIt(); },
						msOnlineShare.params.delay
					);
                return false;
            });
            this.target.bind('mouseout', function () {
                msOnlineShare.timer = setTimeout(
						function () { msOnlineShare.toggleIt(); },
						msOnlineShare.params.delay
					);
            });
            this.target.bind('mouseover', function () {
                clearTimeout(msOnlineShare.timer);
            });
            /* Hide immediately when parent is clicked */
            this.pageWrapper = $('div#pageWrapper'); /* outermost div in layout */
            if (this.pageWrapper !== false) {
                this.pageWrapper.click(function () {
                    clearTimeout(msOnlineShare.timer)
                    msOnlineShare.toggleIt('off');
                });
            }
            /* END Attach hide/show events */


        },

        /**
        * toggleIt
        * Hides/shows bookmarks div.
        * Set target DIV ID via this.params.holder property - msOnlineShare.init({holder: '#your-div-id'});
        */
        toggleIt: function (toggle) {
            var pos = this.target.position();
            pos = pos.left;

            if (toggle == 'off') {
                this.target.css({
                    'left': this.offLeft + 'px'
                });
            } else if (pos == this.offLeft || this.target.hasClass(this.params.hideClass)) {
                this.target.removeClass(this.params.hideClass);
                this.target.css({
                    'left': this.left + 'px',
                    'top': this.boxTop + 'px'
                });
            } else {
                this.target.css({
                    'left': this.offLeft + 'px'
                });
            }
        },

        /* garbage collection */
        unload: function () {
            if (this.target && this.target !== false) { this.target = null; }
            if (this.pageWrapper && this.pageWrapper !== false) { this.pageWrapper = null; }
        }


    };

    $(document).ready(function () {
        msOnlineShare.init();
    });


    /* -------------------- */
}; /* close jQuery test */
/* -------------------- */

