/**
 * @author      suches@btbtd.org
 * @date        2011-6-17 
 */ 
void function()
{
    function XFixPosition( $box, $params )
    {
        this.model = new Model();
        this.model.box = $box;
        this.model.initParams($params);
        
        this.view = new View( this.model );
    }
    
    XFixPosition.exec =
    function( $box, $params )
    {
        return new XFixPosition( $box, $params ).init();
    };
    
    XFixPosition.prototype = 
    {
        init:
        function()
        {
            if(!this.model.box) return null;
                        
            this.model.defaultPosition = this.model.getPosition();
            this.view.init();
            
            return this;
        },
        
        fix:
        function()
        {
            var _ = this;
            _.model.box.style.position = 'static';
            _.model.box.style.top = '0px';
            _.model.box.style.left = '0px';
            this.view.fixPosition()();
        }
    };
    
    function Model()
    {
        this.box;
        this.defaultPosition = { x: 0, y: 0};
    }
    
    Model.prototype =
    {
        initParams:
        function($params)
        {
            if(!$params) return;
            
            for( var k in $params ) this[k] = $params[k];
        },
        getPosition:
        function()
        {           
            var t = ele_pos_f(this.box);            
            return { x: t.left, y: t.top };
        }
          
    };
    
    function View($model)
    {
        this.model = $model;
    }
    
    View.prototype =
    {
        init:
        function()
        {
            
            attach_event_f
            (
                window, 
                'scroll',
                this.fixPosition()
            );
            
            attach_event_f
            (
                document.body, 
                'click',
                this.fixPosition()
            );
        },
        
        fixPosition:
        function()
        {            
            var _ = this;
            
            function anon( $evt )
            {
                //document.title = document.documentElement.scrollTop + ', ' +_.model.defaultPosition.y;
                
                if( document.documentElement.scrollTop > _.model.defaultPosition.y )
                {
                    if( window.XMLHttpRequest )
                    {
                        _.model.box.style.position = 'fixed';
                        _.model.box.style.left = _.model.defaultPosition.x + 'px';
                        _.model.box.style.top = '0';
                    }
                    else
                    {
                        _.model.box.style.position = 'absolute';
                        _.model.box.style.left = _.model.defaultPosition.x + 'px';
                        _.model.box.style.top = document.documentElement.scrollTop + 'px';
                    }
                }
                else
                {
                    _.model.box.style.position = 'static';
                    _.model.box.style.top = '0px';
                    _.model.box.style.left = '0px';
                }       
            }
            
            return anon;
        }
        
        
    };
    
/*  
    版本：2008-9-9
    作用：取某个元素的座标和大小
必填参数：arg_e
*/
function ele_pos_f(arg_e)
{/* shawl.qiu code, return object, func:none */
  var a = [];
  var t = arg_e.offsetTop, l = arg_e.offsetLeft, w = arg_e.offsetWidth, h = arg_e.offsetHeight; 
  while(arg_e = arg_e.offsetParent){ t += arg_e.offsetTop; l += arg_e.offsetLeft; } 
  return {top:t, left:l, width:w, height:h};
}/* end function ele_pos_f(arg_e) */
    
/*  
    版本：2008-9-22
    作用：兼容各浏览器的附加事件函数
必填参数：e_, event_name_s, func_f
*/
function attach_event_f(e_, event_name_s, func_f, capture_b)
{/* shawl.qiu, void return, func:none */
    if(document.addEventListener){e_.addEventListener(event_name_s, func_f, capture_b);}
    else if(document.attachEvent)
    {
        event_name_s = 'on'+event_name_s;
        if(e_[event_name_s]==null){e_[event_name_s] = func_f;} else{e_.attachEvent(event_name_s, func_f);}
    }
    return;
}/* end function attach_event_f(e_, event_name_s, func_f, capture_b) */
    
    window.XFixPosition = XFixPosition;
}();

