/**
* WebGIS客户端脚本库，DHTML对象
*
* Filename：Dhtml.js
*
* Copyright：Copyright (c) 2005-2006
* Company：EveryWhere.com.cn，AMoy
* author：Contion Chou
* required：IE4+,Firefox1.5+
* version：2.0.0.0
* modified：2006年4月29日 15:00:00
*/

if (window.HTMLElement) {
  var _leafElems = ["IMG", "HR", "BR", "INPUT"];
  var leafElems = {};
  for (var i=0; i<_leafElems.length; i++) {
    leafElems[_leafElems[i]] = true;
  }
  
  function setInnerHTML(str) {
    var r = this.ownerDocument.createRange();
    r.selectNodeContents(this);
    r.deleteContents();
    var df = r.createContextualFragment(str);
    this.appendChild(df);
    
    return str;
  }
    
  function setOuterHTML(str) {
    var r = this.ownerDocument.createRange();
    r.setStartBefore(this);
    var df = r.createContextualFragment(str);
    this.parentNode.replaceChild(df, this);
    return str;
  }

  function getInnerHTML(node) {
    var str = "";
    for (var i=0; i<node.childNodes.length; i++)
      str += getOuterHTML(node.childNodes.item(i));
    return str;
  }

  function getOuterHTML(node) {
    var str = "";
    
    switch (node.nodeType) {
      case 1: // ELEMENT_NODE
        str += "<" + node.nodeName;
        for (var i=0; i<node.attributes.length; i++) {
          if (node.attributes.item(i).nodeValue != null) {
            str += " "
            str += node.attributes.item(i).nodeName;
            str += "=\"";
            str += node.attributes.item(i).nodeValue;
            str += "\"";
          }
        }

        if (node.childNodes.length == 0 && leafElems[node.nodeName])
          str += ">";
        else {
          str += ">";
          str += getInnerHTML(node);
          str += "<" + node.nodeName + ">"
        }
        break;
          
      case 3:  //TEXT_NODE
        str += node.nodeValue;
        break;
        
      case 4: // CDATA_SECTION_NODE
        str += "<![CDATA[" + node.nodeValue + "]]>";
        break;
            
      case 5: // ENTITY_REFERENCE_NODE
        str += "&" + node.nodeName + ";"
        break;

      case 8: // COMMENT_NODE
        str += "<!--" + node.nodeValue + "-->"
        break;
    }

    return str;
  }
  
  HTMLElement.prototype.__defineSetter__("innerHTML", setInnerHTML);
  HTMLElement.prototype.__defineGetter__("innerHTML", function() {
    return getInnerHTML(this);
  });
  
  HTMLElement.prototype.__defineSetter__("outerHTML", setOuterHTML);
  HTMLElement.prototype.__defineGetter__("outerHTML", function() {
    return getOuterHTML(this);
  });
}

if (window.XMLDocument) {
  XMLDocument.prototype.loadXML = function(xmlString) {
    var childNodes = this.childNodes;
    for (var i = childNodes.length - 1; i >= 0; i--)
      this.removeChild(childNodes[i]);

    var dp = new DOMParser();
    var newDOM = dp.parseFromString(xmlString, "text/xml");
    var newElt = this.importNode(newDOM.documentElement, true);
    this.appendChild(newElt);
  }

  function element_prototype_get_text() {
    return this.childNodes[0].nodeValue;
  }
  Element.prototype.__defineGetter__("text", element_prototype_get_text);
  
  // check for XPath implementation
  if( document.implementation.hasFeature("XPath", "3.0") )
  {
    // prototying the XMLDocument
    XMLDocument.prototype.selectNodes = function(cXPathString, xNode)
    {
      if( !xNode ) { xNode = this; } 
      var oNSResolver = this.createNSResolver(this.documentElement)
      var aItems = this.evaluate(cXPathString, xNode, oNSResolver, 
      XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null)
      var aResult = [];
      for( var i = 0; i < aItems.snapshotLength; i++)
      {
        aResult[i] = aItems.snapshotItem(i);
      }
      return aResult;
    }

    // prototying the Element
    Element.prototype.selectNodes = function(cXPathString)
    {
      if(this.ownerDocument.selectNodes)
      {
        return this.ownerDocument.selectNodes(cXPathString, this);
      }
      else
      {
        throw "selectNodes For XML Elements Only";
      }
    }
  }
  
  // check for XPath implementation
  if( document.implementation.hasFeature("XPath", "3.0") )
  {
    // prototying the XMLDocument
    XMLDocument.prototype.selectSingleNode = function(cXPathString, xNode)
    {
      if( !xNode ) { xNode = this; } 
      var xItems = this.selectNodes(cXPathString, xNode);
      if( xItems.length > 0 )
      {
        return xItems[0];
      }
      else
      {
        return null;
      }
    }

    // prototying the Element
    Element.prototype.selectSingleNode = function(cXPathString)
    { 
      if(this.ownerDocument.selectSingleNode)
      {
        return this.ownerDocument.selectSingleNode(cXPathString, this);
      }
      else
      {
        throw "For XML Elements Only";
      }
    }
  }
}

function ToolTips (strTipIds) {
  this.show = _show;
  this.hide = _hide;
  this.setAlphaOpacity = _setAlphaOpacity;

  function _hide() {  //关闭工具栏提示
    if (document.getElementById(strTipIds)) {
      window.setTimeout("document.getElementById(\"" + strTipIds + "\").style.display = \"none\"", 200);
    }
  }
  
  function _setAlphaOpacity(intValue) {
    if (document.getElementById(strTipIds)) {
      document.getElementById(strTipIds).filters.alpha.opacity = intValue;
    }
  }

  function _show(strMsg) {  //显示工具栏提示
    try {
      if (!document.getElementById(strTipIds)) {  //创建工具栏提示
        var strHTML  = '<div onclick=window.setTimeout("document.getElementById(\'' + strTipIds + '\').style.display=\'none\'",200) id="' + strTipIds + '" style="position:absolute;left:1px;top:1px;width:100px;padding-top:4px;padding-left:4px;padding-right:4px;padding-bottom:2px;';
            strHTML += 'font-family:宋体;font-size:12px;border:1px #000000 solid;background-color:#ffffe1;';
            strHTML += 'color: #000000;display:none;z-index:10;filter:alpha(opacity=100)"></div>';

        if ("complete" == document.readyState) {
          document.body.insertAdjacentHTML("beforeEnd", strHTML);
        }
      }

      if (null != strMsg && strMsg.length) {
        strMsg = strMsg.replace(/ +\n/g, "<br>").replace(/\n/g,"<br>").replace(/\\n/g,"<br>");
        var iCalcWidth = _calcWidth(strMsg);
        if (iCalcWidth > document.body.clientWidth)
          iCalcWidth = document.body.clientWidth;
        var iGivenWidth = (arguments.length > 3) ? arguments[3] : 0;

        document.getElementById(strTipIds).style.pixelWidth = ((iGivenWidth > iCalcWidth) ? iGivenWidth : iCalcWidth) + 10;
        document.getElementById(strTipIds).innerHTML = strMsg.toHtmlSpace();
        document.getElementById(strTipIds).style.display = "";
        _setAlphaOpacity(100);
      }
      else {
        document.getElementById(strTipIds).innerHTML = "";
        document.getElementById(strTipIds).style.display = "none";
        return;
      }
       
      if (arguments.length > 1) {
        var iLeft = ((typeof(arguments[1]) != "number") ? parseInt(arguments[1]) : arguments[1]) + 2;
        var iLeftMax = document.body.offsetWidth - document.getElementById(strTipIds).offsetWidth;
      }
      if (arguments.length > 2) {
        var iTop = ((typeof(arguments[2]) != "number") ? parseInt(arguments[2]) : arguments[2]) + 10;
        var iTopMax = document.body.offsetHeight - document.getElementById(strTipIds).offsetHeight;
      }
      
      _posnToolTips((iLeft > iLeftMax) ? iLeftMax : iLeft, (iTop > iTopMax) ? iTopMax : iTop)
    } catch (exception) {
      ;
    }
  }

  function _posnToolTips(iLeft, iTop) {  //定位提示栏
    if (document.all.item(strTipIds)) {
      document.getElementById(strTipIds).style.pixelLeft = iLeft;
      document.getElementById(strTipIds).style.pixelTop = iTop;
    }
  }

  function _calcWidth(sTdHTML) {  //计算工具栏提示的像素宽度
     var aTdHTML = sTdHTML.toLowerCase().split("<br>");
     
     var fWidth = 0;
     for (var i=0; i<aTdHTML.length; i++) {
       var sTmp = aTdHTML[i].toLowerCase();
       var fTimes = (sTmp.indexOf("<b>") == -1 && sTmp.indexOf("font-weight:bold") == -1) ? 6 : 6.5;
       var fCurWidth = 0;
       
       sTmp = sTmp.trim().trimHtml();
       for (var j=0; j<sTmp.length; j++) {
         fCurWidth += (128<sTmp.charCodeAt(j)) ? (2*fTimes) : (fTimes + 0.5);
       }
       
       if (fWidth < fCurWidth) {
         fWidth = fCurWidth;
       }
     }
     
    return Math.round(fWidth);
  }
}
