//g*全局
function $(s){if(document.getElementById){return eval('document.getElementById("' + s + '")');}else{return eval('document.all.' + s);}}
var gIsIE = (document.all) ? true : false;
var gIsIE6 = gIsIE && ([/MSIE (\d)\.0/i.exec(navigator.userAgent)][0][1] == 6);
var gClass = {
	create: function() {
		return function() { this.initialize.apply(this, arguments); }
	}
}
var gExtend = function(destination, source) {
	for (var property in source) {
		destination[property] = source[property];
	}
}
var gBind = function(object, fun) {
	return function() {
		return fun.apply(object, arguments);
	}
}
var gEach = function(list, fun){
	for (var i = 0, len = list.length; i < len; i++) { fun(list[i], i); }
};
var gContains = function(a, b){
	return a.contains ? a != b && a.contains(b) : !!(a.compareDocumentPosition(b) & 16);
}

var Window = gClass.create();
Window.prototype = {
  initialize: function(options) {
    var id;
    var optionIndex = 0;
    if(arguments.length > 0) {
      if(typeof arguments[0] == "string" ){
        id = arguments[0];
        optionIndex = 1;
      }
      else
        id = arguments[0] ? arguments[0].id : null;
    }
    if(!id)
      id = "window_" + new Date().getTime();
			
		this.SetOptions(options);
		this.id = id;
		this.CreateWindow(id,this.options);
		this.Box = $(id);//显示层
		this.Fixed = !!this.options.Fixed;
		this.Over = !!this.options.Over;
		this.Center = !!this.options.Center;
		this.onShow = this.options.onShow;
		this.Box.style.zIndex = 2000;
		this.Box.style.display = "none";
		this.Opacity=10;
		
		//兼容ie6用的属性
		if(gIsIE){
			this._top = this._left = 0; this._select = [];
			this._fixed = gBind(this, function(){ this.Center ? this.SetCenter() : this.SetFixed(); });
		}
		Windows.register(this);
  },
	//创建DIV
  CreateWindow: function(id,options) {
		var d = document.createElement("div");
		d.id = id;
		d.className = 'dialog';
		d.style.width = (options['width']+32)+'px';
		//d.style.display = 'none';
		d.innerHTML = '\
		<div class="mac_os_x_close" id="window_1272436192843_close" onclick="Windows.close(\''+ id +'\', event)"></div>\
		<table class="table_window" style = "cursor:move;" id="'+ id +'_row1" width="'+(options['width']+32)+'">\
			<tbody>\
				<tr>\
					<td class="mac_os_x_nw"></td>\
					<td class="mac_os_x_n"><span class="mac_os_x_title" id="'+id+'_title">&nbsp;'+options['title']+'&nbsp;</span></td>\
					<td class="mac_os_x_ne"></td>\
				</tr>\
			</tbody>\
		</table>\
		<table class="table_window" id="'+ id +'_row2" width="'+(options['width']+32)+'">\
			<tbody>\
				<tr>\
					<td class="mac_os_x_w"></td>\
					<td class="mac_os_x_content" valign="top"><iframe id="'+ id +'_content" name="window_1272436192843_content" src="'+options['url']+'" frameborder="0" width="'+options['width']+'" height="'+options['height']+'"></iframe></td>\
					<td class="mac_os_x_e"></td>\
				</tr>\
			</tbody>\
		</table>\
		<table class="table_window" id="'+ id +'_row3" width="'+(options['width']+32)+'">\
			<tbody>\
				<tr>\
					<td class="mac_os_x_sw"></td>\
					<td class="mac_os_x_s"><div class="status_bar" id="window_1272436192843_bottom"><span style="FLOAT: left; WIDTH: 1px; HEIGHT: 1px"></span></div></td>\
					<td class="mac_os_x_sizer" id="window_1272436192843_sizer"></td>\
				</tr>\
			</tbody>\
		</table>\
		';
		document.body.insertBefore(d, document.body.childNodes[0]);
		
		$(id+'_row1').onmousedown = function (a) {
			var d = document;
			var win_ = $(id);
			if (!a) a = window.event;
			
			var x = a.clientX + d.body.scrollLeft - parseInt(win_.style.marginLeft);
			var y = a.clientY + d.body.scrollTop - parseInt(win_.style.marginTop);
			d.ondragstart = "return false;"
			d.onselectstart = "return false;"
			d.onselect = "document.selection.empty();"
	
			if (this.setCapture) this.setCapture();
			else if (window.captureEvents) window.captureEvents(Event.MOUSEMOVE | Event.MOUSEUP);
	
			d.onmousemove = function (a) {
				if (!a) a = window.event;
				win_.style.marginLeft = a.clientX + d.body.scrollLeft - x;
				win_.style.marginTop = a.clientY + d.body.scrollTop - y;
			}
	
			d.onmouseup = function () {
				if (this.releaseCapture) this.releaseCapture();
				else if (window.captureEvents) window.captureEvents(Event.MOUSEMOVE | Event.MOUSEUP);
				d.onmousemove = null;
				d.onmouseup = null;
				d.ondragstart = null;
				d.onselectstart = null;
				d.onselect = null;
			}
		}
  },
  //设置默认属性
  SetOptions: function(options) {
    this.options = {//默认值
			Over:	false,//是否显示覆盖层
			Fixed:	false,//是否固定定位
			Center:	true,//是否居中
			onShow:	function(){}//显示时执行
		};
    gExtend(this.options, options || {});
  },
  //兼容ie6的固定定位程序
  SetFixed: function(){
		this.Box.style.top = document.documentElement.scrollTop - this._top + this.Box.offsetTop + "px";
		this.Box.style.left = document.documentElement.scrollLeft - this._left + this.Box.offsetLeft + "px";
		
		this._top = document.documentElement.scrollTop; this._left = document.documentElement.scrollLeft;
	},
		//兼容ie6的居中定位程序
	SetCenter: function(){
		var top=document.documentElement.scrollTop;
		var left=document.documentElement.scrollLeft;
		if(top==0){
			top=document.body.scrollTop; 
			left=document.body.scrollLeft;     
		}
		
		this.Box.style.marginTop = top - this.Box.offsetHeight / 2 + "px";
		this.Box.style.marginLeft = left - this.Box.offsetWidth / 2 + "px";
  },
  //显示
  showCenter: function(options) {
		if(Windows.windows.length>1){
			win=Windows.windows[0];
			Windows.windows=[win];
			return;
		}
		//固定定位
		this.Box.style.position = this.Fixed && !gIsIE ? "fixed" : "absolute";
		//覆盖层
		this.Box.style.display = "block";
		//居中
		if(this.Center){
			//设置margin
			var top=document.documentElement.scrollTop;
			var left=document.documentElement.scrollLeft;
			if(top==0){
				top=document.body.scrollTop; 
				left=document.body.scrollLeft;     
			}
			this.Box.style.marginTop = top - this.Box.offsetHeight / 2 + "px";
			this.Box.style.marginLeft = left - this.Box.offsetWidth / 2 + "px";
			
			this.Box.style.top = this.Box.style.left = "50%";
		}
		//兼容ie6
		if(gIsIE6){
			if(!this.Over){
				//没有覆盖层ie6需要把不在Box上的select隐藏
				this._select.length = 0;
				gEach(document.getElementsByTagName("select"), gBind(this, function(o){
					if(!gContains(this.Box, o)){ o.style.visibility = "hidden"; this._select.push(o); }
				}))
			}
			//设置显示位置
			this.Center ? this.SetCenter() : this.Fixed && this.SetFixed();
			//设置定位
			this.Fixed && window.attachEvent("onscroll", this._fixed);
		}
		this.onShow();
		with(this.Box.style){
			//设置透明度
			gIsIE ? filter = "alpha(opacity:" + this.Opacity + ")" : opacity = this.Opacity / 100;
		}
		//alert(1);
		gTimer=setInterval("win.Starts()",100);
		//setTimeout("win.Starts()",5000);
		//alert(2);
  },
  //渐隐
  Fade: function() {
		if(win.Opacity>20)win.Opacity-=10;
		else{
			clearInterval(gTimer);
			this.Box.style.display = "none";
			if(gIsIE){
				window.detachEvent("onscroll", this._fixed);
				gEach(this._select, function(o){ o.style.visibility = "visible"; });
			}
			Windows.unregister(win);
			//document.body.removeChild(this.Box);
			//win=null;
			return;
		}
		with(this.Box.style){
			gIsIE ? filter = "alpha(opacity:" + win.Opacity + ")" : opacity = win.Opacity / 100;
		}
	},
  //渐现
  Starts: function() {
		if(win.Opacity<100)win.Opacity+=10;
		else{
			clearInterval(gTimer);
			with($(win.id+'_title').style){
				gIsIE ? filter = "alpha(opacity:100)" : opacity = 1;
			}
		}
		with(this.Box.style){
			gIsIE ? filter = "alpha(opacity:" + win.Opacity + ")" : opacity = win.Opacity / 100;
		}
	},
  //关闭
  close: function() {
		//alert(id);
		//alert(win.Opacity);
		if(win.Opacity<100)clearInterval(gTimer);
		gTimer=setInterval('win.Fade()',100);
  },
  // Sets window size
  setSize: function(width, height, useEffect) {
    var width = parseFloat(width);
    var height = parseFloat(height);
		$(win.id).style.width = (width+32)+'px';
		$(win.id).style.height = (height+70)+'px';
		$(win.id+'_row1').style.width = (width+32)+'px';
		$(win.id+'_row2').style.width = (width+32)+'px';
		$(win.id+'_row3').style.width = (width+32)+'px';
		$(win.id+'_content').style.width = width+'px';
		$(win.id+'_content').style.height = height+'px';
  },
  _recenter: function(event) {     
		//居中
		if(this.Center){
			this.Box.style.top = this.Box.style.left = "50%";
			//设置margin
			if(this.Fixed){
				this.Box.style.marginTop = - this.Box.offsetHeight / 2 + "px";
				this.Box.style.marginLeft = - this.Box.offsetWidth / 2 + "px";
			}else{
				this.SetCenter();
			}
		}
  }
};
var Windows = {
	windows: [],
  register: function(win) {
    this.windows.push(win);
  },
  unregister: function(win) {
    var results = [];
		for(var i = 0;i <this.windows.length;i++){
      if(this.windows[i].id != win.id)results.push(this.windows[i]);
		}
		this.windows = results;
  },
  close: function(id, event) {
		for(var i = 0;i <this.windows.length;i++){
			if(this.windows[i].id == id){
				//alert(id);
				win = this.windows[i];
				win.close();
				break;
			}
		}
  }
}