var dragID = 0;
var startDragX = 0;
var startDragY = 0;
var isDrag = false;
var confirmCode = 0;
var dragType = 'drag';
function KRF_confirm(msg, code){
	d = layer('KRF_confirm');
	if (!d) return;
	m = layer('KRF_confirm_msg');
	if (m) m.write(msg);
	confirmCode = code;
	KRF_showconfirm('KRF_confirm')	
}
function KRF_showconfirm(id){
	d = layer(id);
	if (!d) return;
	posx = getScrollX() + 0.5*(getWindowWidth()  - d.getWidth() );
	posy = getScrollY() + 0.5*(getWindowHeight() - d.getHeight());
	d.moveTo(posx, posy);
	d.show();
}

function KRF_confirmOK(){
	d = layer('KRF_confirm');
	if (d) d.hide();

	if (confirmCode) 
	{
		eval(confirmCode);
	}
}

function KRF_confirmCancel(){
	d = layer('KRF_confirm');
	if (d) d.hide();
}

function KRF_startDrag(id){
	startDragX = mousex;
	startDragY = mousey;
	dragID = id;
}

function KRF_endDrag(){
	if (!dragID) return;
	d = layer(dragID); if (!d) return;
	
	dragID = 0;
}

function KRF_drag(){
	if (!dragID) return;
	if (mouseb == -10 || mouseb == 2) return;
	d = layer(dragID);
	if (!d) return;
	c = layer(dragID+'_content');
	d.css['overflow'] = 'hidden';
	c.css['overflow'] = 'hidden';
	  
	if (dragType == 't-resize' && c){
		h = d.getHeight();
		nt = d.getTop() + mousey - startDragY;
		b = d.getTop() + h;
		h = b - nt - 2;
		if (mousey < startDragY || h > 102){
			c.css['height'] = h+'px'; h+=2;
			d.css['height'] = h+'px';
		}
		d.moveTo(d.getLeft(), nt);
	}
	else if(dragType == 'b-resize' && c){
		t = d.getTop();
		h = d.getHeight();
		b = t + h + mousey - startDragY;
		h = b - t - 2;
		if (mousey > startDragY || h > 102){
			c.css['height'] = h+'px'; h+=2;
			d.css['height'] = h+'px';
		}
		else
		{
			d.moveTo(d.getLeft(), d.getTop() + mousey - startDragY);
		}
	}
	else if (dragType == 'r-resize' && c){
		w  = d.getWidth();
		nw = w + mousex - startDragX-2;
		if (nw > 202 || mousex > startDragX){
			c.css['width'] = nw+'px'; nw+=2; 
			d.css['width'] = nw+'px'; 
			
		}
		else d.moveTo(d.getLeft() + mousex - startDragX, d.getTop());
	}
	else if (dragType == 'l-resize' && c){
		w  = d.getWidth();
		nw = w - mousex + startDragX-2;
		if (nw > 202 || mousex < startDragX){
			c.css['width'] = nw+'px';nw+=2; 
			d.css['width'] = nw+'px'; 
		}
		d.moveTo(d.getLeft() + mousex - startDragX, d.getTop());
	}
	else{
		d.moveTo(d.getLeft() + mousex - startDragX, d.getTop() + mousey - startDragY);
	}
	
	d.css['overflow'] = 'auto';
	c.css['overflow'] = 'auto';
	startDragX = mousex;
	startDragY = mousey; 
}

function KRF_dragtype(id){
	if (dragID) return;
	d = layer(id);
	if (!d) return;
	t = d.getTop();
	b = t + d.getHeight();
	l = d.getLeft();
	r = l + d.getWidth();
	
	if (mousey-t < 3){
		d.css['cursor'] = 'n-resize';
		dragType = 't-resize';
		return;
	}
	else{
		if (b-mousey < 3){
			d.css['cursor'] = 'n-resize';
			dragType = 'b-resize';
			return;
		}
		else {
			if (mousex - l < 3){
				d.css['cursor'] = 'e-resize';
				dragType = 'l-resize';
				return;
			}
			else{
				if (r - mousex < 3){
					d.css['cursor'] = 'e-resize';
					dragType = 'r-resize';
					return;
				}
			}
		}
	}
	d.css['cursor'] = 'default';
	dragType = 0; 
}
