
/* 
          class: FixBox
         author: shawl.qiu
        version: 2.0
           date: 2008-9-22
     
      use class: none
 use global ver: none
  
        updated: v2.1, shawl.qiu, 2008-9-23 21:10:47, 添加响应 window.onresize 功能.
        updated: ver, name, date, summary.
*/

window.FB_TOP_LEFT      = 1;
window.FB_TOP_CENTER    = 2;
window.FB_TOP_RIGHT     = 3;
window.FB_MIDDLE_LEFT   = 4;
window.FB_MIDDLE_CENTER = 5;
window.FB_MIDDLE_RIGHT  = 6;
window.FB_BOTTOM_LEFT   = 7;
window.FB_BOTTOM_CENTER = 8;
window.FB_BOTTOM_RIGHT  = 9;

FixBox.prototype.main = FixBox_main_f;

function FixBox(e_)
{
  var this_ = this;
  
  this_.e                 = ele_f(e_, 'FixBox(e_): "e_" is null!');
  
  this_.init_position     = 6;
  this_.custom_position_o = undefined; //{left: 100; top:200;}
  
  this_.x = {};
  this_.x.ele_size_o      = {width:this_.e.offsetWidth, height:this_.e.offsetHeight}; 
}/* function FixBox() */

/*  
    版本：2008-9-22
    作用：类的入口函数
必填参数：none
*/
function FixBox_main_f()
{/* shawl.qiu, void return, func: FixBox_set_position_ie, attach_event_f, FixBox_set_position_non_ie */
  var this_ = this;
  if(!this_.e) { return; }
  
  this_.e.style.display = 'block';
  
  attach_event_f(window, 'onresize', onscroll_f);
  
  if(ie_f())
  {
    FixBox_set_position_ie(this_);
    attach_event_f(window, 'onscroll', onscroll_f);
  }
  else
  {
    FixBox_set_position_non_ie(this_);
  }
  
  function onscroll_f(evt)
  {
    if(ie_f())
    {
      FixBox_set_position_ie(this_);
    }
    else
    {
      FixBox_set_position_non_ie(this_);
    }
  }
  
  if(this_.e.id)
  {
    var close_e = document.getElementById(this_.e.id+'_close');
    if(close_e!=null)
    {
      attach_event_f
      (
        close_e
        , 'onclick'
        , 
          function(evt)
          { 
            if(confirm('现在关闭吗?'))
            {
              this_.e.style.display='none';
            }            
          }
      );
    }
  }/* if(this_.e.id) */
  
  return;
}/* function FixBox_main_f() */

/*  
    版本：2008-9-23 9:36:43
    作用：针对IE浏览器设置显示位置
必填参数：this_
*/
function FixBox_set_position_ie(this_)
{  /* shawl.qiu, void return, func: viewport_f */
  var scroll_top_i  = document.documentElement.scrollTop;
  var scroll_left_i = document.documentElement.scrollLeft;
  
  this_.e.style.position = 'absolute';
  
  if(this_.custom_position_o)
  {
    this_.e.style.left  = this_.custom_position_o.left+scroll_left_i+'px';
    this_.e.style.top   = this_.custom_position_o.top+scroll_top_i+'px';
  }
  else
  {
    var left_i = top_i = 0;
    var screen_o    = viewport_f();
    var fix_width_i = -0;
    
    switch(this_.init_position)
    {
      case 1:
        top_i   = scroll_top_i;
        left_i  = scroll_left_i;
        break;
       
      case 2:
        left_i  = (screen_o.width - this_.x.ele_size_o.width+fix_width_i)/2+scroll_left_i;
        top_i   = scroll_top_i;
        break;
       
      case 3:
        left_i  = screen_o.width - this_.x.ele_size_o.width+fix_width_i+scroll_left_i;
        top_i   = scroll_top_i;
        break;
       
      case 4:
        left_i  = scroll_left_i;
        top_i   = (screen_o.height - this_.x.ele_size_o.height)/2+scroll_top_i;
        break;
       
      case 5:
        left_i  = (screen_o.width - this_.x.ele_size_o.width+fix_width_i)/2+scroll_left_i;
        top_i   = (screen_o.height - this_.x.ele_size_o.height)/2+scroll_top_i;
        break;
       
      case 6:
        left_i  = screen_o.width - this_.x.ele_size_o.width+fix_width_i+scroll_left_i;
        top_i   = (screen_o.height - this_.x.ele_size_o.height)/2+scroll_top_i;
        break;
       
      case 7:
        left_i  = scroll_left_i;
        top_i   = screen_o.height - this_.x.ele_size_o.height+scroll_top_i;
       break;
       
      case 8:
        left_i  = (screen_o.width - this_.x.ele_size_o.width+fix_width_i)/2+scroll_left_i;
        top_i   = screen_o.height - this_.x.ele_size_o.height+scroll_top_i;
        break;
       
      default:
        left_i  = screen_o.width - this_.x.ele_size_o.width+fix_width_i+scroll_left_i;
        top_i   = screen_o.height - this_.x.ele_size_o.height+scroll_top_i;
        break;      
    }/* switch(this_.init_position) */
    
    this_.e.style.left  = left_i+'px';
    this_.e.style.top   = top_i+'px';
  }/* if(this_.custom_position_o)/else */
  
  return;
}/* function FixBox_set_position_ie(this_) */

/*  
    版本：2008-9-23 9:36:40
    作用：针对非IE浏览器设置显示位置
必填参数：this_
*/
function FixBox_set_position_non_ie(this_)
{/* shawl.qiu, void return, func: viewport_f */
  this_.e.style.position = 'fixed';
  
  if(this_.custom_position_o)
  {
    this_.e.style.left  = this_.custom_position_o.left+'px';
    this_.e.style.top   = this_.custom_position_o.top+'px';
  }
  else
  {
    var left_i = top_i = 0;
    var screen_o    = viewport_f();
    var fix_width_i = -16;
    
    switch(this_.init_position)
    {
      case 1:
        break;
       
      case 2:
        left_i  = (screen_o.width - this_.x.ele_size_o.width+fix_width_i)/2;
        break;
       
      case 3:
        left_i  = screen_o.width - this_.x.ele_size_o.width+fix_width_i;
        break;
       
      case 4:
        top_i   = (screen_o.height - this_.x.ele_size_o.height)/2;
        break;
       
      case 5:
        left_i  = (screen_o.width - this_.x.ele_size_o.width+fix_width_i)/2;
        top_i   = (screen_o.height - this_.x.ele_size_o.height)/2;
        break;
       
      case 6:
        left_i  = screen_o.width - this_.x.ele_size_o.width+fix_width_i;
        top_i   = (screen_o.height - this_.x.ele_size_o.height)/2;
        break;
       
      case 7:
        top_i   = screen_o.height - this_.x.ele_size_o.height;
       break;
       
      case 8:
        left_i  = (screen_o.width - this_.x.ele_size_o.width+fix_width_i)/2;
        top_i   = screen_o.height - this_.x.ele_size_o.height;
        break;
       
      default:
        left_i  = screen_o.width - this_.x.ele_size_o.width+fix_width_i;
        top_i   = screen_o.height - this_.x.ele_size_o.height;
        break;      
    }/* switch(this_.init_position) */
    
    this_.e.style.left  = left_i+'px';
    this_.e.style.top   = top_i+'px';    
  }/* if(this_.custom_position_o)/else */
  
  return;
}/* function FixBox_set_position_non_ie(this_) */

/*  
    版本：2008-9-19
    作用：取HTML Element对象
必填参数：e_
*/
function ele_f(e_, prompt_s, document_o, no_alter_b)
{/* shawl.qiu code, return DOM Element, func:none */
  if(!prompt_s) prompt_s = "ele_f couldn't get the dom object!";
  if(!document_o) document_o = document;
  if(!no_alter_b) no_alter_b = false; 
  if((!e_)&&(!no_alter_b))
  {
    alert(prompt_s);
    return null ;
  };
  switch(e_.constructor)
  {
    case String: e_ = document_o.getElementById(e_); break;   
    case Array: e_ = e_[0]; break;
  } 
  if(e_==null&&no_alter_b==false){ alert(prompt_s); return null; }
  return e_;
}/* function ele_f(e_, prompt_s, document_o, no_alter_b) */

/*  
    版本：2008-9-19
    作用：可屏幕的可用大小相关值
必填参数：none
*/
function viewport_f() 
{/* shawl.qiu code, return object, func: none */
  var myWidth = 0, myHeight = 0;
  if(typeof(window.innerWidth ) == 'number' ) 
  {/* Non-IE */
    width_i = window.innerWidth; height_i = window.innerHeight;
  } 
  else if 
  (
    document.documentElement &&( document.documentElement.clientWidth || document.documentElement.clientHeight ) 
  ) 
  {/* IE 6 */
    width_i = document.documentElement.clientWidth; height_i = document.documentElement.clientHeight;
  } 
  else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) 
  {/* IE 4 */
    width_i = document.body.clientWidth; height_i = document.body.clientHeight;
  }
  return {
            width:width_i
            , height:height_i
            , max_width: width_i+document.documentElement.scrollLeft
            , max_height: height_i+document.documentElement.scrollTop
          };
}/* function viewport_f() */

function ie_f(){ return navigator.appName == 'Microsoft Internet Explorer';}

/*  
    版本：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){event_name_s = event_name_s.slice(2);e_.addEventListener(event_name_s, func_f, capture_b);}
  else if(document.attachEvent)
  {
    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) */
