// Build 9.00.7257.6

if (!Qva.Mgr) Qva.Mgr = {}

Qva.Mgr.toolwindowtable = function (owner, elem, name, prefix) {
    if (!Qva.MgrSplit (this, name, prefix)) return;
    owner.AddManager(this);
    this.Element = elem;
    this.BinderId = owner.ID;
    this.BaseName = owner.Name;
}

Qva.Mgr.toolwindowtable.prototype.Inside = function (pos, type) {
    for (var ix = 0; ix < this.DropTargets.length; ++ix) {
        var cell = this.DropTargets[ix];
//        if (cell.dragAccept != type) continue;
        if (!cell.dropObj) continue;
        if (cell.dropObj.Type != type) continue;
        var cellPos    = Qva.GetPageCoords(cell);
        var cellWidth  = parseInt(cell.offsetWidth);
        var cellHeight = parseInt(cell.offsetHeight);
        if (pos.x > cellPos.x && pos.x < cellPos.x + cellWidth &&
            pos.y > cellPos.y && pos.y < cellPos.y + cellHeight) {
            if (pos.y<cellPos.y + 0.37 * cellHeight)
                var vertPosition = 'insertbefore';
            else if (pos.y>cellPos.y + 0.70 * cellHeight)
                var vertPosition = 'insertafter';
            else
                var vertPosition = 'dragto';
//                var vertPosition = (pos.y>cellPos.y + 0.80 * cellHeight) ?'insertafter' : 'dragto';
            if ((cell.dropObj.DropOn && vertPosition == 'dragto') ||(cell.dropObj.InsertAfter && vertPosition == 'insertafter')||(cell.dropObj.InsertBefore && vertPosition == 'insertbefore'))
                return {'Element':cell,'VerticalPosition':vertPosition};
        }
    }
    return null;
}

Qva.Tooltip = null;
Qva.ShowTooltip = function (event, cell) {
    if (!cell.Tooltip) return;
    if(!Qva.Tooltip) {
        Qva.Tooltip = document.createElement("div");
        Qva.Tooltip.className = "QvHover";
        Qva.Tooltip.style.zIndex = 666;
        Qva.Tooltip.style.display = "none";
        Qva.Tooltip.style.position = "absolute";
        Qva.Tooltip.style.backgroundColor = "#FFFFCC";
        Qva.Tooltip.style.border = "solid 1px black";
        Qva.Tooltip.style.padding = "1px 3px 2px 3px";
        document.body.appendChild(Qva.Tooltip);
    }
    Qva.Tooltip.innerHTML = "";
    var lines = cell.Tooltip;
    for (var i = 0; i < lines.length; i++) {
        var p = document.createElement ("span");
        p.innerText = lines [i];
        Qva.Tooltip.appendChild (p);
        var br = document.createElement ("br");
        Qva.Tooltip.appendChild (br);
    }
    if (!event) event = window.event;
    Qva.Tooltip.style.left = (event.clientX + Qva.GetScrollLeft() + 5) + "px";
    Qva.Tooltip.style.top  = (event.clientY + Qva.GetScrollTop() + 5)  + "px";
    Qva.Tooltip.style.display = "";
}

Qva.HideTooltip = function () {
    if(Qva.Tooltip) Qva.Tooltip.style.display = "none";
}

Qva.Mgr.toolwindowtable.prototype.Paint = function(mode, node) {
    this.Touched = true;
    this.DropTargets = [];
    var table = this.Element;
    var rix = 0;
    var colgroup = table.firstChild;
    while(colgroup.firstChild) colgroup.removeChild(colgroup.firstChild);
    for (var line = node.firstChild; line != null; line = line.nextSibling) {
        if (line.nodeName != 'column') continue;
        var col = document.createElement("col");
        col.width = parseInt(line.getAttribute("width"));
        colgroup.appendChild(col);
    }
//    table.style.borderCollapse = "separate";
    for (var line = node.firstChild; line != null; line = line.nextSibling) {
        if (line.nodeName != 'row') continue;
        var row = (rix < table.rows.length) ? table.rows[rix] : table.insertRow(-1);
        ++rix;
        switch(line.getAttribute("class")) {
        case "header":
            row.className = "ToolProperty-Header";
            break;
        case "navigation":
            row.className = "ToolProperty-Navigation";
            break;
        case "warning":
            row.className = "ToolProperty-Warning";
            break;
        default:
            row.className = "";
            break;
        }
       

        var cix = 0;
        for (var child = line.firstChild; child != null; child = child.nextSibling) {
            var cmd = child.getAttribute("name");
            if (cmd) cmd = this.BaseName + '.' + cmd;
            var cell = (cix < row.cells.length) ? row.cells[cix] : row.insertCell(-1);
            ++cix;
            var indent = child.getAttribute ('indent');
            if (indent) {
                indent = 12 * parseInt(indent);
                cell.style.paddingLeft = indent + 'pt';
            } else {
                cell.style.paddingLeft = '';
            }
            var label = child.getAttribute("label");
            switch(child.getAttribute("class")) {
            case "highlight":
                cell.className = "ToolProperty-Highlight";
                break;
            }

            var colspan = child.getAttribute ('colspan');
            if (colspan) {
                cell.colSpan = parseInt(colspan);
            } else {
                cell.colSpan = 1;
            }
            
            var tips = child.getElementsByTagName ('tooltip');
            var tiplen = tips.length;
            if (tiplen > 0) {
                var tiplist = [];            
                for (var ix = 0; ix < tiplen; ++ix) {
                    tiplist.push(tips[ix].getAttribute('text'));
                }
                cell.Tooltip = tiplist;
                cell.onmouseover = function (event) { Qva.ShowTooltip(event, this); }
                cell.onmouseout = function () { Qva.HideTooltip(); }
            } else {
                cell.Tooltip = null;
            }
            switch(child.nodeName) {
            case 'title':
                cell.innerText = child.getAttribute("text");
                break;
            case 'link':
                cell.innerText = child.getAttribute("text");
                cell.BinderId = this.BinderId;
                cell.Name = cmd;
                cell.className = "ToolProperty-Link";
                cell.onclick = function() {
                    var binder = Qva.GetBinder(this.BinderId);
                    if (!binder.Enabled) return;
                    binder.Set (this.Name, "action", "", true);
                }
                break;
            case 'literal':
                cell.innerHTML = "<span></span>"
                cell.className = "ToolProperty-Literal";
                cell.firstChild.innerText = child.getAttribute("text");
                break;
            case 'text':
            case 'numeric':
                var actor = null;
                if (indent) cell.style.paddingLeft = indent + 'pt';
                if (label) {
                    cell.innerHTML = "<span></span> <input />"
                    cell.firstChild.innerText = label;
                    cell.firstChild.className = "ToolProperty-Label";
                } else {
                    var range = child.getAttribute("range");
                    if (range == 'percent') {
                        cell.innerHTML = "<table cellspacing='0' cellpadding='0' width1='100%' ><tr><td width='30pt' >0%</td><td width='134px'><span style='position:absolute;'><hr style='margin:0pt;position:absolute;width:128px;' /><button style='top:-4px;position:absolute;width:4px;height:9px;background-color:lightgrey;border:solid 1px darkgray'></button></span></td><td width='36pt' align='right'>100%</td></tr></table>"
                        actor = cell.lastChild.rows[0].cells[1].firstChild.lastChild;
                    } else {
                        var hsize = parseInt(child.getAttribute("size"));
                        if (isNaN(hsize)) {
                            cell.innerHTML = "<input/>"
                        } else {
                            cell.innerHTML = "<textarea rows='" + hsize + "'></textarea>"
                        } 
                    }
                    cell.className = "ToolProperty-TextInput";
                }
                if (actor == null) actor = cell.lastChild;
                if (child.nodeName == "numeric" && actor.tagName == 'INPUT') {
                    actor.style.width = "24pt";
                    actor.style.textAlign = 'right';
                }
                actor.Name = cmd;
                if (actor.tagName != 'BUTTON') {
                    actor.BinderId = this.BinderId;
                    actor.readOnly = child.getAttribute("mode") != "enabled";
                    actor.value = child.getAttribute("value");
                    actor.onchange = function () {
                        var binder = Qva.GetBinder(this.BinderId);
                        if (!binder.Enabled) return;
                        binder.Set (this.Name, "value", this.value, true);
                    }
                } else {
                    var pos = parseInt(child.getAttribute("value"));
                    pos = isNaN(pos) ? 0 : pos / 2;
                    actor.style.left = pos + 'px';
                    actor.binderid = this.BinderId;
                    actor.moveObj = '*';
                    actor.xOnly = true;
                    actor.xMin = parseInt(child.getAttribute("min")) / 2;
                    actor.xMax = parseInt(child.getAttribute("max")) / 2;
                    actor.disabled = child.getAttribute("mode") != "enabled";
                    actor.style.cursor = actor.disabled ? '' : 'pointer';
                    actor.onmousedown = Qva.Move.mouseDown;
                }

                break;
            case 'check':
                if (indent) cell.style.paddingLeft = indent + 'pt';
                cell.innerHTML = "<input type='checkbox'/> <span></span>"
                cell.lastChild.innerText = label;
                cell.lastChild.className = "ToolProperty-Label";
                
                var actor = cell.firstChild;
                actor.checked = child.getAttribute("value") == "1";
                actor.disabled = child.getAttribute("mode") != "enabled";
                actor.Name = cmd;
                actor.BinderId = this.BinderId;
                actor.onclick = function () {
                    var binder = Qva.GetBinder(this.BinderId);
                    if (!binder.Enabled) return;
                    binder.Set (this.Name, "value", this.checked ? "1" : "0", true)
                }
                break;
            case 'icon':
                cell.innerHTML = "<img />";
                cell.className = "ToolProperty-IconCell";
                var img = cell.firstChild;
                img.className = "ToolProperty-Icon";
                img.disabled = child.getAttribute("mode") != "enabled";
                if (child.getAttribute("menu") == "true") img.position = cmd;
                var imgstamp = null;
                var imgname = child.getAttribute("image");
                if (child.getAttribute("type") == "radio") {
                    imgstamp = child.getAttribute("stamp");
                    imgname += "." + child.getAttribute("value");
                }
                img.src = Qva.GetBinder(this.BinderId).BuildBinaryUrl(child.getAttribute("path"), imgstamp, imgname + (img.disabled ? ".DISABLED" : ""));
                img.title = label;
                cell.BinderId = this.BinderId;
                cell.Name = cmd;
                img.dragAccept = child.getAttribute("accept");
                if (img.dragAccept) {
                    this.DropTargets[this.DropTargets.length] = img;
                }
                var clickable = true;
                var highlight = true;
                switch(child.getAttribute("type")) {
                case "drag":
                    img.dragObj = { 'Name': cmd, 'Type': child.getAttribute("content"), 'Value': child.getAttribute("value") };
                    if (child.getAttribute("dropat") != null) img.dragObj.DropAt = true;
                    img.BinderId = this.BinderId;
                    img.Name = cmd;
                    img.onmousedown = Qva.DragDrop.mouseDown;
//                  clickable = img.dragAccept != null;
                    break;
                case "drop":
                    img.BinderId = this.BinderId;
                    img.Name = cmd;
                    break;
                case "action":
                    cell.Value = child.getAttribute("value");
                    break;
                case "radio":
                    highlight = false;
                    cell.Value = child.getAttribute("value");
                    if (child.getAttribute("selected") == "true") {
                        cell.className = "ToolProperty-IconSelected";
                    } else {
                        cell.className = "ToolProperty-IconUnselected";
                    }
                    break;
                case null:
                    clickable = false;
                    break;
                default:
                    clickable = false;
                    alert(child.getAttribute("type"));
                    break;
                }
                if (clickable) {
                    if (!img.disabled) {
                        if (highlight) {
                            img.onmouseover = function () { this.className = "HighlightImage"; }
                            img.onmouseout = function () { this.className = ""; }
                        }
                        cell.onclick = function() {
                            var binder = Qva.GetBinder(this.BinderId);
                            if (!binder.Enabled) return;
                            if (this.Value != null) {
                                binder.Set (this.Name, "value", this.Value, true);
                            } else {
                                binder.Set (this.Name, "action", "", true);
                            }
                        }
                    }
                }
                break;
            case 'select':
            case 'combo':
                if (label) {
                    cell.innerHTML = "<span></span> <select ></select>"
                    cell.firstChild.innerText = label;
                    cell.firstChild.className = "ToolProperty-Label";
                } else {
                    cell.innerHTML = "<select style='width:100%'></select>"
                }
                var actor = cell.lastChild;
                actor.disabled = child.getAttribute("mode") != "enabled";
                actor.Name = cmd;
                actor.BinderId = this.BinderId;
                actor.onchange = function () {
                    if (this.selectedIndex < 0) return;
                    var binder = Qva.GetBinder(this.BinderId);
                    if (!binder.Enabled) return;
                    var opt = this.options [this.selectedIndex];
                    binder.Set (this.Name, 'value', opt.value, true);
                }
                var choices = child.getElementsByTagName ("option");
                var cholen = choices.length;
                actor.options.length = cholen;
                var currentValue = child.getAttribute("value");
                var found = false;
                for (var ix = 0; ix < cholen; ++ix) {
                    var cho = choices [ix];
                    var opt = actor.options [ix];
                    opt.text = cho.getAttribute("text");
                    opt.value = cho.getAttribute('value');
                    if (opt.value == currentValue) {
                        opt.selected = true;
                        found = true;
                    }
                }
                if (!found) {
                    actor.options.length = cholen + 1;
                    var cho = choices [cholen];
                    var opt = actor.options [cholen];
                    opt.text = "";
                    opt.value = currentValue;
                    opt.selected = true;                    
                }
                if (child.nodeName == "combo") {
                    var selectactor = actor;
                    setTimeout (function () {Qva.Mgr.toolwindowtable.CreateCombobox (selectactor)}, 0);	
                }

                break;
            case 'color':
                cell.disabled = child.getAttribute("mode") != "enabled";
                cell.Name = cmd;
                cell.BinderId = this.BinderId;
                cell.onclick = function () {
                    var binder = Qva.GetBinder(this.BinderId);
                    if (!binder.Enabled) return;
                    binder.Set (this.Name, "value", this.Color, true);
                }
                if (label)
                    cell.title = label;
                var selected = child.getAttribute("selected") == "1";
                cell.innerHTML = "<div>&nbsp</div>"
                cell.Color = child.getAttribute("value");
                cell.firstChild.className = "ToolProperty-Color";
                cell.firstChild.style.backgroundColor = "#" + child.getAttribute("value"); 
                cell.style.backgroundColor = selected ? "Highlight": "Transparent";

                break;
            }
            var warning = child.getAttribute("warning");
            if (warning) {
                var img = document.createElement("IMG");
//                img.src = "htc/Images/Properties/warning.png";
                img.src = Qva.GetBinder(this.BinderId).BuildBinaryUrl(child.getAttribute("path"), null, "warning");
                img.title = warning;
                img.style.paddingLeft = "2pt";
                cell.appendChild(img);
                cell.title = warning;
            }
        }
        while(cix < row.cells.length) row.deleteCell(cix);
        
        // Add drag & drop attributes for row
        var acceptDrop = line.getAttribute("accept");
        var canDrag = (line.getAttribute("type")== "drag") && line.getAttribute("content");
        if (canDrag || acceptDrop) {
            var rowName = line.getAttribute("name");
            if (rowName) rowName = this.BaseName + '.' + rowName;
            row.BinderId = this.BinderId;
            row.Name = rowName;
            if (acceptDrop) {
                var dropTypes = line.getAttribute("at").split(';');
                var dropOn = false;
                var dropBefore = false;
                var dropAfter = false;
                for (var aix = 0; aix < dropTypes.length; aix++) {
                    if (dropTypes[aix] == 'on') dropOn = true;
                    if (dropTypes[aix] == 'before') dropBefore = true;
                    if (dropTypes[aix] == 'after') dropAfter = true;
                }
                row.dropObj = { 'Name': rowName, 'Type': acceptDrop, 'DropOn': dropOn, 'InsertAfter': dropAfter, 'InsertBefore': dropBefore };
                this.DropTargets[this.DropTargets.length] = row;
            }
            if (canDrag) {
                var dragContent = line.getAttribute("content");
                if (line.getAttribute("label")) row.title = line.getAttribute("label");
                row.dragObj = { 'Name': rowName, 'Type': dragContent };
                row.onmouseover = function () { 
                    if(this.className.indexOf("DragTarget")==-1)
                        this.className += " DragTarget"; 
                }               
                row.onmouseout = function () { 
                    //should be " DragTarget" below but initial space doesn't exist in Firefox
                    this.className = this.className.replace("DragTarget","");
                    // trim
                    this.className = this.className.replace(/^\s+|\s+$/g,"")
                }
                row.onmousedown = Qva.DragDrop.mouseDown;
                    
            }
        }

    }
    while(rix < table.rows.length) table.deleteRow(rix);

}

Qva.Mgr.toolwindowbody = function (owner, elem, name, prefix) {
    if (!Qva.MgrSplit (this, name, prefix)) return;
    owner.AddManager(this);
    this.Element = elem;
    this.Managers = {};
    Qva.DragDrop.DropTargets[Qva.DragDrop.DropTargets.length] = this;
}

Qva.Mgr.toolwindowbody.prototype.Scan = function() {
    this.IsCustom = true;
    var scanner = new Qva.Scanner(this);
    scanner.Scan(this.Element, this.Name, this);
}

Qva.Mgr.toolwindowbody.prototype.Inside = function (pos, type) {
    for (var key in this.Managers) {
        var list = this.Managers[key];
        for (var ix = 0; ix < list.length; ++ix) {
            var mgr = list[ix];
            if (!mgr.Inside) continue;
            var fnd = mgr.Inside(pos, type);
            if (fnd != null) return fnd; 
        }
    }
    return null;
}

Qva.Mgr.toolwindowbody.prototype.Paint = function(mode, node, prefix) {
    this.Touched = true;
    if (this.IsCustom) {
        for (var child = node.firstChild; child != null; child = child.nextSibling) {
            var name = child.getAttribute("name");
            var mode = child.getAttribute("mode");
            switch (mode) {
                case "hidden": mode = 'h'; break;
                case "enabled": mode = 'e'; break;
                default: mode = 'd'; break;
            }
            var list = this.Managers[prefix + '.' + name];
            if (list) {
                for (var ix = 0; ix < list.length; ++ix) {
                    var mgr = list[ix];
                    mgr.Paint(mode, child, name);
                }
            }
        }
    }
    var selected = node.getAttribute("value");
    if (selected != this.Selected) {
        this.Managers = {};
        this.Element.innerHTML = "<table width='95%'></table>";
        var table = this.Element.firstChild;
        table.cellpadding = 0;
        table.cellspacing = 0;
        for (var child = node.firstChild; child != null; child = child.nextSibling) {
            if (child.nodeName != 'property') continue;
            var row = table.insertRow(-1);
            var cell = row.insertCell(-1);
            var name = child.getAttribute("name");
            var indent = child.getAttribute('indent');
            if (indent) indent = 12 * parseInt(indent);
            cell.innerHTML = "<table width='98%'><colgroup></colgroup></table>";
            cell.colSpan = 2;
            var mgr = new Qva.Mgr.toolwindowtable(this, cell.firstChild, '.' + name, this.Name);
        }
        this.Body = table.tBodies[0];
        this.Selected = selected;
    }
    for (var child = node.firstChild; child != null; child = child.nextSibling) {
        if (child.nodeName != 'property') continue;
        var name = this.Name + '.' + child.getAttribute('name');
        var list = this.Managers[name];
        if (list != null) {
            var mode = 'd';
            switch (child.getAttribute('mode')) {
                case "hidden":
                    mode = 'h';
                    break;
                case "enabled":
                    mode = 'e';
                    break;
            }
            var xlen = list.length;
            for (var ixx = 0; ixx < xlen; ++ixx) {
                var mgr = list[ixx];
                var mgrmode = mode;
                if (mode != 'n' && mgr.HideIf && mgr.HideIf(child.getAttribute('value'), child.getAttribute('text'))) mgrmode = 'n';
                mgr.Paint(mgrmode, child, name);
            }
        }
    }
    // adjust height to fit frame
    //    var frame = this.Element.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode;
    //    this.Element.style.height = parseInt(frame.style.height) - 47 + 'pt';
    //  
}

Qva.Mgr.toolwindowbody.prototype.Append = function () {}

Qva.Mgr.toolwindowbody.prototype.AddManager = function (mgr) {
    mgr.PageBinder = this;
    mgr.Touched = false;
    mgr.Dirty = false;
    var list = this.Managers[mgr.Name];
    if (list == null) {
        list = [];
        this.Managers[mgr.Name] = list;
    }
    list.push(mgr);
}
Qva.Mgr.toolwindownavigation = function (owner, elem, name, prefix) {
    if (!Qva.MgrSplit (this, name, prefix)) return;
    owner.AddManager(this);
    this.Element = elem;
    this.Managers = {};
}

Qva.Mgr.toolwindownavigation.prototype.Paint = function(mode, node) {
    this.Touched = true;
    var div = this.Element;
    if (mode == 'h') {
        div.style.display = 'none';
        return;
    }
    div.style.display = '';
    while (div.firstChild) div.removeChild(div.firstChild);
    div.innerHTML = "";
    var choices = node.getElementsByTagName("option");
    var cholen = choices.length;
    for (var ix = 0; ix < cholen; ++ix) {
        if (ix > 0) {
            var sep = document.createElement("span");
            sep.innerText = " > ";
            div.appendChild(sep);
        }
        var cho = choices[ix];
        var opt = document.createElement("span");
        if (ix < cholen - 1) {
            opt.className = "Navigation-Link";
            opt.BinderId = this.BinderId;
            opt.Name = this.Name;
            opt.Link = cho.getAttribute("value");
            opt.onclick = function() {
                var binder = Qva.GetBinder(this.BinderId);
                if (!binder.Enabled) return;
                binder.Set(this.Name, "value", this.Link, true);
            }
        }
        opt.innerText = cho.getAttribute("text");
        div.appendChild(opt);
    }
}
Qva.Mgr.toolwindowtable.CreateCombobox = function(selectactor) {
    var actor2 = document.createElement ("input");
    actor2.style.border = "none";
    actor2.style.marginTop = "2px";
    actor2.style.marginLeft = "4px";
    actor2.style.position = "absolute";
    actor2.style.height = Math.max(parseInt(selectactor.offsetHeight) - 6, 0) + "px";
    actor2.style.left = parseInt(selectactor.parentNode.offsetLeft) + "px";
    actor2.style.width = Math.max(parseInt(selectactor.offsetWidth) - 21, 0) + "px";
    if (selectactor.selectedIndex>=0) actor2.value = selectactor.options[selectactor.selectedIndex].text;
   
    selectactor.parentNode.appendChild (actor2);
    actor2.Name = selectactor.Name;
    actor2.BinderId = selectactor.BinderId;
    if (selectactor.disabled) {
        actor2.disabled = true;
    } else {
        actor2.onchange = function () {
            var binder = Qva.GetBinder(this.BinderId);
            if (!binder.Enabled) return;
            binder.Set (this.Name, "text", this.value, true);
        }
    }

}

Qva.ToggleToolPane = function(row) {
    var pane = row.nextSibling;
    if (pane.style.display == 'none') {
        row.cells[0].firstChild.src = 'collapse.png';
        pane.style.display = '';
    } else {
    row.cells[0].firstChild.src = 'expand.png';
    pane.style.display = 'none';
}
}