var index = 0;


function KeyDown(event)
{
   var key;
   if(window.event)
   {
      key = event.keyCode;
   }
   else if(e.which)
   {
      key = e.which
   }
   if(key == 39)
      Next();
   else if(key == 37)
      Previous();
}

function AssignWidth(width)
{
	var x = document.getElementById("ImageTable");
	x.width = width + "px";
}

function UseImage(index2)
{
   index = parseInt(index2);
   Move(imageNames[index]);
}

function ReplaceImage(name)
{
   var span = document.getElementById("BlowupSpan");
   span.innerHTML = "<img src=\"" + name + "\" border=\"0\" alt=\"Please wait, Image is loading\" id=\"BlowupImage\" style=\"margin:0px 0px 0px 0px;\" onload = \"Center()\" onclick=\"Next()\" usemap=\"#BlowupMap\">";
   var mapSpan = document.getElementById("MapSpan");
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
       mapSpan.innerHTML = "<map name=\"BlowupMap\">" + xmlhttp.responseText + "</map>";
    }
  }
xmlhttp.open("GET","GetAreaMap.php?Image="+name,true);
xmlhttp.send();

   var nextImage = document.getElementById("TempNext");
   var prevImage = document.getElementById("TempPrev");
   nextImage.src = imageNames[IncIndex(index)];
   prevImage.src = imageNames[DecIndex(index)];
   
}

function DisplayTaggedName(event, name)
{
   var span = document.getElementById("TaggedName");
   span.style.position = "fixed";

   span.style.left = (event.clientX) + "px";
   span.style.top = (event.clientY) + "px";
   span.innerHTML = name;
   span.style.backgroundColor = "#363621";
   span.style.color = "#FFFFFF";
   span.style.padding = "5px";
   span.style.border = "2px solid #EEF527";
   span.style.zIndex = 100;
}

function CloseTaggedName()
{
   var span = document.getElementById("TaggedName");
   span.style.left = "-4000px";
   span.style.top = "-4000px";
}

function Center()
{
   var div = document.getElementById("ImageTable");
   div.style.zIndex = 90;
   var image = document.getElementById("BlowupImage");
   var left = (document.body.clientWidth / 2 - image.width / 2 - 50);
   if(left < 0)
      left = 0;
   div.style.left =  left + "px";
   var top = (document.body.scrollTop + document.body.clientHeight / 2 - image.height / 2 - 50);
   if(top < 0)
      top = 0;
   div.style.top = top + "px";
}

function Move(imageName)
{
   ReplaceImage(imageName);
   grayOut(true);
}
function Close()
{
   var x = document.getElementById("ImageTable");
   x.style.left = "-4000px";
   x.style.top = "-4000px";
   var tagSpan = document.getElementById("TaggedName");
   tagSpan.style.left = "-4000px";
   tagSpan.style.top = "-4000px";
   grayOut(false);
}

function Next()
{
   index = IncIndex(index);
   Move(imageNames[index]);
}

function IncIndex(index2)
{
   index2 = index2 + 1;
   if(index2 > max)
   {
      index2 = 0;
   }
   return index2;
}

function Previous()
{
   index = DecIndex(index);
   Move(imageNames[index]);	
}

function DecIndex(index2)
{
   index2 = index2 - 1;
   if(index2 < 0)
   {
      index2 = max;
   }
   return index2;
}

//taken from http://www.hunlock.com/blogs/Snippets:_Howto_Grey-Out_The_Screen
function grayOut(vis, options) {
  // Pass true to gray out screen, false to ungray
  // options are optional.  This is a JSON object with the following (optional) properties
  // opacity:0-100         // Lower number = less grayout higher = more of a blackout 
  // zindex: #             // HTML elements with a higher zindex appear on top of the gray out
  // bgcolor: (#xxxxxx)    // Standard RGB Hex color code
  // grayOut(true, {'zindex':'50', 'bgcolor':'#0000FF', 'opacity':'70'});
  // Because options is JSON opacity/zindex/bgcolor are all optional and can appear
  // in any order.  Pass only the properties you need to set.
  var options = options || {}; 
  var zindex = options.zindex || 50;
  var opacity = options.opacity || 70;
  var opaque = (opacity / 100);
//  var bgcolor = options.bgcolor || '#000000';
  var bgcolor = options.bgcolor || '#999999';
  var dark=document.getElementById('darkenScreenObject');
  if (!dark) {
    // The dark layer doesn't exist, it's never been created.  So we'll
    // create it here and apply some basic styles.
    // If you are getting errors in IE see: http://support.microsoft.com/default.aspx/kb/927917
    var tbody = document.getElementsByTagName("body")[0];
    var tnode = document.createElement('div');           // Create the layer.
        tnode.style.position='absolute';                 // Position absolutely
        tnode.style.top='0px';                           // In the top
        tnode.style.left='0px';                          // Left corner of the page
        tnode.style.overflow='hidden';                   // Try to avoid making scroll bars            
        tnode.style.display='none';                      // Start out Hidden
        tnode.id='darkenScreenObject';                   // Name it so we can find it later
    tbody.appendChild(tnode);                            // Add it to the web page
    dark=document.getElementById('darkenScreenObject');  // Get the object.
  }
  if (vis) {
    // Calculate the page width and height 
    if( document.body && ( document.body.scrollWidth || document.body.scrollHeight ) ) {
        var pageWidth = document.body.scrollWidth+'px';
        var pageHeight = document.body.scrollHeight+'px';
    } else if( document.body.offsetWidth ) {
      var pageWidth = document.body.offsetWidth+'px';
      var pageHeight = document.body.offsetHeight+'px';
    } else {
       var pageWidth='100%';
       var pageHeight='100%';
    }   
    //set the shader to cover the entire page and make it visible.
    dark.style.opacity=opaque;                      
    dark.style.MozOpacity=opaque;                   
    dark.style.filter='alpha(opacity='+opacity+')'; 
    dark.style.zIndex=zindex;        
    dark.style.backgroundColor=bgcolor;  
    dark.style.width= pageWidth;
    dark.style.height= pageHeight;
    dark.style.display='block';           
  } else {
     dark.style.display='none';
     var imageTable = document.getElementById('ImageTable');
     imageTable.style.top='-4000px';
     imageTable.style.left='-4000px';
  }
}

