/*
 * floating 0.11 - jQuery plugin
 * Copyright (c) 2010
 * http://www.liba.com
 * 功能: 固定浮动层插件
 * 作者: 刘松
 * 0.1  2010-08-24  created.
 * 0.11 2011-11-24  fixed top:middle & left:center.
 */
(function($){
    var defaults = {
        top: 'top',
        marginTop: 0,
        left: 'left',
        marginLeft: 0
    },
    isIE = $.browser.msie && !$.support.opacity,
    isIE6 = isIE && $.browser.version < 7,
    $window;
    
    $.fn.floating = function (options) {
        var settings = $.extend( true, {}, defaults, options );
        $window = $(window);
        
        this.each(function () {
            var $this = $(this);
            var style = {};
            
            style['position'] = isIE6 ? 'absolute' : 'fixed';
            style['z-index'] = 5000;
            if ('middle' === settings.top) {
                if (isIE6) {
                    style['top'] = $window.scrollTop() + parseInt(($window.height() - $this.outerHeight()) / 2);
                } else {
                    style['top'] = '50%';
                    //style['margin-top'] = parseInt(settings.marginTop - $this.outerHeight() / 2);
                    style['margin-top'] = settings.marginTop;
                }
            } else if ('bottom' === settings.top) {
                if (isIE6) {
                    style['top'] = $window.height() - $this.outerHeight() - settings.marginTop;
                } else {
                    style['bottom'] = settings.marginTop;
                }
            } else { //top
                style['top'] = settings.marginTop;
            }
            if ('center' === settings.left) {
                style['left'] = '50%';
                //style['margin-left'] = parseInt(settings.marginLeft - $this.outerWidth() / 2);
                style['margin-left'] = settings.marginLeft;
            } else if ('right' === settings.left) {
                style['right'] = settings.marginLeft;
            } else { //left
                style['left'] = settings.marginLeft;
            }
            
            var IE6Position = function () {
                if ('middle' === settings.top) {
                    $this.css('top', $window.scrollTop() + parseInt(($window.height() - $this.outerHeight()) / 2));
                } else if ('bottom' === settings.top) {
                    $this.css('top', $window.height() - $this.outerHeight() - settings.marginTop + $window.scrollTop());
                } else {
                    $this.css('top', $window.scrollTop() + style['top']);
                }
            }
            
            if (isIE6) {
                $this.css(style).resize(function () {
                    IE6Position();
                });
                $window.scroll(function () {
                    IE6Position();
                }).resize(function () {
                    IE6Position();
                }).trigger('scroll');
            } else {
                $this.css(style);
            }
        });
        
        return this;
    }
})(jQuery);

