// JavaScript Document
var helpMode = 0;

var lastX;
var lastY;

function showHelp(id,title,height)
{ 
 displayPopupDiv(title,'ajax/showhelp.php?id=' + id,600,height,7);
}

function getReferenceTop()
{
 var refObj = document.getElementById('bodydiv');
 
 var iReturnValue = 0;
 
 while(refObj != null)
 {  
  iReturnValue+= refObj.offsetTop;
  refObj = refObj.offsetParent;
 }
 
 return iReturnValue;  
}

function getReferenceLeft()
{
 var refObj = document.getElementById('bodydiv');
 
 var iReturnValue = 0;
 
 while(refObj != null)
 {  
  iReturnValue+= refObj.offsetLeft;
  refObj = refObj.offsetParent;
 }
 
 return iReturnValue;  
}

function getPageCoordY(coord_x,coord_y)
{
  var rtnval = coord_y;
  
  var highestZ = 0;
  for(var ii = 1; ii < 7; ii++)
  {
   //This popup div is visible, find out what's inside it
   
   if(ii > 1)
    var divtype = ii;   
   else
    var divtype = "";
    
   var popobj = document.getElementById('popupdiv' + divtype);
      
   var pop_left = popobj.style.left;
   pop_left = parseInt(pop_left.substring(0,pop_left.lastIndexOf('px')));
   
   var pop_width = document.getElementById('popupdiv' + divtype).style.width;      
   pop_width = parseInt(pop_width.substring(0,pop_width.lastIndexOf('px')));
            
   var pop_top = popobj.style.top;
   pop_top = parseInt(pop_top.substring(0,pop_top.lastIndexOf('px')));
   
   var pop_height = document.getElementById('popupdiv' + divtype).style.height;      
   pop_height = parseInt(pop_height.substring(0,pop_height.lastIndexOf('px')));
   
   if (window.getComputedStyle)                       
     var thisZIndex = document.defaultView.getComputedStyle(document.getElementById('popupdiv' + divtype),null).getPropertyValue('z-index');   
   else
     var thisZIndex = document.getElementById('popupdiv' + divtype).currentStyle['zIndex'];
     
   if(popobj != null && popobj.style.display == '' && 
      coord_x >= pop_left && coord_x <= parseInt(pop_left + pop_width) &&
      coord_y >= pop_top && coord_y <= parseInt(pop_top + pop_height) && thisZIndex > highestZ)
   {
    highestZ = thisZIndex;
    rtnval = coord_y - pop_top;       
   }
  }
  
  return rtnval;
}

function getPageCoordX(coord_x,coord_y)
{
  var rtnval = coord_x;
  
  var highestZ = 0;
  for(var ii = 1; ii < 7; ii++)
  {
   //This popup div is visible, find out what's inside it
   
   if(ii > 1)
    var divtype = ii;   
   else
    var divtype = "";
    
   var popobj = document.getElementById('popupdiv' + divtype);
      
   var pop_left = popobj.style.left;
   pop_left = parseInt(pop_left.substring(0,pop_left.lastIndexOf('px')));
   
   var pop_width = document.getElementById('popupdiv' + divtype).style.width;      
   pop_width = parseInt(pop_width.substring(0,pop_width.lastIndexOf('px')));
            
   var pop_top = popobj.style.top;
   pop_top = parseInt(pop_top.substring(0,pop_top.lastIndexOf('px')));
   
   var pop_height = document.getElementById('popupdiv' + divtype).style.height;      
   pop_height = parseInt(pop_height.substring(0,pop_height.lastIndexOf('px')));
   
   if (window.getComputedStyle)                       
     var thisZIndex = document.defaultView.getComputedStyle(document.getElementById('popupdiv' + divtype),null).getPropertyValue('z-index');   
   else
     var thisZIndex = document.getElementById('popupdiv' + divtype).currentStyle['zIndex'];
     
   if(popobj != null && popobj.style.display == '' && 
      coord_x >= pop_left && coord_x <= parseInt(pop_left + pop_width) &&
      coord_y >= pop_top && coord_y <= parseInt(pop_top + pop_height) && thisZIndex > highestZ)
   {
    highestZ = thisZIndex;
    rtnval = coord_x - pop_left;       
   }
  }
  
  return rtnval;
}

function getPageUri(coord_x,coord_y)
{
  var rtnval = document.getElementById('adminpageuri').innerHTML;
  
  var highestZ = 0;
  for(var ii = 1; ii < 7; ii++)
  {
   //This popup div is visible, find out what's inside it
   
   if(ii > 1)
    var divtype = ii;   
   else
    var divtype = "";
    
   var popobj = document.getElementById('popupdiv' + divtype);
      
   var pop_left = popobj.style.left;
   pop_left = parseInt(pop_left.substring(0,pop_left.lastIndexOf('px')));
   
   var pop_width = document.getElementById('popupdiv' + divtype).style.width;      
   pop_width = parseInt(pop_width.substring(0,pop_width.lastIndexOf('px')));
            
   var pop_top = popobj.style.top;
   pop_top = parseInt(pop_top.substring(0,pop_top.lastIndexOf('px')));
   
   var pop_height = document.getElementById('popupdiv' + divtype).style.height;      
   pop_height = parseInt(pop_height.substring(0,pop_height.lastIndexOf('px')));
   
   if (window.getComputedStyle)                       
     var thisZIndex = document.defaultView.getComputedStyle(document.getElementById('popupdiv' + divtype),null).getPropertyValue('z-index');   
   else
     var thisZIndex = document.getElementById('popupdiv' + divtype).currentStyle['zIndex'];
     
   //alert('popupdiv' + divtype);
   //alert(popobj.style.display);
   //alert(coord_x + ">=" + pop_left);
   //alert(coord_x + "<=" + parseInt(pop_left + pop_width));
            
   if(popobj != null && popobj.style.display == '' && 
      coord_x >= pop_left && coord_x <= parseInt(pop_left + pop_width) &&
      coord_y >= pop_top && coord_y <= parseInt(pop_top + pop_height) && thisZIndex > highestZ)
   {
    highestZ = thisZIndex;
    rtnval = document.getElementById('contentsource' + divtype).innerHTML;    
   }
  }
  
  return rtnval;
}

function checkHelpMovement()
{
   var mouseX = parseInt(document.getElementById('MouseX').value) + 20;
   var mouseY = parseInt(document.getElementById('MouseY').value) + 20;
   
   if(document.getElementById('adminmousepos') != null)
    document.getElementById('adminmousepos').innerHTML = mouseX + 'x' + mouseY;
}

function checkHelpModeExecution()
{ 
  var mouseX = parseInt(document.getElementById('MouseX').value) + 20;
  var mouseY = parseInt(document.getElementById('MouseY').value) + 20;
  
  var uri = getPageUri(mouseX,mouseY);
  var coord_x = getPageCoordX(mouseX,mouseY);
  var coord_y = getPageCoordY(mouseX,mouseY);
  
  if(coord_x == mouseX && coord_y == mouseY)
  {
   coord_x -= getReferenceLeft();
   coord_y -= getReferenceTop();   
  }
  
  //ADD HELP BUTTON
  if(helpMode == 1)
  {   
   var vRunAjax = new runAjax('ajax/admin_addhelpicon.php?uri=' + uri + '&coord_x=' + coord_x + '&coord_y=' + coord_y);    
   clearAdminHelperTxt();
  }
  
  //DELETE HELP BUTTON
  else if(helpMode == 2)
  {
   var vRunAjax = new runAjax('ajax/admin_delhelpicon.php?uri=' + uri + '&coord_x=' + coord_x + '&coord_y=' + coord_y);        
    clearAdminHelperTxt(); 
  }
  
  //SELECT HELP BUTTON
  else if(helpMode == 3)
  {
   setAdminHelperTxt('Please select new coordinate to place help button');   
   helpMode = 4;
   lastX = coord_x;
   lastY = coord_y;
  }
   
  //MOVE HELP BUTTON
  else if(helpMode == 4)
  {
   var vRunAjax = new runAjax('ajax/admin_movehelpicon.php?uri=' + uri + '&icon_x=' + lastX + '&icon_y=' + lastY + '&coord_x=' + coord_x + '&coord_y=' + coord_y);           
   clearAdminHelperTxt();   
   
   lastX = null;
   lastY = null;
  }
      
  if(helpMode > 0 && lastX == null && lastY == null)
  {
    var ok = true;
    for(var stdivnum = 6; stdivnum >= 1; stdivnum--)
    {
     var divit = stdivnum;
     
     if(divit == 1)
      divit = '';
     
     if(document.getElementById('popupdiv' + divit).style.display == '')
     {
       if (window.getComputedStyle)                       
         var thisZIndex = document.defaultView.getComputedStyle(document.getElementById('popupdiv' + divit),null).getPropertyValue('z-index');   
       else
         var thisZIndex = document.getElementById('popupdiv' + divit).currentStyle['zIndex'];    
    
       var newZIndex = thisZIndex;
              
      ok = false;
      break;
     }
    }
    
    //alert('ok');
          
    
    if(ok)
     disableBackgroundShield();          
    else
     enableBackgroundShield(null,newZIndex);
    
    if(helpMode != 4)
      helpMode = 0;
      
    alert("Please reload page to see result");
  }
}

function addHelpButton()
{
 enableBackgroundShield(null,500);
 helpMode = 1; 
 setAdminHelperTxt('Please select coordinate to place help button');
}

function delHelpButton()
{
 enableBackgroundShield(null,500);
 helpMode = 2; 
 setAdminHelperTxt('Please select coordinate to remove help button');
}

function moveHelpButton()
{
 enableBackgroundShield(null,500);
 helpMode = 3; 
 setAdminHelperTxt('Please select help button');
}

function setAdminHelperTxt(text)
{
  setelementvisibilitys('adminmousepos',true);
  document.getElementById('adminstatus').innerHTML = text;
}

function clearAdminHelperTxt(text)
{
  setelementvisibilitys('adminmousepos',false);  
  document.getElementById('adminstatus').innerHTML = "";
}
