﻿// Copyright (c) 2010 Robust Haven, Leblanc Meneses
// dependencies: jquery, jqueryui
jQuery.fn.SlideInMenuBehavior = function (configure) {
    var id = $(this).attr('id');
    $(this).css("position", "absolute");
    $(this).css("top", $(this).position().top + 'px');

    var initialTopPosition = $(this).offset().top;


    if (configure.anchorside == "right") {
        $(this).css("right", $(this).width() * -1 + configure.pixelcollapsedview + 'px');
    } else if (configure.anchorside == "left") {
        $(this).css("left", $(this).width() * -1 + configure.pixelcollapsedview + 'px');
    }

    $('#' + configure.bottom).click(function (e) {
        e.preventDefault();
        $('html, body').stop().animate(
                    { scrollTop: $(document).height() - $(window).height() },
                    {
                        duration: 1500,
                        easing: 'easeOutQuint'
                    });
    });

    $('#' + configure.top).click(function (e) {
        e.preventDefault();
        $('html, body').stop().animate(
                    { scrollTop: 0 },
                    {
                        duration: 1500,
                        easing: 'easeOutQuint'
                    });
    });


    $('#' + configure.pagedown).click(function (e) {
        e.preventDefault();
        $('html, body').stop().animate(
                    { scrollTop: $(document).scrollTop() + $(window).height() },
                    {
                        duration: 1500,
                        easing: 'easeOutQuint'
                    });
    });

    $('#' + configure.pageup).click(function (e) {
        e.preventDefault();
        $('html, body').stop().animate(
        { scrollTop: $(document).scrollTop() - $(window).height() },
        {
            duration: 1500,
            easing: 'easeOutQuint'
        });
    });


    $('#' + configure.expand).click(function (e) {
        e.preventDefault();
        var direction = $(this).data('direction');
        if (direction == null || direction == 'collapsed') {
            $(this).data('direction', 'expanded');
            var animate = {};
            if (configure.anchorside == "right") {
                animate = { right: 0 }
            } else if (configure.anchorside == "left") {
                animate = { left: 0 }
            }
            $('#' + id)
            .animate(animate, {
                duration: 1500,
                easing: 'easeOutQuint',
                complete: configure.expand_complete
            });
        }
        else {
            $(this).data('direction', 'collapsed');
            var animate = {};
            if (configure.anchorside == "right") {
                animate = { right: $('#' + id).width() * -1 + configure.pixelcollapsedview }
            } else if (configure.anchorside == "left") {
                animate = { left: $('#' + id).width() * -1 + configure.pixelcollapsedview }
            }
            $('#' + id)
            .animate(animate, {
                duration: 1500,
                easing: 'easeOutQuint',
                complete: configure.expand_complete
            });
        }
    });


    $(window).scroll(function () {
        setTimeout(
            function () {
                var viewportHeight = $(window).height();
                var menuHeight = $('#' + id).height();

                if (viewportHeight >= menuHeight) {
                    var offset = (viewportHeight - menuHeight) / 2;
                }
                else {
                    var offset = viewportHeight - menuHeight;
                }

                var final = $(document).scrollTop() + offset - initialTopPosition;
                final = (final <= 0)? 0:final;

                $('#' + id).stop().animate(
                    { top: final },
                    {
                        duration: 1500,
                        easing: 'easeOutQuint'
                    });
            },
            1500);
        //            
        //            $('#' + id).fadeOut(100, function() {
        //                var scrl = setTimeout(function() {
        //                    animActive = false;
        //                    $('#' + id).fadeIn(500);
        //                }, 2000);
        //            });
    });

    $(window).scroll();
}

