﻿// Global Variables
var dragObject = new Object( );
var resizeObject = new Object( );

function getMinWidth( id )
{
   // Local Variables
   retVal = 0;

   if ( id == "FindDevices" )
   {
      retVal = 450;
   }
   else if ( id == "ReportsWindow" )
   {
      retVal = 750;   
   }
   else if ( id == "OptionsWindow" )
   {
      retVal = 750;   
   }
   else if ( id == "DataWindow" )
   {
      retVal = 750;   
   }
  
   return retVal;
}


function getMinHeight( id )
{
   // Local Variables
   retVal = 0;

   if ( id == "FindDevices" )
   {
      retVal = 180;
   }
   else if ( id == "ReportsWindow" )
   {
      retVal = 500;   
   }
   else if ( id == "OptionsWindow" )
   {
      retVal = 430;   
   }
   else if ( id == "DataWindow" )
   {
      retVal = 150;   
   }

   
   return retVal;
}


function dragWindow( event, id )
{
   // Local Variables
   var el = document.getElementById( id );
   var x, y;

   if ( el != null )
   {
      // Get the current mouse co-ordinates
      if ( browser.engine != "MSIE" )
      {
         x = event.clientX + window.scrollX;
         y = event.clientY + window.scrollY;
      }
      else
      {
         // Grab Focus
         setWindowFocus( id );

         x = window.event.clientX + document.documentElement.scrollLeft +
                 document.body.scrollLeft;
         y = window.event.clientY + document.documentElement.scrollTop + 
                 document.body.scrollTop;
      }

      // Initialise the drag object
      dragObject.element = el;
      dragObject.startX = x;
      dragObject.startY = y;
      dragObject.startLeft = parseInt( el.style.left ); 
      dragObject.startRight = parseInt( el.style.right ); 
      dragObject.startTop = parseInt( el.style.top ); 
      dragObject.startBottom = parseInt( el.style.bottom ); 

      // Attach events to the document to handle the rest of the drag and drop event
      if ( browser.engine != "MSIE" )
      {
         document.addEventListener( "mousemove", moveWindow, true );
         document.addEventListener( "mouseup", dropWindow, true );
         event.preventDefault( );
      }
      else
      {
         document.attachEvent( "onmousemove", moveWindow );
         document.attachEvent( "onmouseup", dropWindow );
         window.event.cancelBubble = true;
         window.event.returnValue = false;
      }
   }
}


function moveWindow( event )
{
   // Local Variables
   var x, y;

   if ( dragObject.element != null )
   {

      // Get the current mouse co-ordinates
      if ( browser.engine != "MSIE" )
      {
         x = event.clientX + window.scrollX;
         y = event.clientY + window.scrollY;
      }
      else
      {
         x = window.event.clientX + document.documentElement.scrollLeft +
                document.body.scrollLeft;
         y = window.event.clientY + document.documentElement.scrollTop + 
                document.body.scrollTop;
      }

      // Move the window
      if ( ! isNaN( dragObject.startLeft ) )
         dragObject.element.style.left = ( dragObject.startLeft + x - dragObject.startX ) + "px"; 

      if ( ! isNaN( dragObject.startRight ) )
         dragObject.element.style.right = ( dragObject.startRight - x + dragObject.startX ) + "px"; 

      if ( ! isNaN( dragObject.startTop ) )
         dragObject.element.style.top =( dragObject.startTop  + y - dragObject.startY ) + "px";

      if ( ! isNaN( dragObject.startBottom ) )
         dragObject.element.style.bottom =( dragObject.startBottom - y + dragObject.startY ) + "px";

      // Cancel the default event handling
      if ( browser.engine != "MSIE" )
      {
         event.preventDefault();
      }
      else
      {
         window.event.cancelBubble = true;
         window.event.returnValue = false;
      }
   }
}


function dropWindow( event )
{
   // Clear the drag element
   dragObject.element = null;

   // Detach the event handlers and drop the floating element
   if ( browser.engine != "MSIE" )
   {
      document.removeEventListener( "mousemove", moveWindow, true );
      document.removeEventListener( "mouseup", dropWindow, true );
   }
   else
   {
      document.detachEvent( "onmousemove", moveWindow );
      document.detachEvent( "onmouseup", dropWindow );
   }
}


function resizeWindow( event, windowid, direction )
{
   // Local Variables
   var el = document.getElementById( windowid );
   var x, y;

   if ( el != null )
   {
      // Get the current mouse co-ordinates
      if ( browser.engine != "MSIE" )
      {
         x = event.clientX + window.scrollX;
         y = event.clientY + window.scrollY;
      }
      else
      {
         // Grab Focus
         setWindowFocus( windowid );

         x = window.event.clientX + document.documentElement.scrollLeft +
                 document.body.scrollLeft;
         y = window.event.clientY + document.documentElement.scrollTop + 
                 document.body.scrollTop;
      }

      // Initialise the drag object
      resizeObject.element = el;
      resizeObject.direction = direction;
      resizeObject.startX = x;
      resizeObject.startY = y;
      resizeObject.height = parseInt( el.style.height ); 
      resizeObject.width = parseInt( el.style.width ); 
      resizeObject.startLeft = parseInt( el.style.left ); 
      resizeObject.startRight = parseInt( el.style.right ); 
      resizeObject.startTop = parseInt( el.style.top ); 
      resizeObject.startBottom = parseInt( el.style.bottom ); 

      // Attach events to the document to handle the rest of the drag and drop event
      if ( browser.engine != "MSIE" )
      {
         document.addEventListener( "mousemove", doResizeWindow, true );
         document.addEventListener( "mouseup", stopResizeWindow, true );
         event.preventDefault( );
      }
      else
      {
         document.attachEvent( "onmousemove", doResizeWindow );
         document.attachEvent( "onmouseup", stopResizeWindow );
         window.event.cancelBubble = true;
         window.event.returnValue = false;
      }
   }
}


function doResizeWindow( event )
{
   // Local Variables
   var x, y;

   if ( resizeObject.element != null )
   {
      // Get the current mouse co-ordinates
      if ( browser.engine != "MSIE" )
      {
         x = event.clientX + window.scrollX;
         y = event.clientY + window.scrollY;
      }
      else
      {
         x = window.event.clientX + document.documentElement.scrollLeft +
                document.body.scrollLeft;
         y = window.event.clientY + document.documentElement.scrollTop + 
                document.body.scrollTop;
      }

      // Resize the window
      doVerticalResize( resizeObject.element, resizeObject.height, ( y - resizeObject.startY ), 
                        resizeObject.startTop, resizeObject.startBottom, resizeObject.direction );

      doHorizontalResize( resizeObject.element, resizeObject.width, ( x - resizeObject.startX ), 
                          resizeObject.startLeft, resizeObject.startRight, resizeObject.direction );

      scaleElements( resizeObject.element );

      // Cancel the default event handling
      if ( browser.engine != "MSIE" )
      {
         event.preventDefault();
      }
      else
      {
         window.event.cancelBubble = true;
         window.event.returnValue = false;
      }
   }
}


function doVerticalResize( element, currentHeight, resizeAmount, top, bottom, direction )
{
   // Get the minimum allowed height for this element
   var minHeight = getMinHeight( element.id );

   // Attempt to resize the object   
   if ( ! isNaN( currentHeight ) )
   {
      if ( ( direction == 'NW' ) || ( direction == 'N' ) || ( direction == 'NE' ) )
      {
         // Verify that this resize won't exceed our size constraints
         if ( ( currentHeight - resizeAmount ) > minHeight )
         {
            // Resize the window
            element.style.height = ( currentHeight - resizeAmount ) + "px"; 
            
            // As long as we're at least of minimum size; move the window in response to the resize
            if ( ! isNaN( top ) )
               element.style.top =( top  + resizeAmount ) + "px";
         }
         else
         {
            // Enforce the minimum window height
            element.style.height = minHeight + "px";      
         }   
      }
      else if ( ( direction == 'SW' ) || ( direction == 'S' ) || ( direction == 'SE' ) )
      {
         // Verify that this resize won't exceed our size constraints
         if ( ( currentHeight + resizeAmount ) > minHeight )
         {
            element.style.height = ( currentHeight + resizeAmount ) + "px"; 
            
            // As long as we're at least of minimum size; move the window in response to the resize
            if ( ! isNaN( bottom ) )
               element.style.bottom =( bottom - resizeAmount ) + "px";
         }
         else
         {
            // Enforce the minimum window height
            element.style.height = minHeight + "px"; 
         }
      }
   }
}


function doHorizontalResize( element, currentWidth, resizeAmount, left, right, direction )
{
   // Get the minimum allowed height for this element
   var minWidth = getMinWidth( element.id );

   if ( ! isNaN( currentWidth ) )
   {
      if ( ( direction == 'NW' ) || ( direction == 'W' ) || ( direction == 'SW' ) )
      {
         // Verify that this resize won't exceed our size constraints
         if ( ( currentWidth - resizeAmount ) > minWidth )
         {
            element.style.width = ( currentWidth - resizeAmount ) + "px"; 
            
            // As long as we're at least of minimum size; move the window in response to the resize
            if ( ! isNaN( left ) )
               element.style.left =( left + resizeAmount ) + "px";
         }
         else
         {
            // Enforce the minimum window height
            element.style.width  = minWidth + "px";           
         }
      }
      else if ( ( direction == 'NE' ) || ( direction == 'E' ) || ( direction == 'SE' ) )
      {
         // Verify that this resize won't exceed our size constraints
         if ( ( currentWidth + resizeAmount ) > minWidth )
         {
            // Resize the window
            element.style.width = ( currentWidth + resizeAmount ) + "px"; 

            // As long as we're at least of minimum size; move the window in response to the resize
            if ( ! isNaN( right ) )
               element.style.right =( right - resizeAmount ) + "px";
         }
         else
         {
            // Enforce the minimum window height
            element.style.width = minWidth + "px";          
         }   
      }
   }
}

function scaleElements( element )
{
   // Scale the resize elements
   var children = element.childNodes;
      
   for ( var i = 0; i < children.length; i++ )
   {
      var child = children[i];
         
      // Handle Height Changes
      if ( ( child.className == "Resize-W" ) || ( child.className == "Resize-E" ) )
      {
         child.style.height = ( parseInt( element.style.height ) - 100 ) + "px";
      }
      else if ( ( child.className == "Resize-SW1" ) || ( child.className == "Resize-SE1" ) )
      {
         child.style.top = ( parseInt( element.style.height ) - 50 ) + "px";         
      }
      else if ( ( child.className == "Resize-SW2" ) || ( child.className == "Resize-S" )  || 
                ( child.className == "Resize-SE2" ) )
      {
         child.style.top = ( parseInt( element.style.height ) - 1 ) + "px";         
      }
      
      // Handle Width Changes
      if ( ( child.className == "Resize-N" ) || ( child.className == "Resize-S" ) )
      {
         child.style.width = ( parseInt( element.style.width ) - 100 ) + "px";
         child.style.height = "3px";
      }
      else if ( ( child.className == "Resize-NE1" ) || ( child.className == "Resize-SE2" ) )
      {
         child.style.left = ( parseInt( element.style.width ) - 50 ) + "px";         
      }
      else if ( ( child.className == "Resize-NE2" ) || ( child.className == "Resize-E" )  || 
                ( child.className == "Resize-SE1" ) )
      {
         child.style.left = ( parseInt( element.style.width ) - 1 ) + "px";
      }
   }

   // Call the custom resize logic
   customResize( element );
}


function customResize( element )
{
   // Custom Scale Logic for Find Devices Window
   if ( element.id == "FindDevices" )
   {
      var c1 = document.getElementById( "DeviceList" );
      var c2 = document.getElementById( "FindDevices_Body" );
      var c3 = document.getElementById( "FindDevices_Wrapper" );
            
      if ( c1 != null )
      {
         c1.style.height = ( parseInt( element.style.height ) - 115  ) + "px";
         c1.style.width =  ( parseInt( element.style.width )  - 150 ) + "px";
      }
      if ( c2 != null )
      {
         // Resize the div that holds the window content
         c2.style.height = element.style.height;
         c2.style.width =  element.style.width;
      }
      if ( c3 != null )
      {
         // Resize the iframe that blocks form elements from popping through
         c3.style.height = element.style.height;
         c3.style.width =  element.style.width;
      }
   }
   else if ( element.id == "ReportsWindow" )
   {
      var c1 = document.getElementById( "ReportList" );
      var c2 = document.getElementById( "LocationHistoryParameters" );
      var c3 = document.getElementById( "ResultsTab" );
      var c4 = document.getElementById( "DeviceListLH" );
      var c5 = document.getElementById( "Results_ScrollPane" );
      var c6 = document.getElementById( "AccountUsageReport" );
      
      if ( c1 != null )
      {
         c1.style.height = ( parseInt( element.style.height ) - 28 ) + "px";
      }

      if ( c2 != null )
      {
         c2.style.height = ( parseInt( element.style.height ) - 28 ) + "px";
         c2.style.width =  ( parseInt( element.style.width ) - 185 ) + "px";
      }

      if ( c3 != null )
      {
         c3.style.height = ( parseInt( element.style.height ) - 28 ) + "px";
         c3.style.width =  ( parseInt( element.style.width ) - 185 ) + "px";
      }
      
      if ( c4 != null )
      {
         c4.style.height = ( parseInt( element.style.height ) - 200 ) + "px";
      }

      if ( c5 != null )
      {
         c5.style.height = ( parseInt( element.style.height ) - 68 ) + "px";
      }
      
      if ( c6 != null )
      {
         c6.style.height = ( parseInt( element.style.height ) - 28 ) + "px";
         c6.style.width =  ( parseInt( element.style.width ) - 185 ) + "px";
      }
   }
   else if ( element.id == "OptionsWindow" )
   {
      var c1 = document.getElementById( "OptionList" );
      var c2 = document.getElementById( "Options_DeviceGroups" );
      var c3 = document.getElementById( "Options_AutoPoll" );

      if ( c1 != null )
      {
         c1.style.height = ( parseInt( element.style.height ) - 28 ) + "px";
      }

      if ( c2 != null )
      {
         c2.style.height = ( parseInt( element.style.height ) - 28 ) + "px";
         c2.style.width =  ( parseInt( element.style.width ) - 185 ) + "px";
      }
      
      if ( c3 != null )
      {
         c3.style.height = ( parseInt( element.style.height ) - 28 ) + "px";
         c3.style.width =  ( parseInt( element.style.width ) - 185 ) + "px";
      }
   }  
   else if ( element.id == "DataWindow" )
   {
      var c1 = document.getElementById( "Data_ScrollPane" );      
      var c2 = document.getElementById( "DataWindow_Wrapper" );

      if ( c1 != null )
      {
         c1.style.height = ( parseInt( element.style.height ) - 28 ) + "px";
         c1.style.width =  ( parseInt( element.style.width )  - 10 ) + "px";
      }
      if ( c2 != null )
      {
         // Resize the iframe that blocks form elements from popping through
         c2.style.height = element.style.height;
         c2.style.width =  element.style.width;
      }
   }
}


function stopResizeWindow( )
{
   // Clear the drag element
   resizeObject.element = null;

   // Detach the event handlers and drop the floating element
   if ( browser.engine != "MSIE" )
   {
      document.removeEventListener( "mousemove", doResizeWindow, true );
      document.removeEventListener( "mouseup", stopResizeWindow, true );
   }
   else
   {
      document.detachEvent( "onmousemove", doResizeWindow );
      document.detachEvent( "onmouseup", stopResizeWindow );
   }
}


function addResizeHandles( windowid )
{
   // Generate window resize handles
   document.write( "<img src=\"Images/spacer.gif\" class=\"Resize-NW1\" onmousedown=\"resizeWindow( event, '" + windowid + "', 'NW' );\"/>" );
   document.write( "<img src=\"Images/spacer.gif\" class=\"Resize-N\"   onmousedown=\"resizeWindow( event, '" + windowid + "', 'N'  );\"/>" );
   document.write( "<img src=\"Images/spacer.gif\" class=\"Resize-NE1\" onmousedown=\"resizeWindow( event, '" + windowid + "', 'NE' );\"/>" );
   document.write( "<img src=\"Images/spacer.gif\" class=\"Resize-NW2\" onmousedown=\"resizeWindow( event, '" + windowid + "', 'NW' );\"/>" );
   document.write( "<img src=\"Images/spacer.gif\" class=\"Resize-NE2\" onmousedown=\"resizeWindow( event, '" + windowid + "', 'NE' );\"/>" );
   document.write( "<img src=\"Images/spacer.gif\" class=\"Resize-W\"   onmousedown=\"resizeWindow( event, '" + windowid + "', 'W'  );\"/>" );
   document.write( "<img src=\"Images/spacer.gif\" class=\"Resize-E\"   onmousedown=\"resizeWindow( event, '" + windowid + "', 'E'  );\"/>" );
   document.write( "<img src=\"Images/spacer.gif\" class=\"Resize-SW1\" onmousedown=\"resizeWindow( event, '" + windowid + "', 'SW' );\"/>" );
   document.write( "<img src=\"Images/spacer.gif\" class=\"Resize-SE1\" onmousedown=\"resizeWindow( event, '" + windowid + "', 'SE' );\"/>" );
   document.write( "<img src=\"Images/spacer.gif\" class=\"Resize-SW2\" onmousedown=\"resizeWindow( event, '" + windowid + "', 'SW' );\"/>" );
   document.write( "<img src=\"Images/spacer.gif\" class=\"Resize-S\"   onmousedown=\"resizeWindow( event, '" + windowid + "', 'S'  );\"/>" );
   document.write( "<img src=\"Images/spacer.gif\" class=\"Resize-SE2\" onmousedown=\"resizeWindow( event, '" + windowid + "', 'SE' );\"/>" );
   
   // Ensure the elements are placed correctly
   var element = document.getElementById( windowid );
   scaleElements( element );
}


function setWindowFocus( id )
{
   // Local Variables
   var navWindow = document.getElementById( "Nav" );
   var deviceDataWindow = document.getElementById( "DataWindow" );
   var helpWindow = document.getElementById( "HelpWindow" );
   
   // Reset each of the window's z-index
   navWindow.style.zIndex = "150";
   deviceDataWindow.style.zIndex = "150";
   helpWindow.style.zIndex = "150";
   
   // Set the z-index for this particular window
   var element = document.getElementById( id );
   element.style.zIndex = 151;
}
