function Box(setId)
{
  this.id = setId;
  this.boxName = "box_"+setId;
  this.ajax = null;
  this.mouse = null;
}


Box.prototype.open=function(url)
{
  if(this.ajax == null)
  {
    this.ajax = new Ajax(url.replace("http://www.1w.se/",Global.toRoot)+"?ajax=true&id="+this.id, null, this.boxName+"_loading", this, "responseXML", null, "get");
  }
}

Box.prototype.sendMail=function()
{
  if(this.ajax == null)
  {
    try
    {
      var from = encodeURIComponent(document.getElementById(this.boxName+"_from").value);
      var subject = encodeURIComponent(document.getElementById(this.boxName+"_subject").value);
      var message = encodeURIComponent(document.getElementById(this.boxName+"_message").value);
    }
    catch (e)
    {
      document.getElementById(this.boxName+"_content").innerHTML = "        <div id=\""+this.boxName+"_loading\"><span></span></div><br /><br /><br /><strong>Ditt mail gick inte att skicka</strong><br />";
      return;
    }
    this.ajax = new Ajax(Global.toRoot+"kontakta/send/?id="+this.id, null, this.boxName+"_loading", this, "responseXML", "from="+from+"&subject="+subject+"&message="+message, "post");
  }
}

Box.prototype.loaded=function()
{
  try
  {
    if(this.ajax.content == null)
    {
      document.getElementById(this.boxName+"_content").innerHTML = "        <div id=\""+this.boxName+"_loading\"><span></span></div><br /><br /><br /><strong>Datan kunde inte laddas</strong><br />";
      this.ajax = null;
      return;
    }
    var page = this.ajax.content.getElementsByTagName("page");
    if(page.length > 0)
    {
      var menu = page[0].getElementsByTagName("menu");
      var content = page[0].getElementsByTagName("content");
      if(menu.length > 0 && content.length > 0)
      {
        document.getElementById(this.boxName+"_title").innerHTML = page[0].getAttribute("title");
        document.getElementById(this.boxName+"_icon").src = page[0].getAttribute("icon");
        document.getElementById(this.boxName).style.height = page[0].getAttribute("height");
        document.getElementById(this.boxName).style.width = page[0].getAttribute("width");

        document.getElementById(this.boxName+"_menu").innerHTML = "";        
        HtmlGen.innerXml(document.getElementById(this.boxName+"_menu"), menu[0]);
        
        document.getElementById(this.boxName+"_content").innerHTML = "        <div id=\""+this.boxName+"_loading\"><span></span></div>";
        HtmlGen.innerXml(document.getElementById(this.boxName+"_content"), content[0]);
        
        this.ajax = null;
        
        return;
      }
    }
    document.getElementById(this.boxName+"_content").innerHTML = "        <div id=\""+this.boxName+"_loading\"><span></span></div><br /><br /><br /><strong>All data kunde inte laddas</strong><br /><br />";
    this.ajax = null;
  }
  catch(ex)
  {
    document.getElementById(this.boxName+"_content").innerHTML = "        <div id=\""+this.boxName+"_loading\"><span></span></div><br /><br /><br /><strong>All data kunde inte laddas</strong><br /><br />";
    this.ajax = null;
  }
}

Box.prototype.draw=function(url, icon, title, width, height, top, left)
{
  
  var html  = "    <div id=\""+this.boxName+"\" class=\"box\" style=\"top: "+top+"; left:"+left+"; width: "+width+"; z-index: "+Global.zIndex()+";\">";
      html += "      <div class=\"box_head_left\"></div>";
      html += "      <div class=\"box_head\" onmousedown=\"BoxStatic.moveStart\">";
      html += "        <img src=\""+Global.toRoot+"Layout/Images/Box/"+icon+"\" id=\""+this.boxName+"_icon\" alt=\"\" class=\"icon\" />";
      html += "        <h1 id=\""+this.boxName+"_title\">"+title+"</h1>";
      html += "        <a href=\"http://www.1w.se/\" class=\"close\" id=\""+this.boxName+"_close\" onmouseout=\"ToolTip.hide\" onclick=\"BoxStatic.close\" name=\""+this.id+"\"><img src=\""+Global.toRoot+"/Layout/Images/Box/Close.png\" alt=\"Stäng\"/></a>";
      html += "        <ul class=\"boxmenu\" id=\""+this.boxName+"_menu\">";
      html += "        </ul>";
      html += "      </div>";
      html += "      <div class=\"box_head_right\"></div>";
      html += "      <div class=\"box_shade\"></div>";
      html += "      <div class=\"box_content\" id=\""+this.boxName+"_content\">";
      html += "        <div id=\""+this.boxName+"_loading\"><span></span></div>";
      html += "      </div>";
      html += "      <div class=\"box_bottom_left\"></div>";
      html += "      <div class=\"box_bottom\"></div>";
      html += "      <div class=\"box_bottom_right\"></div>";
      html += "    </div>";
      
  HtmlGen.innerHtml(document.getElementById("main"), html);
  
  document.getElementById(this.boxName+"_close").onmouseover= function(event) {ToolTip.show(event, this, "<span>Stäng</span>", null, -28, false, false);};
  
  this.open(url);
}

Box.prototype.close=function(event)
{
  var dom = document.getElementById(this.boxName);
  dom.parentNode.removeChild(dom);
}

Box.prototype.print=function()
{
}

Box.prototype.moveStart=function(event)
{
  this.mouse = Mouse.position(event);
  document.getElementById(this.boxName).style.zIndex = Global.zIndex();
}

Box.prototype.moveStop=function()
{
  this.mouse = null;
}

Box.prototype.move=function(event)
{
  if(this.mouse == null)
  {
    return;
  }
  var mouse = Mouse.position(event);
  var X = parseInt(document.getElementById(this.boxName).style.left)+mouse.X - this.mouse.X;
  var Y = parseInt(document.getElementById(this.boxName).style.top) +mouse.Y - this.mouse.Y;
  this.mouse.X = mouse.X;
  this.mouse.Y = mouse.Y<-10?-10:mouse.Y;
  
  Y = Y<-10?-10:Y;
  document.getElementById(this.boxName).style.left = X+"px";
  document.getElementById(this.boxName).style.top  = Y+"px";
}
