               /* ................................................. */
               /* file: weldmart.js                                 */
               /* author: Ted O'Hara                                */
               /* purpose: WeldingMart theme specific scripts       */                       
               /* Created: 08/19/2009                               */
               /* Copyright (C) 2008 - 2009 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
  }
  
// ==== WeldingMart theme specific. Namespaced ----------------------------------------------------
  
var weldTheme = new Object() //namespace for music theme specific code

//Initialize form fields
weldTheme.initFormFields = function()
  {

    var formfields = [ {fm:document.emailform, fld:'mr_email',ds:'Enter Email'},
                       {fm:document.sitesearch, fld:'search_string',ds:'Search Site'}]
    
    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.label = document.getElementById('lbl_' + formfield.id)
                if(formfield.label)
                  {
                    formfield.defaultString = formfield.label.firstChild.nodeValue;
                    formfield.label.style.display = 'none'
                  }
                else {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

//print page  
weldTheme.printPage = function()
  {
    if (window.print) {self.print()}
    else {alert('Choose Print from the browser\'s File menu')}
  }
  
weldTheme.setBookmark = function()
  {
    var title = document.title    
    var url = window.location  
    // Mozilla Firefox Bookmark          
    if (window.sidebar) {window.sidebar.addPanel(title, url,"");} 
    // IE Favorite
    else if( window.external ) {window.external.AddFavorite( url, title);}
    else if(window.opera && window.print)// Opera Hotlist
     { 
      var elem = document.createElement('a');
      elem.setAttribute('href',url);
      elem.setAttribute('title',title);
      elem.setAttribute('rel','sidebar');
      elem.click();             
     }
   return false;
  }
  
weldTheme.initJSLinks = function()
  {
    var targetElem = document.getElementById('JS_links')
    if(targetElem ==  null) return false //bail if container isn't present.
    if(document.getElementById('printlink') != null) return false //bail if links have already been created (ie if initialization file is re-called after page load
    //create the print page link
    var printLk = createElementAndText("Print This Page",targetElem,'A');
    printLk.setAttribute('id','printlink');
    printLk.onclick = this.printPage
    //create the bookmark link if browser is capable
    if (window.sidebar || window.external || (window.opera && window.print))
      {
        var bookmarkLk = createElementAndText("Bookmark",targetElem,'A');
        bookmarkLk.setAttribute('id','bookmark');
        bookmarkLk.onclick = this.setBookmark
      }
  }

//hides all but the current Banner div  
weldTheme.collapseBanners = function(Bannerdiv)
  {
    var currDiv 
    for (var i = 0; i < weldTheme.BannerArray.length; i++)
      {
        currDiv = weldTheme.BannerArray[i]
        if(currDiv != Bannerdiv) 
          {
            currDiv.style.display = 'none'
            currDiv.Bannerlink.className = ""; 
          }
      }
  }
  
weldTheme.rotationPeriod = 8000; //time in ms between Banner rotations
weldTheme.timeout = null;

//method to advance automatically to the next Banner
weldTheme.rotateBanner = function()
  {
    var nextBanner = weldTheme.currBanner + 1
    if(nextBanner >= weldTheme.allBannerLx.length){nextBanner = 0}
    weldTheme.allBannerLx[nextBanner].onclick()
  }  
  
//method called directly on click, or indirectly via a timeout to rotate Banner to show an Banner div  
weldTheme.showBanner = function()
  {
    clearTimeout(weldTheme.timeout);
    weldTheme.collapseBanners(this.Banner)
    this.Banner.style.display = 'block'
    this.className = 'activeBanner'
    weldTheme.currBanner = this.idx;
    weldTheme.timeout = setTimeout(weldTheme.rotateBanner,weldTheme.rotationPeriod);
    return false;
  }
  
//shop by form event handlers
weldTheme.handleShopBy = function()
  {
    var targetOption = this.options[this.selectedIndex]
    var targetId = targetOption.value
    var targetText = encodeURIComponent(targetOption.text.replace(/-/g,'')) + '/';
    if(targetId == '') return false
    if(this.id == 'shopbygroup') {var destination = '/category/' + targetText + targetId}
    else {var destination = '/manufacturer/' + targetText + targetId}
    window.location = destination;
  }
  
weldTheme.initShopBy = function()
  {
    if(document.getElementById('shopbygroup')){document.getElementById('shopbygroup').onchange = this.handleShopBy}
    if(document.getElementById('shopbybrand')){document.getElementById('shopbybrand').onchange = this.handleShopBy}
  }
 
//Add a rollover event handler to images with a class of "ro" 
weldTheme.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
  
//adds rollover handlers to the search form 'Enter' button  
weldTheme.initSubmits = function()
  {
    var idArray = ['search_enter','email_join'];
    for (var i = 0; i < idArray.length; i++)
      {
        var thisBtn = document.getElementById(idArray[i])
        if(thisBtn != null) {thisBtn.onmouseover = btnOn; thisBtn.onmouseout = btnOff;}
      }
    
  }
  
//initializes rotating Banners
weldTheme.initBanners = function()
  {
    this.BannerArray = new Array //array of the Banner divs
    var linkContainer = document.getElementById('wm_triggerlinks'); //reference to the container of the trigger links
    this.bannerContainer = document.getElementById('wm_banners')
    if(this.bannerContainer == null) {return false}
    var bannerDivs = this.bannerContainer.getElementsByTagName('DIV');
    if(bannerDivs.length == 0) return false;
    if (linkContainer == null) return false;
    this.allBannerLx = linkContainer.getElementsByTagName('A');  //node list of the trigger links
    
    var thisLink, thisDiv, visibleNum
    
   
    
    
    for (var i = 0, divlen = bannerDivs.length; i < divlen; i++)
      {
        thisDiv = bannerDivs[i]
        if(!thisDiv.className.match(/wm_banner/)){continue} //not a banner, move on...
        this.BannerArray.push(thisDiv)
        visibleNum = this.BannerArray.length;
        thisLink = createElementAndText(visibleNum,linkContainer,'A')
        thisLink.href = '#' + thisDiv.id;
        thisLink.idx = visibleNum - 1;
        
        thisLink.Banner = thisDiv //connect Banner to link
        thisLink.Banner.Bannerlink = thisLink; //connect the link back to the Banner
        thisLink.onclick = this.showBanner
      }
    this.allBannerLx[0].onclick() //trip the first link
  }
  



//add popup event handlers to all links in the content area.  
weldTheme.checkLinks = function()
  {
    var contentLx = document.getElementById('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 run all initialization
weldTheme.initialize = function()
  {
    if (!(document.getElementById && document.getElementsByTagName && document.createElement)) {return false}
    
    this.checkLinks();
    this.initFormFields();      
    this.initImages();
    this.initSubmits();
    this.initBanners(); 
    this.initShopBy();
    this.initJSLinks();
  }
  
window.postAJAXfn = function() {weldTheme.initialize()};
weldTheme.initialize()


  
