var m_modalDlgObject = {
	moving: 0,
	visible: 0,
	lastMouseX : 0,
	lastMouseY : 0,
	nextBtnId  : 0,
	buttonsWidth: 0
};

function startModalDlg(sizeX, sizeY, title, icoX, icoY)
{
	var background   = document.getElementById('modal-overlay');
	var modalDlg     = document.getElementById('modal-owner'),
		modalTitle   = document.getElementById("modal-title"),
		modalIcon    = document.getElementById("modal-icon"),
		modalClient  = document.getElementById("modal-client"),
		modalButtons = document.getElementById("modal-buttons");

	try { background.style.filter  = "alpha(opacity=60)"; } catch (e) {}
	try { background.style.opacity = 0.6; } catch (e) {}

	background.style.background = "#FFFFFF";
	background.style.zIndex     = 121;

	sizeX = parseInt(sizeX);
	sizeY = parseInt(sizeY);
	
	modalTitle.innerHTML = title;
	modalTitle.unselectable           = "on";   // ie
	modalTitle.style.MozUserSelect    = "none";	// moz
	modalTitle.style.KhtmlUserSelect  = "none";	// safari
	modalTitle.style.width            = (sizeX - ((icoX == 4 && icoY == 1) ? 50 : 100) + parseInt(modalClient.style.marginLeft)*2 - 20) + "px";
	modalTitle.style.left             = (icoX == 4 && icoY == 1) ? '30px' : '80px';

	modalIcon.style.backgroundPosition = "-"+(icoX*49)+"px -"+(icoY*49)+"px";
	modalIcon.style.display            = (icoX == 4 && icoY == 1) ? 'none' : 'block';

	modalClient.style.width  = sizeX + "px";
	modalClient.style.height = sizeY + "px";

//	modalButtons.style.top   = (sizeY+50) + "px";
	m_modalDlgObject.buttonsWidth += 10;
	modalButtons.style.left    = ((sizeX +  20) - m_modalDlgObject.buttonsWidth) / 2 + "px";
	modalButtons.style.width   = (m_modalDlgObject.buttonsWidth) + "px";

	modalDlg.style.width  = (sizeX +   6 + parseInt(modalClient.style.marginLeft)*2) + "px";
//	modalDlg.style.height = (sizeY + 110) + "px";

	modalDlg.style.left = (g_prevSize.w - (sizeX +  20))/2 + "px";
	modalDlg.style.top  = (g_prevSize.h - (sizeY + 110))/2 + g_prevSize.s + "px";
	modalDlg.style.display   = "block";
	m_modalDlgObject.visible = true;

	if (icoX == 4 && icoY == 1 && parseInt(modalClient.style.marginTop) > 10) modalClient.style.marginTop = "10px";
	resizeWindow();
}

function addModalBtn(text, code, forceWidth, forceId)
{
	var buttonHolder = document.getElementById("modal-buttons");
	
	var m_div = modal_createNormalButton(text, code, 
					(forceWidth) ? forceWidth : 150,
					(forceId   ) ? forceId    : null);

	buttonHolder.appendChild(m_div);
	m_modalDlgObject.buttonsWidth += parseInt(m_div.style.width) + 10;

	return m_div;
}

function modal_createButton(text, code, width, m_forceId, m_defaultLoc, img_url, img_h, img_w)
{
	var m_div = document.createElement("DIV");

	m_div.id = m_forceId;
	m_div.m_codeExecute = code;
	m_div.className = 'modal_btn';

	m_div.onclick      = modalButton_click;
	m_div.onmouseover  = modalButton_over;
	m_div.onmouseout   = modalButton_out;
	m_div.m_buttH      = img_h;
	m_div.m_defaultLoc = m_defaultLoc;

	m_padTop = Math.round((img_h-14)/2)-1;//Math.round((img_h-20)/3+6);

	m_div.style.width              = parseInt(width)+"px";		// required
	m_div.style.height             = img_h          +"px";
	m_div.style.lineHeight         = "16px";	

	m_div.innerHTML = 
			"<div id='"+m_div.id+"l' style='float:left; background: url("+img_url+") no-repeat 0px -"+((m_defaultLoc  )*img_h)+"px; width:"+img_w+"px; height:"+img_h+"px; vertical-align:middle;' unselectable='on'></div>"+
			"<div id='"+m_div.id+"c' style='float:left; background: url("+img_url+") repeat-x  0px -"+((m_defaultLoc+6)*img_h)+"px; width:"+(width-img_w*2)+"px; height:"+((typeof browser_IE_version != "undefined" && browser_IE_version && browser_IE_version < 7) ? img_h : (img_h-m_padTop))+"px; padding-top: "+m_padTop+"px;' unselectable='on'>"+text+"</div>"+
			"<div id='"+m_div.id+"r' style='float:left; background: url("+img_url+") no-repeat 0px -"+((m_defaultLoc+3)*img_h)+"px; width:"+img_w+"px; height:"+img_h+"px;' unselectable='on'></div>";

	m_div.setAttribute('unselectable', 'on');

	return m_div;
}

function modal_createSmallButton(text, code, width, m_forceId, m_defaultLoc, m_defaultImage)
{
	return modal_createButton(text, code, 
		(width       ) ? width : 150, 
		(m_forceId   ) ? m_forceId : "modal_btn_" + ++m_modalDlgObject.nextBtnId,
		(m_defaultLoc) ? m_defaultLoc-1 : 1,
		(m_defaultImage) ? m_defaultImage : local_server_URL+"images/modal_ui/button_s.gif", 35, 20);
}

function modal_createNormalButton(text, code, width, m_forceId, m_defaultLoc, m_defaultImage)
{
	return modal_createButton(text, code, 
		(width       ) ? width : 150, 
		(m_forceId   ) ? m_forceId : "modal_btn_" + ++m_modalDlgObject.nextBtnId,
		(m_defaultLoc) ? m_defaultLoc-1 : 1,
		(m_defaultImage) ? m_defaultImage : local_server_URL+"images/modal_ui/button_n.gif", 45, 20);
}

function modal_createHugeButton(text, code, width, m_forceId, m_defaultLoc, m_defaultImage)
{
	return modal_createButton(text, code, 
		(width       ) ? width : 150, 
		(m_forceId   ) ? m_forceId : "modal_btn_" + ++m_modalDlgObject.nextBtnId,
		(m_defaultLoc) ? m_defaultLoc-1 : 1,
		(m_defaultImage) ? m_defaultImage : local_server_URL+"images/modal_ui/button_h.gif", 60, 20);
}




function modalButton_out()
{
	document.getElementById(this.id+"l").style.backgroundPosition = "0px -"+(this.m_buttH*(this.m_defaultLoc  ))+"px";
	document.getElementById(this.id+"c").style.backgroundPosition = "0px -"+(this.m_buttH*(this.m_defaultLoc+6))+"px";
	document.getElementById(this.id+"r").style.backgroundPosition = "0px -"+(this.m_buttH*(this.m_defaultLoc+3))+"px";
}

function modalButton_over()
{
	document.getElementById(this.id+"l").style.backgroundPosition = "0px -"+(this.m_buttH*2)+"px";
	document.getElementById(this.id+"c").style.backgroundPosition = "0px -"+(this.m_buttH*8)+"px";
	document.getElementById(this.id+"r").style.backgroundPosition = "0px -"+(this.m_buttH*5)+"px";
}

function modalButton_click()
{
	if (this.m_codeExecute)
	{
		m_ret = false;
		try { m_ret = eval(this.m_codeExecute); } catch (e) { sayDebugMessage(e); }
		if (m_ret) return;
	}

	endModalDlg();
}

function endModalDlg()
{
	if (typeof m_toolTipShown != 'undefined' && m_toolTipShown.visible) editor_cancelToolTip(1);

	var background   = document.getElementById('modal-overlay');
	var modalDlg     = document.getElementById('modal-owner'),
		modalClient  = document.getElementById("modal-client"),
		modalButtons = document.getElementById("modal-buttons");

	background.style.background = "transparent";
	background.style.zIndex     = -1200;
	modalDlg  .style.display = "none";

	document.getElementById('modal-close').style.visibility = 'visible';

	m_modalDlgObject.visible      = false;
	m_modalDlgObject.moving       = false;
	m_modalDlgObject.nextBtnId    = 0;
	m_modalDlgObject.buttonsWidth = 0;

	while (modalClient .firstChild) modalClient .removeChild( modalClient .firstChild );
	while (modalButtons.firstChild) modalButtons.removeChild( modalButtons.firstChild );	modalButtons.style.width = "0px";
	modalButtons.style.display = 'block';
	modalClient .style.margin  = "23px 0px 0px 10px";
}

function modalStartDrag(event)
{
	if (!event) try { event = window.event; parseInt(event.clientX); } catch (e) { return m_dragNDropObject.working = m_editorResizeColumns.working = 0; }
	if (!event || !m_modalDlgObject.visible) return false;

	m_modalDlgObject.moving = true;
	m_modalDlgObject.lastMouseX = event.clientX;
	m_modalDlgObject.lastMouseY = event.clientY;
}

function modalEndDrag(event)
{
	m_modalDlgObject.moving = false;
}

function modalDoDrag(event)
{
	if (!event) try { event = window.event; parseInt(event.clientX); } catch (e) { return m_dragNDropObject.working = m_editorResizeColumns.working = 0; }
	if (!event || !m_modalDlgObject.moving) return false;

	if (m_modalDlgObject.lastMouseX != event.clientX || m_modalDlgObject.lastMouseY != event.clientY)
	{
		var modalDlg   = document.getElementById('modal-owner');

		modalDlg.style.left = (parseInt(modalDlg.style.left) + event.clientX - m_modalDlgObject.lastMouseX) + "px";
		modalDlg.style.top  = (parseInt(modalDlg.style.top ) + event.clientY - m_modalDlgObject.lastMouseY) + "px";
	}

	m_modalDlgObject.lastMouseX = event.clientX;
	m_modalDlgObject.lastMouseY = event.clientY;
}