var Forms = new Array();

var GlobalFormLoaded = new Loaded();

function FormHandler() {
    this.AddCloseIcon = true;
    this.Page = "";
    this.Form = null;
    this.Timer = 300;
    this.Loaded = function () { };
    var Pointer = this;
    this.StartX = 0;
    this.StartY = 0;
    this.Sender = null;
    this.MouseOn = false;
    this.OnClick = null;

    this.Show = function () {
        var div = document.createElement("div");
        div.style.position = "absolute";
        div.style.top = "0px";
        div.style.left = "0px";
        div.style.visibility = "hidden";
        document.getElementsByTagName("body")[0].appendChild(div);
        var Ajax = new AjaxHandler();
        Ajax.Page = this.Page;
        if (this.Sender != null && this.Sender != undefined) {
            this.StartX = GetObjectLeft(this.Sender) + this.Sender.offsetWidth / 2;
            this.StartY = GetObjectTop(this.Sender) + this.Sender.offsetHeight / 2;
        }
        this.Form = div;

        this.Form.style.zIndex = Forms.length;
        Forms[Forms.length] = this.Form;

        Ajax.Place = div;
        Ajax.Loaded = function () {
            var obj = Pointer.CreateTemp(); 
            Pointer.MoveIt(obj);
        };
        Ajax.LoadPage();
        return div;
    }

    this.MoveIt = function (Moveable) 
    {          
        if (parseFloat(Moveable.style.width) < Pointer.Form.offsetWidth) {
            var step = Pointer.Timer / 10;
            var addWidth = parseFloat(Pointer.Form.offsetWidth / step);
            var addHeight = parseFloat(Pointer.Form.offsetHeight / step);

            var addX = parseFloat((getScrollXY()[0] + (Window.Width - Pointer.Form.offsetWidth) / 2 - Pointer.StartX) / step);
            var addY = parseFloat((getScrollXY()[1] + (Window.Height - Pointer.Form.offsetHeight) / 2 - Pointer.StartY) / step);
            Moveable.style.width = parseFloat(Moveable.style.width) + addWidth + "px";
            Moveable.style.height = parseFloat(Moveable.style.height) + addHeight + "px";
            Moveable.style.top = parseFloat(Moveable.style.top) + addY + "px";
            Moveable.style.left = parseFloat(Moveable.style.left) + addX + "px";
            
            setTimeout(function () { Pointer.MoveIt(Moveable); }, 10);
        } else {
            
            Moveable.parentNode.replaceChild(Pointer.Form, Moveable);
            Pointer.Form.style.visibility = "visible";
            Pointer.Relocate(Pointer.Form);
         
        } 
    }                                         

    this.CreateTemp = function () 
    {
        var tmp = document.createElement("div");
        tmp.style.position = "absolute";
        tmp.style.top = this.StartY + "px"
        tmp.style.left = this.StartX + "px";
        tmp.style.width = "0px";
        tmp.style.height = "0px"
        tmp.style.border = "1px solid silver";
        tmp.style.overflow = "hidden";
        document.getElementsByTagName("body")[0].appendChild(tmp);
        return tmp;
    }

    this.Relocate = function () {
        this.Form.style.top = Window.ScrollY + (Window.Height - this.Form.offsetHeight) / 2 + "px";
        this.Form.style.left = Window.ScrollX + (Window.Width - this.Form.offsetWidth) / 2 + "px";
                                  
        if(parseInt(this.Form.style.top) < 0)
            this.Form.style.top = "5px";
        if(parseInt(this.Form.style.left) < 0)
            this.Form.style.left = "5px";
            
        AttachEvent(this.Form, "mouseover", function () {
           Pointer.Form.style.zIndex = 1;
        }, true);
        AttachEvent(this.Form, "mouseout", function () {
           Pointer.Form.style.zIndex = 0;
        }, true);
        try {
            this.Loaded();
        } catch (ex) {
            alert(ex);
        }                       
        GlobalFormLoaded.Run();
        if(this.OnClick != null){
            AttachEvent(this.Form,"click",this.OnClick,true);
        }  
        if (this.AddCloseIcon) {
            var img = document.createElement("img");
            img.src = "Images/x.png";
            img.style.position = "absolute";
            img.style.cursor = "pointer";
            img.style.width = "16px";
            img.style.height = "16px";
            img.style.top = "-8px";
            img.style.left = this.Form.offsetWidth - 8 + "px";
            img.id = "LastFormCloser";
            //Pointer.Form.appendChild(img);
            /*if (this.Form.hasChildNodes()) {
                this.Form.insertBefore(img, this.Form.childNodes[0]);
                AttachEvent(img, "click", function () { Pointer.Close();GetObject("background").style.display = "none"; }, true);
                AttachEvent(img, "mouseover", function () {
                    img.style.width = "24px";
                    img.style.height = "24px";
                    img.style.top = "-12px";
                    img.style.left = Pointer.Form.offsetWidth - 12 + "px";
                }, true);
                AttachEvent(img, "mouseout", function () {
                    img.style.width = "16px";
                    img.style.height = "16px";
                    img.style.top = "-8px";
                    img.style.left = Pointer.Form.offsetWidth - 8 + "px";
                }, true);
            } */
        }
    }
    this.Close = function () {
        DeleteObject(this.Form);
    }
}


var LastForm = null;

function Parser(from, to) {
    if (typeof (from) == "object" && typeof (to) == "object") {
        for (var b in from)
            for (var a in to)
                if (a == b)
                    to[b] = from[a];
    }
}

function Deparser(obj) {
    var data = "{";
    for (var a in obj) 
    {
        data += "'" + a + "':'" + obj[a] + "',";
    }
    data = data.substring(0, data.length - 1);
    data += "}";
    return data;
}

function CreateBackground() 
{
    var div = document.createElement("div"); 
    div.style.width = Window.Width + "px"; 
    div.style.height = Window.Height + "px"; 
    div.style.background = "black";
    div.id = "background";           
    div.style.position = "absolute";  
    div.style.top = "0px";
    div.style.left = "0px";
    div.style.opacity = "0.2";
    div.style.filter = "alpha(opacity = 20)";
    document.getElementsByTagName("body")[0].appendChild(div);   
}

function ShowForm(URL, close) {
    var Form = new FormHandler();
    /*
    if(GetObject("background") == null)
        CreateBackground();
    else
        GetObject("background").style.display = "block";
    */
    
    var e = GetEvent(arguments);
    Form.Sender = (e.target) ? e.target : e.srcElement;
    switch(typeof (URL)) {
        case"string":
            Form.Page = URL;      
        break;
        case "object":
            Parser(URL, Form);
            break;
        default:
            alert("Error at show form!");
            break;
    }
    close = (close == null) ? true : close;
    //if (close && LastForm != null)
    //    LastForm.Close();

    Form.Show();
    LastForm = Form;
    return Form;
}

