/*
 * def.id : id of popup window
 * def.title : title-bar text of popup window
 * def.style : additional style of popup window
 * def.content : content of popup window
 */
function popup_window_create(def)
{
    var container = document.createElement('div');
    container.style.display='none';
    var pw_class = "popup-window";
    if (def.additional_class)
	pw_class += (" " + def.additional_class);
    var html = '<div class="'+pw_class+'"';
    if (def.id)
	html += (' id="'+def.id+'"');
    if (def.style)
	html += (' style="'+def.style+'"');
    html += '>';
    html += '<div class="close-button" onclick="this.parentNode.parentNode.removeChild(this.parentNode)">x</div>';
    if (def.title)
    {
	html += '<div class="title-bar">';
	html += def.title;
	html += '</div>';
    }
    if (def.content)
	html += def.content;
    html += '</div>';
    container.innerHTML = html;
    var popup_window = container.childNodes[0];
    container.removeChild(popup_window);
    return popup_window;
}

function opacity(id, opacStart, opacEnd, millisec) {
    //speed for each frame
    var speed = Math.round(millisec / 100);
    var timer = 0;

    //determine the direction for the blending, if start and end are the same nothing happens
    if(opacStart > opacEnd) {
        for(i = opacStart; i >= opacEnd; i--) {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    } else if(opacStart < opacEnd) {
        for(i = opacStart; i <= opacEnd; i++)
            {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    }
}

//change the opacity for different browsers
function changeOpac(opacity, id) {
    var object = document.getElementById(id).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
}
function popup_window_show(id, autoclose_timeout)
{
	var p = document.getElementById(id);
	if (p)
	{
	    p.parentNode.removeChild(p);
	    document.body.appendChild(p);
	    p.style.display="block";
	    p.style.left = ((document.body.clientWidth - p.clientWidth) / 2)+'px';
	    if (typeof ToolMan != 'undefined')
	    {
		    var drag_handle = p.firstChild;
		    drag_handle.style.cursor = 'pointer';
		    ToolMan.drag().createSimpleGroup(p, drag_handle);
	    }
	    changeOpac(0, id);
	    opacity(id, 0, 100, 500);
	}
	if (autoclose_timeout > 0)
		window.setTimeout('var p=document.getElementById(\''+id+'\');if(p)p.parentNode.removeChild(p);', autoclose_timeout*1000);
}

function popup_window_content(content_html)
{
    var container = document.createElement('div');
    container.innerHTML = content_html;
    return container;
}

