/************************ Utilities *****************************************/

/*
	Sets up all anchor tags with "rel=facebox" as facebox triggers.
*/
function activateFacebox() {
	$j(document).ready(function($) {
		$j('a[rel*=facebox]').facebox({
		  opacity : 0.7,
		  loadingImage : '/images/facebox/loading.gif',
		  closeImage : '/images/facebox/closelabel.gif'
		}); 
	})
}

/*
	Loads data from a remote URL into facebox.
*/
function faceboxRemote(url) {
	$j.facebox(function($) { 
	  $j.get(url, function(data) {
	    $j.facebox(data)
	  })
	})	
}

/*
  Close Facebox pop-up.
*/
function closeFacebox() {
  $j.facebox.close();
}

/*
  Tweak the page size to handle small browser windows.
*/
PageSizer = Class.create();
PageSizer.prototype = {
  paddingCorrectionIE: 45,
  paddingCorrectionOther: 25,
  
  initialize: function() {
    window.PageSizer = this;
  },
  
  setDefaults: function() {
    this.page = $j('#page');
    if(navigator.appName == 'Microsoft Internet Explorer') {
      this.nativeWindowWidth = this.stripUnitFromAttribute(this.page.css('width')) - this.paddingCorrectionIE;
    } else {
      this.nativeWindowWidth = this.stripUnitFromAttribute(this.page.css('width')) + 
                               this.stripUnitFromAttribute(this.page.css('padding-left')) - this.paddingCorrectionOther;
    }
    this.nativePageWidth = this.nativeWindowWidth;
    this.nativePagePadding = this.stripUnitFromAttribute(this.page.css('padding-left'));
    this.minPageWidth = this.nativePageWidth - this.nativePagePadding;
    
    this.doResize();
    window.onresize = window.PageSizer.doResizeReferrer;
  },

  doResize: function() {
    if(this.page == undefined) {
      return false;
    }
    
    currentWindowWidth = this.windowWidth();

    if(currentWindowWidth < this.nativeWindowWidth) {
      this.offset = this.nativeWindowWidth - currentWindowWidth;
      if(this.offset > this.nativePagePadding) {
        this.offset = this.nativePagePadding;
      }
      this.page.css({ 
        padding: '0 0 0 ' + (this.nativePagePadding - this.offset) + 'px', 
        background: "url(/images/common/page_bg.jpg) no-repeat -" + this.offset + 'px',
        margin: '0'
      });
    } else {
      this.page.css({ 
        padding: '0 0 0 ' + this.nativePagePadding + 'px',
        background: "url(/images/common/page_bg.jpg) no-repeat left",
        margin: '0 auto 0 auto'
      })
    }
    
    return true;
  },
  
  doResizeReferrer: function() {
    window.PageSizer.doResize();
  },
  
  windowWidth: function() {
    return window.innerWidth != null?
      window.innerWidth: document.body.clientWidth != null?
      document.body.clientWidth:null;
  },
  
  stripUnitFromAttribute: function(value) {
    if(value != null) {
      return parseFloat(value.substring(0, value.length - 2));
    }
  }  
}

/************************ Document Prep *************************************/

$j = jQuery.noConflict();

new PageSizer();

$j(document).ready(function($) {
  activateFacebox();

  window.PageSizer.setDefaults();

  if(!DetectFlashVer(8, 0, 0)) {
    if(confirm('This website uses the YouTube embedded video player which requires installation of or upgrade to the Adobe Flash browser plug-in.  Click OK below to install now, or CANCEL to continue without video.')) {
      document.location.href='http://get.adobe.com/flashplayer/';
    }
  }
});

/****************************************************************************/
