               /* ................................................. */
               /* file: royal.js                                     */
               /* author: Ted O'Hara                                */
               /* purpose: Roayl Discount theme specific scripts    */                       
               /* Created: 10/21/2008                               */
               /* Copyright (C) 2008 bx.com, Inc.                   */
               /* ................................................. */ 
               
function btnOn() 
  {
    var srcString = this.src
    srcString = srcString.replace(/.gif/,'_over.gif')
    this.src = srcString
  }
function btnOff() 
  {
    var srcString = this.src
    srcString = srcString.replace(/_over.gif/,'.gif')
    this.src = srcString
  }
  
// ==== Royal Discount specific. Namespaced ----------------------------------------------------
  
var royaltheme = new Object() //namespace for Royal Discount specific code

//Initialize form fields
royaltheme.initFormFields = function()
  {

    var formfields = [ {fm:document.search, fld:'search_string',ds:'Search'},
                       {fm:document.royal_subscription, fld:'subscription_email',ds:'Enter Email Address'}]
    
    var selectedForm,formfield;
    
    for (var bt = 0; bt < formfields.length; bt++)
      {
        selectedForm = formfields[bt].fm;
        if (selectedForm)
          {
            formfield = selectedForm.elements[formfields[bt].fld]
            if (formfield)
              {
                formfield.defaultString = formfields[bt].ds;
                formfield.onfocus = function(){if(this.value == this.defaultString) {this.value = '' }}
                formfield.onblur = function(){if(this.value == '') {this.value = this.defaultString }};
                formfield.onblur()  
              } //end, formfield exists
          } //end, if selected form
      } //end, for
  
  
  }//end, initFormFields
 
//Add a rollover event handler to images with a class of "ro" 
royaltheme.initImages = function()
  {
    var pageImgs = document.getElementsByTagName('IMG');
    for (i = 0; i < pageImgs.length; i++)
      {
        if (pageImgs[i].className == 'ro'){pageImgs[i].onmouseover = btnOn; pageImgs[i].onmouseout = btnOff; }
      }  
  }//END: init images


//function to switch navigation between manufacturers and categories  
royaltheme.switchNav = function()
  {
    this.className = 'thisgroup';
    this.otherbutton.className = '';
    this.other.style.display = 'none';
    this.nav.style.display = 'block';
    return false
  } 

//function to initialize the nav switcher  
royaltheme.initSelector = function()
  {
    var itemArray = ['manufact','categori']
    var thisBtn, otherid
    var startingLink
    for (var i = 0; i < itemArray.length; i++)
      {
        thisBtn = document.getElementById('bb_' + itemArray[i])
        thisBtn.nav = document.getElementById('royal_' + itemArray[i])
        otherid = (itemArray[i] == 'manufact') ? 'categori' : 'manufact'
        thisBtn.other = document.getElementById('royal_' + otherid)
        thisBtn.otherbutton = document.getElementById('bb_' + otherid)
        thisBtn.onclick = this.switchNav
        thisBtn.ondblclick = function(){window.location = this.href}
        if (thisBtn.className.match(/thisgroup/)) {startingLink = thisBtn}
      }
    
    if (!startingLink || !startingLink.onclick){startingLink = document.getElementById('bb_manufact')}
    startingLink.onclick()
  }



//add popup event handlers to all links in the content area.  
royaltheme.checkLinks = function()
  {
    var contentLx = document.getElementById('royal_pagebody').getElementsByTagName('A')
    for (var cl = 1; cl < contentLx.length; cl++)
    {
      if (contentLx[cl].className.match(/popup/)) {contentLx[cl].onclick = function(){newwindow(this.href); return false}}
    }
  }
  
//function to toggle subsidiary groups
royaltheme.toggleSubsidiary = function()
  {
    var myParent = this.parentNode
    if (myParent == null) {alert('Parent is null'); return false}
    if (!myParent.className.match(/open|closed/)) {return true} //allow default action
    if (myParent.className == 'open')
      {
        myParent.className = 'closed';
        this.style.color = ''
      }
    else
      {
        myParent.className = 'open'
        this.style.color = '#A90303'
      }
    
    
    return false;
  }

//function to initialize groups
royaltheme.initGroups = function()
  {
    var groupcontainer = document.getElementById('group_subsidiary'); 
    if (groupcontainer == null) {return false}
    var liArray = groupcontainer.getElementsByTagName('LI') //get all LIs within
    if (liArray.length == 0) {return false}
    var thisLI,firstLink //establishing
    for (var l = 0; l < liArray.length; l++)
      {
        thisLI = liArray[l];
        if (thisLI.className.match(/open|closed/)) //item has a subsidiary list
          {
            firstLink = thisLI.getElementsByTagName('A').item(0)
            if (firstLink != null) 
            {
              firstLink.onclick = this.toggleSubsidiary
              firstLink.ondblclick = function(){window.location = this.href}
            }
          }
      }
  }




//Function to run all initialization
royaltheme.initialize = function()
  {
    if (!(document.getElementById && document.getElementsByTagName && document.createElement)) {return false}
    
    this.checkLinks();
    this.initFormFields();      
    this.initImages()
    this.initSelector()
    this.initGroups()
    
  }
  
window.postAJAXfn = royaltheme.initialize;
royaltheme.initialize()
  
