//BODY MASS CALCULATOR
	function calcEnglish(form, feet, inches, pounds) {
		if ((! inches) || isNaN(inches)) inches = 0;
		TotalInches = eval(feet*12) + eval(inches);
		//form.calcval.value = Math.round( (pounds * 703) / (TotalInches*TotalInches) );
		form.calcval.value = Math.round(pounds*703*10/(TotalInches*TotalInches))/10;
	}

	function calcMetric(form, meters, kilograms) {
	   form.calcval.value = Math.round(kilograms*10 /(meters*meters))/10;
	}



function init(){
  // add .js class to body, indicating javascript is enabled
  $('body').addClass('js');

  // Recreate $.post with 'error' callback
  $.post = function( url, data, callback, type ) {
		if ( jQuery.isFunction( data ) || (data != null && (jQuery.isFunction( data.success ) || jQuery.isFunction( data.error ))) ) {
			type = type || callback;
      callback = data;
			data = {};
		}
    settings = {
			type: "POST",
			url: url,
			data: data,
			dataType: type
		};
    if ( jQuery.isFunction( callback ) ) {
      settings.success = callback;
    } else {
      if ( jQuery.isFunction( callback.success ) ) {
        settings.success = callback.success;
      }
      if ( jQuery.isFunction( callback.error ) ) {
        settings.error = callback.error;
      }
    }

		return jQuery.ajax(settings);
	}
  // Recreate $.get with 'error' callback
  $.get = function( url, data, callback, type ) {
		if ( jQuery.isFunction( data ) || (data != null && (jQuery.isFunction( data.success ) || jQuery.isFunction( data.error ))) ) {
			type = type || callback;
      callback = data;
			data = null;
		}
    settings = {
			type: "GET",
			url: url,
			data: data,
			dataType: type
		};
    if ( jQuery.isFunction( callback ) ) {
      settings.success = callback;
    } else {
      if ( jQuery.isFunction( callback.success ) ) {
        settings.success = callback.success;
      }
      if ( jQuery.isFunction( callback.error ) ) {
        settings.error = callback.error;
      }
    }

		return jQuery.ajax(settings);
  };

  // Create missing postJSON function
  $.postJSON = function(url, data, callback) {

    // Force disable jQuery cache setting
    var cache = jQuery.ajaxSettings.cache; jQuery.ajaxSettings.cache = false;

    $.post(url, data, callback, "json");

    // Restore jQuery caching setting
    jQuery.ajaxSettings.cache = cache;
  };

  // IE/IE6 Specific jQuery
  if ($.browser.msie) {
    // Replace built-in fade functions in IE to disable cleartype bug
    jQuery.fn.fadeIn = function(speed, callback) {
      return this.animate({opacity: 'show'}, speed, function() {
        if (jQuery.browser.msie)
          this.style.removeAttribute('filter');
        if (jQuery.isFunction(callback))
          callback();
      });
    };

    jQuery.fn.fadeOut = function(speed, callback) {
      return this.animate({opacity: 'hide'}, speed, function() {
        if (jQuery.browser.msie)
          this.style.removeAttribute('filter');
        if (jQuery.isFunction(callback))
          callback();
      });
    };

    jQuery.fn.fadeTo = function(speed,to,callback) {
      return this.animate({opacity: to}, speed, function() {
        if (to == 1 && jQuery.browser.msie)
          this.style.removeAttribute('filter');
        if (jQuery.isFunction(callback))
          callback();
      });
    };

    if ($.browser.version < '7.0') {
    }
  }

  // Set up superfish menus
  $('#mainnav ul.sf-menu')
  .supersubs({
    minWidth:   12, // min width in em
    maxWidth:   19, // max width in em
    extraWidth: 1   // extra width to ensure lines don't turn over due to rounding/font family
  })
  .superfish({
    autoArrows: false,
    dropShadows: false
  });

  /* --- image rotation on homepage --- */
  if ($('#home-left').length > 0) {
    var tgt = $('#home-left'),
        img = $('img',tgt);
    // Retrieve list of images
    $.getJSON('/images/homepage/images.php',function(data){
      // Shuffle images
      data.sort(function() {return 0.5 - Math.random()});
      // Append images to list by cloning first image and changing src
      for (i in data) {
        if (data[i] != img.attr('src')) {
          tgt.append(img.clone().attr('src',data[i]));
        }
      }
      // Start cycling images
      tgt.cycle();
    });
  }
  /* --- end image rotation on homepage --- */

  /* --- Expose buttons on homepage --- */
  // Get position of element within page (helper for detecting
  // location of a click relative to the element)
  $.fn.elementlocation = function() {
    var curleft = 0;
    var curtop = 0;
    var obj = this;
    do {
      curleft += obj.attr('offsetLeft');
      curtop += obj.attr('offsetTop');
      obj = obj.offsetParent();
    } while ( obj.attr('tagName') != 'BODY' );
    return ( {x:curleft, y:curtop} );
  };

  // Set up 'expose' action
  $('.exposebtn').bind('expose',function(){
    var tgt = $(this).addClass('exposed');
    tgt.expose({
      color: '#504978',
      opacity: 0.4,
      onBeforeLoad: function() {
        $('ul',tgt).stop().slideDown('normal');
      },
      onBeforeClose: function(){
        $('ul',tgt).stop().slideUp('normal',function(){
          tgt.removeClass('exposed');
        });
      }
    });
  })
  // Assign click events to header
  .find('h3').click(function(e){
    e.preventDefault();
    var tgt = $(this).parent();
    // Trigger 'expose' if not already exposed
    if (!tgt.hasClass('exposed')) {
      tgt.trigger('expose');
    }
    // Otherwise close mask
    else {
      var location = $(this).elementlocation();
      var x = e.pageX - location.x;
      var y = e.pageY - location.y;
      if (x >= 225 && x <= 237 && y >= 10 && y <= 22) {
        $.mask.close();
      }
    }
  }).end()
  // Trigger 'expose' action on hover
  .hover(function(){
    $(this).trigger('expose');
  });
  /* --- Finish Expose buttons on homepage --- */

  /* --- homepage blog posts --- */
  if ($('#home-blog-posts li').length > 1) {
    var container = $('#blogposts'),
        ul = $('#home-blog-posts',container),
        links = $('<ul id="blog-links"/>').appendTo(container);
    // Hide all but first
    $('#home-blog-posts li').filter(':gt(0)').hide().end()
    .each(function(idx){
      var li = $(this);
      $('<li class="post-toggle"/>')
      .text(idx+1)
      .addClass(idx==0?'curr':'')
      .hover(function(){ $(this).toggleClass('hover'); },function(){ $(this).toggleClass('hover'); })
      .click(function(){
        $('.post-toggle.curr',links).removeClass('curr');
        $(this).addClass('curr');
        $('li:visible',ul).fadeOut(500,function(){
          li.fadeIn(500);
        });
      }).appendTo(links);
    });
  }




  /* --- Video Links --- */
  $('a[href$=mp4],a[href$=flv]').click(function(e){
    e.preventDefault();
    /* if player doesn't yet exist, create it */
    var container = $('#video-container');
    if (container.length == 0) {
      /* create player overlay */
      container = $('<div id="video-container" />')
                  /* specify video source */
                  .data('src',$(this).attr('href'))
                  /* create player within overlay */
                  .append($('<div class="player" />').flowplayer("/includes/flowplayer/flowplayer-3.2.1.swf"))
                  /* append to document */
                  .appendTo('body')
                  /* add close button */
                  .append('<a class="close">&nbsp;</a>');
      /* trigger overlay and play video */
      container.overlay({
        top: 'center',
        mask: {
          color: '#504978',
          opacity: 0.4
        },
        load: true,
        onLoad: function(){
          $f().load().play(container.data('src'));
        },
        onClose: function(){
          $f().unload();
        }
      });
    }
    /* if player already exists, update video source, trigger overlay */
    else {
      container
      .data('src',$(this).attr('href'))
      .data('overlay').load();
    }
  });
  /* --- End Video Links --- */
}
