/*
**
**	jQuery Util
**	Author: 		Derek Yu
**  Email:          lonefool@gmail.com
**	Version:		0.1 (September 01, 2011)
**	
**  Copyright (c) 2011 Derek Yu.
**
**
*/
(function ($) {
    $.fn.extend({
        LFCenter: function (options) {
            var options = $.extend({
                inside: window,
                transition: 0,
                minX: 0,
                minY: 0,
                withScrolling: true,
                vertical: true,
                horizontal: true
            }, options);

            return this.each(function () {
                var props = { position: 'absolute' };
                if ($(this).css("position") == "fixed")
                    props = { position: 'fixed' };
                if (options.vertical) {
                    var top = ($(options.inside).height() - $(this).outerHeight()) / 2;
                    if (options.withScrolling) top += $(options.inside).scrollTop() || 0;
                    top = (top > options.minY ? top : options.minY);
                    $.extend(props, { top: top + 'px' });
                }
                if (options.horizontal) {
                    var left = ($(options.inside).width() - $(this).outerWidth()) / 2;
                    if (options.withScrolling) left += $(options.inside).scrollLeft() || 0;
                    left = (left > options.minX ? left : options.minX);
                    $.extend(props, { left: left + 'px' });
                }
                if (options.transition > 0)
                    $(this).animate(props, options.transition);
                else
                    $(this).css(props);

                return $(this);
            });
        },
        LFResize: function (f) {
            LFSavedSize = { savedWidth: 0, savedHeight: 0 };

            function resizeOnce() {
                var version = parseInt($.browser.version, 10);
                if ($.browser.msie && version == 8) {
                    if ($(window).height() == LFSavedSize.savedHeight && $(window).width() == LFSavedSize.savedWidth) {
                        return false;
                    }
                    LFSavedSize.savedHeight = $(window).height();
                    LFSavedSize.savedWidth = $(window).width();
                }

                return true;
            }

            function handleLFResize(e) {
                if (resizeOnce()) {
                    return f.apply(this, [e]);
                }
            }

            this.each(function () {
                if (this == window) {
                    $(this).resize(handleLFResize);
                }
                else {
                    $(this).resize(f);
                }
            });
        }
    });
})(jQuery);


$(document).ready(function () {

    if (window.location.protocol != "https:" && document.location.href.toLowerCase().indexOf("basket.aspx") < 0) {
        $("#topMenu").LFCenter({ vertical: false });

        $(window).scroll(function () {
            var scrollTop = $(window).scrollTop();
            var menuTop = $("#topMenu").css("top").replace("px", "");

            if (scrollTop < 121)
                $("#topMenu").css("position", "absolute").css("top", 121);
            else if (scrollTop > 121)
                $("#topMenu").css("top", 0).css("position", "fixed");
        });

        $(window).LFResize(function () {
            $("#topMenu").LFCenter({ vertical: false });
        });
    }
});


