<!--
  function JsWindow(args){
//  SYSTEM      :  PJSWIN
//  PROGRAM     :  pascual
//  CREATE      :  2007.5.11
//  FILE NAME   :  jswin.js

//@sample 
//var jwina = new JsWindow({id: 'aa', width : 600, Height : 90, title :"Js-Window", body : 'abc<br>zzz'});
//var jwina = new JsWindow({id: 'aa', width : 600, Height : 90, title :"Js-Window"});
//var jwina = new JsWindow({id: 'aa', width : 600, Height : 90, title :"Js-Window", action : {URL: "down.htm", }});

//jwina.resizeable = false;
//jwina.dragable = false;
//jwina.setBody("NEW BODY");
//var winAction = {src:'down.htm', onComplete : function(response){alert(response);}}
//var jwinb = new JsWindow({id: 'bb', width : 300, Height : 300, title :"Js-BBB", action : winAction});
//jwinb.moveTo(10, 200);
//var winAction = {src:'down.htm', form: document.main}
//var winAction = {src:'down.htm'}
//jwinb.action(winAction);
    var id = (args && args.id)?args.id:this.getId("");
    this.resizeable = true;
    this.dragable = true;
    this.min_width = 150;
    this.min_height = 25;
    this._title = "";
    this._content = "";
    this.SizeRange = {min_width : 0, min_height : 0, max_width : 0, max_height : 0}
    this.DropOnClose = 0;
    this.HttpIframe = "";
    
    if(args && this.getId(id)) {
      delete args;
      throw new Error("JsWindow obj exist: [" +id+ "]");
      return false;
    }
    
    if(typeof(args.resizeable) != 'undefined') this.resizeable = args.resizeable;


    if(args && args.DropOnClose) this.DropOnClose = 1;
    var obj  = document.body.appendChild(document.createElement("DIV"));
    obj.setAttribute('id', id);
    this.setTag(obj.id, 0, 0);
    obj.thisWindow = this.thisDrag;
    if(args.head_imgs) {
      this.head_imgs = args.head_imgs;
    } else {
      this.head_imgs = new Array(
			"https://value.pchome.com.tw/img_ssl/up-l.gif",
			"https://value.pchome.com.tw/img_ssl/bn_small.gif",
			"https://value.pchome.com.tw/img_ssl/bn_big.gif",
			"https://value.pchome.com.tw/img_ssl/bn_close.gif",
			"https://value.pchome.com.tw/img_ssl/up-r.gif"
		);
    }
    var WinMain = this.toHtmlTag();
    WinMain.innerHTML = '\
<table width="100%" height="21" style="height:21px" border="0" cellpadding="0" cellspacing="0" ondblclick="JsWindow.prototype.resizeme(this)"> \
   <tr>\
      <td width="5" align="left" valign="bottom"><img id="JW_'+id+'_0" src="'+this.head_imgs[0]+'" width="5" height="20"></td>\
      <td valign="middle" align="center" style="font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 16px;color: #FFFFFF;background-color:#FF7F00;">' + 
((browser.msie)?'<iframe src="about:blank" scrolling="no" frameborder="0" \
style="z-index:-1;position:absolute; top:0px; left:5px;width:100%;height:100%;"> \
</iframe>':'') + 
'<span>&nbsp;</span>\
</td> \
      <td width="52" align="right" valign="middle" style="color: #FFFFFF;background-color:#FF7F00;">\
<img id="JW_'+id+'_1" src="'+this.head_imgs[1]+'" Height="15" hspace="3" border="0" align="absmiddle" style="cursor:pointer" onclick="JsWindow.prototype.jswin_resize(this,\'min\')">\
<img id="JW_'+id+'_2" src="'+this.head_imgs[2]+'" Height="15" border="0" align="absmiddle" style="cursor:pointer" onclick="JsWindow.prototype.resizeme(this)">\
<img id="JW_'+id+'_3" src="'+this.head_imgs[3]+'" Height="15" border="0" align="absmiddle" style="cursor:pointer" onclick="JSWin.getWinById(\''+id+'\').close();"></td>\
      <td width="5" align="right" valign="bottom"><img id="JW_'+id+'_4" src="'+this.head_imgs[4]+'" width="5" height="20"></td> \
   </tr> \
</table>';

    WinMain.innerHTML +=  '<div style="font-size: 13px;line-height: 18px;padding: 0px;border:1px #FF7F00 solid; background-color:#FFFFFF; ">' +
((browser.msie)?'<iframe src="about:blank" scrolling="no" frameborder="0" \
style="z-index:-1;position:absolute; top:21px; left:0px;width:100%;height:100%;"> \
</iframe>':'') + '</div>';

    this.titleBar = WinMain.childNodes[0];
    this.bindHeadImg();
    this.winBody = WinMain.childNodes[1];
    this.winBody.style.borderRightWidth = "2px";
    this.winBody.style.borderBottomWidth = "2px";
    this.min_height = this.winBody.offsetHeight + parseInt(this.titleBar.style.height);
    
    if(args && args.body) this.setBody(args.body);
    this.resize(parseInt(args.width), parseInt(args.Height));
    
    if(args && args.action) this.action(args.action);
    if(args && args.title) this.setTitle(args.title);
    this.beforeMouseDown(this.mousedown);
    this.afterDrag(this.DoafterDrag);
    obj.onmousemove=this.mousemove;
    return;
  }
  
  JsWindow.prototype = new DRAG();
  JsWindow.prototype.constructor = JsWindow;
  
  JsWindow.prototype.bindHeadImg = function() {  	
    var head_imgs = this.head_imgs;
    var tmpImg = new Image();
    var id = this.id;
    tmpImg.src = head_imgs[0];
    tmpImg.onload = function() {
      for(var i=0; i < head_imgs.length ; i ++) {
        if( this.src == head_imgs[i] && i < head_imgs.length) {
          $('JW_'+id+'_' + i).src = this.src;
      	  tmpImg.src= head_imgs[i + 1];
        }
      }
    }
  }
  
  JsWindow.prototype.mousemove = function(ee) {
    this.thisWindow().titleBar.rows[0].cells[1].style.cursor =(this.thisWindow().dragable)?"move":"default";	
    if(!this.thisWindow().resizeable) return;

    var off_info = this.thisWindow().get_se_resize(this, ee);
    var offx = (document.all)?(this.canvas.scrollLeft+event.clientX):(document.getElementById)?ee.pageX:"";
    var offy = (document.all)?(this.canvas.scrollTop+event.clientY):(document.getElementById)?ee.pageY:"";
    var ox = (offx >= off_info[0] && offx <= off_info[1]);
    var oy = (offy >= off_info[2] && offy <= off_info[3]);
    if( ox || oy ) {
      if(ox && oy) {
        this.style.cursor = "se-resize";
      } else if(ox) {
        this.style.cursor = "e-resize";
      } else {
        this.style.cursor = "s-resize";
      }
    } else {
      this.style.cursor = "default";
    }
  }
  JsWindow.prototype.DoafterDrag = function() {this.thisWindow().setMinPosition('afterDrag');}

  JsWindow.prototype.mousedown = function(ee) {
    if((ee && ee.which != 1) || (!ee && event.button != 1)) return false;
    var obj = this;
    var off_info = this.thisWindow().get_se_resize(this, ee);
    var offx = (document.all)?(this.canvas.scrollLeft+event.clientX):(document.getElementById)?ee.pageX:"";
    var offy = (document.all)?(this.canvas.scrollTop+event.clientY):(document.getElementById)?ee.pageY:"";
    var ox = (offx >= off_info[0] && offx <= off_info[1]);
    var oy = (offy >= off_info[2] && offy <= off_info[3]);
    
    if(this.thisWindow().resizeable && (ox || oy)) {
      this.do_resize = 1;
      this.save_x = offx;
      this.save_y = offy;
      this.save_mouseup = document.onmouseup;
      this.ox = ox;
      this.oy = oy;
      this.style.zIndex = DragRange.Z_index++;
      if(this.setCapture) {
        this.setCapture();
      }else if(window.captureEvents) {
        window.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP);
      }
      document.onmouseup = 
      function (ee) {
        JsWindow.prototype.setiframeMove();
        obj.do_resize = 0; 
        document.mouseup = obj.save_mouseup;
        if(obj.releaseCapture) {
           obj.releaseCapture();
        }else if(window.releaseEvents) {
           window.releaseEvents(Event.MOUSEMOVE|Event.MOUSEUP);
        }
        document.onmousemove = obj.save_mousemove;
      }
      
      obj.save_mousemove = document.onmousemove;
    
      document.onmousemove = function(ee) {
        if(browser.msie && event.button == 0) {
          document.onmouseup(ee);
          return false;
        }
        var offx = (document.all)?(obj.canvas.scrollLeft+event.clientX):(document.getElementById)?ee.pageX:"";
        var offy = (document.all)?(obj.canvas.scrollTop+event.clientY):(document.getElementById)?ee.pageY:"";
        if(obj.do_resize) {
          
          if(obj.ox) var n_w = offx - parseInt(obj.style.left);
          if(obj.oy) var n_h = offy - parseInt(obj.style.top);
          obj.thisWindow().resize(n_w, n_h);
        }
        return true;
      }
      
      if(window.captureEvents){
        var offinfo = getOffset(obj.thisWindow().winIframe);
        JsWindow.prototype.iframeMove(window, obj, 0 - offinfo.left, 0 - offinfo.top);
      }
      return false;
    } else {
      if(!this.thisWindow().dragable) return false;
      //for(var obj = event.srcElement; obj ; obj=obj.parentNode) {if(obj == this.thisWindow().titleBar) return true;}
      if(window.captureEvents && obj.thisWindow().winIframe){
      	obj.thisWindow().winIframe.mouse_top = obj.offY(ee) - parseInt(obj.style.top);
      }
      return true;
    }
    obj.do_resize = 0;
    return true;
  }

  with (JsWindow) {
    prototype._iframeSetMove = new Array();
    prototype.getId = function(id){
      if(!id) {
      	this.id = JSWin.getId();
        return this.id;
      }
      if(JSWin.all[id]) return true;
      this.id = id;
      JSWin.all[id] = this;
      return false;
    }
    
    prototype.thisWindow = function() {return JSWin.all[this.id];}
    
    prototype.display = function () {this.toHtmlTag().style.display = "";},
    
    prototype.close = function() {
      this.setMinPosition(false);
      this.toHtmlTag().style.display = "none";
      if(this.DropOnClose) DropObj(this.toHtmlTag());
    },
    
    prototype.jswin_resize = function(obj, type) {
      var tmpObj, Jswin = "";
      for(tmpObj = obj; tmpObj; tmpObj = tmpObj.parentNode ) {
      	if(tmpObj.tagName == 'DIV') {
      	  Jswin = tmpObj.thisWindow();
      	  break;
      	}
      }
      if(!Jswin) return false;
      
      if(type == 'close') {
        var obj = Jswin.toHtmlTag();
        Jswin.setMinPosition(false);
        Jswin.toHtmlTag().style.visibility = "hidden";
      	if(Jswin.DropOnClose) DropObj(Jswin.toHtmlTag());
      } else if( type == 'min' ) {
        if(Jswin.winBody.style.visibility != "hidden") {
          if(Jswin.winsize != 'max' && Jswin.winsize != 'min') Jswin.norm_info = Jswin.get_offset();
          Jswin.winBody.style.visibility = "hidden";
          Jswin.resize(Jswin.min_width, Jswin.min_height);
          Jswin.setMinPosition(true);
          Jswin.resizeable = false;
          Jswin.winsize = 'min';
        }else{
          Jswin.resizeable = true;
          Jswin.winsize = 'normal';
          Jswin.setMinPosition(false);
          Jswin.resize(parseInt(Jswin.norm_info.width), parseInt(Jswin.norm_info.Height));
          Jswin.winBody.style.visibility = "";
          Jswin.moveTo(Jswin.norm_info.left, Jswin.norm_info.top);
        }
      }
    }
    
    prototype.action = function(action) {
      var obj = this.winBody;
      var i = (browser.msie)?1:0;
      var src_height = "";
      
      //if(!this.HttpIframe) this.HttpIframe = new HTTPIFM({'onComplete' :completeCallback, 'onStart' : startCallback,'custom': "abc", 'set_timeout': 1000});
      if(this.HttpIframe) i++;
      while (obj.childNodes.length > i ) obj.removeChild(obj.lastChild);
      if(!this.HttpIframe) {
        action.parent = this.winBody;
        action.style = "width:100%;height:100%;";
      	//if(window.captureEvents) action.onload = "JsWindow.prototype.iframeMousemove(this);";
        this.HttpIframe = new HTTPIFM(action);
        this.winIframe = this.winBody.lastChild;
        (action.form)?this.HttpIframe.submit(action.form):this.HttpIframe.submit();
      }else{
        this.winIframe = this.winBody.lastChild;
        this.winBody.lastChild.style.display="";
        this.HttpIframe.setOpt(action);
        (action.form)?this.HttpIframe.submit(action.form):this.HttpIframe.submit();
      }
      
      if(this.winIframe) this.winIframe.style.height = parseInt(this.toHtmlTag().style.height) - parseInt(this.titleBar.style.height);
    }
    
    prototype.setBody = function(content) {
      var obj = this.winBody;
      var i = (browser.msie)?1:0;
      var src_height = "";
      this._content = content;
      if(this.winBody.style.height) src_height = this.winBody.style.height;
      this.winBody.style.height = "";
      if(this.HttpIframe) i ++;
      //this.HttpIframe = "";
      while (obj.childNodes.length > i ) {
      	obj.removeChild(obj.lastChild);
      }
      if(this.HttpIframe) {
        this.winBody.lastChild.style.display="none";
        this.winIframe = this.winBody.lastChild;
      }
      
      this.winBody.innerHTML += content;
      this.min_height = this.winBody.offsetHeight + parseInt(this.titleBar.style.height);
      if(src_height) this.winBody.style.height = src_height;
    }
    
    prototype.resizeme = function(obj) {
      var status = 0;
      while(obj.tagName != 'TABLE' && obj.parentNode) obj = obj.parentNode;
      if(!obj) return;
      var Jswin = obj.parentNode.thisWindow();
      Jswin.setMinPosition(false);
      Jswin.resizeable = true;
      if(!Jswin.winsize || Jswin.winsize =='normal' || Jswin.winsize =='min') {
        if(Jswin.winsize != 'min') Jswin.norm_info = Jswin.get_offset();
        var canvas = Jswin.toHtmlTag().canvas;
        Jswin.winBody.style.visibility = "";
        Jswin.moveTo(canvas.scrollLeft, canvas.scrollTop);
        Jswin.resize(canvas.clientWidth, canvas.clientHeight);
        Jswin.winsize = 'max';
      } else {
        Jswin.moveTo(Jswin.norm_info.left, Jswin.norm_info.top);
        Jswin.resize(parseInt(Jswin.norm_info.width), parseInt(Jswin.norm_info.Height));
        Jswin.winsize = 'normal';
      }
    }
    
    prototype.resize = function(w,h){
      if(!this.resizeable) return;
      if(w && this.SizeRange.min_width && w < this.SizeRange.min_width)  w = this.SizeRange.min_width;
      if(w && this.SizeRange.max_width && w > this.SizeRange.min_width)  w = this.SizeRange.max_width;
      if(h && this.SizeRange.min_height && h < this.SizeRange.min_height)  h = this.SizeRange.min_height;
      if(h && this.SizeRange.max_height && h > this.SizeRange.max_height)  h = this.SizeRange.max_height;
      
      if(w) {
        w = Math.max(this.min_width, w);
        this.tag.style.width = w;
      }
      if(h) {
      	h = Math.max(this.min_height, h);
        this.tag.style.height = h;
        this.winBody.style.height = h - parseInt(this.titleBar.style.height);
        if(this.HttpIframe) this.winIframe.style.height = h - parseInt(this.titleBar.style.height);
      }
    }

    prototype.setTitle = function(title){
      if(typeof(title) == 'undefined') return;
      var obj= this.toHtmlTag();
      if(!title) title = "&nbsp;";
      for(var i=0; i < obj.childNodes.length; i ++ ){
        if(obj.childNodes[i].tagName == 'TABLE') {
          var table = obj.childNodes[i];
          if(table.rows[0] && table.rows[0].cells[1]) {
            var column = table.rows[0].cells[1];
            for(var i=0; i < column.childNodes.length; i ++ ){
              if(column.childNodes[i].tagName == 'SPAN') {
                column.childNodes[i].innerHTML = title;
                this._title = title;
                return;
              }
            }
          }
          return;
        }
      }
    }
    
    prototype.get_se_resize = function (obj, ee) {
      var ret = new Array();
      ret[0] = parseInt(obj.style.left) + parseInt(obj.style.width) - 5;
      ret[1] = ret[0] + 10;
      ret[2] = parseInt(obj.style.top) + parseInt(obj.style.height) - 5;
      ret[3] = ret[2] + 10;
      return ret;
    }
    
    prototype.setMinPosition = function(type) {
      var i = 0, tmp = new Array();
      if(type=='afterDrag' && !JSWin.min_status[this.id]) {
        if(this.winsize!='min') return;
        JSWin.min_status[this.id] = this;
        this.minId = JSWin.minId ++;
      }
      //if(type=='afterDrag' && !JSWin.min_status[this.id]) return;
      var canvas = this.toHtmlTag().canvas;
      for(var id in JSWin.min_status) tmp[i++] = JSWin.min_status[id];
      if(type) {
      	i = 0;
      	if(type == true) this.minId = JSWin.minId ++;
      	
      	for(var id in JSWin.min_status) i++;
      	//tmp.sort(function(a,b) { return (a.minId - b.minId)})
      	var canvas = this.toHtmlTag().canvas;
    	if(type == 'afterDrag') {
    	  var off_info =  this.get_offset();
    	  i = 0;
    	  if((parseInt(off_info.top) >= (canvas.clientHeight + canvas.scrollTop -40))) {
    	     tmp.sort(function(a,b) { return (a.minId - b.minId)})
    	     this.scroll_fix(false);
    	     type=true;
    	     for(var j=0; j < tmp.length; j ++) if(tmp[j] == this) {i = j; break; }
    	  }
    	}
        var n_left = (this.min_width + 1)* i;
        var n_top = canvas.clientHeight + canvas.scrollTop - 20;
    	
    	if(type==true) {
    	  JSWin.min_status[this.id] = this;
      	  this.moveTo(n_left, n_top);
      	  this.scroll_fix(true);
      	}
      	if(!i) setTimeout('JSWin.fix_minWind()',100);
      }
      if(type != true) {
        delete JSWin.min_status[this.id];
        if(type == 'afterDrag' || (!type && this.winsize=='normal')) {
          tmp.sort(function(a,b) { return (a.minId - b.minId)})
          var n_top = canvas.clientHeight + canvas.scrollTop - 20;
          for(j=0; j < tmp.length; j ++) if(tmp[j] == this) break;
          
          for(j+=1; j < tmp.length; j ++) {
            var n_left = (this.min_width + 1)* (j - 1);
            tmp[j].scroll_fix(false);
            tmp[j].moveTo(n_left, n_top);
            tmp[j].scroll_fix(true);
          }
        }
        this.scroll_fix(type);
      }
    }
    
    prototype.setiframeMove = function(DocObj){
      var saveMoveObj = prototype._iframeSetMove;
      if(DocObj) {
        saveMoveObj[saveMoveObj.length] = DocObj;
      } else {
        if(!saveMoveObj.length) return;
        for(var i= saveMoveObj.length - 1; i >=0 ; i --) {
          if(saveMoveObj[i]._setSrcMove) {
            saveMoveObj[i].onmousemove= saveMoveObj[i]._mousemove;
            saveMoveObj.splice(i, 1);
          }
        }
      }
    }
    
    prototype.iframeMove = function(win, resizeObj, offx, offy){
      var iframes = win.document.getElementsByTagName("IFRAME");
      for(var i=0; i < iframes.length; i++) {
        var ifr = iframes[i];
        var offinfo = getOffset(ifr);
        var Doc = ifr.contentWindow.document;
        if(!Doc._setSrcMove) {
          Doc._mousemove = Doc.onmousemove;
          Doc._setSrcMove = true;
        }
        prototype.setiframeMove(Doc);
        Doc._offx = offx + offinfo.left;
        Doc._offy = offy + offinfo.top;
        Doc.onmousemove = 
        function(ee) {
          if(resizeObj.do_resize) {
            var n_w = (resizeObj.ox)?(ee.pageX + this._offx - 1): "";
            var n_h = (resizeObj.oy)?(ee.pageY + this._offy + resizeObj.childNodes[0].offsetHeight - 1):"";
            resizeObj.thisWindow().resize(n_w, n_h);
          }
        }
        Doc.onmouseup = function(ee) {
          resizeObj.do_resize = 0;
          prototype.setiframeMove(false);
          return fasle;
        }
        if(Doc.getElementsByTagName("IFRAME").length) JsWindow.prototype.iframeMove(ifr.contentWindow, resizeObj, Doc._offx, Doc._offy);
      }
    }
  }
  JSWin = {
    all : [], nameId : 0, min_status : [], minId : 0,
    getId : function(id) {return "JSWIN_" + this.nameId ++;},
    fix_minWind : function() {
      var i = 0;
      for(var id in JSWin.min_status){
        i++;
        var JSWIN = JSWin.min_status[id];
        var obj = JSWIN.toHtmlTag();
        if(obj.draging) continue;
        canvas = obj.canvas;
        var off_info =  JSWIN.get_offset();
        if(parseInt(off_info.top) != (canvas.clientHeight + canvas.scrollTop - 20) && 
            parseInt(off_info.top) > (canvas.clientHeight + canvas.scrollTop - 40)) {
          JSWIN.scroll_fix(false);
          JSWIN.moveTo("", canvas.clientHeight + canvas.scrollTop -20);
          JSWIN.scroll_fix(true);
        }
      }
      if(i) setTimeout('JSWin.fix_minWind()',100);
    },
    getWinById : function(id) {
    	return JSWin.all[id];
    }
  }
//-->

