// JavaScript Document
var xmlHttp = null;
var xmlHttpBBox = null;

var IE = navigator.userAgent.toLowerCase().indexOf("msie") == -1 ? false : true;
var Width = null;
var Heidth = null;         

function openPopup (fileName,width,height){
    var popPosWidth = Math.round((screen.width/2)-(width/2));
    var popPosHeight = Math.round((screen.height/2)-(height/2));
    
    win3 = window.open(fileName, "Window3", "width="+width+",height="+height+",scrollbars=no,menubar=no,toolbar=no,screenX="+popPosWidth+",screenY="+popPosHeight+"");
}

function GetXmlHttpObject() {
  var xmlHttp=null;
  try { // Firefox, Opera 8.0+, Safari
    xmlHttp = new XMLHttpRequest();
  }
  catch (e) { // Internet Explorer
    try {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e) {
      try {
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch (e) {
        alert("Your browser does not support AJAX!");
        return false;
      }
    }
  }
  return xmlHttp;
}

function gautiBbox(uuid) {
  xmlHttpBBox = GetXmlHttpObject();
  var method = "post";

  var data = "<parameters>\n";
  data += "  <uuid>"+uuid+"</uuid>\n";
  data += "</parameters>";
  xmlHttpBBox.onreadystatechange = gautasBbox;
  xmlHttpBBox.open(method, proxyUrl, true/*, "uname", "pswd"*/);
//Send the proper header information along with the request
  xmlHttpBBox.setRequestHeader("serverUrl", BBoxRequestUrl);
  xmlHttpBBox.setRequestHeader("content-type","text/xml");
  xmlHttpBBox.send(data);    
}

function gautasBbox() {
  if(xmlHttpBBox.readyState == 4 && xmlHttpBBox.status == 200) {
    if (xmlHttpBBox.responseXML.getElementsByTagName("x1").length &&
        xmlHttpBBox.responseXML.getElementsByTagName("y1").length &&
        xmlHttpBBox.responseXML.getElementsByTagName("x2").length &&
        xmlHttpBBox.responseXML.getElementsByTagName("y2").length) {
      var x1 = xmlHttpBBox.responseXML.getElementsByTagName("x1")[0].childNodes[0].nodeValue;
      var y1 = xmlHttpBBox.responseXML.getElementsByTagName("y1")[0].childNodes[0].nodeValue;
      var x2 = xmlHttpBBox.responseXML.getElementsByTagName("x2")[0].childNodes[0].nodeValue;
      var y2 = xmlHttpBBox.responseXML.getElementsByTagName("y2")[0].childNodes[0].nodeValue;
      document.getElementById('bbox_x1').value = x1;
      document.getElementById('bbox_y1').value = y1;
      document.getElementById('bbox_x2').value = x2;
      document.getElementById('bbox_y2').value = y2;
      mapTimerId = window.setInterval("restoreZoom("+x1+", "+y1+", "+x2+", "+y2+")",100);
    }
  }
}
  
function gautiPaveiksliuka(uuid, width, height) {
  xmlHttp = GetXmlHttpObject();
  var method = "post";

  var data = "<parameters>\n";
  data += "  <uuid>"+uuid+"</uuid>\n";
  data += "  <width>"+width+"</width>\n";
  data += "  <height>"+height+"</height>\n";
  if (IE) data += "  <draw>vml</draw>\n";
  else    data += "  <draw>svg</draw>\n";
  data += "</parameters>";
  xmlHttp.onreadystatechange = paveiksliukas;
  xmlHttp.open(method, proxyUrl, true/*, "uname", "pswd"*/);
//Send the proper header information along with the request
  xmlHttp.setRequestHeader("serverUrl", MiniMapRequestUrl);
  xmlHttp.setRequestHeader("content-type","text/xml");
  xmlHttp.send(data);    
}

if(typeof document.importNode != "function") document.importNode=function(myXMLNode,deep) {
  var e;
  try {
    var e = document.createElement("div");
    e.insertAdjacentHTML("afterBegin", myXMLNode.cloneNode(deep).xml);
    var d=document.createDocumentFragment();
    var l=e.childNodes.length;
    for(var i=0;i<l;i++) d.appendChild(e.firstChild);
    return d;
  } catch(E) {}
  return null;
}

function paveiksliukas() {
  if(xmlHttp.readyState == 4 && xmlHttp.status == 200) {
    if(IE) {
      var iframeDoc = document.getElementById("image").contentWindow.document;
      if (xmlHttp.responseXML.getElementsByTagName("miniMapUrl").length &&
          xmlHttp.responseXML.getElementsByTagName("width").length &&
          xmlHttp.responseXML.getElementsByTagName("height").length) {
        var minimapUrl = xmlHttp.responseXML.getElementsByTagName("miniMapUrl")[0].childNodes[0].nodeValue;
        var width      = xmlHttp.responseXML.getElementsByTagName("width")[0].childNodes[0].nodeValue;
        var height     = xmlHttp.responseXML.getElementsByTagName("height")[0].childNodes[0].nodeValue;
        var bgMap = "url("+mapUrl;
        bgMap += "&WIDTH="+width+"&HEIGHT="+height+"&BBOX="+ minimapUrl +")";
        iframeDoc.body.style.backgroundRepeat = "no-repeat";
        iframeDoc.body.style.backgroundImage = bgMap;
        if (xmlHttp.responseXML.getElementsByTagName("vml").length) {
          iframeDoc.namespaces.add("v","urn:schemas-microsoft-com:vml");
          var style = iframeDoc.createStyleSheet();
          style.addRule('v\\:*',"behavior: url(#default#VML); "+"position: relative; display: inline-block;");
          var xml = xmlHttp.responseXML;
          var paths =     xml.getElementsByTagName("path");
          var polylines = xml.getElementsByTagName("polyline");
          var ovals     = xml.getElementsByTagName("oval");
          for(i = 0; i < paths.length; i++) {
            coords = paths[i].childNodes[0].nodeValue;
            coords = coords.split(" ");
            shape = iframeDoc.createElement("v:shape");
            shape.style.position = "absolute";
            shape.style.filter ="alpha(opacity=50)";
            shape.style.left = "0px";
            shape.style.top  = "0px";
            shape.style.width = width;
            shape.style.height = height;
            shape.stroke = "true";
            shape.strokecolor = "grey";
            shape.strokeweight = "1";
            shape.fill = "true";
            shape.fillcolor = "green";
            shape.coordorigin = "0 0";
            shape.coordsize = width + " " + height;
            shape.path = "m ";
            for(i = 0; i < coords.length; i++) { // x in coords
              num = new Number(coords[i]);
              shape.path += num.toFixed();
              if(1 == i) shape.path += " l ";
              else shape.path += ",";
            }
            shape.path = shape.path.substr(0, shape.path.length-1) + " x e";
            iframeDoc.body.appendChild(shape);
          }
          for(i = 0; i < polylines.length; i++) {
            coords = polylines[i].childNodes[0].nodeValue;
            coords = coords.split(" ");
            polyline = iframeDoc.createElement("v:polyline");
            polyline.style.position = "absolute";
            polyline.style.filter ="alpha(opacity=75)";
            polyline.style.left = "0px";
            polyline.style.top  = "0px";
            polyline.style.width = Width;
            polyline.style.height = Height;
            polyline.stroke = "true";
            polyline.strokecolor = "black";
            polyline.strokeweight = "2";
            polyline.coordorigin = "0 0";
            polyline.coordsize = width + " " + height;
            polyline.points = "";
            for(i = 0; i < coords.length; i++) { // x in coords
              num = new Number(coords[i]);
              polyline.points += num.toFixed();
              if (i%2) polyline.points += " ";// odd
              else polyline.points += ",";// even
            }
            fill = iframeDoc.createElement("v:fill");
            fill.opacity = "0";
            polyline.appendChild(fill);
            iframeDoc.body.appendChild(polyline);
          }
          for(i = 0; i < ovals.length; i++) {
            coords = ovals[i].childNodes[0].nodeValue;
            coords = coords.split(",");
            if (coords.length == 2) {
              var x = coords[0]-3;
              var y = coords[1]-3;
              oval = iframeDoc.createElement("v:oval");
              oval.fillcolor = "red";
              oval.style.position = "absolute";
              oval.style.left = x.toFixed();
              oval.style.top  = y.toFixed();
              oval.style.width  = "6px";
              oval.style.height = "6px";
              iframeDoc.body.appendChild(oval);
            }
          }
        }
      }
    }
    else {
      if (xmlHttp.responseXML.getElementsByTagName("miniMapUrl").length &&
          xmlHttp.responseXML.getElementsByTagName("width").length &&
          xmlHttp.responseXML.getElementsByTagName("height").length) {
        var minimapUrl = xmlHttp.responseXML.getElementsByTagName("miniMapUrl")[0].childNodes[0].nodeValue;
        var width      = xmlHttp.responseXML.getElementsByTagName("width")[0].childNodes[0].nodeValue;
        var height     = xmlHttp.responseXML.getElementsByTagName("height")[0].childNodes[0].nodeValue;
        var bgMap = "url("+mapUrl;
        bgMap += "&WIDTH="+width+"&HEIGHT="+height+"&BBOX="+ minimapUrl +")";
        document.getElementById("image").contentDocument.body.style.backgroundImage = bgMap;
        document.getElementById("image").contentDocument.body.style.backgroundRepeat = "no-repeat";
        
      }
      if (xmlHttp.responseXML.getElementsByTagName("svg").length) {
        var svg_result = xmlHttp.responseXML.getElementsByTagName("svg")[0].cloneNode(true);
        var svg = document.getElementById("image").contentDocument.documentElement;
        svg.appendChild(svg_result);
      }
    }
  }
}

/*cookies*/

function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    { 
    c_start=c_start + c_name.length+1; 
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    } 
  }
return "";
}

/*cookies*/
