var operateBuffer={
		'id':0,
		'current':null,
		'usingButton':null,
		'operates':[],
		'minZ':500,
		'maxZ':500,
		'width':600,
		'height':480,
		'left':0,
		'top':0
};

function operate_gradual () {
	var operate=operate_get (operateBuffer.id);
	if (null != operate)
		operate.gradualProcess ();
}

function operate_to_front (id) {
	var operate=operate_get (id);
	if (null == operate)
		return;
	
	operate.toFront ();
}

function operate_max (id) {
	var operate=operate_get (id);
	if (null != operate)
		operate.max ();
}

function operate_close (id) {
	var operate=operate_get (id);
	if (null != operate)
		operate.close ();
}

function operate_get (id) {
	for (var i=0;i<operateBuffer.operates.length;i++) {
		if (operateBuffer.operates[i].id==id)
			return operateBuffer.operates[i];
	}
	return null;
}

function Operate (id) {
	this.id=id;
	this.div=null;
	this.main=null;
	this.window=null;
	
	this.button=null;
	this.isMax=false;
	this.show=false;
	this.ok=false;
	this.lock=false;
	
	this.gradual=0;
	this.handle=null;
	
	this.top=0;
	this.left=0;
	this.width=0;
	this.height=0;
	
	this.defaultHeight=null;
	this.defaultWidth=null;
	
	this._init=false;
	
	this._tmpId=0;
	
	this.init=function () {
		if (this._init)
			return true;
		this.div=_I('divOperate_'+this.id);
		if (null==this.div ||  undefined==this.div || 'div'!=this.div.nodeName.toLowerCase ()) {
			// create div
			this.div=document.createElement('div');
			this.div.id='divOperate_'+this.id;
			this.div.style.display='none';
			this.div.className='divOperate';
			this.div.style.zIndex= ++operateBuffer.maxZ;
			
			this.div.style.filter='Alpha(Opacity=0)';    // for IE
			this.div.style.opacity=0;					 // for FF
			
			if (this.defaultWidth)
				this.setDivWidth (this.defaultWidth);
			else
				this.setDivWidth (operateBuffer.width);
			
			if (this.defaultHeight)
				this.setDivHeight (this.defaultHeight);
			else
				this.setDivHeight (operateBuffer.height);
			
			var top=document.documentElement.scrollTop;
			var left=document.documentElement.scrollLeft;
			
			this.div.style.left=(left+30)+'px';
			this.div.style.top=(top+30)+'px';
			
			document.body.appendChild(this.div);
		}
		
		this._init=true;
		operateBuffer.operates.push(this);
		return true;
	};
	
	this.display=function (title, body) {
		this.init();
		
		var buf  = '<div id="operateHead_'+this.id+'" class="operateHead"><table width="100%"><tr><td id="operateTitle_'+this.id+'" class="operateTitle">'+title+'</td><td id="operateHandle_'+this.id+'" class="operateHandle">';
		
		if (!this.lock) {
			buf += '<input type="button" id="buttonOperateMax_'+this.id+'" value="';
			if (this.isMax)
				buf += lang.resume;
			else
				buf += lang.max;
			buf += '" onclick="operate_max ('+this.id+');" /> ';
		}
		
		buf += '<input type="button" value="'+lang.close+'" onclick="operate_close ('+this.id+');" /></td></tr></table></div>';
		buf += '<div id="operateMain_'+this.id+'" class="operateMain">';
	
		buf += body;
		this.div.innerHTML=buf;
		
		this.main=_I('operateMain_'+this.id);
		
		this.setMainWidth();
		this.setMainHeight();
		
		var top=document.documentElement.scrollTop + operateBuffer.top;
		var left=document.documentElement.scrollLeft + operateBuffer.left;
		
		//operateBuffer.top += 50;
		//operateBuffer.left += 50;
		
		this.div.style.left=(left+30)+'px';
		this.div.style.top=(top+30)+'px';
		
		if (!this.lock)
			_I('operateHead_'+this.id).ondblclick = function (event) { operate_max (operate_from_head (this).id);};
		
		_I('operateHead_'+this.id).onmousedown = function(event) { drag_start (event,operate_from_head (this).div.id); };
		this.div.onmousedown=function (event) {operate_to_front (operate_from_head (this).id);};
		
		if (null != operateBuffer.usingButton)
			this.button=operateBuffer.usingButton;
		
		if (this.handle) {
			window.clearInterval(this.handle);
		}
		
		this.div.style.display='block';
		this.show=true;
		this.ok=false;
		this.gradual=0;
		this._tmpId=operateBuffer.id;
		operateBuffer.id=this.id;
		this.handle=window.setInterval(operate_gradual, 1);
	};
	
	this.close=function () {
		this.init();
		
		if (null!=this.button) {
			this.button.className='usedButton';
			this.button=null;
		}
		
		if (this.handle) {
			window.clearInterval(this.handle);
		}
		
		this.div.style.display='block';
		this.show=false;
		this.ok=false;
		this.gradual=100;
		this._tmpId=operateBuffer.id;
		operateBuffer.id=this.id;
		this.handle=window.setInterval(operate_gradual, 1);
	};
	
	this.toFront=function () {
		this.div.style.zIndex= ++operateBuffer.maxZ;
	};
	
	this.setDivWidth=function (w) {
		this.div.style.width=_CVNU (w)+'px';
	};
	
	this.setDivHeight=function (h) {
		this.div.style.height=_CVNU (h)+'px';
	};
	
	this.setMainWidth=function () {
		if (null != this.main)
			this.main.style.width=(_CVNU (this.div.style.width)-20)+'px';
	};
	
	this.setMainHeight=function () {
		if (null != this.main)
			this.main.style.height=(_CVNU (this.div.style.height)-40)+'px';
	};
	
	this.setWidth=function (w) {
		w=_CVNU (w);
		this.div.style.width=w+'px';
		this.main.style.width=(w-20)+'px';
		this.window=_I('iframeOperateWindow_'+this.id);
		if (null != this.window) {
			this.window.style.width=(w-20)+'px';
		}
	};
	
	this.setHeight=function (h) {
		h=_CVNU (h);
		this.div.style.height=h+'px';
		this.main.style.height=(h-40)+'px';
		
		this.window=_I('iframeOperateWindow_'+this.id);
		if (null != this.window) {
			this.window.style.height=(h-40)+'px';
		}
	};
	
	this.max=function () {
		var button=_I('buttonOperateMax_'+this.id);
		
		if (this.isMax) {
			this.div.style.top=this.top;
			this.div.style.left=this.left;
			
			this.setWidth(this.width);
			this.setHeight(this.height);
			
			button.value=lang.max;
			this.isMax=false;
		} else {
			var w=document.documentElement.clientWidth;
			var h=document.documentElement.clientHeight;
			var top=document.documentElement.scrollTop;
			var left=document.documentElement.scrollLeft;
			
			if (w<600)
				w=600;
			if (h<500)
				h=500;
			
			w -= 50;
			h -= 50;
			
			this.top=this.div.style.top;
			this.left=this.div.style.left;
			this.width=this.div.style.width;
			this.height=this.div.style.height;
			
			this.setWidth(w);
			this.setHeight(h);
			
			this.div.style.left=(left+10)+'px';
			this.div.style.top=(top+10)+'px';
			
			button.value=lang.resume;
			this.isMax=true;
		}
	};
	
	this.closeButton=function () {
		return '<input type="button" value="'+lang.close+'" onclick="operate_close ('+this.id+');" />';
	};
	
	this.gradualProcess=function () {
		if (this.show)
			this.gradual += 10;
		else
			this.gradual -= 10;
		
		this.div.style.filter='Alpha(Opacity='+this.gradual+')';  		// for IE
		this.div.style.opacity=this.gradual/100;					 	// for FF
		if (this.gradual >= 100) {
			window.clearInterval(this.handle);
			this.gradual=100;
			operateBuffer.id=this._tmpId;
			this.ok=true;
		}
		if (this.gradual <= 0) {
			window.clearInterval(this.handle);
			this.gradual=0;
			this.div.innerHTML='';
			this.div.style.display='none';
			operateBuffer.id=this._tmpId;
			this.ok=true;
		}
		
	};
	
	this.openWindow=function (title, url) {
		this.init();
		
		var width=(_CVNU (this.div.style.width)-20)+'px';
		var height=(_CVNU (this.div.style.height)-40)+'px';
		
		var body='<iframe id="iframeOperateWindow_'+this.id+'" src="'+url+'" frameborder="0" scrolling="Auto" style="width:'+width+';height:'+height+'"></iframe>';
		this.display(title, body);
	};
}


function _CVNU (v) {
	if (!v || v==0)
		return 0;
	if ('string'!=typeof (v))
		return parseInt (v);
	return v.substr (0, v.indexOf ('px'));
}

function operate_from_head (div) {
	var id=div.id;
	var buf=id.indexOf ('_')+1;
	return operate_get (id.substr (buf, id.length-buf));
}

function display_operate (title, html) {
	if (null == operateBuffer.current) {
		operateBuffer.current=new Operate (operateBuffer.id);
	}
	operateBuffer.current.display (title, html);
}

function operate_close_button () {
	if (null == operateBuffer.current) {
		operateBuffer.current=new Operate (operateBuffer.id);
	}
	return operateBuffer.current.closeButton ();
}

function close_operate () {
	if (null == operateBuffer.current) {
		operateBuffer.current=new Operate (operateBuffer.id);
	}
	operateBuffer.current.close ();
}

function max_operate () {
	if (null == operateBuffer.current) {
		operateBuffer.current=new Operate (operateBuffer.id);
	}
	operateBuffer.current.max();
}

function is_operate_max () {
	if (null == operateBuffer.current) {
		operateBuffer.current=new Operate (operateBuffer.id);
	}
	return operateBuffer.current.isMax;
}

function set_click_button (button) {
	if (operateBuffer.usingButton!=null && operateBuffer.usingButton!=button) {
		operateBuffer.usingButton.className='usedButton';
	}
	
	if ('usingButton'==button.className) {
		button.className='usedButton';
		operateBuffer.usingButton=null;
		close_operate ();
		return false;
	}
	
	button.className='usingButton';
	operateBuffer.usingButton=button;
	return true;
}

function open_sub_window (title, url) {	
	if (null == operateBuffer.current) {
		operateBuffer.current=new Operate (operateBuffer.id);
	}
	operateBuffer.current.openWindow (title, url);
}

function close_sub_window () {
	if (null == operateBuffer.current) {
		operateBuffer.current=new Operate (operateBuffer.id);
	}
	operateBuffer.current.close ();
}





