/**
 * Main menu and thumbnail mouse event processing 
 * Also preloads images and sets up current active menu selections.
 *
 * @package    craftstore
 * @author     John Malis
 * @version    SVN: $Id: menu.js 12474 2009-10-31 10:41:27Z john $
 */

// ***** Globals used throughout the entire script

var image_path = '';
var preloadMenuFlag = false;

// ***** Globals for main menu event processing (All Templates)

var preloaded_images = [];     // Preloaded main menu image elements (Just Storage)
var curmenuitem = '';          // Current menu item selected

// ***** Globals for thumbnail image mouse processing  (Show Templates)

var preloaded_show_images_src = [];       // Source file names for large images for corresponding thumbnails
var preloaded_show_images_element = [];   // Elements for the main large images (not used, just stored, avoid compiler optimization?) 
var show_photo = null;                    // Element for main image show area


 /**
   * Event processing for thumbnail mouse over event
   * Puts a new image in main image display area when a 
   * thumbnail is moused over.
   *
   */

function showImage()
  {
  var id = this.get('id');
  var thumbnail;
  
  if ($chk(id))
    {
    thumbnail = id.substring(5);      // Derive which thumbnmail from the id name
    thumbnail = thumbnail.toInt();
    if ($chk(show_photo))
      show_photo.set('src', preloaded_show_images_src[thumbnail]);   //S ets the new filename for the main image area
    }
  }




 /**
   * Event processing for main menu mouse out event
   * Sets a new image when menu item moused out
   *
  **/

function rollMenuOff()
  {
  var id = this.get('id');
  
  if ($chk(id) && preloadMenuFlag)
    {
    if (id != curmenuitem)
      {
      var pos;
      var image = this.get('src');
      if ((pos = image.indexOf('-rollover')) >= 0)
        this.set('src', image.substring(0, pos) + image.substring(pos+9));
      }
    }
  }

/**
   *
   * Event processing for main menu mouseover event
   * Sets a new image when menu item is moused over
   *
  **/

function rollMenuOn()
  {
  var id = this.get('id');
  
  if ($chk(id) && preloadMenuFlag)
    {
    if (id != curmenuitem)
      {
      var image = this.get('src');
      if (image.indexOf('-rollover') == -1)
        {
        var pos = image.indexOf('.');
        if (pos >= 0)
          this.set('src', image.substring(0, pos) + '-rollover' + image.substring(pos));
        }
      }
    }
  }

/**
  *
  * Sets up active selections for the main menu and
  * side bar. Also preloads the main menu and side bar images.
  * and coresponding main show images for the thumbnails.
  *
  * Called by onload in HTML body Element
  *
 **/

function setActiveMenuSelections()
  {
  
  // **** Preload main menu rollover images
   
    var i = 0;
    var menuimages = $('menu').getElements('img');
    var image;
    do
      {
      var id = menuimages[i].get('id');
      if ($chk(id))
        {
        preloaded_images[i] = new Element('img');
        var imagesrc = menuimages[i].get('src');
        var pos = imagesrc.indexOf('.');
        if (pos >= 0)
          preloaded_images[i].set('src', imagesrc.substring(0, pos) + '-rollover' + imagesrc.substring(pos));
        i++;
        }
      } while (++i < menuimages.length);
    preloadMenuFlag = true;
  
  // **** Setup the current main menu rollover(s)

  i = 0;
  do 
    {
    var itemid = setActiveMenuSelections.arguments[i];
    if (itemid)
        {
        var item = $(itemid);
        if ($chk(item))
          {
          curmenuitem = itemid;
          var imagesrc = item.get('src');
          var pos = imagesrc.indexOf('.');
          if (pos >= 0)
            item.set('src', imagesrc.substring(0, pos) + '-rollover' + imagesrc.substring(pos));
          }
        }
      i++;
      } while (itemid);
   }
  
/**
  *
  * Handles Mootools Domready event.
  * Sets up mouse event processing for main menu and thumbnails.
  *
  * Called by Mootools Domready
  *
 **/


function setupMouseEvents()
  {
  
  // **** If we have thumbnails, preload the corrsponding large images
  
   var i = 1;
   var thumb_element;
   while (thumb_element = $('thumb' + i))
     {
     var thumb = thumb_element.get('src');
     var thumbpos = thumb.indexOf('-thmb');    // Strip out the -thmb suffix to get the corresponding large image name
     preloaded_show_images_src[i] = thumb.substring(0, thumbpos) + thumb.substring(thumbpos+5);
     preloaded_show_images_element[i] = new Element('img');
     preloaded_show_images_element[i].set('src', preloaded_show_images_src[i]);
     i++;
     }
   show_photo = $('show-photo');
    
  // **** If we have an index title, setup a cookie with the heading and reference
  
  var heading_element = $('list_heading');
  if (heading_element)
    {
    Cookie.write('listheading', heading_element.get('html'), {path: '/'});
    Cookie.write('listurl', document.URL, {path: '/'});
    }
    
  // ***** If we have a go back to index element, get the cookie and set the text and href
  
  var gobacktoindex_element = $('backtoindex');
  if (gobacktoindex_element)
    {
    var listCookie = Cookie.read('listheading');
    if (listCookie)
      {
      //gobacktoindex_element.set('html', listCookie);
      var hrefCookie = Cookie.read('listurl');
      if (hrefCookie)
        {
        hrefCookie = hrefCookie.substring(0, 100);   // **** No more than 100 characters
        var regex = new RegExp("^[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$"); 
        if (regex.test(hrefCookie)) 
          gobacktoindex_element.set('href', hrefCookie);
        }
      }
    }
    
  // **** If we have a cart cookie, display the number of items next to the cart icon
  
  var cartCookie = Cookie.read('cartitems');
  if (cartCookie)
    {
    cartCookie = cartCookie.substring(0,2);   // **** Shouldn't be more than 2 characters
    if (!isNaN(parseInt(cartCookie)))         // **** Should be an integer
      {
      if (cartCookie != '0')
        {
        var carttext_element = $('cart_show');
        if (carttext_element)
          {
          var itemstring = cartCookie + ' item';
          if (cartCookie != '1')
            itemstring = itemstring + 's';
          carttext_element.set('html', itemstring);
          }
        }
      }
    }
    
  // **** Setup mouse events for mouseover on thumbnail images
     
  var thumbnails = $$('#thumbnail_list img');
  if ($chk(thumbnails))
    thumbnails.addEvent('mouseover', showImage);
    
  // **** Setup mouse events for main menu
  
  var menuimages = $$('#menu-left img');
  menuimages = menuimages.combine($$('#menu-right img'));
  if ($chk(menuimages))
    {
    menuimages.addEvents({
  "mouseover": rollMenuOn,
    
  "mouseout": rollMenuOff
      });
    }
    
  }

/***
   * Setup to handle domready event from mootools.
   *
 ***/
 
window.addEvent('domready', setupMouseEvents);

