
window.addEvent('domready', function() {
	checkExpandedWidth();
	initializeSearchForm();
	if(!$(document.body).hasClass('fullWidth')) {initializeSideBarFixer('SideBar');}
});

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function initializeSearchForm() {
	$('SearchForm').getElements('input').each(function(item){
			item.focused = false; 
			if(item.hasClass('text')){item.setProperty('value', 'Search');}//insure Search text is there on load
			item.addEvents({	
				focus: function(){
							this.focused = true;
							this.getParent('form').getElement('.text').setProperty('value', '');
							//help ie6 & 7
							this.addClass('inFocus');
						},
				blur: function(){
					this.focused = false;
					this.delayCheck = (function(){
						this.delayCheck = $clear(this.delayCheck);
						this.removeClass('inFocus');
						if(this.getParent('form').getElements('input').every(function(item){return !item.focused})) {
							this.getParent('form').getElement('.text').setProperty('value', 'Search');
						}
					}).delay(100, this);
				}
	 		});
	});
	
	/*help ie6*/
	if(Browser.Engine.trident && Browser.Engine.version <= 4) {
		$('SearchForm').addEvents({
			mouseenter: function() {
						//help ie6
						this.addClass('hover');
					},
					
			mouseleave: function() {
						//help ie6
						this.removeClass('hover');
					}
		});
	}
}

function initializeSideBarFixer(wrap) {
	var sidebar = $(wrap);
	if(!sidebar) return;
	if(sidebar.getStyle('display') == 'none') return;
	if(Browser.Engine.trident && Browser.Engine.version <= 4) return;

	var multislideCheck = $(document.body).getElement('.ms_expander');
		
	this.ABAsidebar = sidebar;
	var c = sidebar.container = sidebar.getParent();
	
	if(c.getSize().y < sidebar.getSize().y+(sidebar.getLast().getSize().y*2) && !multislideCheck) return; //too small to worry about
	sidebar.topLimit = c.getCoordinates().top+c.getStyle('padding-top').toInt();
	sidebar.botLimit = c.getCoordinates().bottom-c.getStyle('padding-bottom').toInt();
	sidebar.botOffset = c.getStyle('padding-bottom').toInt()+sidebar.getStyle('margin-bottom').toInt();
	sidebar.leftOffset = sidebar.getCoordinates(c).left;
	sidebar.rightOffset = c.getStyle('padding-right').toInt();
	sidebar.fixedBottom = sidebar.getCoordinates().bottom - sidebar.topLimit;
	sidebar.check = true;
	sidebar.checkHeight = function() {
		var parent = this.getParent();
	
		return (parent.getSize().y - sidebar.botOffset - parent.getStyle('padding-top').toInt()) > 
				(this.getSize().y +this.getStyle('margin-top').toInt() +this.getStyle('margin-bottom').toInt());
	}

	this.addEvents({
		scroll: function(){
			var sidebar = this.ABAsidebar;
			var sy = this.getScroll().y;
			var dif = sidebar.getSize().y - this.getSize().y;
			var dynamicBottom = sidebar.container.getCoordinates().bottom-sidebar.botOffset;
			var smooth = (sy > sidebar.topLimit && sy < sidebar.topLimit+dif) ? dif - (sidebar.topLimit + dif - sy) : dif;
			var bottomedOut = (sy+sidebar.getSize().y >= dynamicBottom + Math.max(0,dif) -1);
					
			if(sy >= sidebar.topLimit && sidebar.checkHeight()) {
				if(bottomedOut) {
					sidebar.setStyles({
						position: 'absolute',
						left: '',
						top: '',
						bottom: sidebar.botOffset,
						right: sidebar.rightOffset
					});
				}
				else {
					sidebar.setStyles({
						position: 'fixed',
						left: Math.ceil(sidebar.container.getCoordinates().left)+sidebar.leftOffset-this.getScroll().x,
						top: Math.min(0, -1*smooth),
						right: '',
						bottom: ''
					});
				}
			}
			else {
				sidebar.setStyles({
					position: 'relative',
					left: '',
					top: '',
					right: '',
					bottom: ''
				});
			}
		},
		
		resize: function() {
			if(Browser.Engine.gecko && window.outerWidth % 2 == 0) {//fix rounding error in Firefox
				window.outerWidth-=1;
			}
			if(this.ABAsidebar.getStyle('position') == 'fixed') {
				this.ABAsidebar.setStyles({
					left: sidebar.container.getCoordinates().left+sidebar.leftOffset-this.getScroll().x
				});
			}
			this.fireEvent('scroll');
		}
	});
	
	this.fireEvent('resize');
}

function checkExpandedWidth() {
	var con = $('Content');
	var wrap = $('PageWrapper');
	var check = $A(con.getElements('div')).combine(con.getElements('table')).combine(con.getElements('img'));
	
	var frame = con.getStyle('border-right-width').toInt() + con.getStyle('border-left-width').toInt() + con.getStyle('padding-right').toInt() + con.getStyle('padding-left').toInt();
	var maxWidth = con.getSize().x - frame;
	var dif = 0;

	check.each(function(item, index) {
			item = $(item);
			var w = item.getCoordinates().width+item.getStyle('margin-left').toInt()+item.getStyle('margin-right').toInt();

			if(w > maxWidth) {	
				dif = w-maxWidth+dif;
				maxWidth = w;
			}
		}
	);

	if(dif) {
		wrap.setStyle('width', wrap.getStyle('width').toInt()+dif);
		$(document.body).addClass('flexWidth');
	}
}

/*  
	MultiSlide, version 1.0
	(c) 2009 Intereactive, LLC - Ryan Hargrave
	http://blueprint.intereactive.net/multislide-for-mootools
	
	Compatible with Mootools v1.2 - http://mootools.net
	Extends the capability of the Fx.Slide Class  - http://mootools.net/docs/Plugins/Fx.Slide
	
	This code is freely distributable under the terms of an MIT-style license.
*/

var multislide = new Class({
	
	Implements: [Events, Options],

	options: {
	
		slide_duration:			300,
		slide_transition: 		'sine:in:out',
		class_expand_all: 		'expand_all',
		class_collapse_all: 	'collapse_all',
		class_toggler: 			'ms_toggler',
		class_expander: 		'ms_expander',
		class_ecbutton:			'ecButton',
		build_ecbuttons:		true,
		jumpToHash:				true,
		
		//these take functions
		addTransition:   		$empty,
		onExpand: 				$empty,
		onCollapse: 			$empty, 
		onExpand_all: 			$empty, 
		onCollapse_all: 		$empty
	},
	
	//sets up the animations for the team layout
	initialize: function(container_class, options) {
		this.setOptions(options);
		
		//set empty array to hold all the sliders
		this.person_slide = [];
		this.expanders = []; // PCC - Added by DT
		this.togglers = []; // PCC - Added by DT

		//create the sliders and set up event listeners
		$$('.'+container_class).each(function(container, i){
			//Added expand/collaspe all links - SAS
			if(i == 0 && this.options.build_ecbuttons) { //add only before first toggle element
				var buttonWrap = new Element('p');
				var eAll = new Element('a', {
					'href': '#', 
					'class': this.options.class_expand_all+' '+this.options.class_ecbutton,
					'html': 'Expand All'
				});
				var cAll = new Element('a', {
					'href': '#', 
					'class': this.options.class_collapse_all+' '+this.options.class_ecbutton,
					'html': 'Collapse All'
				});
				
				buttonWrap.inject(container, 'before');
				buttonWrap.grab(eAll);
				buttonWrap.grab(cAll);
			}
			
			this.addEvent('transitioning', function(start) { // PCC - added by SAS for SideBarFixer
					if(!this.options.addTransition) return;
					if(start) {
						this.transitionEffects = $clear(this.transitionEffects);
						this.transitionEffects = (function(){(this.options.addTransition).attempt()}).periodical(10, this);
					}
					else {
						this.transitionEffects = $clear(this.transitionEffects);
					}
				}.bind(this)
			);

			this.togglers[i] = container.getElement('.'+this.options.class_toggler); // PCC - modded by DT
			
			//set up the slide instance for each element and hide all the sliders
			this.expanders[i] = container.getElement('.'+this.options.class_expander) // PCC - modded by DT							  
			this.person_slide[i] = new Fx.Slide(this.expanders[i], { // PCC - modded by DT
					duration: this.options.slide_duration, 
					transition: this.options.slide_transition, 
					link : 'ignore',
					onStart : function(){
						if(this.person_slide[i].open){
							this.fireEvent('collapse', this.togglers[i]); // PCC - modded by DT
						} else {
							this.fireEvent('expand', this.togglers[i]); // PCC - modded by DT
						}
						this.fireEvent('transitioning', 1); // PCC - added by SAS
					}.bind(this),
					onComplete: function(){
						this.fireEvent('transitioning'); // PCC - added by SAS
					}.bind(this)
			}).hide();
			
			//add the toggle slide event		
			this.togglers[i].addEvent('click', function(e){ // PCC - modded by DT
					e.stop();
					this.person_slide[i].toggle();
			}.bind(this));
		}.bind(this));
		
		$$('.'+this.options.class_expand_all).addEvent('click', function(e){
			e.stop();
			this.expand_all();
		}.bind(this));
		
		
		$$('.'+this.options.class_collapse_all).addEvent('click', function(e){
			e.stop();
			this.collapse_all();
		}.bind(this));
		
		if(this.options.jumpToHash) {
			//make sure empty anchors have height to scroll to properly - add by SAS
			$$('a[id]').each(function(item){
				if(item.getSize().y == 0 && item.getProperty('html') == '') {
					item.setStyles({
						'height' : '1em',
						'width' : 0,
						'display' : 'inline-block'
					});
				}
			});
			
			//read url hash, strip off the leading #, get element if exists, and go straight to it
			this.expand_ifcontains($(window.location.hash.substr(1)), 1);
			
			//add click events for local page links to hashes in multislide
			$(document.body).getElements('a[href*=#]').each(function(item, i){
				var id = /#[^\?]*/.exec(item.getProperty('href'))[0].substr(1);
				if(id!='') {
					item.addEvent('click', function(id){
							this.expand_ifcontains($(id));
						}.pass(id, this)
					);
				}
			}, this);
		}

	},
	
	expand_all: function() {
		//run the effect on the entire array
		this.person_slide.each(function(sl){
			if(!sl.open){
				sl.slideIn();
			}
		});
		this.fireEvent('expand_all');
	},
	
	collapse_all: function() {
		//run the effect on the entire array
		this.person_slide.each(function(sl){
			if(sl.open){
				sl.slideOut();
			}
		});
		this.fireEvent('collapse_all');
	},

	// PCC - Added by DT; modified by SAS to accomodate immediate display if in url hash
	expand_ifcontains: function(el, set) {
		this.person_slide.some(function(sl, i){ //returns true if element is in a slider
			if (this.expanders[i].hasChild(el) | this.togglers[i].hasChild(el)) {
				if(!sl.open){
					if(set) {//go straight to it
						sl.show();
						var myFx = new Fx.Scroll(window).set(el.getCoordinates().left, el.getCoordinates().top);
					}
					else {//go to by scrolling
						sl.addEvent('complete', function(){
							var myFx = new Fx.Scroll(window).toElement(el);
						});
						sl.slideIn();
					}
				}
			}
		},this);
	}
});

// Multi-slide - used to hide information on long pages. User clicks on heading to reveal the information.
function Enable_Multislide() { 	window.addEvent('domready',function() {
		multislide = new multislide('ms_container', 
				{	onExpand: function(el){
						el.addClass('arrow');
					},	
					onCollapse: function(el){
						el.removeClass('arrow');
					},
					addTransition: function(){
						window.fireEvent('scroll') //this syncronizes the 'fixed' sidebar
					}
				});
		
	});
}

// Google Analytics

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-16661812-1']);
_gaq.push(['_trackPageview']);

(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();