/*
 * Copyright (C) butor.com. All rights reserved.
 *
 * This software is published under the terms of the GNU Library General
 * Public License (GNU LGPL), a copy of which has been included with this
 * distribution in the LICENSE.txt file. 
 */

function centerDialog(sizeToContent) {
	var body = document.body;

	if (sizeToContent == null || sizeToContent) {
		var xx = document.createElement("div");
		body.appendChild(xx);
		var height = findPosY(xx);
		body.removeChild(xx);	
		window.resizeTo(body.offsetWidth +50, height);
		window.resizeBy(body.offsetWidth -body.clientWidth, height -body.clientHeight);
		window.resizeBy(10, 10);
	}

	// center on parent
	if (opener) {
		if (!document.all) {
			var x = opener.screenX + (opener.outerWidth - window.outerWidth) / 2;
			var y = opener.screenY + (opener.outerHeight - window.outerHeight) / 2;
			
		} else {
			var x = (screen.availWidth -body.offsetWidth) / 2;
			var y = (screen.availHeight -body.offsetHeight) / 2;
		}
		window.moveTo(x, y);
	}
}

//--------------------------
//----------------------------
var popupDoneEvent = null;
var popupArgs = null;
function openDialog(id, url, args, doneEvent, scroll) {
	popupDoneEvent = doneEvent;
	popupArgs = args;

	popup = window.open(url, id, 'toolbar=no, directories=no, location=no, status=no, statusbar=no, menubar=no, resizable=yes, scrollbars=' +(scroll?'yes':'no') +', width=100, height=100, modal=yes');

	if (!popup.opener) {
		popup.opener = self; 
	}
}

// disable / enable window with a barrier div ----------------------------
var _barrierDiv = null;
function disableWindow() {
	if (!document.createElement) {
		//TODO
		return true;
	}
	
	if (_barrierDiv == null) {
		_barrierDiv = document.createElement("div");
		_barrierDiv.id = "barrier";
		document.body.appendChild(_barrierDiv);
	}
	showhide('barrier', 1);
}
function enableWindow() {
	showhide('barrier', 0);
}

// drop down menu ----------------------------
function PopUp(id, caption, image, width, height) {
//<!--ridge inset outset -->
	document.write('<span class="popupTool" onclick="popIt(this, ' +"'" +caption +"'" +', ' +width +', ' +height +", getPopUpContent('" +id +"'));\">");
	document.write(caption);
	document.write('</span>');
}

var _popupTimeout = null
var _dropdown_iframe = null;
function popIt(triggerElem, caption, width, height, content) {
	if (!_dropdown_iframe) {
		_dropdown_iframe = createIFrame("dropdown_iframe");
		if (_dropdown_iframe == null) {
			return false;
		}
	}

	var doc = getIFrameDoc(_dropdown_iframe);
	if (doc == null) {
		return false;
	}

	if (_popupTimeout != null) {
		clearTimeout(_popupTimeout);
	}

	var div = '<html><head></head>'
	div += '<body style="margin:0px; padding:0px; border:1px outset #888;"><div style="height:100%;">'
	div += content;
	div += '</div></body></html>';

	doc.open();
	doc.write(div);
	doc.close();	

	pos = getElementXY(triggerElem);
	var heightOffset = 10;
	if (triggerElem.offsetHeight) {
		heightOffset = triggerElem.offsetHeight;
	}
	_dropdown_iframe.style.width = width;
	_dropdown_iframe.style.height = height;
	_dropdown_iframe.style.top = (pos[1] +heightOffset) +px;
	_dropdown_iframe.style.left = pos[0] +px;

	registerForDocClick(closePopUp);	
	showhide("dropdown_iframe", 1);	
	_popupTimeout = setTimeout("showhide('dropdown_iframe', 0)", 10000);

	return false;
}
/*
function popIt(triggerElem, caption, width, height, contentId, ev) {
	var div;
	var doc;

	if (!is_ie) {
		doc = document;
	} else {
		// IE stinks
		var popup = this.iePopup = window.createPopup();
		doc = popup.document;
		doc.open();
		//@import url(" + _editor_url + "plugins/ContextMenu/menu.css);
		doc.write("<html><head><style type='text/css'> html, body { padding: 0px; margin: 0px; overflow: hidden; border: 0px; }</style></head><body unselectable='yes'></body></html>");
		doc.close();
	}
	div = doc.createElement("div");
	if (is_ie)
		div.unselectable = "on";
	//div.oncontextmenu = function() { return false; };
	div.className = "htmlarea-context-menu";
	if (!is_ie)
		div.style.left = div.style.top = "0px";
	doc.body.appendChild(div);
	alert(getElement(contentId));
	div.appendChild(getElement(contentId));
	
	var pos = getPos(triggerElem);
	var x = pos.x;
	var y = pos.y;
//	var target = HTMLArea.is_ie ? ev.srcElement : ev.target;
//	var ifpos = getPos(self.editor._iframe);
//	var x = ev.clientX + ifpos.x;
//	var y = ev.clientY + ifpos.y;

	if (!is_ie) {
		var dx = x + div.offsetWidth - window.innerWidth + 4;
		var dy = y + div.offsetHeight - window.innerHeight + 4;
		if (dx > 0) x -= dx;
		if (dy > 0) y -= dy;
		div.style.left = x + "px";
		div.style.top = y + "px";
		showhide(contentId, 1);	
	} else {
		// determine the size (did I mention that IE stinks?)
		var foobar = document.createElement("div");
		foobar.className = "htmlarea-context-menu";
		foobar.innerHTML = div.innerHTML;
		document.body.appendChild(foobar);
		var w = foobar.offsetWidth;
		var h = foobar.offsetHeight;
		document.body.removeChild(foobar);
		this.iePopup.show(ev.screenX, ev.screenY, w, h);
	}

	return false;
}
*/

function toggleMenuX(triggerElem, menuName) {
	//TODO
	if (!is_ie) {
		toggleMenu(triggerElem, menuName);
		return;
	}
	
	// IE stinks
	var div;
	var doc;
		
	var popup = this.iePopup = window.createPopup();
	doc = popup.document;
	doc.open();
	//@import url(" + _editor_url + "plugins/ContextMenu/menu.css);
	doc.write("<html><head><style type='text/css'> html, body { padding: 0px; margin: 0px; overflow: hidden; border: 0px; }</style></head><body unselectable='yes'></body></html>");
	doc.close();

	div = doc.createElement("div");
	if (is_ie)
		div.unselectable = "on";
		
	//div.oncontextmenu = function() { return false; };
	div.className = "htmlarea-context-menu";
	if (!is_ie)
		div.style.left = div.style.top = "0px";
		
//	div.appendChild(doc.createTextNode("Hello"));
//	div.innerHTML = getElement(menuName).innerHTML;
doc.body.appendChild(getElement(menuName));
//	doc.body.appendChild(div);

	this.iePopup.show(0, 0, 100, 200);
	return;

//----------
	
	cancelDocClick(menuName);	
	mouseClickOnDocument(); // hide any poup
	
	var elem = getElement(menuName);
	var st = getStyle(menuName);
	flag = (st.visibility==(document.layers?"show":"visible"));
	if (flag || !triggerElem) {
		showhide(menuName, 0);
		return;
	}

	pos = getElementXY(triggerElem);
	var heightOffset = 10;
	if (triggerElem.offsetHeight) {
		heightOffset = triggerElem.offsetHeight;
	}
	st.top = (pos[1] +heightOffset) +px;
	st.left = pos[0] +px;
	
	var menuWidth = elem.offsetWidth;
	var docWidth = document.body.offsetWidth;

	if (pos[0]+ menuWidth > docWidth) {
		//st.left = (docWidth -menuWidth) +px;
		st.left = (pos[0] -menuWidth +triggerElem.offsetWidth) +px;
	}

	if (is_ie) {
		stopEvent(window.event);
	}
	registerForDocClick(menuName, function () {toggleMenu(triggerElem, menuName);});	
	showhide(menuName, 1);
}

function closePopUp() {
	showhide("dropdown_iframe", 0);	
}

function hello() {
	alert('hello');
}
