               /* ................................................. */
               /* file: gmusic.js                                   */
               /* author: Ted O'Hara                                */
               /* purpose: Worship Music 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
  }
  
// ==== Worship Music specific. Namespaced ----------------------------------------------------
  
var musicTheme = new Object() //namespace for music theme specific code

//Initialize form fields
musicTheme.initFormFields = function()
  {

    var formfields = [ {fm:document.emailform1, fld:'email1',ds:'Enter Your Email Address'},
                       {fm:document.emailform2, fld:'email2',ds:'Enter Your 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.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

//hides all but the current offer div  
musicTheme.collapseOffers = function(offerdiv)
  {
    var currDiv 
    for (var i = 0; i < musicTheme.OfferArray.length; i++)
      {
        currDiv = musicTheme.OfferArray[i]
        if(currDiv != offerdiv) 
          {
            currDiv.style.display = 'none'
            currDiv.offerlink.className = ""; 
          }
      }
  }
musicTheme.rotationPeriod = 3500; //time in ms between offer rotations
musicTheme.timeout = null;

//method to advance automatically to the next offer
musicTheme.rotateOffer = function()
  {
    var nextOffer = musicTheme.currOffer + 1
    if(nextOffer >= musicTheme.allOfferLx.length || !musicTheme.allOfferLx[nextOffer].idx){nextOffer = 0}
    musicTheme.allOfferLx[nextOffer].onclick()
  }  
  
//method called directly on click, or indirectly via a timeout to rotate offer to show an offer div  
musicTheme.showOffer = function()
  {
    clearTimeout(musicTheme.timeout);
    musicTheme.collapseOffers(this.offer)
    this.offer.style.display = 'block'
    this.className = 'activeoffer'
    musicTheme.currOffer = this.idx;
    musicTheme.timeout = setTimeout(musicTheme.rotateOffer,musicTheme.rotationPeriod);
    musicTheme.stopLink.className = '';
    return false;
  }
 
//Add a rollover event handler to images with a class of "ro" 
musicTheme.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  
musicTheme.initSearchSubmit = function()
  {
    var submitBtn = document.getElementById('search_enter')
    if(submitBtn != null) {submitBtn.onmouseover = btnOn; submitBtn.onmouseout = btnOff;}
  }
  
musicTheme.stopRotator = function()
  {
    clearTimeout(musicTheme.timeout);
    this.className = 'activeoffer';
    return false;
  }
  
//initializes rotating offers
musicTheme.initOffers = function()
  {
    this.OfferContainer = document.getElementById('wm_offercontainer')
    if(this.OfferContainer == null) {return false}
    
    this.OfferArray = new Array //array of the Offer divs
    var linkContainer = document.getElementById('wm_triggerlinks'); //reference to the container of the trigger links
    
    var OfferDivs = this.OfferContainer.getElementsByTagName('DIV');
    if(OfferDivs.length == 0) return false;
    if (linkContainer == null) return false;
    this.allOfferLx = linkContainer.getElementsByTagName('A');  //node list of the trigger links
    
    var thisLink, thisDiv, visibleNum    
    
    for (var i = 0, divlen = OfferDivs.length; i < divlen; i++)
      {
        thisDiv = OfferDivs[i]
        if(!thisDiv.className.match(/wm_offer/)){continue} //not a Offer, move on...
        this.OfferArray.push(thisDiv)
        visibleNum = this.OfferArray.length;

        thisLink = createElementAndText(visibleNum,linkContainer,'A')
        thisLink.href = '#' + thisDiv.id;
        thisLink.setAttribute('title','Go to offer ' + visibleNum)
        thisLink.idx = visibleNum - 1;
        
        thisLink.offer = thisDiv //connect Offer to link
        thisLink.offer.offerlink = thisLink; //connect the link back to the Offer
        thisLink.onclick = this.showOffer
      }
    this.stopLink = createElementAndText('\u00D7',linkContainer,'A')
    this.stopLink.setAttribute('title','Stop rotation');
    this.stopLink.href = '/index.cfm'
    this.stopLink.onclick = this.stopRotator;
    
    this.allOfferLx[0].onclick() //trip the first link
  }


//add popup event handlers to all links in the content area.  
musicTheme.checkLinks = function()
  {
    var contentLx = document.getElementById('maincontent').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
musicTheme.initialize = function()
  {
    if (!(document.getElementById && document.getElementsByTagName && document.createElement)) {return false}
    
    this.checkLinks();
    this.initFormFields();      
    this.initImages()
    this.initSearchSubmit()
    this.initOffers();    
  }

// shim for links embedded in content  
function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

  
window.postAJAXfn = function() {musicTheme.initialize()};
musicTheme.initialize()
  
