var g_bubbleBtnConfirm_btn = false;
var g_bubbleBtnConfirm_do = false;
var browserType = 'unknown';
if (navigator.userAgent.match(/Opera/)) {
	browserType = 'opera';
} else if (navigator.userAgent.match(/MSIE/)) {
	browserType = 'ie';
} else if (navigator.userAgent.match(/Mozilla/)) {
	browserType = 'ns';
}



function alAlert(message)
{
    showBackground(false);
    $('systemAlertMessage').innerHTML = message;
    $('systemAlertPopup').style.display = 'block';
}
function alAlert_close()
{
    showBackground(true);
    $('systemAlertPopup').style.display = 'none';
}

function alBigAlert(message)
{
    showBackground(false);
    $('systemBigAlertMessage').innerHTML = message;
    $('systemBigAlertPopup').style.display = 'block';
}
function alBigAlert_close()
{
    showBackground(true);
    $('systemBigAlertPopup').style.display = 'none';
}

function alConfirm(message)
{
    return confirm(message);
    /*
	if (g_bubbleBtnConfirm_do)
	{
		g_bubbleBtnConfirm_do = false;
		return true;
	}
	g_bubbleBtnConfirm_btn = obj;
	if (!target)
	{
	    target = window;
	    align = 'c';
	}
    */
	/*
	bubbleShow('main',
	           target,
	           '300,a,qc,'+align+',,bubbleBtnConfirmDo(this,false);',
	           msg + '<br><br><a href="javascript:void(0);" onclick="bubbleBtnConfirmDo(this,true);"><img align="right" src="img/de/btn_yes.png" width="80" height="20" alt="OK" border="0" style="margin-top:4px;" hspace="4"></a><a href="javascript:void(0);" onclick="bubbleBtnConfirmDo(this,false);"><img align="right" src="img/de/btn_no.png" width="80" height="20" alt="OK" border="0" style="margin-top:4px;" hspace="4"></a>');
    */
    /*
    showBackground(false);
    document.getElementById('systemConfirmMessage').innerHTML = msg;
    document.getElementById('systemConfirmPopup').style.display = 'block';


	domEventPreventDefault();
	domEventCancelBubble();
	return false;
    */
}
function showMessage(type, message)
{
    if (type == 'alert')
    {
        domGet('systemAlertMessage').innerHTML = message;
        domShow(domGet('systemAlertPopup'));
    }
    else if (type == 'confirm')
    {
        domGet('systemConfirmMessage').innerHTML = message;
        domShow(domGet('systemConfirmPopup'));
    }
    showBackground(false);
}
function hideMessage(obj)
{
    domHide(obj);
    showBackground(true);
}

function alConfirm_close()
{
    document.getElementById('systemConfirmPopup').style.display = 'none';
    showBackground(true);
}

function alPrompt(string)
{
    text = prompt(string, '');
    return text;
}



function flashShow(obj)
{
	for (var i=0; i<obj.childNodes.length; i++) {
		var sn = obj.childNodes[i];
		if (sn.nodeType != 1) continue;
		if (sn.flashHide_hidden) {
			sn.flashHide_hidden = null;
			sn.style.visibility = 'visible';
		}
		flashShow(sn);
	}
}
function bubbleFocus(obj)
{
	if (typeof obj == 'string') obj = domGet('bubble_' + obj);
	while (obj && !(obj.id && obj.id.match(/^bubble_/))) obj = obj.parentNode;
	var oBody = domGetBody();
	var zIndex = 1;
	for (var i=0; i<oBody.childNodes.length; i++) {
		if (!oBody.childNodes[i].id) continue;
		if (oBody.childNodes[i].id.match(/^bubble_/)) {
			zIndex = Math.max(zIndex, oBody.childNodes[i].style.zIndex);
		}
	}
	zIndex++;
	obj.style.zIndex = zIndex;
}
function flashHide(obj, x, y, w, h)
{
	for (var i=0; i<obj.childNodes.length; i++) {
		var sn = obj.childNodes[i];
		if (sn.nodeType != 1) continue;
		if ((browserType == 'ie' && sn.nodeName.toLowerCase() == 'object' ) || (browserType != 'ie' && sn.nodeName.toLowerCase() == 'embed')) {
			var a = domGetOffset(sn);
			var fx = a.x; var fy = a.y; var fw = sn.offsetWidth; var fh = sn.offsetHeight;
			if ((fy > y && fy < y+h) || (fy+fh > y && fy+fh < y+h) || (y > fy && y < fy+fh) || (y+h > fy && y+h < fy+fh)) {
				if ((fx > x && fx < x+w) || (fx+fw > x && fx+fw < x+w) || (x > fx && x < fx+fw) || (x+w > fx && x+w < fx+fw)) {
					sn.flashHide_hidden = true;
					sn.style.visibility = 'hidden';
				}
			}
		}
		flashHide(sn, x, y, w, h);
	}
}
function domGetScreenOffset(obj)
{
	obj = domGet(obj);
	var offset = { x:0, y:0 };
	if (obj.offsetX) return { x:obj.offsetX, y:obj.offsetY };
	while (obj) {
		offset.x += obj.offsetLeft - obj.scrollLeft;
		offset.y += obj.offsetTop - obj.scrollTop;
		obj = obj.offsetParent;
	}
	return offset;
}
function domGetBody()
{
	var tmp = document.getElementsByTagName('BODY');
	return tmp[0];
}
function domEventPreventDefault()
{
	if (window.event)
	{
		window.event.returnValue = false;
	}
	else
	{
		//window.nsevent.preventDefault();
	}
}

function domEventCancelBubble()
{
	if (window.event)
	{
		window.event.cancelBubble = true;
	}
	else
	{
		//window.nsevent.stopPropagation();
	}
}

function bubbleBtnConfirmDo(obj, clicked)
{
	hideMessage(obj);
	if (g_bubbleBtnConfirm_btn && clicked)
	{
		g_bubbleBtnConfirm_do = true;
		g_bubbleBtnConfirm_btn.click();
		g_bubbleBtnConfirm_btn = false;
	}
}
/*
function bubbleBtnConfirm(obj, msg, target, align)
{
	if (g_bubbleBtnConfirm_do)
	{
		g_bubbleBtnConfirm_do = false;
		return true;
	}
	g_bubbleBtnConfirm_btn = obj;
	if (!target)
	{
	    target = window; align = 'c';
	}
	//bubbleShow('main', target, '300,a,qc,'+align+',,bubbleBtnConfirmDo(this,false);', msg + '<br><br><a href="javascript:void(0);" onclick="bubbleBtnConfirmDo(this,true);"><img align="right" src="img/de/btn_yes.png" width="80" height="20" alt="OK" border="0" style="margin-top:4px;" hspace="4"></a><a href="javascript:void(0);" onclick="bubbleBtnConfirmDo(this,false);"><img align="right" src="img/de/btn_no.png" width="80" height="20" alt="OK" border="0" style="margin-top:4px;" hspace="4"></a>');
	domEventPreventDefault();
	domEventCancelBubble();
	return false;
}
*/




function bubbleShow(id, obj, size, content)
{
	var sizeTest = document.createElement('DIV');
	sizeTest.innerHTML = '<font class="white">'+content+'</font>';
	sizeTest.style.width = '210px';
	domGetBody().appendChild(sizeTest);
	var neededHeight = sizeTest.offsetHeight;
	domRemove(sizeTest);
	var sizeOptions = size.split(',', 5);
	var width = parseInt(sizeOptions[0]);
	var height = 30;
	var heightNum = '1';
	if (neededHeight > 50 && sizeOptions[1] == 'a') sizeOptions[1] = 'b';
	if (neededHeight > 90 && sizeOptions[1] != 'c') sizeOptions[1] = 'c';
	switch (sizeOptions[1]) {
		case 'a': heightNum = '1'; height = 30; break;
		case 'b': heightNum = '2'; height = 70; break;
		case 'c': heightNum = '3'; height = 130; break;
	}
	var icon = false;
	var closeButton = false;
	if (sizeOptions.length > 2) for (var i=0; i<sizeOptions[2].length; i++) switch (sizeOptions[2].charAt(i)) {
		case 'q': icon = 'question'; break;
		case 'i': icon = 'info'; break;
		case 'e': icon = 'error'; break;
		case 'c': closeButton = true; break;
	}

	var width_lo = 40;
	var width_li = 30;
	var width_ro = 40;
	var width_ri = 30;
	var border_l = '';
	var border_r = '';
	if (sizeOptions.length > 3) for (var i=0; i<sizeOptions[3].length; i++) switch (sizeOptions[3].charAt(i)) {
		case 'l': width_lo = 70; width_li = 60; border_l = '_a'; break;
		case 'r': width_ro = 70; width_ri = 60; border_r = '_a'; break;
	}

	var closeJS = '';
	if (sizeOptions.length > 5) closeJS = sizeOptions[5];

	var oBody = domGetBody();
	var oOldDiv = domGet('bubble_' + id);
	if (oOldDiv) {
		hideMessage(oOldDiv);
	}

	if (obj == window) {
		var body = domGetBody();
		var targetPos = { x:0, y:body.scrollTop };
		var target = { offsetWidth:Math.min(820, ((browserType=='ns') ? window.innerWidth : body.offsetWidth)), offsetHeight:((browserType=='ns') ? window.innerHeight : body.offsetHeight) };
	} else {
		obj = domGet(obj);
		var target = obj;
		var targetPos = domGetScreenOffset(target);
	}
	if (browserType != 'ie') flashHide(domGetBody(), targetPos.x+target.offsetWidth, targetPos.y-60, 300, 100);

	var oDiv = document.createElement('DIV');
	oDiv.id = 'bubble_' + id;
	oDiv.style.position = 'absolute';

	if (border_l) {
		oDiv.style.left = (targetPos.x + target.offsetWidth) + 'px';
		oDiv.style.top = (targetPos.y - 40 - height) + 'px';
	} else if (border_r) {
		oDiv.style.left = (targetPos.x - width - 50) + 'px';
		oDiv.style.top = (targetPos.y - 40 - height) + 'px';
	} else {
        //oDiv.style.left = (targetPos.x + target.offsetWidth/2 - (width+20)/2) + 'px';
		oDiv.style.left = (window.document.body.clientWidth/2 - (width+20)/2) + 'px';
		oDiv.style.top = (targetPos.y + target.offsetHeight/2 - (height+60)/2) + 'px';
	}

	if (parseInt(oDiv.style.left) + width > domGetBody().offsetWidth) {
		oDiv.style.left = (domGetBody().offsetWidth-width) + 'px';
	}

	var html = '';
	html += '<div style="position:absolute; top:0px; left:0px; width:'+(width+width_li+width_ri-40)+'px; z-index:2;"><table cellpadding="0" cellspacing="0" border="0" width="100%">';
	html += '<tr height="20">';
		html += '<td align="left" valign="top" width="'+width_li+'" onmousedown="hideMessage(this);'+closeJS+'"><img src="img/de/sp.gif" width="'+width_li+'" height="20" border="0"></td>';
		html += '<td align="left" valign="top" onmousedown="hideMessage(this);"><img src="img/de/sp.gif" width="100%" height="20" border="0"></td>';
		html += '<td align="left" valign="top" width="'+width_ri+'" onmousedown="hideMessage(this);'+closeJS+'">';
		if (closeButton) {
			html += '<img src="img/de/sp.gif" width="'+width_ri+'" height="20" border="0" style="cursor:pointer;">';
		} else {
			html += '<img src="img/de/sp.gif" width="'+width_ri+'" height="20" border="0">';
		}
		html += '</td>';
	html += '</tr>';
	html += '<tr height="50">';
		html += '<td align="left" valign="top" width="'+width_li+'" onmousedown="hideMessage(this);'+closeJS+'"><img src="img/de/sp.gif" width="'+width_li+'" height="'+(height+20)+'" border="0"></td>';
		html += '<td align="left" valign="top">';
		if (icon)  {
			html += '<img src="img/de/bubble_'+icon+'.png" width="40" height="40" border="0" alt="'+icon+'" align="left">';
			html += '<font class="white">'+content+'</font>';
		} else {
			html += '<font class="white">'+content+'</font>';
		}
		html += '</td>';
		html += '<td align="left" valign="top" width="'+width_ri+'" onmousedown="hideMessage(this);'+closeJS+'"><img src="img/de/sp.gif" width="'+width_li+'" height="'+(height+20)+'" border="0"></td>';
	html += '</tr>';
	html += '<tr height="30">';
		html += '<td align="left" valign="top" width="'+width_li+'" onmousedown="hideMessage(this);'+closeJS+'"><img src="img/de/sp.gif" width="'+width_li+'" height="30" border="0"></td>';
		html += '<td align="left" valign="top" onmousedown="hideMessage(this);"><img src="img/de/sp.gif" width="100%" height="30" border="0"></td>';
		html += '<td align="left" valign="top" width="'+width_ri+'" onmousedown="hideMessage(this);'+closeJS+'"><img src="img/de/sp.gif" width="'+width_ri+'" height="30" border="0"></td>';
	html += '</tr>';
	html += '</table></div>';

	html += '<div style="position:absolute; top:0px; left:0px; width:'+(width+width_li+width_ri-40)+'px; z-index:1;"><table cellpadding="0" cellspacing="0" border="0" width="100%">';
	html += '<tr height="30">';
		html += '<td align="left" valign="top" width="'+width_lo+'"><img src="img/de/bubble'+border_l+'_tl.png" width="'+width_lo+'" height="30" border="0"></td>';
		html += '<td align="left" valign="top"><img src="img/de/bubble_t.png" width="100%" height="30" border="0"></td>';
		html += '<td align="left" valign="top" width="'+width_ro+'"><img src="img/de/bubble'+border_r+'_tr'+(closeButton?'_close':'')+'.png" width="'+width_ro+'" height="30" border="0"></td>';
	html += '</tr>';
	html += '<tr height="30">';
		html += '<td align="left" valign="top" width="'+width_lo+'"><img src="img/de/bubble'+border_l+'_l_'+heightNum+'.png" width="'+width_lo+'" height="'+height+'" border="0"></td>';
		html += '<td align="left" valign="top"><img src="img/de/bubble_c.png" width="100%" height="'+height+'" border="0"></td>';
		html += '<td align="left" valign="top" width="'+width_ro+'"><img src="img/de/bubble'+border_r+'_r_'+heightNum+'.png" width="'+width_ro+'" height="'+height+'" border="0"></td>';
	html += '</tr>';
	html += '<tr height="40">';
		html += '<td align="left" valign="top" width="'+width_lo+'"><img src="img/de/bubble'+border_l+'_bl.png" width="'+width_lo+'" height="40" border="0"></td>';
		html += '<td align="left" valign="top"><img src="img/de/bubble_b.png" width="100%" height="40" border="0"></td>';
		html += '<td align="left" valign="top" width="'+width_ro+'"><img src="img/de/bubble'+border_r+'_br.png" width="'+width_ro+'" height="40" border="0"></td>';
	html += '</tr>';
	html += '</table></div>';
	oDiv.innerHTML = html;

	if (!closeButton) {
		oDiv.onmousedown = function() { hideMessage(this); };
	}
	oBody.appendChild(oDiv);
	bubbleFocus(oDiv);
    hideMessage(id);
}
