////
//// support code for Majestic Yoga site
////

//
// required:
//   jquery.js (version 1.3.2+) (see jquery.com)
//   jquery.preloadImg.js
//

// note - in retrospect, probably should have used CSS sprites for navigation...

$(document).ready(function(){
  //
  // function gets the mouseover version of the specified image src
  var getMouseoverSrc = function(src) {
    return src.replace(/_off/, "_ro");
  }
  // function gets the on version of the specified image src
  var getOnSrc = function(src) {
    return src.replace(/_off/, "_on");
  }
  // function extracts image url from background-image: url() property
  var getBkgImgURL = function(obj) {
    var bkgprop = $(obj).css("background-image");
    var urlre = /^url\((.*)\)$/;
    var results = urlre.exec(bkgprop);
    return results[1];
  }

  // preload mouseover images for primary nav
  //
  $("#divprimarynav a")
    .each( function(i) {
      // get background image
      var img = getBkgImgURL(this);
      img = getMouseoverSrc(img);
      $.preloadImg.add(img);
    });
  // preload mouseover images for secondary nav
  // (in secondary nav, mouseover version is on version)
  //
  $("#divsecondarynav a")
    .each( function(i) {
      var img = getBkgImgURL(this);
      img = getOnSrc(img);
      $.preloadImg.add(img);
    });

  // start preload
  $.preloadImg.start();

});


////
//// cancellation box
//// this makes it disappear if it has no content
////

$(document).ready(function(){
  var hascontent = false;

  // get all text in div (no HTML)
  var combinedtext = $("#divcancellations").text();

  // remove all spaces and &nbsp;
  combinedtext = combinedtext.replace(/&nbsp;/g, '');  // (not sure if this is needed)
  combinedtext = combinedtext.replace(/\s/g, '');

  // decide if we have any content
  if (combinedtext.length > 0) {
    hascontent = true;
  }

  // if we have content, add a class; if not, hide the box
  if (hascontent) {
    $("#divcancellations").addClass('active');
  }
  else {
    $("#divcancellations").hide();
  }
});


