/*
document.js.php

Copyright (c) Gareth Hadfield 2008

Opendots HTML Document Handling
*/

function is_template_attribute(aName){
  return(head_is(aName, OPENDOTS_TP));
}

function set_head_style(aValue){
  var aStyles = getn("style");
  var aHeadStyle;

  if((aStyles != undefined) && (aStyles.length != 0)){
    aHeadStyle = aStyles[0];
  }

  if(aHeadStyle == undefined){
    aHeadStyle = document.createElement("style");
    aHeadStyle.setAttribute("type", "text/css");
    getn("head")[0].appendChild(aHeadStyle);
  }

  if(aHeadStyle != undefined){
    if(IS_IE){
      aHeadStyle.styleSheet.cssText = aValue;
    }
    else{
      // clear existing style info
      while(aHeadStyle.firstChild != undefined){
        var aStyle = aHeadStyle.firstChild;
        aHeadStyle.removeChild(aStyle);
      }

      // add new style info
      aHeadStyle.appendChild(document.createTextNode(aValue));
    }
  }
}

function set_head_script(aValue){
  if(aValue == undefined){
    aValue = "";
  }

  var aScripts = getn("script");
  var aHeadScript;

  if((aScripts != undefined) && (aScripts.length!=0)){
    aHeadScript = aScripts[0];
  }

  if(aHeadScript == undefined){
    aHeadScript = document.createElement("script");
    aHeadScript.type = "text/javascript";
    getn("head")[0].appendChild(aHeadScript);
  }

  if(aHeadScript != undefined){
    if(aHeadScript.innerHTML != aValue){
      if(IS_IE){
        aHeadScript.text = aValue;
      }
      else{
        aHeadScript.innerHTML = aValue;
      }
      eval(aValue);
    }
  }
}

function init_document_methods(){
  // methods to allow for getting and setting of document CSS and HTML etc
  var db = document.body;

  db.setCSS = bind(db, function(aNewCSS){
    setCSS(this, aNewCSS);
  });

  db.getCSS = bind(db, function(){
    return(getCSS(this));
  });

  db.doAddTemplateInfo = bind(db, function(aName){
    return((aName != "template") && (!head_is(OPENDOTS_TP, aName)));
  });

  db.setAttributeText = bind(db, function(aName, aValue){

    if(aName != "id"){// can not change id

      if(aName == OPENDOTS_TP+"template"){
        // load the template's template
        template_load(aValue);
      }
      else if(this.doAddTemplateInfo(aName)){
        aValue = this.getTemplateInfo(aName) + aValue;
      }

      if(is_template_attribute(aName)){
        // grab a copy of the current non-template info before any more is
        // added
        var aNonTemplateName = aName.substr(OPENDOTS_TP.length);
        var aNonTemplateValue = this.getAttributeText(aNonTemplateName);

        if(aNonTemplateValue == undefined){
          aNonTemplateValue = "";
        }
      }

      if(aName == "head_style"){
        set_head_style(aValue);
        setAttributeText(this, aName, aValue);
        refresh_sub_classes();
      }
      else if(aName == "head_extra"){
        var aHead = getn("head")[0];
        if(aHead != undefined){
          for(var iNode=aHead.childNodes.length-1; iNode >= 0; iNode--){
            var aNode = aHead.childNodes[iNode];
            if(aNode.extra_head===true){
              aHead.removeChild(aNode);
            }
          }
          var aExtraHead = document.createElement("div"); // changed to div since safari doesnt like head
          aExtraHead.innerHTML = aValue;
          while(aExtraHead.childNodes.length > 0){
            var aNode = aExtraHead.childNodes[0];
            aNode.extra_head=true;
            aHead.appendChild(aNode);
          }
          delete aExtraHead; // todo ?
        }
        setAttributeText(this, aName, aValue);
        //refresh_sub_classes(); // todo ?
      }
      else if(aName == "head_script"){
        set_head_script(aValue);
        setAttributeText(this, aName, aValue);
      }
      else if(aName == "document_title"){
        document.title = aValue;
        setAttributeText(this, aName, aValue);
      }
      else if(aName == "template"){
        // load all the info from the given template
        var aCurrentTemplate = this.getAttributeText("template");
        if(aCurrentTemplate == undefined){
          aCurrentTemplate = "";
        }

        if(aCurrentTemplate != ""){
          // remove template info
          this.initTemplate();

          this.keepDots = all_dots();
          this.keepDots = delete_all_template_dots(this.keepDots);

          var aPropertiesDiv = getPropertiesDiv();
          aPropertiesDiv.freeOnClose = true;
          aPropertiesDiv.close();

          var aOpendotsListDiv = getOpendotsListDiv();
          aOpendotsListDiv.close();
        }

        template_load(aValue);

        setAttributeText(this, aName, aValue);

        if(aCurrentTemplate != ""){
          replace_child_dots(this.keepDots);
          opendot_properties_div();
          opendots_list_div();
        }
      }
      else{
        setAttributeText(this, aName, aValue);
      }

      if(is_template_attribute(aName)){
        if(aNonTemplateName != "template"){
          // refresh the attribute to pick up the template info
          this.setAttributeText(aNonTemplateName, aNonTemplateValue);
        }
      }

    }

  });

  db.sa = bind(db, function(aName, aValue){
    // a shorthand for setAttribute
    return(this.setAttribute(aName, aValue));
  });

  db.sat = bind(db, function(aName, aValue){
    // a shorthand for setAttributeText
    return(this.setAttributeText(aName, aValue));
  });

  db.getTemplateInfo = bind(db, function(aName){
    var result = this.getAttributeText(OPENDOTS_TP + aName);
    if(result == undefined){
      result = "";
    }
    return(result);
  });

  db.removeTemplateInfo = bind(db, function(aValue, aName){
    var result = aValue;
    if((result != undefined) && (!is_template_attribute(aName))){
      result = result.substr(this.getTemplateInfo(aName).length);
    }
    return(result);
  });

  db.getAttributeText = bind(db, function(aName){
    var result = "";

    if(aName == "document_title"){
      result = document.title;
    }
    else{
      result = getAttributeText(this, aName);
    }

    if(this.doAddTemplateInfo(aName)){
      result = this.removeTemplateInfo(result, aName);
    }

    return(result);
  });

  db.removeCSSAttribute = bind(db, function(aName){
    return(removeCSSAttribute(this, aName));
  });

  db.remove_standard_dialogs = bind(db, function(){
    // global message
    remove_global_message();

    document.GCP = undefined;

    if(OPENDOTS_MODE == OPENDOTS_EDIT_MODE){
      // temporarily remove menus
      this.aMenus = remove_menus();

      // temporarily remove floats
      this.floats = all_floats();

      close_all_floats();
    }

  });

  db.restore_standard_dialogs = bind(db, function(){
    if(OPENDOTS_MODE == OPENDOTS_EDIT_MODE){
      // replace menus
      replace_menus(this.aMenus);

      replace_floats(this.floats);
    }
  });

  db.setContent = bind(db, function(aNewContent){
    this.remove_standard_dialogs();
    setContent(this, aNewContent);
    this.restore_standard_dialogs();
  });

  db.getContent = bind(db, function(){
    return(getContent(this));
  });

  db.setCSSAttributeFor = bind(db, function(aName, aValue, aAttributeName){
    return(setCSSAttributeFor(this, aName, aValue, aAttributeName));
  });

  db.setCSSAttribute = bind(db, function(aName, aValue){
    return(setCSSAttribute(this, aName, aValue));
  });

  db.scssa = bind(db, function(aName, aValue){
    // shorthand for setCSSAttribute
    this.setCSSAttribute(aName, aValue);
  });

  db.getCSSAttribute = bind(db, function(aName){
    return(getCSSAttribute(this, aName));
  });

  db.gcssa = bind(db, function(aName){
    // shorthand for getCSSAttribute
    return(this.getCSSAttribute(aName));
  });

  db.execScripts = bind(db, function(){
    exec_scripts(this);
  });

  db.attributeExists = bind(db, function(aName){
    return(this.getAttribute(aName) != undefined);
  });

  db.initAttributeText = bind(db, function(aName, aValue, aOverwrite){
    assert(aOverwrite != undefined, "document.body.initAttributeText(aOverwrite) : aOverwrite cannot be undefined");
    if( aOverwrite || ( (!this.attributeExists(aName)) && (!in_array(aName, NO_INIT_ATTRIBUTES)) ) ){
      this.setAttributeText(aName, aValue);
    }
  });

  db.iat = bind(db, function(aName, aValue, aOverwrite){
    // shorthand for initAttributeText
    return(this.initAttributeText(aName, aValue, aOverwrite));
  });

  db.initTemplate = bind(db, function(aOverwrite){
    if(aOverwrite == undefined){
      aOverwrite = true; //NB
    }

    this.iat(OPENDOTS_TP+"head_style", "", aOverwrite);
    this.iat(OPENDOTS_TP+"head_script", "", aOverwrite);
    this.iat(OPENDOTS_TP+"title", "", aOverwrite);
    this.iat(OPENDOTS_TP+"document_title", "", aOverwrite);
    this.iat(OPENDOTS_TP+"style", "", aOverwrite);
    this.iat(OPENDOTS_TP+"template", "", aOverwrite);
    this.iat(OPENDOTS_TP+"favicon", "", aOverwrite);
    this.iat(OPENDOTS_TP+"onclick", "", aOverwrite);
    this.iat(OPENDOTS_TP+"onload", "", aOverwrite);
    this.iat(OPENDOTS_TP+"head_extra", "", aOverwrite);
  });
}

function init_document_attributes(aOverwrite){
  var db = document.body;

  if(aOverwrite == undefined){
    aOverwrite = true; //NB
  }

  db.initTemplate(aOverwrite);

  db.iat("head_style", "", aOverwrite);

  db.iat("head_script", "", aOverwrite);

  db.iat("title", "", aOverwrite);

  db.iat("document_title", "", aOverwrite);

  db.iat("style", "", aOverwrite);

  db.iat("template", "", aOverwrite);

  db.iat("favicon", "", aOverwrite);

  db.iat("onclick", "", aOverwrite);

  db.iat("onload", "", aOverwrite);

  db.iat("head_extra", "", aOverwrite);
}

function init_document(){

  init_document_methods();

  init_document_attributes();

  document.GCP = undefined;

  document.loading = 0;
}

