var objDrag = null;
var objAnz = null;
var objLeer = null;
var dragobjekt = null;
var mouseX = 0;
var mouseY = 0;
var offX = 0;
var offY = 0;
var akt_id = 0;
var dragx = 0;
var dragy = 0;

IE = document.all&&!window.opera;
DOM = document.getElementById&&!IE;

var css_code = "<style type='text/css'>" +
               "<!--" +
               ".box {" +
               "margin:0;" +
               "border:"+border+";" +
               "}" +
               ".content {" +
               "overflow:auto;" +
               "}" +
               ".leer {" +
               "border:solid 0px;" +
               "background-color:"+leer_farbe+";" +
               "}" +
               "-->" +
               "</style>";

document.write(css_code);

function write_box(id,typ,style,left,top,width,height,padding,bgcol,inhalt) {
  var cont_style = " style='width:"+width+"px;padding:"+padding+";background-color:"+bgcol+";";
  var box_style = " style='width:"+width+"px;height:"+height+"px;background-color:"+bgcol+";";
  var leer_style = " style='width:"+width-eck_icon_width+"px;'";
  var box_kopf = "";
  var box_fuss = "";

  if(id == "")
    id=0;
  if(typ == "")
    typ = "div";
  if(style == "")
    style = "r";

  if(style.search("r")>=0) {
    box_fuss = "<tr><td class='leer' id='leerfeld_"+id+"'"+leer_style+">&nbsp;</td><td style='font-size:1px; background-image: url(\""+eck_icon_url+"\"); width:"+eck_icon_width+"px; height:"+eck_icon_height+"px; cursor:se-resize; border:solid 0px;' onmousedown='startSize("+id+");'>&nbsp;</td></tr>";
  }
  if(style.search("h")>=0) {
    if(box_kopf_close=="")
      box_kopf = "<tr><td colspan='2' align='right' valign='middle' style='background-color:"+box_kopf_farbe+";height:"+box_kopf_hoehe+"px;' onmousedown='dragstart("+id+")'></td></tr>";
    else
      box_kopf = "<tr><td colspan='2' align='right' valign='middle' style='background-color:"+box_kopf_farbe+";height:"+box_kopf_hoehe+"px;' onmousedown='dragstart("+id+")'><a href='javascript:closebox("+id+")' style='margin-right:5px;' class='box_kopf_link'>"+box_kopf_close+"</a></td></tr>";
    if(style.search("d")>=0) {
      box_style = box_style + "position:absolute; left:"+left+"px;top:"+top+"px;";
    }
  }

  var box_kopf = "<table id='BoxID_"+id+"' class='box' border='0' cellspacing='0' cellpadding='0'"+box_style+"'>" + box_kopf;

  if(style.search("r")<0)
    cont_style = cont_style + "height:"+height+"px;'";
  else
    cont_style = cont_style + "height:"+(height-eck_icon_height)+"px;'";

  var box_content = "<tr>" +
                    "<td colspan='2'>"+
                    "<"+typ+" id='DivID_"+id+"' name='DivID_"+id+"' class='content'"+cont_style+">" +
                    inhalt +
                    "</"+typ+">" +
                    "</td>" +
                    "</tr>";

  box_fuss = box_fuss + "</table>";

  var box = box_kopf + box_content + box_fuss;
  document.write(box);
}



function init(){
  document.onmousemove = doSize;
  document.onmouseup = stopSize;
}


function startSize(id) {
  akt_id = id;

  var boxid = "BoxID_" + id;
  var divid = "DivID_" + id;
  var leerid = "leerfeld_" + id;

  objDrag = document.getElementById(boxid);
  objAnz = document.getElementById(divid);
  objLeer = document.getElementById(leerid);

  offX = mouseX - objDrag.offsetWidth;
  offY = mouseY - objDrag.offsetHeight;
}

function doSize(ereignis) {
  mouseX = (IE) ? window.event.clientX : ereignis.pageX;
  mouseY = (IE) ? window.event.clientY : ereignis.pageY;

  if (objDrag != null) {
    objDrag.style.width = (mouseX - offX) + "px";
    objDrag.style.height = (mouseY - offY) + "px";

    objAnz.style.width = (mouseX - offX) + "px";
    objAnz.style.height = ((mouseY - offY)-20)  + "px";
    objLeer.style.width = ((mouseX - offX)-20)  + "px";
  }
  if(dragobjekt != null) {
    dragobjekt.style.left = (mouseX - dragx) + "px";
    dragobjekt.style.top = (mouseY - dragy) + "px";
  }
}

function stopSize(ereignis) {
  objDrag = null;
  objAnz = null;
  objLeer = null;
  dragobjekt = null;
}

function closebox(id) {
  var elem = "BoxID_"+id;
  document.getElementById("body").removeChild(document.getElementById(elem));
}

function dragstart(id) {
  var boxid = "BoxID_" + id;
  dragobjekt = document.getElementById(boxid);
  dragx = mouseX - dragobjekt.offsetLeft;
  dragy = mouseY - dragobjekt.offsetTop;
}
