/*
MapTime(tm)
-----------------------
This converts locations on the map into clickables.
-----------------------
*/

function createLinks()
{

// Fixed vars
  var differenceHE = 5.08743946216137e+17;
  var differenceVE = 4.72860102256057e+17;
  var hrangeE = 8.452669174806551e+17;
  var vrangeE = 9.57312947953911e+17;
  var mapWidth = (5059 / 100) * mapZoomFactor;
  var mapHeight = (5718 / 100) * mapZoomFactor;
  
  var secColors = new Array(10); // 10 securities
  secColors[0] = "#ff2340"; // Damn I warped into a bubble -.-
  secColors[1] = "#ff2340";
  secColors[2] = "#ff2340";
  secColors[3] = "#ba5e14";
  secColors[4] = "#cd8347"; // Sentries still here...
  secColors[5] = "#ffd323";
  secColors[6] = "#adea43";
  secColors[7] = "#adea43";
  secColors[8] = "#9dea43";
  secColors[9] = "#57ea43";
  secColors[10] = "#23fcff"; // Even the rats don't go here
  
  var xmlhttp = new XMLHttpRequest();
  xmlhttp.open("GET","../xml/systems.xml", false);
  xmlhttp.send();
  var xmlDoc = xmlhttp.responseXML;
  
  var systems = xmlDoc.getElementsByTagName("solarsystem");
  
  var topOffset = document.getElementById("mapcontainer").offsetTop;
  var leftOffset = document.getElementById("mapcontainer").offsetLeft;
  
  // The further you zoom out, the less you see. A bit like Google Maps
  var skip = 1;
  if (mapZoomFactor == 100) { skip = 1; }
  if (mapZoomFactor == 90) { skip = 1; }
  if (mapZoomFactor == 80) { skip = 2; }
  if (mapZoomFactor == 70) { skip = 3; }
  if (mapZoomFactor == 60) { skip = 4; }
  if (mapZoomFactor == 50) { skip = 5; }
  if (mapZoomFactor == 40) { skip = 6; }
  if (mapZoomFactor == 30) { skip = 7; }
  if (mapZoomFactor == 20) { skip = 8; }
  
  for (var i = 0; i <= 5430; i=i+skip)
  {
    var xposstr = systems[i].getElementsByTagName("xpos")[0].childNodes[0].nodeValue;
    var zposstr = systems[i].getElementsByTagName("zpos")[0].childNodes[0].nodeValue;
  
    var xposE = parseFloat(xposstr) + differenceHE;
    var zposE = parseFloat(zposstr) + differenceVE;
  
    var percentX = (xposE / hrangeE) * 100;
    var percentY = (zposE / vrangeE) * 100;
  
    var pixXpos = ((mapWidth / 100) * percentX + 2);
    var pixYpos = ((mapHeight / 100) * percentY - 2);
	
	// Render only visible systems within confines of browser window
	if ((leftOffset + pixXpos) > 0 && pixXpos < (window.innerWidth - leftOffset))
	{  
	  if (pixYpos < (window.innerHeight - topOffset) )
	  {
	    var theColor = "";
        var securityFloat = parseFloat(systems[i].getElementsByTagName("security")[0].childNodes[0].nodeValue);
        var security = 0;
	
        if (securityFloat <= 0)
        { security = "0.0"; theColor = secColors[0]; }
        else
        {
          security = Math.round(securityFloat*10)/10;
	      var secNumber = security * 10;
	      theColor = secColors[secNumber];
        }
	
	    var systemName = systems[i].getElementsByTagName("name")[0].childNodes[0].nodeValue;
	
	    var maptime = document.getElementById("maptime");
	    var newLoc = document.createElement('div');
	    newLoc.setAttribute('style', 'position: absolute; left: ' + pixXpos + 'px; top: '+pixYpos+ 'px; width: 6px; height: 6px; background-color: '+theColor+';');
	    newLoc.setAttribute('id', 'system'+i);
		newLoc.setAttribute('onmouseover', 'hoverData('+i+',\''+systemName+'\','+security+')');
		// newLoc.setAttribute('onclick', 'systemClicked();');
		// newLoc.setAttribute('onclick', 'displayDataFor(\''+systemName+'\')');
	    
		// Only show systems we want to see
		if (security > 0.4 && document.landmarks.highsecChecker.checked == true)
	    { maptime.appendChild(newLoc); }
		
		if (security >= 0.1 && security < 0.5 && document.landmarks.lowsecChecker.checked == true)
	    { maptime.appendChild(newLoc); }
		
		if (security == "0.0" && document.landmarks.zeroChecker.checked == true)
	    { maptime.appendChild(newLoc); }
		
	  }
	}
  }

}




function createLandmarks()
{

// Fixed vars
  var differenceHE = 5.08743946216137e+17;
  var differenceVE = 4.72860102256057e+17;
  var hrangeE = 8.452669174806551e+17;
  var vrangeE = 9.57312947953911e+17;
  var mapWidth = (5059 / 100) * mapZoomFactor;
  var mapHeight = (5718 / 100) * mapZoomFactor;
  
  var xmlhttp = new XMLHttpRequest();
  xmlhttp.open("GET","../xml/landmarks.xml", false);
  xmlhttp.send();
  var xmlDoc = xmlhttp.responseXML;
  
  var landmarks = xmlDoc.getElementsByTagName("attraction");
  
  var topOffset = document.getElementById("mapcontainer").offsetTop;
  var leftOffset = document.getElementById("mapcontainer").offsetLeft;
  
  for (var i = 0; i <= 44; i++) // Only 45 Landmarks alltogether
  {
    var xposstr = landmarks[i].getElementsByTagName("xpos")[0].childNodes[0].nodeValue;
    var zposstr = landmarks[i].getElementsByTagName("zpos")[0].childNodes[0].nodeValue;
  
    var xposE = parseFloat(xposstr) + differenceHE;
    var zposE = parseFloat(zposstr) + differenceVE;
  
    var percentX = (xposE / hrangeE) * 100;
    var percentY = (zposE / vrangeE) * 100;
  
    var pixXpos = ((mapWidth / 100) * percentX + 2);
    var pixYpos = ((mapHeight / 100) * percentY - 2);
	
	var lmName = landmarks[i].getElementsByTagName("name")[0].childNodes[0].nodeValue;
	var lmDesc = landmarks[i].getElementsByTagName("description")[0].childNodes[0].nodeValue;
	
	var maptime = document.getElementById("maptime");
	var newLM = document.createElement('div');
	newLM.setAttribute('style', 'position: absolute;z-index: 2; left: ' + (pixXpos-16) + 'px; top: '+(pixYpos-16)+ 'px;');
	newLM.setAttribute('id', 'landmark'+i);
		// newLM.setAttribute('onmouseover', 'hoverData('+i+',\''+systemName+'\','+security+')');
		// newLoc.setAttribute('onclick', 'systemClicked();');
		// newLoc.setAttribute('onclick', 'displayDataFor(\''+systemName+'\')');
	
	maptime.appendChild(newLM);
		
	document.getElementById("landmark"+i).innerHTML = "<img  onclick=\"showLandmarkData(\'"+lmName+"\', \'"+lmDesc+"\');\" src=\"img/lmBeacon.png\">";
  }

}



function createRegions()
{

// Fixed vars
  var differenceHE = 5.08743946216137e+17;
  var differenceVE = 4.72860102256057e+17;
  var hrangeE = 8.452669174806551e+17;
  var vrangeE = 9.57312947953911e+17;
  var mapWidth = (5059 / 100) * mapZoomFactor;
  var mapHeight = (5718 / 100) * mapZoomFactor;
  
  var xmlhttp = new XMLHttpRequest();
  xmlhttp.open("GET","../xml/regions.xml", false);
  xmlhttp.send();
  var xmlDoc = xmlhttp.responseXML;
  
  var regions = xmlDoc.getElementsByTagName("regData");
  
  var topOffset = document.getElementById("mapcontainer").offsetTop;
  var leftOffset = document.getElementById("mapcontainer").offsetLeft;
  
  for (var i = 0; i <= 66; i++) // 67 Regions
  {
    var xposstr = regions[i].getElementsByTagName("xpos")[0].childNodes[0].nodeValue;
    var zposstr = regions[i].getElementsByTagName("zpos")[0].childNodes[0].nodeValue;
  
    var xposE = parseFloat(xposstr) + differenceHE;
    var zposE = parseFloat(zposstr) + differenceVE;
  
    var percentX = (xposE / hrangeE) * 100;
    var percentY = (zposE / vrangeE) * 100;
  
    var pixXpos = ((mapWidth / 100) * percentX + 2);
    var pixYpos = ((mapHeight / 100) * percentY - 2);
	
	var rgName = regions[i].getElementsByTagName("name")[0].childNodes[0].nodeValue;
	
	var maptime = document.getElementById("maptime");
	var newReg = document.createElement('div');
	newReg.setAttribute('style', 'width: 200px; position: absolute;z-index: 2; left: ' + (pixXpos-25) + 'px; top: '+(pixYpos-5)+ 'px;');
	newReg.setAttribute('id', 'region'+i);
		// newLM.setAttribute('onmouseover', 'hoverData('+i+',\''+systemName+'\','+security+')');
		// newLoc.setAttribute('onclick', 'systemClicked();');
		// newLoc.setAttribute('onclick', 'displayDataFor(\''+systemName+'\')');
	
	maptime.appendChild(newReg);
	
	document.getElementById("region"+i).innerHTML = "<font face=\"Arial\" size=4 color=#ffba00>" + rgName + "</font>";
  }

}



function removeRegions()
{
  var theMap = document.getElementById("maptime");
  for (var i=0; i<=66; i++)
  {
    if (document.getElementById("region"+i))
	{
      rgToRemove = document.getElementById("region"+i);
 	  theMap.removeChild(rgToRemove);
	}
  }
}



function removeLandmarks()
{
  var theMap = document.getElementById("maptime");
  for (var i=0; i<=44; i++)
  {
    if (document.getElementById("landmark"+i))
	{
      lmToRemove = document.getElementById("landmark"+i);
 	  theMap.removeChild(lmToRemove);
	}
  }
}


function checkForLandmarks()
{
  if (document.landmarks.landmarkChecker.checked == true)
  { createLandmarks(); }
  if (document.landmarks.landmarkChecker.checked == false)
  { removeLandmarks(); }
}


function checkForRegions()
{
  if (document.landmarks.regionChecker.checked == true)
  { createRegions(); }
  if (document.landmarks.regionChecker.checked == false)
  { removeRegions(); }
}


function showLandmarkData(name, description)
{

  var windowByTwo = (window.innerWidth / 2);
  var spaceFromLeft = windowByTwo - 400;
  var heightOfWindow = (window.innerHeight - 170);
  document.getElementById("detailedData").style.left = spaceFromLeft;
  document.getElementById("detailedData").style.top = 120;
  document.getElementById("detailedData").style.height = heightOfWindow;
  document.getElementById("detailedData").style.display = "block";

  addslashes(description);
  
  var dataContent = "";

  dataContent += "<div style=\"position: absolute; left: 10px; top: 10px; width: 800px; height: 40px; background-image: url('img/det_header.png');overflow-x: hidden; overflow-y: auto;\">";
  dataContent += "<div style=\"position: absolute; left: 10px; top: 5px;\"><font face=\"Arial\" size=5 color=#EEEEEE>Landmark: <b>"+name+"</b></a></font></div>";
  dataContent += "</div>";
  dataContent += "<div style=\"position: absolute; left: 10px; top: 50px; width: 800px; overflow-x: hidden; overflow-y: auto;\"><br>";
  dataContent += "<center><input type=\"button\" style=\"background-image: url(\'img/searchBack.png\'); border: 1px solid #666666; width: 75px; height: 25px; font: 15px Arial; color: #DDDDDD\" value=\"OK\" onclick=\"javascript:closeLandmarkData();\"></center><br>";
  dataContent += "<font face=\"Arial\" size=2 color=#EEEEEE>"+description+"</font>";
  dataContent += "</div>";

  document.getElementById("detailedData").style.display = "block";
  document.getElementById("detailedData").innerHTML = dataContent;

}


function closeLandmarkData()
{
  document.getElementById("detailedData").style.display = "none";
  document.getElementById("detailedData").innerHTML = "";
}

function addslashes( str ) {    
  return (str+'').replace(/([\\"'])/g, "\\$1").replace(/\0/g, "\\0");  
} 

function hoverData(id,name,security)
{
  var systemLeft = document.getElementById("system"+id).offsetLeft;
  var systemTop = document.getElementById("system"+id).offsetTop;
  
  document.getElementById("hoverInfo").style.display = "block";
  document.getElementById("hoverInfo").style.left = (systemLeft - 80);
  document.getElementById("hoverInfo").style.top = (systemTop - 20);
  document.getElementById("hoverInfo").innerHTML = "<font face=\"Arial\" size=\"1\" color=#FFFFFF>"+name+"<br><font color=#cccccc>Security: "+security+"</font>";
  
  document.getElementById("search").value = name;
}


function clearHoverData()
{
  document.getElementById("hoverInfo").innerHTML = "";
  document.getElementById("hoverInfo").style.display = "none";
  document.getElementById("search").value = "";
}


function reloadDots()
{
  var theMap = document.getElementById("maptime");
  for (var i=0; i<=5430; i++)
  {
    if (document.getElementById("system"+i))
	{
      systemToRemove = document.getElementById("system"+i);
 	  theMap.removeChild(systemToRemove);
	}
  }
  
  createLinks();
  modifyLines();
}



function modifyLines()
{
  var theMapWidth = (5059 / 100) * mapZoomFactor;
  var theMapHeight  = (5718 / 100) * mapZoomFactor;
  
  var theWidth = (theMapWidth / 3);
  var theHeight = (theMapHeight / 3);
  
  document.getElementById("sq1").src = "img/parts/"+mapZoomFactor+"-1.gif";
  document.getElementById("sq1").style.position = "absolute";
  document.getElementById("sq1").style.left = 0;
  document.getElementById("sq2").src = "img/parts/"+mapZoomFactor+"-2.gif";
  document.getElementById("sq2").style.position = "absolute";
  document.getElementById("sq2").style.left = parseInt(theWidth);
  document.getElementById("sq3").src = "img/parts/"+mapZoomFactor+"-3.gif";
  document.getElementById("sq3").style.position = "absolute";
  document.getElementById("sq3").style.left = parseInt(theWidth*2);
  
  document.getElementById("sq4").src = "img/parts/"+mapZoomFactor+"-4.gif";
  document.getElementById("sq4").style.position = "absolute";
  document.getElementById("sq4").style.left = 0;
  document.getElementById("sq4").style.top = parseInt(theHeight);
  document.getElementById("sq5").src = "img/parts/"+mapZoomFactor+"-5.gif";
  document.getElementById("sq5").style.position = "absolute";
  document.getElementById("sq5").style.left = parseInt(theWidth);
  document.getElementById("sq5").style.top = parseInt(theHeight);
  document.getElementById("sq6").src = "img/parts/"+mapZoomFactor+"-6.gif";
  document.getElementById("sq6").style.position = "absolute";
  document.getElementById("sq6").style.left = parseInt(theWidth*2);
  document.getElementById("sq6").style.top = parseInt(theHeight);
  
  document.getElementById("sq7").src = "img/parts/"+mapZoomFactor+"-7.gif";
  document.getElementById("sq7").style.position = "absolute";
  document.getElementById("sq7").style.left = 0;
  document.getElementById("sq7").style.top = parseInt(theHeight*2);
  document.getElementById("sq8").src = "img/parts/"+mapZoomFactor+"-8.gif";
  document.getElementById("sq8").style.position = "absolute";
  document.getElementById("sq8").style.left = parseInt(theWidth);
  document.getElementById("sq8").style.top = parseInt(theHeight*2);
  document.getElementById("sq9").src = "img/parts/"+mapZoomFactor+"-9.gif";
  document.getElementById("sq9").style.position = "absolute";
  document.getElementById("sq9").style.left = parseInt(theWidth*2);
  document.getElementById("sq9").style.top = parseInt(theHeight*2);
 
  //alert("done");
}



function showAboutBox()
{
  canZoom = false;

  document.getElementById("aboutbox").style.display = "block";
  document.getElementById("aboutbox").style.top = 120;
  
  var mapWidth = (5059 / 100) * mapZoomFactor;
  var mapHeight = (5718 / 100) * mapZoomFactor;  
  
  var windowByTwo = (window.innerWidth / 2);
  var spaceFromLeft = windowByTwo - 250;
  document.getElementById("aboutbox").style.left = spaceFromLeft;
  
  var aboutBoxContent = "<br><br><center><img src=\"img/ab_cMaps.png\"><br><font face=\"Arial\" size=1 color=#EEEEEE>Version 1.0.3 | <a href=\"http://www.eveonline.com/ingameboard.asp?a=topic&threadID=1356174\" target=\"_blank\">Thread and Changelog</a></font></center><br><br>";
  aboutBoxContent += "<font face=\"Arial\" size=2 color=#EEEEEE>A web-based implementation, with the goal in mind to accurately display all solar systems in New Eden, as well as available information coming from the creators of <i>EVE Online</i>. Focusing on performance and ease of use, Cerberus Maps aims to be one of best tools for New Eden cartography available.";
  aboutBoxContent += "<br><br>";
  aboutBoxContent += "When used with the in-game browser, the browser will request trust. Trust is required for navigation features to work.";
  aboutBoxContent += "<br><br>";
  aboutBoxContent += "This program uses portions or the full framework of the following technologies:<br><br><br>";
  aboutBoxContent += "<center><img src=\"img/ab_cMapTime.png\"></center><br><br>";
  aboutBoxContent += "<font face=\"Arial\" size=1 color=#EEEEEE><i>MapTime&trade;</i> is a mapping and coordinate conversion engine designed and engineered by <a href=\"http://www.se51.net\" target=\"_blank\">Dan O\'Connor</a>. The engine retrieves data for all solar systems and converts into 100% accurate pixel location on screen. The engine was specifically developed and engineered for this project, and focuses on speed.";
  aboutBoxContent += "<br><br><br>";
  aboutBoxContent += "<center><img src=\"img/ab_cEveOnline.png\"></center><br><br>";
  aboutBoxContent += "EVE Online and the EVE logo are the registered trademarks of CCP hf. All rights are reserved worldwide. All other trademarks are the property of their respective owners. EVE Online, the EVE logo, EVE and all associated logos and designs are the intellectual property of CCP hf. All artwork, screenshots, characters, vehicles, storylines, world facts or other recognizable features of the intellectual property relating to these trademarks are likewise the intellectual property of CCP hf. CCP hf. has granted permission to [insert your name or site name] to use EVE Online and all associated logos and designs for promotional and information purposes on its website but does not endorse, and is not in any way affiliated with, [insert name or site name]. CCP is in no way responsible for the content on or functioning of this website, nor can it be liable for any damage arising from the use of this website.";
  aboutBoxContent += "<br><br>";
  aboutBoxContent += "<i>Cerberus Maps</i> employs the data dump, provided by CCP in the <a href=\"http://www.eveonline.com/community/toolkit.asp\" target=\"_blank\">Community Toolkit</a>.";
  aboutBoxContent += "<br><br><br>";
  aboutBoxContent += "<center><img src=\"img/ab_cScriptGate.png\"></center><br><br>";
  aboutBoxContent += "A small but efficient implementation to send Javascript variables to PHP (server side) for further processing, and send back the results into the client side. Developed and engineered for Cerberus Maps by Cerberus Research. Development of such a feature was required as detailed data for solar systems is immense.<br><br><br>";
  aboutBoxContent += "<center><img src=\"img/ab_cResearch.png\"></center><br><br>";
  aboutBoxContent += "<i>Cerberus Research</i> is the programming and development division of the <i>Cerberus Network</i>, a EVE Online corporation, run by Dan O\'Connor.<br><br>";
  aboutBoxContent += "<center><img src=\"img/ab_cNetwork.png\"></center><br><br>";
  aboutBoxContent += "A corporation that focuses on industry and research, as well as manufacturing of ships for competive prices.";
  aboutBoxContent += "<br><br><br>";
  aboutBoxContent += "<center><i>This was a triumph!<br>I\'m making a note here: HUGE SUCCESS.<br>It\'s hard to overstate my satisfaction.<br>Cerberus Research - We do what we must...because we can.<br>For the good of all of us. Except the ones who are dead.<br>But there\'s no sense crying over every mistake.<br>You just keep on trying \'til you run out of cake.<br>And the Science gets done. And you make a neat gun.<br>For the people who are still alive.</i></center>";
  aboutBoxContent += "<div style=\"position: absolute; top: 5px; right: 20px;\"><font face=\"Arial\" size=2 color=#FFFFFF><b><a onclick=\"javascript:closeAboutBox();\"><u>CLOSE</u></a></div>";
  
  document.getElementById("aboutbox").innerHTML = aboutBoxContent;
  
}


function closeAboutBox()
{
  document.getElementById("aboutbox").style.display = "none";
  canZoom = true;
}



function getDataForSystem()
{
  var head = document.getElementsByTagName('head').item(0);
  document.getElementById('startLooking').onclick = function () {
    var windowByTwo = (window.innerWidth / 2);
    var spaceFromLeft = windowByTwo - 400;
    var heightOfWindow = (window.innerHeight - 170);
    document.getElementById("detailedData").style.left = spaceFromLeft;
    document.getElementById("detailedData").style.top = 120;
    document.getElementById("detailedData").style.height = heightOfWindow;
    document.getElementById("detailedData").style.display = "block";

    document.getElementById("detailedData").innerHTML = "<br><br><center><font face=\"Arial\" size=3 color=#DDDDDD><b>CONNECTING...</b><br><br><br><img src=\"img/spinner.gif\"></center>";

    // alert("what bout my star?"); - Ranka FTW! I love that song
    var script = document.createElement('script');
    script.setAttribute( 'type', 'text/javascript' );
    var systemToFind = document.getElementById("search").value;
    script.setAttribute( 'src', 'data.php?name='+systemToFind );
    head.insertBefore( script, head.firstChild );
    canZoom = false;
  };
}


function closeDetails()
{
  document.getElementById("detailedData").style.display = "none";
  document.getElementById("detailedData").innerHTML = "";
  canZoom = true;
}
