var svgns = 'http://www.w3.org/2000/svg';
var http_root = 'maps/';

var doc_width = document.documentElement.getAttribute('width');
var doc_height = document.documentElement.getAttribute('height');

// Basic SVG Javascript Methods, Copy as required //

function element(ele) {
  if(typeof(ele)=='string') {
    return document.getElementById(ele);
  }
  if(ele.localName) {
    return ele;
  }
  return null;
}

function pop_element(target) {
  ele = element(target);
  parent = ele.parentNode;
  if(parent) {
    parent.removeChild(ele)
  }
  return ele;
}

function empty_element(target) {
  if (target.hasChildNodes()) {
    while(target.childNodes.length >= 1 ) {
      target.removeChild( target.firstChild );
    } 
  }

}

var fade_duration = 0.4; // seconds

function fade(target, out) {
  var animate = document.createElementNS(svgns, 'animate');
  var out = out ? 0 : 1;
  animate.setAttribute('attributeName', 'opacity');
  animate.setAttribute('to', out);
  animate.setAttribute('dur', fade_duration);
  animate.setAttribute('begin', 'indefinite');
  animate.setAttribute('fill', 'freeze');
  target.appendChild(animate);
  enableAnimator(animate); // For firefox
  animate.beginElement();
  if(out==0) {
    setTimeout("pop_element('"+target.getAttribute('id')+"')", 500);
  }
}

function getBBoxAsRectElement(elm) {
  var bb = elm.getBBox();
  var rect = document.createElementNS(svgns, "rect");
  rect.x.baseVal.value = bb.x;
  rect.y.baseVal.value = bb.y;
  rect.width.baseVal.value = bb.width;
  rect.height.baseVal.value = bb.height;
  return rect;
}

function height(target) { return parseFloat(target.getAttribute('height')); }
function get_y(target) { return parseFloat(target.getAttribute('y')); }
function middle(target) { return height(target) / 2; }
function abs_middle(target) { return middle(target) + get_y(target); }

function width(target) { return parseFloat(target.getAttribute('width')); }
function get_x(target) { return parseFloat(target.getAttribute('x')); }
function center(target) { return width(target) / 2; }
function abs_center(target) { return center(target) + get_x(target); }

function scale_to(target, rect) {
  if(target && rect) {
    xScale = width(rect) / width(target);
    yScale = height(rect) / height(target);
    if(xScale < yScale) {
      return xScale;
    }
    return yScale;
  }
}

function scaleUp(target) {
  var rect = getBBoxAsRectElement(target);
  var scale = scale_to(rect, bounds);
  var oM = (middle(rect) * scale) + (get_y(rect) * scale);
  var oC = (center(rect) * scale) + (get_x(rect) * scale);
  var cY = abs_middle(bounds) - oM;
  var cX = abs_center(bounds) - oC;

  var animate = document.createElementNS(svgns, 'animateTransform');
  animate.setAttribute('attributeName', 'transform');
  animate.setAttribute('attributeType', 'XML');
  animate.setAttribute('type', 'matrix');
  animate.setAttribute('to', scale+',0,0,'+scale+','+cX+','+cY);
  animate.setAttribute('dur', 1);
  animate.setAttribute('begin', 'indefinite');
  animate.setAttribute('fill', 'freeze');
  animate.setAttribute('additive', 'replace');
  target.appendChild(animate);

  enableAnimator(animate); // For firefox
  animate.beginElement();
}

function httpSVG(url, onsucess, onfail, args) {
  var method1 = function(xml) { return onsucess(xml, args); }
  var method2 = function(xml) { return onfail(xml, args); }
  //call getURL() if available
  if (window.getURL) {
    getURL(url, method1);
  }
  //call XMLHttpRequest() if available
  else if (window.XMLHttpRequest) {
    // this nested function is used to make XMLHttpRequest threadsafe
    // (subsequent calls would not override the state of the request and can use the variable/object context of the parent function)
    // this idea is borrowed from http://www.xml.com/cs/user/view/cs_msg/2815 (brockweaver)
    function XMLHttpRequestCallback() {
      if (xmlRequest.readyState == 4) {
        if (xmlRequest.status == 200) {
            //parse and import the SVG fragment
            var importedNode = document.importNode(xmlRequest.responseXML.documentElement,true);
            //call function addGeom
            return method1(importedNode);
        }
        method2(xmlRequest.status);
      }
    }
    var xmlRequest = null;
    xmlRequest = new XMLHttpRequest();
    xmlRequest.open("GET", url, true);
    xmlRequest.onreadystatechange = XMLHttpRequestCallback;
    xmlRequest.send(null);
  }
  //write an error message if either method is not available
  else {
    alert("your browser/svg viewer neither supports window.getURL nor window.XMLHttpRequest!");
  }
}

