var draggableWindow;

/** Crossbrowser Drag Handler  http://www.webtoolkit.info/ */
var DragHandler = {

	// private property.
	_oElem : null,


	// public method. Attach drag handler to an element.
	attach : function(oElem) {
		oElem.onmousedown = DragHandler._dragBegin;
		// callbacks
		oElem.dragBegin = new Function();
		oElem.drag = new Function();
		oElem.dragEnd = new Function();
		oElem.itemDir = '';
		return oElem;
	},

	removeAttach : function(oElem) {
		oElem.onmousedown = function(){};
	},
	
	// private method. Begin drag process.
	_dragBegin : function(e) {
		currentlyDragging = true;
		var oElem = DragHandler._oElem = this;

		if (isNaN(parseInt(oElem.style.left))) { oElem.style.left = '0px'; }
		if (isNaN(parseInt(oElem.style.top))) { oElem.style.top = '0px'; }

		var x = parseInt(oElem.style.left);
		var y = parseInt(oElem.style.top);

		e = e ? e : window.event;
		oElem.mouseX = e.clientX;
		oElem.mouseY = e.clientY;

		oElem.dragBegin(oElem, x, y);

		document.onmousemove = DragHandler._drag;
		document.onmouseup = DragHandler._dragEnd;
		return false;
	},


	// private method. Drag (move) element.
	_drag : function(e) {
		var oElem = DragHandler._oElem;

		var x = parseInt(oElem.style.left);
		var y = parseInt(oElem.style.top);

		e = e ? e : window.event;
		oElem.style.left = x + (e.clientX - oElem.mouseX) + 'px';
		oElem.style.top = y + (e.clientY - oElem.mouseY) + 'px';

		oElem.mouseX = e.clientX;
		oElem.mouseY = e.clientY;

		oElem.drag(oElem, x, y);

		return false;
	},


	// private method. Stop drag process.
	_dragEnd : function() {
		currentlyDragging = false;
		var oElem = DragHandler._oElem;

		var x = parseInt(oElem.style.left);
		var y = parseInt(oElem.style.top);

		oElem.dragEnd(oElem, x, y);

		document.onmousemove = null;
		document.onmouseup = null;
		DragHandler._oElem = null;
	}
};

function attachDrag() {
	draggableWindow = DragHandler.attach(document.getElementById('floatingWindow'));
	draggableWindow.dragEnd = fitWindow;
}

function removeDrag() {
	document.getElementById('floatingWindow').onmousedown = '';
}

function showDraggableWindow(x, y, w, title, activeId) {

	var container = document.getElementById('floatingWindow');
	
	//do this first so the content resizes the div
	container.style.left = x+"px";
	container.style.top = y+"px";
	container.style.width = w+"px";
	
	document.getElementById('floatingWindowTitleCaption').innerHTML = title;
	
	var divs = document.getElementById('floatingWindowContent').childNodes;
	for ( var i = 0 ; i < divs.length ; i++ ) {
		if(divs[i].style) {
			if(divs[i].id == activeId) {
				divs[i].style.display = 'block';
			} else {	
				divs[i].style.display = 'none';
			}
		}
	}
	
	//hack - IE doesn't respect the display property of the color selector
	//when user clicks "close" button and then opens another window
	//color selector was still visible
	if(activeId=='textEditorContainer') {
		setTimeout('hackColorBox(\'block\')', 0);
	} else {
		setTimeout('hackColorBox(\'none\')', 0);		
	}
	
	container.style.display = 'block';
	

	fitWindow();
}


function hackColorBox(style) {
	if(document.getElementById('colorWrapper')) {
		document.getElementById('colorWrapper').style.display=style;
	}
}


function fitWindow() {
	
	draggableWindow = null; //reset the dragging

	var container = document.getElementById('floatingWindow');
	var pos = findPos(container);
	var divLeft = pos[0];
	var divTop = pos[1];
	var divHeight = container.offsetHeight;
	var divWidth = container.offsetWidth;
	
	var docWidth = 0, docHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		docWidth = window.innerWidth;
		docHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		docWidth = document.documentElement.clientWidth;
		docHeight = document.documentElement.clientHeight;
	}

	var scrollX = 0, scrollY = 0;
	if( typeof( window.pageYOffset ) == 'number' ) {
		//Netscape compliant
		scrollY = window.pageYOffset;
		scrollX = window.pageXOffset;
	} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
		//DOM compliant
		scrollY = document.body.scrollTop;
		scrollX = document.body.scrollLeft;
	} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
		//IE6 standards compliant mode
		scrollY = document.documentElement.scrollTop;
		scrollX = document.documentElement.scrollLeft;
	}
	
	//don't let it float off the right
	if(divLeft + divWidth > docWidth) {
		var moveLeft = divLeft + divWidth - docWidth + 20;
		container.style.left = parseInt(container.style.left) - moveLeft +"px";
	}
		
	//scroll the window down if it floats off the bottom
	var newY = divTop + divHeight - docHeight + 20;
	if(newY > scrollY) window.scrollTo(scrollX, newY);
}



function closeDraggableWindow() {
	highlightOther('addTextMarker', false);
	document.getElementById('floatingWindow').style.display = 'none';
}


