﻿var window_id = 0;
var background_div;
var window_array = null;

var owner_document = null;
var owner_window = null;

function find_owner_window()
{
    var root_window = window;
    while (root_window != root_window.parent.window)
    {
        root_window = root_window.parent.window;
    }
    return root_window;
}

function window_initialize()
{
    if (owner_window == null) owner_window = find_owner_window();
    if (owner_document == null) owner_document = owner_window.document;
    if (owner_window.window_array == null) owner_window.window_array = new Array();
}

function window_show_target(title, url, param, width, height, target_id, closed_callback)
{
    var win_div = window_show(title, url, param, width, height, 0,0, closed_callback);
    var target_obj = document.getElementById(target_id);
   
    var left = target_obj.offsetLeft;
    var top = target_obj.offsetTop;
    var off_parent;
   
    off_parent = target_obj;
    while (off_parent.offsetParent != null)
    {
        off_parent = off_parent.offsetParent;
        left += off_parent.offsetLeft;
        top += off_parent.offsetTop;
    }
    top += target_obj.offsetHeight;
    win_div.style.left = left + "px"
    win_div.style.top = top + "px";
    win_div.style.position = "absolute";
    return win_div;
}

function window_show_center(title, url, param, width, height, closed_callback)
{
    var win_div = window_show(title, url, param, width, height, 0,0, closed_callback);
    var left = (owner_document.documentElement.clientWidth - win_div.offsetWidth) / 2;
    var top = (owner_document.documentElement.clientHeight - win_div.offsetHeight) / 2;
    if (left < 0) left = 0;
    if (top < 0) top = 0;
    win_div.style.left = left + "px"
    win_div.style.top = top + "px";
    return win_div;
}

function window_show(title, url, param, width, height, top, left, closed_callback)
{
    window_initialize();
    if (width == null) width = "600px";
    if (height == null) height = "400px";
    if (top == null) top = "10px";
    if (left == null) left = "10px";
    var c = url.indexOf('?')>0?'&':'?';
    if (owner_window.window_array.length == 0) owner_window.window_id = 0;
    var id = owner_window.window_id++;
    var wid = get_wid(id);
    url += (c+"wid="+wid);
    if (param != null) url += ("&"+param);
    return window_create(wid, id, title, url, width, height, top, left, closed_callback);
}

function window_create(wid, id, title,url, width, height, top, left, closed_callback)
{
    var window_div = create_div(wid ,id, title, url, width, height, top, left) 
    owner_document.body.appendChild(window_div);
    owner_window.window_array.push(window_div);
    background_update_core(window_div);  
    if (closed_callback) window_div.onclosed = closed_callback; 
    return window_div;
}

function window_current()
{
    if (owner_window.window_array.length == 0) return null;
    return owner_window.window_array[owner_window.window_array.length-1];
}

function window_close_current()
{
    window_close(window_current(), null);
}

function find_window_index(win_div)
{
    for (var i = 0; i < owner_window.window_array.length; i++)
    {
        if (owner_window.window_array[i] == win_div) return i;
    }
    return -1;
}

function find_window_by_id(wid)
{

    for (var i = 0; i < owner_window.window_array.length; i++)
    {
        if (owner_window.window_array[i].id == wid) return owner_window.window_array[i];
    }
    return null;
}


function window_close_by_id(wid)
{
    window_close(find_window_by_id(wid), null);
}

function window_close(win_div, result)
{
    if (win_div.onclosed) win_div.onclosed(result);
    win_div.ownerDocument.body.removeChild(win_div);
    var index = find_window_index(win_div);
    owner_window.window_array.splice(index, 1);
    background_update();
}



function background_update()
{
    background_update_core(window_current());
}

function background_update_core(win_div)
{
    if (win_div != null)
    {
        if (owner_window.background_div == null) 
        {
            owner_window.background_div = owner_document.createElement('div');
            owner_window.background_div.setAttribute('id',"div_background");
            owner_window.background_div.setAttribute('class',"background");
            owner_window.background_div.onclick = background_div_click;
            owner_document.body.appendChild(owner_window.background_div);
        }
        owner_window.background_div.style.height = document.documentElement.scrollHeight+"px";
        owner_window.background_div.style.display = "block";
        owner_window.background_div.style.zIndex = win_div.style.zIndex-1;
    }
    else
    {
        owner_window.background_div.style.display = "none";
    }
}

function background_div_click()
{
    window_close_current();
}

function get_wid(id)
{
    return "div_window_"+id;
}

function create_div(wid, id, title, url, width, height, top, left) 
{
   var window_div = owner_document.createElement("div");

   window_div.setAttribute("id", wid);
   window_div.setAttribute("class","window");

   window_div.style.position = 'fixed';
   window_div.style.top = top;
   window_div.style.left = left;
   window_div.style.zIndex = 1000 + id*2 + 2;
   if (title != null)
    {
        var div_title = owner_document.createElement("div");
        div_title.setAttribute("class","title");
        div_title.innerText = title;
        window_div.appendChild(div_title);
        var a_close = owner_document.createElement("a");
        a_close.setAttribute("class","close");
        a_close.setAttribute("onclick","window_close_by_id(\""+wid+"\")");
        div_title.appendChild(a_close);
    }
   var iframe = owner_document.createElement("iframe");
   iframe.setAttribute("frameborder","0");
   iframe.setAttribute("src",url);
   iframe.style.width = width;
   iframe.style.height = height;
   window_div.appendChild(iframe);
   return window_div;
} 

function dialog_ok(wid, result)
{
    window_initialize();
    var win_div = find_window_by_id(wid);
    window_close(win_div, result);
}    

function dialog_cancel(wid)
{
    window_initialize();
    window_close(find_window_by_id(wid), null);
/*
    window.returnValue = null;
    window.close();*/
}

