
// (c) Copyright Innet Corporation.
// this source is subject to the 9ipiao.com.
// All other rights reserved.

/// <summary>
/// 弹出层方式
/// </summary>
var LayerOpenMode = {
    /// <summary>
    /// 普通方式
    /// </summary>
    normal : 0,
    /// <summary>
    /// 变换方式
    /// </summary>
    popup  : 1,
    /// <summary>
    /// 宽度--方式
    /// </summary>
    sub: 2,
    /// <summary>
    /// 宽度++方式
    /// </summary>
    add  : 3,
    /// <summary>
    /// 透明度变化方式
    /// </summary>
    opacity: 4
}

function Layer(hasClsbtn) {
    /// <summary>
    /// 是否拥有关闭按钮
    /// </summary>
    this.hasClsbtn = false;
    /// <summary>
    /// 关闭按钮
    /// </summary>
    this.clsBtn = null;
    /// <summary>
    /// 此弹出层的父控件
    /// </summary>
    this.prt = null;
    /// <summary>
    /// 弹出层控件的顶级容器，被包含在prt中
    /// </summary>
    this.pnl = null;
    /// <summary>
    /// 弹出层的状态
    /// </summary>
    this.status = null;  
    
    
    if(hasClsbtn){
       this.hasClsbtn = true;
    }
}

Layer.prototype = {
       
        init : function(){
          if(!this.pnl)
             this.pnl = $crt('div');
        },
        setStyle : function(width,height,left,top,right, bottom,className){
             this.pnl.style.width = (width != null || width == "auto") ? "auto" : width + "px";
             this.pnl.style.height = (width != null || height == "auto") ? "auto" : height + "px";
             if(left != null) this.pnl.style.left = left + "px";
             if(top != null) this.pnl.style.top = top + "px";
             if(right != null) this.pnl.style.right = right + "px";
             if(bottom != null) this.pnl.style.bottom = bottom + "px";
             if(className!=null) this.pnl.className = className;
        },
		/// <summary>
		/// 弹出层
		/// </summary>
		/// <param name="mode">
		/// 弹出方式
		/// </param>
		/// <param name="prt">
		/// 父控件
		/// </param>
		/// <param name="width">
		/// 宽度
		/// </param>
		/// <param name="height">
		/// 高度
		/// </param>
		/// <param name="bgcolor">
		/// 背景颜色
		/// </param>
		/// <param name="clsNm">
		/// CSS样式
		/// </param>
		/// <param name="left">
		/// 左边距
		/// </param>
		/// <param name="top">
		/// 上边距
		/// </param>
		/// <param name="right">
		/// 右边距
		/// </param>
		/// <param name="bottom">
		/// 下边距
		/// </param>
		/// <param name="callback">
		/// 回调函数
		/// </param>
		/// <param name="clsBtnClsNm">
		/// 关闭按钮样式
		/// </param>
		open :  function (mode,prt,width,height,bgcolor,clsNm,left,top,right, bottom,callback,clsBtnClsNm) {
	      if(!parent && !_parent){
              throw new exception();
          }
          this.pnl.style.display="block";
          this.pnl.style.backgroundColor = bgcolor;
          this.pnl.style.width = (width == "auto") ? "auto" : width + "px";
          this.pnl.style.height = (height == "auto") ? "auto" : height + "px";
          this.pnl.className = clsNm;
          
          if(left != null) this.pnl.style.left = left + "px";
          if(top != null) this.pnl.style.top = top + "px";
          if(right != null) this.pnl.style.right = right + "px";
          if(bottom != null) this.pnl.style.bottom = bottom + "px";
          
          $app(this.pnl,prt);
          
          switch(mode){
              case LayerOpenMode.normal:
                   this.showByNormal(this.pnl);
                   break;
                   
              case LayerOpenMode.popup:
                   break;
              
              case LayerOpenMode.sub:
                   break;
                   
              case LayerOpenMode.add:
                   this.showByWidthArea(this.pnl,width,height,0,0,1);
                   break;
                   
              case LayerOpenMode.opacity:
                   this.showOrClsByOpacity(this.pnl,0,0.99,0.03);
                   break;
              default:
                   this.showByNormal(this.pnl);
                   break;
          }
          this.crtClsBtn(clsBtnClsNm); 
          this.status = 1;
          if(typeof(callback) == "function"){
              callback();
          }
		},
        
		/// <summary>
		/// 将HTML对象添加到弹出层中
		/// </summary>
		/// <param name="index">
		/// 
		/// </param>
		insertOf : function(obj) {
			 if(this.pnl){
			   $rmv(this.pnl);
			 }
			 $app(obj,this.pnl);
		},
		
        /// <summary>
		/// 创建关闭按钮
		/// </summary>
		/// <param name="css">
		///  关闭按钮的样式
		/// </param>
		crtClsBtn : function (clsNm) {
			 if(this.hasClsbtn){
          
                  this.clsBtn = $crt("div");
                  if(this.pnl)
                      $app(this.clsBtn,this.pnl);
                  this.clsBtn.className = clsNm;
                  sys.browser.setText(this.clsBtn,"关闭");
                  
                  this.clsBtn.onclick = function(evt){
                      this.parentNode.style.display = "none";
                  }
             }
		},
		/// <summary>
		/// 关闭弹出层
		/// </summary>
		close : function () {
			if(this.pnl)
                    this.pnl.style.display = "none";
		},
		/// <summary>
		/// 释放按钮
		/// </summary>
		dispose : function () {
			
		},

		/// <summary>
		/// 以普通的方式显示
		/// </summary>
		/// <param name="obj">
		/// 层对象
		/// </param>
		showByNormal : function (obj) {
			
		},

		/// <summary>
		/// 以颜色渐变的方式显示层
		/// </summary>
		/// <param name="obj">
		/// 层对象
		/// </param>
		/// <param name="start">
		/// 开始透明度
		/// </param>
		/// <param name="end">
		/// 结束透明度
		/// </param>
		/// <param name="end">
		/// 每次更新数值 
		/// </param>
		showOrClsByOpacity : function (obj, start, end, step) {
		   sys.browser.opacityStep(obj,start,end,step);
//		   obj.style.opacity = start;
//           var i = setInterval(function(){  
//               if(parseFloat(obj.style.opacity) < end){
//                  obj.style.opacity = parseFloat(obj.style.opacity) + step;
//               }
//               else{
//                  clearInterval(i);
//               }
//           },1);
		},

		/// <summary>
		/// 以宽度 ++/-- 的方式显示或关闭层
		/// </summary>
		/// <param name="obj">
		/// 层对象
		/// </param>
		/// <param name="maxW">
		/// 最大宽度
		/// </param>
		/// <param name="maxH">
		/// 最大高度
		/// </param>
		/// <param name="minW">
		/// 最小宽度
		/// </param>
		/// <param name="minH">
		/// 最小高度
		/// </param>
		/// <param name="mode">
		/// 变化的方式 0表示缩小（关闭） 1表示扩大（打开）
		/// </param>
		showByWidthArea : function (obj,maxW, maxH, minW, minH, mode) {
//		   if(mode == LayerOpenMode.sub) {
//		       obj.style.width = maxW;
//		       obj.style.height = maxH;
//		   }
//		   else{
//		       obj.style.width = minW;
//		       obj.style.height = minH;
//		   }
//           var i = setInterval(function(){
//               if(mode == LayerOpenMode.sub){
//                  if(obj.clientWidth > minW) 
//                  {
//                    obj.style.width = (obj.clientWidth - 10) + "px";
//                    if(obj.clientHeight > minH) 
//                  
//                    obj.style.height = (obj.clientHeight - 8) + "px"
//                  }
//               
//                  else if(obj.clientHeight > minH) obj.style.height = (obj.clientHeight - 8) + "px";
//                  else clearInterval(i);
//               }
//               else{
//                  if(obj.clientWidth < maxW) 
//                  {
//                    obj.style.width = (obj.clientWidth - 10) + "px";
//                    if(obj.clientHeight > maxH) 
//                  
//                    obj.style.height = (obj.clientHeight - 8) + "px"
//                  }
//               
//                  else if(obj.clientHeight > maxH) obj.style.height = (obj.clientHeight - 8) + "px";
//                  else clearInterval(i);
//               }
//               
//           },1);
           this.showOrClsByOpacity(obj,0,0.92,0.03);
		}
		

}

