/***************************************************************************
 *   Copyright (C) 2007 Kennysp2
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the
 *   Free Software Foundation, Inc.
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 ***************************************************************************/
 


function xmenu_init  () 
{   
   //  The maximum width of an image
   max_w = 78;
   // The minimum width of an image
   min_w = 55;
   // The increment of the width of the hovered image (px)
   n = 4;
   // The interval of the 'increment' (millisecund (1 secund/1000) )
   ms = 50;
   setInterval('xmenu_refresh ("xmenu_menu",'+ max_w+','+ min_w+','+ n+');',ms);
   /*var m_images = document.getElementById('xmenu_menu').getElementsByTagName('img');
   for (var i=0; i<m_images.length; i++)
   {
      m_images[i].setAttribute('onmouseover','xmenu_hover(this);');
      m_images[i].setAttribute('onmouseout','xmenu_unhover(this);');
   }*/
}

// It will set the hover attribute of an image 'yes'.
var xmenu_hovered=0;
function xmenu_hover(tag)
{
   xmenu_hovered=tag;
   //document.getElementById('xmenu_text').innerHTML=tag.alt;
   //document.getElementById('xmenu_text').style.display='inline';
}

// It will set the hover attribute of an image 'no'.
function xmenu_unhover(tag)
{
   xmenu_hovered=0;
   //document.getElementById('xmenu_text').innerHTML="";
   //document.getElementById('xmenu_text').style.display='none';
}

// This is refreshing the Mac Style Menu.
function  xmenu_refresh (menu_id, max_w, min_w, n)
{
   var prev_img, next_img;
   var m_images = document.getElementById(menu_id).getElementsByTagName('img');
   // For each image in the menu, do ->
   for (var i=0; i<m_images.length; i++) 
   {
      // curr_width is the width of the image
      var curr_width = m_images[i].getAttribute('width');
      // If it is not the 0th image, 'prev_img' will be the previous image.
      if (i!=0)
      {
         prev_img = m_images[i-1];
      }
      // Else, 'prev_img' will be 'null'.
      else 
      {
         prev_img = "none";
      }
      // If it is not the last image, 'next_img' will be the next image.
      if (i!=m_images.length-1) 
      {
         next_img = m_images[i+1];
      }
      // Else, 'next_img' will be 'null'.
      else
      {
         next_img = "none";
      }
      // If the mouse is on this, do ->
      if (m_images[i]==xmenu_hovered)
      {
         // If it's width will be smaller or equal with 'max_w', than bigger it with 'n'.
         if (curr_width-0+n <= max_w) 
         {
            m_images[i].setAttribute('width', curr_width-0+n);
         }
      }
      // If the mouse is not on this, do ->
      else
      {
         // If the next, or the previous image is hovered, do ->
         if ( prev_img == xmenu_hovered || next_img == xmenu_hovered )
         {
            // If its width will be smaller or equal with 'max_w/2', than bigger it with 'n/2'.
            if (curr_width-0+(n/2) <= max_w/2) 
            {
               m_images[i].setAttribute('width', curr_width-0+n/2);
            }
            // If its width is bigger then 'max_w/2', smaller it with '(n/4)*3'.
            if (curr_width-0 > max_w/2)
            {
               m_images[i].setAttribute('width', curr_width-0-((n/4)*3));
            }
         }
         else 
         {
            // If it will be bigger then the minimum width, smaller it with '(n/4)*3'.
            if (curr_width-0-((n/4)*3) > min_w)
            {
               m_images[i].setAttribute('width', curr_width-0-((n/4)*3));
            }
            else
            {
               // Else if it is bigger than 'min_w', its width will be 'min_w'.
               if (curr_width-0 > min_w)
               {
                  m_images[i].setAttribute('width', min_w);
               }
            } 
         }
      }
      // Set its Height, to be equal, with its width.
      m_images[i].setAttribute('height', m_images[i].getAttribute('width'));
   }
}