//============================================================================
// Copyright (c) 2006 Pelco. All rights reserved.
//
// This file contains trade secrets of Pelco.  No part may be reproduced or
// transmitted in any form by any means or for any purpose without the express
// written permission of Pelco.
//
// $File: compat.js$
// $Revision: 2$
// $Date: 04/07/2008 01:42:39 PM$
// $Author: jwatson$
//============================================================================

// figure out what kind of browser we are in
var gUserAgent = window.navigator.userAgent;
var gIsIE = (gUserAgent.indexOf("MSIE") > 0);

////////////////////////////////////////////////////////////////////////////////

// determine screen position & size info
function getScreenX() { return (gIsIE) ? window.screenLeft : window.screenX; }
function getScreenY() { return (gIsIE) ? window.screenTop : window.screenY; }

function getScreenWidth() { return (gIsIE) ? document.body.clientWidth : window.innerWidth; }
function getScreenHeight() { return (gIsIE) ? document.body.clientHeight : window.innerHeight; }

///////////////////////////////////////////////////////////////////////////////
// returns the position of the mouse from a mouse event
function getMousePosition(ev) { 
  var x = 0;
  var y = 0;

  if (ev.clientX)   {
    x = ev.clientX + (document.documentElement.scrollLeft ?  document.documentElement.scrollLeft : document.body.scrollLeft);
    y = ev.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
  }
  else if(ev.pageX) {
    x = ev.pageX;
    y = ev.pageY;
  }
  return [x,y];
}

////////////////////////////////////////////////////////////////////////////////
// finds the position of any object in the document
function findPosition(obj) {
  var objtop = 0;
  var objleft = 0;

  // walk the offsetParent tree, sum the offsets
  if (obj.offsetParent) {
    objleft = obj.offsetLeft
    objtop = obj.offsetTop
    while (obj = obj.offsetParent) {
      objleft += obj.offsetLeft
      objtop += obj.offsetTop
    }
  }

  return [objleft, objtop];
}

///////////////////////////////////////////////////////////////////////////////
// returns the size of the object
function findSize(obj) { 
  var width  = (obj.offsetWidth) ? obj.offsetWidth : obj.clip.width;
  var height = (obj.offsetWidth) ? obj.offsetHeight : obj.clip.height;
  return [width, height];
}


////////////////////////////////////////////////////////////////////////////////
//
function createXMLHttpRequest() {
  var req = null;

  try {
    if (window.XMLHttpRequest) {
      req = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
      req = new ActiveXObject(getXMLControlPrefix() + ".XmlHttp");
    }
  } catch (e) {
    alert("no XML support");
  }

  return req;
}

////////////////////////////////////////////////////////////////////////////////
//
function createXMLDocument() {
  var doc = null;

  try {
    if (document.implementation && document.implementation.createDocument) {
      doc = document.implementation.createDocument("", "", null);
    } else if (window.ActiveXObject) {
      doc = new ActiveXObject(getXMLControlPrefix() + ".XmlDom");
    }
  } catch (e) {
    alert("no XML support");
  }

  return doc;
}

////////////////////////////////////////////////////////////////////////////////
//
function getXMLControlPrefix() {
   if (getXMLControlPrefix.prefix) { return getXMLControlPrefix.prefix; }

   var prefixes = ["MSXML2", "Microsoft", "MSXML", "MSXML3"];
   for (var idx = 0; idx < prefixes.length; idx++) {
      try {
         var o = new ActiveXObject(prefixes[idx] + ".XmlHttp");
         var o2 = new ActiveXObject(prefixes[idx] + ".XmlDom");
         return getXMLControlPrefix.prefix = prefixes[idx];
      } catch (e) {
      }
   }

   throw new Error("Could not find an installed XML parser");
}

////////////////////////////////////////////////////////////////////////////////
// Add loadXML(), transformNode() and .xml attributes to non-IE browsers to make
// them more compatable with the IE interface and simplify javascript code:
//   http://www.webreference.com/programming/javascript/domwrapper/index.html
if (gIsIE == false) {

  //-------------------------------------------------------------------------
  // add the loadXML() method to the Document class
  Document.prototype.loadXML = function(strXML) {

    // change the readystate
    this.readyState = 1;
        
    // create a DOMParser
    var objDOMParser = new DOMParser();
        
    // create new document from string
    var objDoc = objDOMParser.parseFromString(strXML, "text/xml");

    // remove all nodes from the document
    while (this.hasChildNodes()) { this.removeChild(this.lastChild); }

    // add the nodes from the new document
    for (var i = 0; i < objDoc.childNodes.length; i++) {
                
      // import the node
      var objImportedNode = this.importNode(objDoc.childNodes[i], true);
                
      // append the child to the current document
      this.appendChild(objImportedNode);
    }

    // change the readystate
    this.readyState = 4;
  }

  //-------------------------------------------------------------------------
  // add the transformNode() method to the Document class
  Document.prototype.transformNode = function(xslDoc) {

    // set the XSL stylesheet 
    var xsltProcessor = new XSLTProcessor();
    xsltProcessor.importStylesheet(xslDoc);

    // transform XML into XHTML document
    var xhtmlDoc = xsltProcessor.transformToDocument(this);

    // return string version of XHTML
    return xhtmlDoc.xml;
  }

  //-------------------------------------------------------------------------
  // define .xml attribute function
  function _Node_getXML() {
      
      // create a new XMLSerializer
      var objXMLSerializer = new XMLSerializer;
      
      // get the XML string
      var strXML = objXMLSerializer.serializeToString(this);
      
      // return the XML string
      return strXML;
  }

  // associate the _Node_getXML() function with the .xml attribute of Node class
  Node.prototype.__defineGetter__("xml", _Node_getXML);
}

///////////////////////////////////////////////////////////////////////////////
// Performs XSLT on an XML string using the given XSL stylesheet URL.
// Returns result as a string.
function XslToXhtml(xmlString, xslUrl) {

  // convert XML string to an XmlDocument
  var xml = createXMLDocument();
  xml.async = false;
  xml.loadXML(xmlString);

  // load XSL document
  var xsl = createXMLDocument(); 
  xsl.async = false;
  xsl.load(xslUrl);

  // transform XML document into XHTML string and return it
  return xml.transformNode(xsl);
}


