var Tables = Array();
var Joints = Array();


function AddJoint(objA, objB){
    var Editor = document.getElementById('VisualEditor');
    if (!Editor || !objA || !objB) return false;

    objA.Left   = objA.offsetLeft +GetTable(objA).offsetLeft;
    objA.Right  = objA.offsetLeft +objA.offsetWidth +GetTable(objA).offsetLeft +parseInt(document.getElementById(objA.id+'_Type').offsetWidth);
    objB.Left   = objB.offsetLeft +GetTable(objB).offsetLeft;
    objB.Right  = objB.offsetLeft +objB.offsetWidth +GetTable(objB).offsetLeft +parseInt(document.getElementById(objB.id+'_Type').offsetWidth);
    objA.Center = objA.offsetTop + objA.offsetHeight /2 +GetTable(objA).offsetTop;
    objB.Center = objB.offsetTop + objB.offsetHeight /2 +GetTable(objB).offsetTop;


    /* Get Joints */
    var JointName  = '|'+GetTable(objA).id +'|' +objA.id +'|' +GetTable(objB).id +'|' +objB.id;
    var JointName2 = '|'+GetTable(objB).id +'|' +objB.id +'|' +GetTable(objA).id +'|' +objA.id;
    if ( document.getElementById('JAH' +JointName2) ) JointName = JointName2;


    if ( !document.getElementById('JAH' +JointName) ) {
        var Color = getColor();

        var div = document.createElement('div');
            div.id = 'JAH'+ JointName;
            div.className = 'jointH';
            div.style.border = '1px solid '+Color;
            div.onclick = function ( e ) { JointAcion(div.id); }
        Editor.appendChild(div);

        var div = document.createElement('div');
            div.id = 'JBH'+ JointName;
            div.className = 'jointH';
            div.style.border = '1px solid '+Color;
            div.onclick = function ( e ) { JointAcion(div.id); }
        Editor.appendChild(div);

        var div = document.createElement('div');
            div.id = 'JV'+ JointName;
            div.className = 'jointV';
            div.style.border = '1px solid '+Color;
            div.onclick = function ( e ) { JointAcion(div.id); }
        Editor.appendChild(div);

        var img = document.createElement('img');
            img.id = 'JAB'+ JointName;
            img.className = 'jointB';
            img.src = './img/Bullet.gif';
        Editor.appendChild(img);

        var img = document.createElement('img');
            img.id = 'JBB'+ JointName;
            img.className = 'jointB';
            img.src = './img/Bullet.gif';
        Editor.appendChild(img);
    }

    var JAB = document.getElementById('JAB' +JointName);    // JAB - Joint A Bullet
    var JAH = document.getElementById('JAH' +JointName);    // JAH - Joint A Horizontal
    var JBB = document.getElementById('JBB' +JointName);    // JBB - Joint A Bullet
    var JBH = document.getElementById('JBH' +JointName);    // JBH - Joint B Horizontal
    var JV  = document.getElementById('JV' +JointName);     // JV  - Joint Vertical
    /*/Get Joints */


    if (objA.Right < objB.Left){        // 1
        JAB.style.left  = Normalize(objA.Right);
        JBB.style.left  = Normalize(objB.Left -JBB.offsetWidth);

        JAH.style.width = Normalize((objB.Left -objA.Right) /2 -((document.all) ? 2 : 2));
        JBH.style.width = Normalize(JAH.style.width);

        JAH.style.left  = Normalize(parseInt(JAB.style.left));
        JBH.style.left  = Normalize(parseInt(JBB.style.left) -parseInt(JAH.style.width) +JBB.offsetWidth /2);


        JV.style.left   = Normalize(parseInt(JAB.style.left) +parseInt(JAH.style.width));
        if ( JBB.offsetTop > JAB.offsetTop ){
            JV.style.top    = Normalize(parseInt(JAB.offsetTop) +parseInt(JAB.offsetHeight) /2);
            JV.style.height = Normalize(parseInt(JBB.offsetTop) -parseInt(JAB.offsetTop) +((document.all) ? 2 : -1));
        }else{
            JV.style.top    = Normalize(parseInt(JBB.offsetTop) +parseInt(JBB.offsetHeight) /2);
            JV.style.height = Normalize(parseInt(JAB.offsetTop) -parseInt(JBB.offsetTop) +((document.all) ? 2 : -1));
        }
    }else if(objB.Right < objA.Left){   // 2
        JAB.style.left = Normalize(objA.Left -JAB.offsetWidth);
        JBB.style.left = Normalize(objB.Right);

        JAH.style.width = Normalize((objA.Left -objB.Right) /2                              -((document.all) ? 2 : 2));
        JBH.style.width = Normalize(JAH.style.width);

        JAH.style.left = Normalize(parseInt(JAB.style.left) -parseInt(JAH.style.width) +JBB.offsetWidth /2);
        JBH.style.left = Normalize(JBB.style.left);

        JV.style.left   = Normalize(parseInt(JBB.style.left) +parseInt(JBH.style.width));
        if ( JBB.offsetTop > JAB.offsetTop ){
            JV.style.top    = Normalize(parseInt(JAB.offsetTop) +parseInt(JAB.offsetHeight) /2);
            JV.style.height = Normalize(parseInt(JBB.offsetTop) -parseInt(JAB.offsetTop) +((document.all) ? 2 : -1));
        }else{
            JV.style.top    = Normalize(parseInt(JBB.offsetTop) +parseInt(JBB.offsetHeight) /2);
            JV.style.height = Normalize(parseInt(JAB.offsetTop) -parseInt(JBB.offsetTop) +((document.all) ? 2 : -1));
        }
    }else if(objB.Right < objA.Right){  //3
        JAB.style.left  = Normalize(objA.Right);
        JBB.style.left  = Normalize(objB.Right);

        JAH.style.width = Normalize(0);
        JBH.style.width = Normalize(objA.Right -objB.Right +((document.all) ? 2 : -1));

        JAH.style.left  = Normalize(JAB.style.left);
        JBH.style.left  = Normalize(parseInt(JBB.style.left) +parseInt(JAB.offsetWidth) /2);

        JV.style.left   = Normalize(parseInt(JAB.style.left) +parseInt(JAB.offsetWidth)  /2 +((document.all) ? 0 : -1));
        if ( JBB.offsetTop > JAB.offsetTop ){
            JV.style.top    = Normalize(parseInt(JAB.offsetTop) +parseInt(JAB.offsetHeight) /2);
            JV.style.height = Normalize(parseInt(JBB.offsetTop) -parseInt(JAB.offsetTop) +((document.all) ? 2 : -1));
        }else{
            JV.style.top    = Normalize(parseInt(JBB.offsetTop) +parseInt(JBB.offsetHeight) /2);
            JV.style.height = Normalize(parseInt(JAB.offsetTop) -parseInt(JBB.offsetTop) +((document.all) ? 2 : -1));       	
        }
    }else{                              // 4
        JAB.style.left = Normalize(objA.Right);
        JBB.style.left = Normalize(objB.Right);

        JAH.style.width = Normalize(objB.Right -objA.Right +((document.all) ? 4 : 3));
        JBH.style.width = Normalize(0);

        JAH.style.left = Normalize(JAB.style.left);
        JBH.style.left = Normalize(JBB.style.left);

        JV.style.left   = Normalize(parseInt(JBB.style.left) +parseInt(JBB.offsetWidth) /2 +((document.all) ? 0 : -1));
        if ( JBB.offsetTop > JAB.offsetTop ){
            JV.style.top    = Normalize(parseInt(JAB.offsetTop) +parseInt(JAB.offsetHeight) /2);
            JV.style.height = Normalize(parseInt(JBB.offsetTop) -parseInt(JAB.offsetTop) +((document.all) ? 2 : -1));
        }else{
            JV.style.top    = Normalize(parseInt(JBB.offsetTop) +parseInt(JBB.offsetHeight) /2);
            JV.style.height = Normalize(parseInt(JAB.offsetTop) -parseInt(JBB.offsetTop) +((document.all) ? 2 : -1));
        }
    }

    JAB.style.top  = Normalize(objA.Center -JAB.offsetHeight /2);
    JBB.style.top  = Normalize(objB.Center -JBB.offsetHeight /2);

    JAH.style.top  = Normalize(objA.Center +((document.all) ? 0 : -1));
    JBH.style.top  = Normalize(objB.Center +((document.all) ? 0 : -1));
}


function Normalize( num ){
    return Math.round(Math.abs(parseInt(num))) +'px';
}


function updateNodes( table ){
    for ( var Key in Joints[table] ){
        if ( !Joints[table][Key] ) continue;
        AddJoint(
            document.getElementById(Joints[table][Key]['a']),
            document.getElementById(Joints[table][Key]['b'])
        );
    }
}


function JointAcion( name ){
    var jointInfo = name.split('|');

    DelJoint( jointInfo[1], jointInfo[2], jointInfo[3], jointInfo[4] );
}


function DelJoint( tableA, fieldA, tableB, fieldB ){
    if( Joints[tableA] ) {
        for (var Key in Joints[tableA] ){
            if ( Joints[tableA][Key] && Joints[tableA][Key]['a'] == fieldA && Joints[tableA][Key]['b'] == fieldB ){
                Joints[tableA][Key] = null;
            }
        }
    }

    if( Joints[tableB] ) {
        for (var Key in Joints[tableB] ){
            if ( Joints[tableB][Key] && Joints[tableB][Key]['a'] == fieldA && Joints[tableB][Key]['b'] == fieldB ){
                Joints[tableB][Key] = null;
            }
        }
    }

    DelElement( 'JAH|' +tableA +'|' +fieldA +'|' +tableB +'|' +fieldB );
    DelElement( 'JBH|' +tableA +'|' +fieldA +'|' +tableB +'|' +fieldB );
    DelElement( 'JV|' +tableA +'|' +fieldA +'|' +tableB +'|' +fieldB );
    DelElement( 'JAB|' +tableA +'|' +fieldA +'|' +tableB +'|' +fieldB );
    DelElement( 'JBB|' +tableA +'|' +fieldA +'|' +tableB +'|' +fieldB );
}


function DelElement( objID ) {
    document.getElementById('VisualEditor').removeChild( document.getElementById( objID ) );
}


function DBDatabasesChange( obj ){
    var oobj = document.getElementById('DBTables');
    for (var i=oobj.childNodes.length-1; i>=0; i--){
        oobj.removeChild(oobj.childNodes[i]);
    }

    aSQL.DB = obj.options[obj.selectedIndex].value;
    aSQL.GetTableList( 'DBTables', 'LI' );
}


function DBDatabases_change(event){
    event = getEvent(event);

    var obj = document.getElementById('DBTables');
    for (var i=obj.childNodes.length-1; i>=0; i--){
        obj.removeChild(obj.childNodes[i]);
    }

    aSQL.DB = getEventTarget(event).text;
    aSQL.GetTableList( 'DBTables', 'LI' );
}