// -------------------------------------------------------
var DropMenu_timeoutRootMap   = new Object();
var DropMenu_nextId           = 0;

// state is one of closed opening open closing
var DropMenu_state            = new Object();

// these keys have to match the id of html attribute
DropMenu_state[ 'menu-products']    = 'closed';
DropMenu_state[ 'menu-industries']  = 'closed';
DropMenu_state[ 'menu-services']    = 'closed';
DropMenu_state[ 'menu-upload']      = 'closed';


function DropMenu_open( id )
{  
   var $root = DropMenu_timeoutRootMap[ id ];
   
   var rootid  = $root.attr('id');
      
   var state = DropMenu_state[ rootid ];
   
   // close only if the state is 'closing'
   if ( state == 'opening' )
   {
      $root.find('ul').css('visibility', 'visible');
      
      DropMenu_state[ rootid ] = 'open';
   }
}

function DropMenu_close( id )
{  
   var $root = DropMenu_timeoutRootMap[ id ];
   
   var rootid  = $root.attr('id');
      
   var state = DropMenu_state[ rootid ];
   
   // close only if the state is 'closing'
   if ( state == 'closing' )
   {
      $root.find('ul').css('visibility', 'hidden');
      
      DropMenu_state[ rootid ] = 'closed';
   }
}

function DropMenu_onIn_Root()
{
   var $root   = $(this);
   var rootid  = $root.attr('id');
   
   var state = DropMenu_state[ rootid ];
   
   if ( state == 'opening' )
   {
      // it is already being opened...do nothing
      return;
   }
   else if ( state == 'closing' )
   {
      // somebody has asked this to close...we are going to cancel that order!
      
      DropMenu_state[ rootid ] = 'open';
   }
   else if ( state == 'open' )
   {
      // it's already open..do nothing
      return;
   }
   else if ( state == 'closed' )
   {
      // alright!! we need to open this
      var id = DropMenu_nextId;
      DropMenu_nextId++;
      
      DropMenu_timeoutRootMap[ id ] = $root;
      
      DropMenu_state[ rootid ] = 'opening';
      
      window.setTimeout( "DropMenu_open(" + id + ")", 500 );
      
      return;
   }
}

function DropMenu_onIn_DropDown()
{
   var $root   = $(this).parent('li');
   var rootid  = $root.attr('id');
   
   var state = DropMenu_state[ rootid ];
   
   if ( state == 'opening' )
   {
      // it is already being opened...do nothing
      return;
   }
   else if ( state == 'closing' )
   {
      // somebody has asked this to close...we are going to cancel that order!
      
      DropMenu_state[ rootid ] = 'open';
   }
   else if ( state == 'open' )
   {
      // it's already open..do nothing
      return;
   }
   else if ( state == 'closed' )
   {
      return;
   }
}

function DropMenu_onOut_Root()
{
   var $root   = $(this);
   var rootid  = $root.attr('id');
   
   var state = DropMenu_state[ rootid ];
   
   if ( state == 'opening' )
   {
      // it is being opened...so cancel that order
      
      DropMenu_state[ rootid ] = 'closed';
      
      return;
   }
   else if ( state == 'closing' )
   {
      // somebody has already asked this to close...so do nothing
      
      return;
   }
   else if ( state == 'open' )
   {
      // it's open..so close it

      var id = DropMenu_nextId;
      
      DropMenu_nextId++;
      
      DropMenu_timeoutRootMap[ id ] = $root;
      
      DropMenu_state[ rootid ] = 'closing';
      
      window.setTimeout( "DropMenu_close(" + id + ")", 250 );

      return;
   }
   else if ( state == 'closed' )
   {
      // it's already closed...do nothing
      
      return;
   }
}

function DropMenu_onOut_DropDown()
{
   var $root   = $(this).parent('li');
   var rootid  = $root.attr('id');
   
   var state = DropMenu_state[ rootid ];
   
   if ( state == 'opening' )
   {
      // it is being opened...so cancel that order
      
      DropMenu_state[ rootid ] = 'closed';
      
      return;
   }
   else if ( state == 'closing' )
   {
      // somebody has already asked this to close...so do nothing
      
      return;
   }
   else if ( state == 'open' )
   {
      // it's open..so close it

      var id = DropMenu_nextId;
      
      DropMenu_nextId++;
      
      DropMenu_timeoutRootMap[ id ] = $root;
      
      DropMenu_state[ rootid ] = 'closing';
      
      window.setTimeout( "DropMenu_close(" + id + ")", 250 );

      return;
   }
   else if ( state == 'closed' )
   {
      // it's already closed...do nothing
      
      return;
   }
}

//loading popup with jQuery magic!  

function showPopup_pageModal()
{  
   $("#site-dialog").jqmShow();
}  

//disabling popup with jQuery magic!  

function hidePopup_pageModal()
{  
   $("#site-dialog").jqmHide();
}  

// total life =
var pingIntervalInMinutes = 2;

var pingLifeInHours = 2;

var pingIntervalsLeft = pingLifeInHours * 60 / pingIntervalInMinutes;

$(document).ready(
                     function()
                     {  
                        $('#menu-products').bind(  'mouseover', DropMenu_onIn_Root  );
                        $('#menu-industries').bind('mouseover', DropMenu_onIn_Root  );
                        $('#menu-services').bind(  'mouseover', DropMenu_onIn_Root  );
                        $('#menu-upload').bind(  'mouseover', DropMenu_onIn_Root  );
                        
                        $('#menu-products').bind(  'mouseout',  DropMenu_onOut_Root );
                        $('#menu-industries').bind('mouseout',  DropMenu_onOut_Root );
                        $('#menu-services').bind(  'mouseout',  DropMenu_onOut_Root );
                        $('#menu-upload').bind(  'mouseout',  DropMenu_onOut_Root );

                        $('.DropMenu li > ul').bind('mouseover',  DropMenu_onIn_DropDown );
                        $('.DropMenu li > ul').bind('mouseout',   DropMenu_onOut_DropDown );
                        
                        $('#site-dialog').jqm();
                        
                        // showPopup_pageModal();
                        
                        // setup pinging so tomcat does not timeout
      
                        $(document).everyTime( pingIntervalInMinutes * 60 * 1000, function(){

                           $.post( '/a/ping/ping', {}, function(){
                           
                              pingIntervalsLeft--;
                              
                              if ( pingIntervalsLeft <= 0 )
                              {
                                 $(document).stopTime();
                              }
                           
                           } );

                        } );

                     }
);

