var ua = navigator.userAgent;
var $IE = (navigator.appName == "Microsoft Internet Explorer");
var $Opera = ua.indexOf('Opera') != -1;
if ($Opera) {
  $IE = true;
  $Gecko = false;
  $Safari =  false;
}

function $_t(root,tag,id){
  var ar=root.getElementsByTagName(tag);
  for (var i=0;i<ar.length;i++){
    if (ar[i].id==id) return ar[i];
  }
  return null;
}
function $dele(o,fn,rv){
  var use_rv = typeof rv != 'undefined';
  return function (){
    var rfn = o[fn];
    var rt = rfn.apply(o, arguments);
    if (use_rv) {
      rt = rv;
    }
    return rt
  }
}

function $ge(e){
  return e || window.event;
}

function $addEL(n,e,l,b){
  if ($IE){
    if(n && e){
    if (!n["$__listener_"+e]){
      var lst=function (e){
        
        var f=arguments.callee;
        var ar=f.fList;
        
        e=$ge(e);
        for (var i=0;i<ar.length;i++){
          ar[i](e);
        }
      }
      lst.fList=[];     
      n["$__listener_"+e]=lst;
      n["on"+e]=n["$__listener_"+e];
      
    }
    var fList=n["$__listener_"+e].fList;
    fList[fList.length]=l;
    }
  } else {
    n.addEventListener(e,l,b);
  }
}
function $cancelEvent (e) {
  if ($IE) {
    e.returnValue = false;
    e.cancelBubble = true;
  } else
    e.preventDefault();
};
function $cpAttr(o,p){
  for (var i in p){
    var s=p[i];
    o[i]=s;
  }
  return o;
}
function $getValue(v,d){
  return v==null ? d : v;
}
var $gv=$getValue;

function parseIntSafe(s,b) {
  return parseInt(s, b || 10) || 0;
}
var $dom={
  getClientSize : function(n){
    if ($IE){
      var s= {x:n.clientLeft,y:n.clientTop};
      s.l=s.x;
      s.t=s.y;
      s.r=n.clientRight;
      s.b=n.clientBottom;
      
      s.w=n.clientWidth;
      s.h=n.clientHeight;
      
      return s;
    } else {
      var t=n.style;
      if (t.borderLeftWidth.length==0 || t.borderTopWidth.length==0 || t.borderRightWidth.length==0 || t.borderBottomWidth.length==0){
        
        var l=n.offsetWidth;
        t.borderLeftWidth="0px";
        l-=n.offsetWidth;
        
        var r=n.offsetWidth;
        t.borderRightWidth="0px";
        r-=n.offsetWidth;
        
        var o=n.offsetHeight;
        t.borderTopWidth="0px";
        o-=n.offsetHeight;
        
        var b=n.offsetHeight;
        t.borderBottomWidth="0px";
        b-=n.offsetHeight;
        
        t.borderLeftWidth=l+"px";
        t.borderTopWidth=o+"px";
        t.borderRightWidth=r+"px";
        t.borderBottomWidth=b+"px";
        
        var s={l:l,r:r,t:o,b:b,x:l,y:o};
        
        
        return s;
      } else {
        var s= {
            x: parseIntSafe(n.style.borderLeftWidth),
            y: parseIntSafe(n.style.borderTopWidth),
            r: parseIntSafe(n.style.borderRightWidth),
            b: parseIntSafe(n.style.borderBottomWidth)
          };
        s.l=s.x;
        s.t=s.y;
        return s;
      }
    }
  },
  
  
  
  getSize : function (n,withMargin){
    var c={
      x : n.offsetWidth != null ? n.offsetWidth : 0,
      y : n.offsetHeight != null ? n.offsetHeight : 0
    };
    if (withMargin) {
      var m=this.getMargin(n);
      c.x+=m.l+m.r;
      c.y+=m.t+m.b;
    }
    return c; 
  },
  
  setSize : function(elmt,x,y,withMargin){
    if ($IE){
      if (withMargin){        
        var m=this.getMargin(elmt);
        x-=m.l+m.r;
        y-=m.t+m.b;       
      }
      elmt.style.width=x;     
      elmt.style.height=y;      
    } else {
      var clientSize=this.getClientSize(elmt);
      var dx=clientSize.l+clientSize.r;
      
      var dy=clientSize.t+clientSize.b;
      
      elmt.style.width=x-dx+"px";
      elmt.style.height=y-dy+"px";
    }
  },
  getPosition : function (elmt,withMargin){
    var c;
    
    c={
      x:elmt.offsetLeft,
      y:elmt.offsetTop
    };
    if (withMargin){
      var m=this.getMargin(elmt);
      c.x-=m.l;
      c.y-=m.t;
    }
    
    return c;
  },
  setPosition : function (elmt,x,y,withMargin){
    if (withMargin){
    } 
    elmt.style.left=x+"px";
    elmt.style.top=y+"px";
  },
    
  
  setAlpha : function (n,a){
    return;
    n.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity="+a*100+");";
    n.style.opacity = a;
    n.style.MozOpacity = a;
  }
  
}
var $motion={
  smooth : function (s, e, t){
    if (t>1) t=1;
    return (e - s) * t + s;
  }
}
function PopUp(id, config){
  this.id=id;
  var c = this.config = config;
  c.width   = $gv(c.width,300);
  c.height  = $gv(c.height,200);
  c.minheight = $gv(c.minheight,20);
  c.bottom  = $gv(c.bottom,0);
  c.right   = $gv(c.right,20);
  c.display   = $gv(c.display,true);
  c.contentUrl= $gv(c.contentUrl,"");
  c.motionFunc= $gv(c.motionFunc,$motion.smooth);
  c.position  = {x:0,y:0};
  
  if(config.cssfile && !window['_popwin_css_added']){
    var css_link = document.createElement('link');
    css_link.rel="stylesheet";
    css_link.type="text/css";
    css_link.href=config.cssfile;
    document.getElementsByTagName('head')[0].appendChild(css_link);
    window['_popwin_css_added']=1;
  }
  
  var t=c.time;
  t.slideIn = $gv(t.slideIn,10);
  t.hold    = $gv(t.hold,10);
  t.slideOut  = $gv(t.slideOut,10);
  
  t.slideIn   *= 1000;
  t.hold    *= 1000;
  t.slideOut  *= 1000;
  
  this.container = document.body;
  this.popup = null;
  this.content = null;
  this.switchButton = null;
  
  this.moveTargetPosition = 0;
  this.startMoveTime = null;
  this.startPosition = null;
  
  this.status = PopUp.STOP;
  this.intervalHandle = null;
  
  this.mm = "max";
  
  this.imgMin = "images/mi/sm.gif";
  this.imgMax = "images/mi/big.gif";
}
PopUp.STOP = 0;
PopUp.MOVE_DOWN = 1;
PopUp.MOVE_UP = 2;
PopUp.SWITCH_TO_MIN = PopUp.MOVE_DOWN | 4;
PopUp.SWITCH_TO_MAX = PopUp.MOVE_UP | 8;

var __o={
  create : function (){
    var doc=document;
    var c=this.config;  
    /*
    #pop-message
      #pop-message_content
        a#closeButton.sys_btns
        a#minMaxButton.sys_btn.s_max
        #pop-message_content_c
           iframe#pop-message_content_iframe
    */
    var p = this.popup = doc.createElement("div");
    this.container.appendChild(p);
    
    var tbl_size=[c.width - 6, c.height - 6];
    
    p.id=this.id;
    p.style.cssText="position:absolute;z-index:9000;overflow:hidden;width:"+(c.width)+"px;height:"+(c.height)+"px;";
    
    if($IE){
      var iff = doc.createElement("iframe");
      iff.src="about:blank";
      iff.width = c.width;
      iff.height = c.height;
      p.appendChild(iff);
    }
    var t = this.content = doc.createElement("div");
    p.appendChild(t);
    
    t.id = this.id+"_content";
    t.className="pop_win";
    t.style.cssText="position:absolute;z-index:1;overflow:hidden;height:"+ (c.height - 6) +"px;width:"+ tbl_size[0] +"px;";
    $dom.setPosition(t,0,0,1);
    
    c.position.y = c.height;
    this.onresize();
    t.innerHTML = (c.closable?'<a id="closeButton" class="sys_btns" href="#" title="关闭">关闭<\/a>':'') + (c.minable?'<a id="minMaxButton" class="sys_btns" href="#" title="最小化">最小化<\/a>':'') +
                  '<div id="'+this.id+'_content_c" style="overflow:hidden;"><iframe id="'+this.id+'_content_iframe" src="'+c.contentUrl+'" frameborder="0" allowtransparency="yes" scrolling="no" width="'+tbl_size[0]+'" height="'+tbl_size[1]+'"><\/iframe><\/div>';
    
    if(c.closable){
        var btnClose = this.btnClose = $_t(t,'a',"closeButton");
        $addEL(btnClose,"click",$dele(this,"hide"),true);
        $addEL(btnClose,"click",$cancelEvent,true);
    }
    if(c.minable){
        var btnMinMax = this.btnMinMax = $_t(t,'a',"minMaxButton");
        $addEL(btnMinMax,"click",$dele(this,"switchMode"),true);
        $addEL(btnMinMax,"click",$cancelEvent,true);
    }
    
    this.content_c = $_t(t,'div',this.id+'_content_c');
    
    var container=$IE ? document.body : document.documentElement;
    
    var onresize_ = $dele(this,"onresize");
    $addEL(document.body,"resize",onresize_,true);
    
    this.__hackTimer=window.setInterval("__popup.onresize()",50);
    
    $addEL(container,"scroll",onresize_,true);
    
    onresize_();
    
    this.closeTimes = 0;
  },
  
  show : function (){
    
    if (!this.config.display) return;
    
    this.moveTargetPosition = 0;
    this.status = PopUp.MOVE_UP;
    this.startMove();
  },
  
  hide : function (){
    this.closeTimes++;
    this.moveTargetPosition = this.config.height;
    this.status = PopUp.MOVE_DOWN;
    this.startMove();
  },
  
  minimize : function (){
    this.mm = "min";
    this.moveTargetPosition = this.config.height - this.config.minheight - 6;
    this.status = PopUp.SWITCH_TO_MIN;
    this.startMove();
    this.__hackTimer=window.setInterval("__popup.onresize()",50);
    
    jQuery(this.btnMinMax).addClass("s_max").html("还原").attr("title", "还原");
  },
  
  maximize : function (){
    if (!this.config.display) return;
    
    this.mm = "max";
    this.moveTargetPosition = 0;
    this.status = PopUp.SWITCH_TO_MAX;
    this.startMove();
    this.__hackTimer=window.setInterval("__popup.onresize()",50);
    
    jQuery(this.btnMinMax).removeClass("s_max").html("最小化").attr("title", "最小化");
  },
  
  delayHide : function (){
    window.setTimeout("__popup.hide()",this.config.time.hold);
  },
  
  delayMin : function (){
    window.setTimeout("__popup.minimize()",this.config.time.hold);
  },
  
  delayMax : function (){
    window.setTimeout("__popup.maximize()",this.config.time.hold);
  },
  
  switchMode : function (){
    if (this.mm == "min"){
      this.maximize();
    } else {
      this.minimize();
    }
  },
  
  startMove : function (){
    this.stopMove();
    
    this.intervalHandle = window.setInterval("__popup.move()",100);
    
    this.startMoveTime = new Date().getTime();
    this.startPosition = this.config.position.y;
  },
  
  stopMove : function (){
    if (this.intervalHandle != null) window.clearInterval(this.intervalHandle);
    this.intervalHandle = null;
  },
  
  
  move : function (){
    
    var t = new Date().getTime();
    t = t - this.startMoveTime;
    
    var total = this.status & PopUp.MOVE_UP ? 
          this.config.time.slideIn : 
          this.config.time.slideOut;
    
    var y = this.config.motionFunc(this.startPosition, this.moveTargetPosition, t/total);
    this.config.position.y = y;
    this.onresize();
        
    if (t >= total){
      this.onFinishMove();
    }
  },
  
  onFinishMove : function (){
    this.stopMove();
    if ((this.status == PopUp.MOVE_UP && this.config.time.hold > 0) || ((this.closeTimes < 2) && this.status == PopUp.MOVE_DOWN)){
      this.delayMin();
      /*
      if(this.mm == "min"){
        this.delayMin();
      }else if(this.mm == "max"){
        this.delayMax();
      }
      */
    } else {
      if (this.__hackTimer!=null) window.clearInterval(this.__hackTimer);
    }
    this.status = PopUp.STOP;
  },
  
  onresize : function (){
    var c=this.config;
    var t=document.body;
    var dx=t.clientWidth + t.scrollLeft;
    
    var x = dx - c.right - c.width;
    
    var screenSize = getScreenSize();
    var y = screenSize.height + document.documentElement.scrollTop - c.height + c.position.y - c.bottom;
    
    $dom.setPosition(this.popup, x, y,1);
    $dom.setSize(this.popup, c.width, c.height-c.position.y);
    
    var h = (c.height - c.position.y);
    if(h > 6){h -= 6;}
    h += 'px';
    this.content && this.content.style && (this.content.style.height = h);
    this.content_c && this.content_c.style && (this.content_c.style.height = h);
  }
}
function getScreenSize(){
      var w = 0;
      var h = 0;
      if( typeof( window.innerWidth ) == 'number' ) {
        w = window.innerWidth;
        h = window.innerHeight;
     } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
       w = document.documentElement.clientWidth;
       h = document.documentElement.clientHeight;
     } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
       w = document.body.clientWidth;
       h = document.body.clientHeight;
     }
     return {width:w,height:h};
}

$cpAttr(PopUp.prototype,__o);
function readCookie(name)
{
  var cookieValue = "";
  var search = name + "=";
  if(document.cookie.length > 0)
  { 
    offset = document.cookie.indexOf(search);
    if (offset != -1)
    { 
      offset += search.length;
      end = document.cookie.indexOf(";", offset);
      if (end == -1) end = document.cookie.length;
      cookieValue = unescape(document.cookie.substring(offset, end))
    }
  }
  return cookieValue;
}

function writeCookie(name, value, hours)
{
  var expire = "";
  if(hours != null)
  {
    expire = new Date((new Date()).getTime() + hours * 3600000);
    expire = "; expires=" + expire.toGMTString();
  }
  document.cookie = name + "=" + escape(value) + expire + ";path=/";
}
