/*
Duosi Principles of Computer Composition Virtual Experiment System ,DS-VLAB v2.0
Copyright(C)2013 ZHANG Wen-fen, Email: yydzhwf@xnu.edu.cn  Address: Xiangnan University, Chenzhou Hunan, China
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public 
License as published by the Free Software Foundation, either version 3 of the License, or any later version.

多思计算机组成原理网络虚拟实验系统, DS-VLAB v2.0 
版权所有(C) 张雯雰, 电子邮箱: yydzhwf@xnu.edu.cn
本程序为自由软件；您可依据自由软件基金会所发表的GNU GENERAL PUBLIC LICENSE，对本程序再次发布和/或修改。

作者：湘南学院计算机与人工智能学院张雯雰老师
*/



//此函数使对象能够被拖动  o:对象ID  s:是否随屏幕滚动而移动
function dragEnabled(o, s) {

    if (typeof o == "string") o = document.getElementById(o);
    o.orig_x = parseInt(o.style.left) - document.documentElement.scrollLeft;
    o.orig_y = parseInt(o.style.top) - document.documentElement.scrollTop;
    o.orig_index = o.style.zIndex;

    o.onmousedown = function (a) {
        if (!a) a = window.event;
        this.style.zIndex = 10000;
        var d = document;
        var startX = a.clientX + d.documentElement.scrollLeft;
        var startY = a.clientY + d.documentElement.scrollTop;
        var isGroupMove = false;
        var groupDivs = [];
        var groupOffsets = [];

        // 判断是否为多选组拖动
        if (window.mycircuit && Array.isArray(mycircuit.selectedComponents) && mycircuit.selectedComponents.length > 1) {
            // 判断当前拖动的o是否在选中组内
            var found = false;
            for (var i = 0; i < mycircuit.selectedComponents.length; i++) {
                if (mycircuit.selectedComponents[i] && mycircuit.selectedComponents[i].id === o.id) {
                    found = true;
                    break;
                }
            }
            if (found) {
                isGroupMove = true;
                // 记录所有选中器件的div和初始偏移
                for (var i = 0; i < mycircuit.selectedComponents.length; i++) {
                    var comp = mycircuit.selectedComponents[i];
                    if (!comp) continue;
                    var compDiv = document.getElementById(comp.id);
                    if (!compDiv) continue;
                    groupDivs.push(compDiv);
                    groupOffsets.push({
                        left: parseInt(compDiv.style.left) - document.documentElement.scrollLeft,
                        top: parseInt(compDiv.style.top) - document.documentElement.scrollTop
                    });
                }
            }
        }

        var x = startX - o.offsetLeft;
        var y = startY - o.offsetTop;

        if (o.setCapture)
            o.setCapture();
        // Removed deprecated window.captureEvents for modern browser compatibility

        d.onmousemove = function (a) {
            if (!a) a = window.event;
            var curX = a.clientX + d.documentElement.scrollLeft;
            var curY = a.clientY + d.documentElement.scrollTop;
            if (isGroupMove) {
                // 计算整体偏移
                var dx = curX - startX;
                var dy = curY - startY;
                for (var i = 0; i < groupDivs.length; i++) {
                    var compDiv = groupDivs[i];
                    var offset = groupOffsets[i];
                    compDiv.style.left = (offset.left + dx) + "px";
                    compDiv.style.top = (offset.top + dy) + "px";
                    compDiv.orig_x = offset.left + dx;
                    compDiv.orig_y = offset.top + dy;
                    if (window.mycircuit && typeof mycircuit.lineReplace === 'function') {
                        mycircuit.lineReplace(compDiv);
                    }
                }
            } else {
                o.style.left = curX - x + "px";
                o.style.top = curY - y + "px";
                o.orig_x = parseInt(o.style.left) - document.documentElement.scrollLeft;
                o.orig_y = parseInt(o.style.top) - document.documentElement.scrollTop;
                if (window.mycircuit && typeof mycircuit.lineReplace === 'function') {
                    mycircuit.lineReplace(o);
                }
            }
        }

        d.onmouseup = function () {
            if (o.releaseCapture)
                o.releaseCapture();
            // Removed deprecated window.captureEvents for modern browser compatibility
            d.onmousemove = null;
            d.onmouseup = null;
            o.style.zIndex = o.orig_index;
            if (isGroupMove) {
                for (var i = 0; i < groupDivs.length; i++) {
                    if (window.mycircuit && typeof mycircuit.lineReplace === 'function') {
                        mycircuit.lineReplace(groupDivs[i]);
                    }
                }
            } else {
                if (window.mycircuit && typeof mycircuit.lineReplace === 'function') {
                    mycircuit.lineReplace(o);
                }
            }
        };
    };

    if (s) {
        var orig_scroll = window.onscroll ? window.onscroll : function () { };
        window.onscroll = function () {
            orig_scroll();
            o.style.left = o.orig_x + document.documentElement.scrollLeft;
            o.style.top = o.orig_y + document.documentElement.scrollTop;
        }
    };
};