﻿

function class_WindowsManager()
{
    this.vp_windows = new Array();
    this.vp_activeWindow = null;
    
    this.vp_frontzIndex = 100;

    this.vp_windowIdCounter = 0;
  


    this.f_newWindow = function(a_windowObject) {
        a_windowObject.f_setId(this.vp_windowIdCounter++);
        if (this.vp_windowIdCounter > 30000) this.vp_windowIdCounter = 0;

        this.vp_windows.push(a_windowObject);
        this.vp_frontzIndex = a_windowObject.f_setZIndex(this.vp_frontzIndex);
        this.vp_activeWindow = a_windowObject;
        if (this.vp_windows.length == 1) {
            m_app.vp_eventManger.f_add("mousedown", this, this.f_mouseDown, this, null);
            m_app.vp_eventManger.f_add("mousemove", this, this.f_mouseMove, this, null);
            m_app.vp_eventManger.f_add("mouseup", this, this.f_mouseUp, this, null);
        }
        return a_windowObject.f_changeState();
    };
    
    this.f_get = function(a_count)
    {
        if(this.vp_windows.length>a_count) return this.vp_windows[a_count];
        else return null;
    };



    this.f_remove = function(a_windowObjet) {
        var v_removedZindex = null;
        for (var v_i = 0; v_i < this.vp_windows.length; v_i++) {
            if (this.vp_windows[v_i] == a_windowObjet) {
                v_removedZindex = a_windowObjet.vp_zIndex;

                this.vp_windows.splice(v_i, 1);
                break;
            }
        }
        // find new active window for close
        if (this.vp_activeWindow == a_windowObjet && this.vp_windows.length > 0) 
        {
            this.vp_activeWindow = this.vp_windows[0];
            for (var v_i = 1; v_i < this.vp_windows.length; v_i++) 
            {
                if (this.vp_windows[v_i].vp_zIndex > this.vp_activeWindow.vp_zIndex) this.vp_activeWindow = this.vp_windows[v_i];
            }
        }



        if (v_removedZindex != null) {

            for (var v_i = 0; v_i < this.vp_windows.length; v_i++) {
                if (this.vp_windows[v_i].vp_zIndex > v_removedZindex) {
                    this.vp_windows[v_i].f_setZIndex(this.vp_windows[v_i].vp_zIndex - 2);
                }
            }
            this.vp_frontzIndex -= 2;

        }
        if (this.vp_windows.length == 0) {
            m_app.vp_eventManger.f_removeByParentObject(this);
            this.vp_activeWindow = null;
        }
        return null;
    };
    
    
    this.f_getById = function(a_id)
    {
        for(var v_i=0;v_i<this.vp_windows.length;v_i++)
        {
            if(this.vp_windows[v_i].vp_id == a_id) return this.vp_windows[v_i];
        }
        return null;
    };

    this.f_mouseDown = function(a_event, a_windowsManager, a_id) {
        if (a_windowsManager.vp_activeWindow != null) a_windowsManager.vp_activeWindow.f_mouseDown(a_event);
    };

    this.f_mouseMove = function(a_event, a_windowsManager, a_id) {
    if (a_windowsManager.vp_activeWindow != null) a_windowsManager.vp_activeWindow.f_mouseMove(a_event);
    };

    this.f_mouseUp = function(a_event, a_windowsManager, a_id) {
    if (a_windowsManager.vp_activeWindow != null) a_windowsManager.vp_activeWindow.f_mouseUp(a_event);
    };

};

function class_BaseWindow(a_parentWindow, a_isModel, a_dimObjectID, a_ancorObjectID, a_title,a_windowCloseCallback,a_windowCloseArg1,a_showEffect) 
{
    this.vp_parentWindow = a_parentWindow;
    this.vp_id = -1;
    this.vp_isModel = a_isModel;
    this.vp_lockNonModelWindow = false;
    this.vp_dimObjectID = a_dimObjectID;
    this.vp_ancorObjectID = a_ancorObjectID;
    this.vp_mounseOverTitle = false;
    this.vp_title = a_title;
    this.vp_windowCloseCallback = a_windowCloseCallback;
    this.vp_windowCloseArg1 = a_windowCloseArg1;
    this.vp_showEffect = a_showEffect == null ? "fade_strech" : a_showEffect;
    this.vp_parentDisabledList = new Array();
    this.vp_zIndex;

    this.vp_x;
    this.vp_y;
    this.vp_mouseDown = false;

    this.f_setId = function(a_id) {
        this.vp_id = a_id;
    };

    this.f_setZIndex = function(a_frontzIndex)
    {
        this.vp_zIndex = a_frontzIndex;

        var v_dimmerDiv = f_docGetNch("dimmer_" + this.vp_id);
        if (v_dimmerDiv != null) v_dimmerDiv.zIndex = this.vp_zIndex;
        
        var v_div = f_docGetNch("windows_" + this.vp_id);
        if(v_div!=null) v_div.zIndex = this.vp_zIndex+1;
        
        
        return a_frontzIndex + 2;
    };


    this.f_windowXOffset = function() { 
        if(this.vp_parentWindow!=null)
            return this.vp_x + this.vp_parentWindow.f_windowXOffset();
         else return this.vp_x;
    };
    
    this.f_windowYOffset = function() { 
        if(this.vp_parentWindow!=null)
            return this.vp_y + this.vp_parentWindow.f_windowYOffset();
         else return this.vp_y;
    };

    this.f_base_setup = function() {

        // get dim object
        var v_dimObj = f_docGet(this.vp_dimObjectID);
        // add dimmer div to document
        var v_dimmerDiv = document.createElement("div");
        v_dimmerDiv.id = "dimmer_" + this.vp_id;
        v_dimmerDiv.className = "dimmer";

        var v_dimPos = f_absolute_posiotion(v_dimObj);
        if (this.vp_parentWindow != null) {
            v_dimPos[0] = v_dimPos[0]; // -this.vp_parentWindow.f_windowXOffset();
            v_dimPos[1] = v_dimPos[1];// -this.vp_parentWindow.f_windowYOffset();
        }

        v_dimmerDiv.style.left = v_dimPos[0] + "px";
        v_dimmerDiv.style.top = v_dimPos[1] + "px";
        v_dimmerDiv.style.width = v_dimObj.offsetWidth + 'px';
        v_dimmerDiv.style.height = v_dimObj.offsetHeight + 'px';
        v_dimmerDiv.zIndex = this.vp_zIndex;
        v_dimmerDiv.style.opacity = (0);
        v_dimmerDiv.style.MozOpacity = (0);
        v_dimmerDiv.style.KhtmlOpacity = (0);
        v_dimmerDiv.style.filter = "alpha(opacity=0)";
        //v_dimObj.parentNode.appendChild(v_dimmerDiv);

        var v_fade = new class_FadeEffect(new Array("dimmer_" + this.vp_id), 0, this.vp_isModel ? 50 : 20, 50, null);
        m_app.vp_EffectsManager.f_newEffect(v_fade);


        var v_div = document.createElement("div");

        // add window div to document
        v_dimObj.parentNode.appendChild(v_dimmerDiv);
        v_dimObj.parentNode.appendChild(v_div);

        v_div.id = "windows_" + this.vp_id;
        v_div.className = "windowdiv";


        v_div.zIndex = this.vp_zIndex + 1;


        var v_mounseover = "m_app.vp_windsManager.f_getById(" + this.vp_id + ").f_mouseOverTitle();";
        var v_mounseOut = "m_app.vp_windsManager.f_getById(" + this.vp_id + ").f_mouseOutTitle();";
        var v_close = "m_app.vp_windsManager.f_getById(" + this.vp_id + ").f_closeWindow();";

        v_tableText = "<table cellSpacing='0' cellPadding='0' border='0' id='windowsTable_" + this.vp_id + "'>" +
        "<tr><td ondblclick='void(0);' onmouseover='" + v_mounseover + "' onmouseout='" + v_mounseOut + "' class='windowTitle' nowrap>" + this.vp_title + "</td>" +
	    "<td align='right' class='windowCloseCorener'><a href='javascript:" + v_close + ";'>" +
	    "<img alt='Close...' title='Close...' src='" + m_baseUrl + "App_Themes/" + m_theme + "/images/window/close.jpg' border='0'></a></td></tr>" +
	    "<tr><td id='windowContentTd_" + this.vp_id + " colspan='2'><div id='windowsContent_" + this.vp_id + "'></div></td></tr></table>";
        v_div.innerHTML = v_tableText;

        this.vp_tb = f_docGet("windowsContent_" + this.vp_id);
        
        
    };

    this.f_base_pre_draw = function() {
    };

    this.f_base_post_draw = function() {
        // set window location

        var v_div = f_docGet("windows_" + this.vp_id);

        if (this.vp_ancorObjectID != null) {
            var v_anchorObj = f_docGet(this.vp_ancorObjectID);
            var v_anchorPos = f_absolute_posiotion(v_anchorObj);
            this.vp_x = (v_anchorPos[0] - 4);
            this.vp_y = (2 + v_anchorObj.offsetHeight + v_anchorPos[1]);



        }
        else {
            var v_dimObj = f_docGet(this.vp_dimObjectID);
            var v_dimPos = f_absolute_posiotion(v_dimObj);
            this.vp_x = ((v_dimObj.offsetWidth / 2) + v_dimPos[0] - (v_div.offsetWidth / 2));
            this.vp_y = ((v_dimObj.offsetHeight / 2) + v_dimPos[1] - (v_div.offsetHeight / 2));
        }


        v_div.style.left = this.vp_x + "px";
        v_div.style.top = this.vp_y + "px";


        // snow
        switch (this.vp_showEffect) {

            case "fade_strech":
                var v_w = v_div.offsetWidth + 10;
                var v_strech = new class_DivStretchEffect("windows_" + this.vp_id, 10, 10, v_div.offsetWidth, v_div.offsetHeight, 100, this.vp_dimObjectID, null, this.f_overflowCallback, this)
                m_app.vp_EffectsManager.f_newEffect(v_strech);

                var v_fade = new class_FadeEffect(new Array("windows_" + this.vp_id), 0, 100, 20, null);
                m_app.vp_EffectsManager.f_newEffect(v_fade);
                break;
            case "fade":
                var v_fade = new class_FadeEffect(new Array("windows_" + this.vp_id), 0, 100, 20, null);
                m_app.vp_EffectsManager.f_newEffect(v_fade);
                break;
            default: f_public_errorAlert("unkonwn window show effect " + this.vp_showEffect, false);
                var v_fade = new class_FadeEffect(new Array("windows_" + this.vp_id), 0, 100, 20, null);
                m_app.vp_EffectsManager.f_newEffect(v_fade);
                break;
        }


        // turn off contorls under this window
        var v_parentPage = this.vp_parentWindow != null ? this.vp_parentWindow : document.body;
        var v_arr = v_parentPage.vp_tb.getElementsByTagName("input");
        for (var v_i = 0; v_i < v_arr.length; v_i++) {
            if (!v_arr[v_i].disabled) {
                v_arr[v_i].disabled = true;
                this.vp_parentDisabledList.push(v_arr[v_i]);
            }
        }
        var v_arr = v_parentPage.vp_tb.getElementsByTagName("select");
        for (var v_i = 0; v_i < v_arr.length; v_i++) {
            if (!v_arr[v_i].disabled) {
                v_arr[v_i].disabled = true;
                this.vp_parentDisabledList.push(v_arr[v_i]);
            }
        }
        var v_arr = v_parentPage.vp_tb.getElementsByTagName("a");
        for (var v_i = 0; v_i < v_arr.length; v_i++) {
            if (!v_arr[v_i].disabled) {
                v_arr[v_i].disabled = true;
                this.vp_parentDisabledList.push(v_arr[v_i]);
            }
        }
    };

    this.vp_tbWidth;
    this.vp_tbHeight;


    this.f_captureWindowSize = function() {
        this.vp_tbWidth = this.vp_tb.offsetWidth;
        this.vp_tbHeight = this.vp_tb.offsetHeight;
    };

    this.f_rescaleWindow = function() {
        if (this.vp_tbWidth == null || this.vp_tbHeight == null) f_public_errorAlert("rescaleWindow call without capture call. call capture before change",false);
        else {
            var v_wDoff = this.vp_tb.offsetWidth - this.vp_tbWidth;
            var v_hDoff = this.vp_tb.offsetHeight - this.vp_tbHeight;
            var v_div = f_docGet("windows_" + this.vp_id);
            var v_strech = new class_DivStretchEffect("windows_" + this.vp_id, v_div.offsetWidth, v_div.offsetHeight, v_div.offsetWidth + v_wDoff, v_div.offsetHeight + v_hDoff, 100, this.vp_dimObjectID, null, this.f_overflowCallback, this)
            m_app.vp_EffectsManager.f_newEffect(v_strech);

            this.vp_tbWidth = null;
            this.vp_tbHeight = null;
        }
    };

    this.f_overflowCallback = function(a_window) {
        var v_obj = f_docGetNch("windowContentTd_" + a_window.vp_id);
        if (v_obj != null) v_obj.style.overflow = "scroll";
    };

    this.f_mouseOverTitle = function() {
        this.vp_mounseOverTitle = true;
    };
    
    this.f_mouseOutTitle = function() {
        this.vp_mounseOverTitle = false;

    };


    this.f_mouseDown = function(a_event) {

        var v_div = f_docGetNch("windows_" + this.vp_id);
        if (v_div) {

            var v_divPos = f_absolute_posiotion(v_div);
            if (a_event.clientX > v_divPos[0] && a_event.clientY > v_divPos[1]
           && a_event.clientX < (v_div.offsetWidth + v_divPos[0]) && a_event.clientY < (v_divPos[1] + v_div.offsetHeight)) {
                if (this.vp_mounseOverTitle) {

                    this.vp_x = a_event.offsetX;
                    this.vp_y = a_event.offsetY;
                    this.vp_mouseDown = true;
                }
            }
            else {
                if (!this.vp_isModel && !this.vp_lockNonModelWindow) {
                    this.f_closeWindow();
                    if (this.vp_parentWindow) this.vp_parentWindow.f_mouseDown(a_event);
                }
            }
        }
    };


    this.f_mouseMove = function(a_event) {
        if (this.vp_mouseDown) {
            var v_div = f_docGet("windows_" + this.vp_id);

            v_div.style.left = (a_event.clientX - this.vp_y + document.body.scrollLeft) + "px";
            v_div.style.top = (a_event.clientY - this.vp_y + document.body.scrollTop) + "px";

            
            return false;
        }
    };

    this.f_mouseUp = function(a_event) {
        this.vp_mouseDown = false;
    }



    this.f_closeWindow = function() {
        if (this.f_base_exitState(null, null, null)) {

            var v_div = f_docGet("windows_" + this.vp_id);
            v_div.parentNode.removeChild(v_div);
            v_div = f_docGet("dimmer_" + this.vp_id);
            v_div.parentNode.removeChild(v_div);

            for (var v_i = 0; v_i < this.vp_parentDisabledList.length; v_i++) {
                this.vp_parentDisabledList[v_i].disabled = false;
            }

            if (this.vp_windowCloseCallback != null)
                this.vp_windowCloseCallback(this.vp_windowCloseArg1);
            m_app.vp_windsManager.f_remove(this);                
        }
    };


    this.f_lockNonModelWindow = function() {
        this.vp_lockNonModelWindow = true;
    };

    this.f_releaseNonModelWindow = function() {
        this.vp_lockNonModelWindow = false;
     };


};



