var dragging;
var offset;
var mousePos;
var action=null;
function startDrag(e){
	createDragCover();
	if(this.parentNode.className=="dragWindow"){
		dragging = this.parentNode;
	}else{
		dragging = this;
	}
	if(readStyle(dragging,"position")=="static"){		
		dragging.style.position = "absolute"
		dragging.style.zIndex=5;
		dragging.style.backgroundColor="#FFFFFF";
		dragging.style.left = stripPX(readStyle(dragging.parentNode,"left")) + stripPX(readStyle(dragging.parentNode,"padding")) +stripPX(readStyle(dragging,"margin")) + "px";
		dragging.style.top = stripPX(readStyle(dragging.parentNode,"top")) + stripPX(readStyle(dragging.parentNode,"padding")) + stripPX(readStyle(dragging,"margin")) + "px";
		
		/*dragging.style.width = readStyle(dragging.parentNode,"width");
		dragging.style.height = readStyle(dragging.parentNode,"height");*/
		//alert(stripPX(readStyle(dragging.parentNode,"width"))- (4*stripPX(readStyle(dragging,"margin"))) + "px")
		dragging.style.width = stripPX(readStyle(dragging.parentNode,"width"))-(2*stripPX(readStyle(dragging.parentNode,"padding"))) - (2*stripPX(readStyle(dragging,"margin"))) + "px";
		dragging.style.height = stripPX(readStyle(dragging.parentNode,"height")) -(2*stripPX(readStyle(dragging.parentNode,"padding"))) - (2*stripPX(readStyle(dragging,"margin"))) + "px";
		dragging.style.margin="0";
		dragging.parentNode.parentNode.appendChild(dragging);
		if(originalPos==null){
			originalPos = new Array(stripPX(dragging.style.left),stripPX(dragging.style.top));
			originalSize= new Array(stripPX(dragging.style.width),stripPX(dragging.style.height));
		}
	}
	if(!isNaN(stripPX(readStyle(dragging,"top")))){
		getMousePos(e);
		setMouseOffset(e);
	};
	if(action==null){
		document.onmousemove = dragItem;
		document.onmouseup = clearDragging;
	}else{
		switch(action){
			case "max":
				 maximizeWin(dragging);
				 clearDragging();
			break;
			case "min":
				minimizeWin(dragging);
				clearDragging();
			break;
			case "anchor":
				anchorWindow(dragging);
				clearDragging();
			break;
			case "resize":				
				document.onmousemove = resizeItem;
				document.onmouseup = clearDragging;
			break;
		}
		action=null;		
	}
	return false;
}
function createDragCover(){
	var dragCover = document.createElement("div");
	gBody().appendChild(dragCover);
	dragCover.id="dragCover";
	dragCover.innerHTML="&nbsp;"
	return false
}
function getMousePos(e){
	var IE=false;						//Browser is IE
	var mString;						//Used to check button two in netscape browser
	var shiftPressed;					//Boolean to indicate whether shift is pressed
	var ctrlPressed;					//Boolean to indicate whether ctrl is pressed
	var altPressed;						//Boolean to indicate whether alt is pressed
	//--begin netscape button check
	if (document.layers){
		btn = e.which==3;
		evnt=e;
		mString =(e.modifiers+32).toString(2).substring(3,6);
		shiftPressed=(mString.charAt(0)=="1");
		ctrlPressed =(mString.charAt(1)=="1");
		altPressed  =(mString.charAt(2)=="1");
	}
	//--end netscape button check
	//--begin Firefox button check
	else if(navigator.userAgent.indexOf("Firefox") != -1){
		btn = e.which==3;
		evnt=e;	
		shiftPressed=evnt.shiftKey;
		ctrlPressed =evnt.ctrlKey;
		altPressed  =evnt.altKey;		
	}
	//--end Firefox button check
	//--begin Internet Explorer button check
	else{
		btn = event.button==2;
		evnt=event;
		shiftPressed=evnt.shiftKey;
		ctrlPressed =evnt.ctrlKey;
		altPressed  =evnt.altKey;
		IE=true;
	}	
	//--end Internet Explorer button check
	//--begin capture mouse position 
	if (IE) { // grab the x-y pos.s if browser is IE - used to position context menu
		tempX = event.clientX + document.body.scrollLeft
		tempY = event.clientY + document.body.scrollTop
	} else {  // grab the x-y pos.s if browser is NS - used to position context menu
		tempX = e.pageX
		tempY = e.pageY
	}  
	// catch possible negative values in NS4
	if (tempX < 0){tempX = 0}
	if (tempY < 0){tempY = 0}  
	mousePos = new Array(tempX,tempY);
	//self.status=mousePos;
	return false;
}
function dragItem(e){
	if(dragging != null){
		var opacity = 0.75;	
		var element = dragging;
		element.style.opacity = opacity
		element.style.filter = 'alpha(opacity=' + (opacity * 100) + ')'
		getMousePos(e)
		if(mousePos[0]+offset[0]>0 && mousePos[0]+offset[0]<(clientWidth - stripPX(dragging.style.width))){
			element.style.left = mousePos[0]+offset[0]+ "px";
		}else{
			if(mousePos[0]+offset[0]>0){
				element.style.left = clientWidth - stripPX(dragging.style.width) + "px";
			}else{
				element.style.left = 0 + "px";
			}
		}
		if(mousePos[1]+offset[1]>0 && mousePos[1]+offset[1]<(clientHeight - stripPX(dragging.style.height))){
			element.style.top = mousePos[1]+offset[1]+"px";	
		}else{
			if(mousePos[1]+offset[1]>0){
				element.style.top = (clientHeight - stripPX(dragging.style.height)) + "px";
			}else{
				element.style.top = 0+ "px";	
			}
		}
		lastPos = new Array(stripPX(element.style.left),stripPX(element.style.top))
	}
	return false;
}
function resizeItem(e){
	if(dragging != null){
		var opacity = 0.75;	
		var element = dragging;
		element.style.opacity = opacity
		element.style.filter = 'alpha(opacity=' + (opacity * 100) + ')'
		getMousePos(e)
		self.status =mousePos[0]
		if(mousePos[0]-stripPX(element.style.left)>originalSize[0] && mousePos[0]<clientWidth){
			self.status =mousePos[0]- stripPX(element.style.left)
			element.style.width = mousePos[0]- stripPX(element.style.left) + "px";
		}else{
			if(mousePos[0]-stripPX(element.style.left)>originalSize[0]){
				element.style.width = clientWidth - stripPX(element.style.left) + "px";
			}else{
				element.style.width = originalSize[0] + "px";
			}
		}
		if(mousePos[1] - stripPX(element.style.top)>originalSize[1] && mousePos[1]<clientHeight){
			self.status+=mousePos[1] - stripPX(element.style.top)
			element.style.height = mousePos[1] - stripPX(element.style.top)+"px";	
		}else{
			if(mousePos[1] - stripPX(element.style.top)>originalSize[1]){
				element.style.height = clientHeight - stripPX(element.style.top)+"px";
			}else{
				element.style.height = originalSize[1]+ "px";	
			}
		}
		lastSize = new Array(stripPX(element.style.width),stripPX(element.style.height))
	}
	resizeContents(element);
	return false;
}

function clearDragging(){
	if(dragging!=null){
		var opacity = 1;	
		var element = dragging;
		element.style.opacity = opacity;
		element.style.filter = 'alpha(opacity=' + (opacity * 100) + ')';
		dragging.className="dragWindow";
	}
	try{
		clearElements(document.getElementById("dragCover"));
	}catch(error){};
	document.onmousemove = getMousePos;//null;
	document.onmouseup = null;
	offset = null;
	mousePos=null;
	dragging = null;
	return false;
}
function readStyle(element, property) {
		if (element.style[property]) {
			return element.style[property]
		} else if (element.currentStyle) {
			return element.currentStyle[property]
		} else if (document.defaultView && document.defaultView.getComputedStyle) {
			var style = document.defaultView.getComputedStyle(element, null)
			return style.getPropertyValue(property)
		} else {
			return null
		}
}
function setMouseOffset(e){
	offset=new Array(stripPX(dragging.style.left) - mousePos[0],stripPX(dragging.style.top)-mousePos[1])
}
function resWin(){
	action="resize";
	return false;
}
function anchorWin(){
	action="anchor";
	return false;
}
function closeWin(){
	
	switch(findDragWin(this).id){
		case "editWindow":
			if(document.getElementById("btn_on_Notes")){
				document.getElementById("btn_on_Notes").onclick();
			}
		break;
		case "glossaryWindow":
			if(document.getElementById("btn_on_Glossary")){
				document.getElementById("btn_on_Glossary").onclick();
			}
		break;
		case "showMeWindow":
			clearElements(findDragWin(this));
		break;
		default:
			findDragWin(this).style.display="none";
		break;
	}
}

function maxWin(){
	action="max";
	return false;
}
function minWin(){
	action="min";
	return false;
}
document.onmousemove = getMousePos;