if(!window.console)
{
	var console = {
		log: function(msg) {}
	};
}

/**
 * Allows detection of browsers support for position:fixed
 */
function supportsFixedPosition()
{
	var isSupported = null;

	if(document.createElement)
	{
		var el = document.createElement("div");

		if(el && el.style)
		{
			el.style.position = "fixed";
			el.style.top = "10px";

			var root = document.body;

			if(root && root.appendChild && root.removeChild)
			{
				root.appendChild(el);
				isSupported = el.offsetTop === 10;
				root.removeChild(el);
			}
		}
	}
	return isSupported;
}

/** default search text */
(function($)
{
	$(document).ready(function()
	{
		$('#header #search input').lineDefaultFieldText({textSrc: 'legend'});
		$('#footer .puff-newsletter input').lineDefaultFieldText({textSrc: 'legend'});
		$('#sidebar #newsletter input').lineDefaultFieldText({textSrc: 'label'});
	});
})
(jQuery);



/** generic show/hide text */
(function($)
{
	$(document).ready(function()
	{
		// look for rel="show_hide"
    $('[rel="show_hide"]').each(
      function() {
        
        var trigger = $(this);
        var target = $(trigger.attr('href')); // the href is like #some_id, so can be used as selector
        
        // add class to trigger for styling
        trigger.addClass('show_hide_trigger');
        
        // see if we are given specific text for show/hide
        var show_text = trigger.data('showtext');
        var hide_text = trigger.data('hidetext');
        
        // always have a show text value (incase they only assign hide text)
        if (!show_text) {
          show_text = trigger.html();
        }
 
        // function to toggle the content
        var toggleContent = function(hide) {
         var hidden = target.is(':hidden');
         trigger.toggleClass('target_content_hidden', hidden);
         trigger.html(hidden ?  show_text : hide_text);
        }    
        
        // initially hide content (unless they want it shown), call toggle function to set up class and text
        var initiallyShow = trigger.data('display');
        
        if (!initiallyShow) {
          target.hide();
        }
        
        toggleContent();
        
        trigger.click(
          function(){  
            target.slideToggle(250, toggleContent)
            return false;
          }
        );
      }
    );
  
	});
})
(jQuery);

/** fix for h1 + .breadcrumb selector - ie6 */
(function($)
{
	if($.browser.msie && parseInt($.browser.version) < 7)
	{
		$(document).ready(function()
		{
			var _breadcrumb = $('h1 + .breadcrumb');
			
			if(_breadcrumb.length)
			{
				_breadcrumb.addClass('breadcrumb-h1-fix');
			}
			
			var _subtext = $('h1 + .subtext');
			
			if(_subtext.length)
			{
				_subtext.addClass('subtext-h1-fix');
			}
		});
	}
})
(jQuery);

/**
 * Creates equal height secondary puffs
 */
(function($)
{
	$(window).load(function()
	{
		var _puffs = $('#sidebar .secondary-puff');
		
		var arr = new Array();
		
		/** dirty <ie7 fix */
		var c;
		_puffs.each(function(i)
		{
			if(i%2==0)
			{
				c = $('<div />').addClass('float-fix').appendTo($(this).parent());
			}
			
			c.append($(this));
		});
		
		_puffs.each(function(i)
		{
			var _top = parseFloat($(this).offset().top);

			if(typeof arr[_top] == 'undefined')
			{
				arr[_top] = new Array();
			}
			arr[_top].push($(this));
		});
		
		
		for(var i in arr)
		{
			if(typeof arr[i] == 'object' && arr[i].length > 1) // 2 or more
			{
				var max_height = 0;

				for(var j in arr[i])
				{
					var r = arr[i];
					var _this = r[j];
					
					if(_this.height() > max_height)
					{
						max_height = _this.height();
					}
				}
				
				for(var j in arr[i])
				{
					var r = arr[i];
					
					for(var k in r)
					{
						// min-height, so this isn't affected by dynamic/expanding puffs
						if($.support.minHeight)
						{
							r[k].css({
								minHeight: max_height + 'px'
							});
						}
						else
						{
							r[k].css({
								height: max_height + 'px'
							});
						}
					}
				}
			}
		}
	});
})
(jQuery);

/**
 * Content height correction
 */
//(function($)
//{
//	$(window).load(function() // wait until all content has loaded...
//	{
//		var _w = $('#main');
//		var _c = $('#content', _w);
//		
//		if(_c.outerHeight() < _w.innerHeight())
//		{
//			//var _h = _w.height() - parseInt(_c.css('padding-top')) - parseInt(_c.css('padding-bottom'));
//			//_c.height(_h);
//
//			var _h = $('#sidebar').height();
//			
//			_c.css({
//				minHeight: _h
//			});
//		}
//	});
//})
//(jQuery);

/**
 * Accordian downloads puff
 */
(function($)
{
	$(document).ready(function()
	{
		var container = $('#documents.secondary-puff');
		
		container
			.find('li.download-item')
			.each(function()
			{
				var _c = $(this);
				
				var _handle = $('dt.title', this);
				var _content = _handle.next();
				
				var _handleLink = $('<a />')
					.attr('href', '#')
					.html(_handle.html())
					.click(function(e)
					{
						e.preventDefault();
						
						// ignore if currently being animated
						if(_content.is(':animated'))
							return false;

						// show/hide
						if(_content.is(':visible'))
						{
							_c.removeClass('open');
							_content.slideUp(300, 'linear');
						}
						else
						{
							container.find('li.download-item')
								.removeClass('open')
								.not(_c)
								.find('dt.title + dd')
								.slideUp(300, 'linear');
								
							_c.addClass('open');
							_content.slideDown(300, 'linear');
						}
					});
					
				// hide the contents on load
				_content.hide();
				
				_handle.html(_handleLink);
			});
	});
})
(jQuery);

/**
 * Gallery puff
 */
(function($)
{
	$(document).ready(function()
	{
		var gallery = $('#gallery.puff');
		
		/** just add the overlay button for now */
		$('li', gallery).each(function()
		{
			var _li = $(this);
			
			if(_li.find('img').length == 1)
			{
				var mask = $('<span />')
					.css({
						opacity: 0
					})
					.addClass('mask')
					.insertAfter(_li.find('img'));
					
				_li.hoverIntent({
					over: function()
					{
						mask.animate({opacity: 0.2}, 200);
					},
					out: function()
					{
						mask.animate({opacity: 0}, 200);
					},
					timeout: 100,
					interval: 10
				});
			}
		});
	});
})
(jQuery);

/**
 * Accordian
 */
(function($)
{
	$(document).ready(function()
	{
		$('.accordian').each(function()
		{
			var _accordian = $(this);
			var _rows = $('.accordian-item', _accordian);
       
			/**
			 * grab heading, turn into control
			 * then wrap all suffixed contents to create collapsible
			 */
			_rows.each(function()
			{
				var _row = $(this);
				var _heading = $('> h2:first-child', _row);
				var _control = $('<a />').attr('href', '#');
				
				var _collapsable = $('<div />')
					.addClass('accordian-collapsable')
					/* wrap all - not using $.wrapAll() because it seems to clone without event bindings */
					.html(_heading.siblings()).insertAfter(_heading)
					.hide();
				
				var _headingWrapper = $('<div />')
					.addClass('accordian-heading')
					.html(_heading)
					.prependTo(_row);
				
				_heading.html(_control.html(_heading.html()));

				/* add the expand/collapse indicator element */
				$('<span />')
					.addClass('accordian-indicator')
					.prependTo(_control);
				
				_control.click(function(e)
				{
					e.preventDefault();
					
					if(_collapsable.is(':animated'))
					{
						return false;
					}
					
					if(_collapsable.is(':visible'))
					{
						_row.removeClass('open');
						_collapsable.slideUp(300, resetPageMinHeight);
					}
					else
					{
						_rows.removeClass('open');
						_rows.find('.accordian-collapsable').slideUp(300);
						_row.addClass('open');
						_collapsable.slideDown(300, function()
						{
							
							var eTop = _row.offset().top;
							var inView = (eTop - $(window).scrollTop()) > 0;

							if(inView)
								return;
							
							$('html, body').animate({
								scrollTop: _row.offset().top
							},
							300, resetPageMinHeight);
						});
					}
					
					/* update url hash so links/states are copy/pastable */
					if(_row.attr('id'))
					{
						// get row id
						var id = _row.attr('id');
						
						// remove the id, to prevent page jump on click
						_row.attr('id', '');
						
						// set the hash
						document.location.hash = id;
						
						// add the id again
						_row.attr('id', id);
					}
				});
				
				// select specified accordian row from url hash
				if(_row.attr('id') == document.location.hash.replace(new RegExp("^[#]+", "g"), ''))
				{
					_control.click();
				}
			});
			
			if(!_rows.hasClass('open'))
			{
				// show the first row by default
        
        $(_rows.get(0))
          .addClass('open')
          .find('.accordian-collapsable')
          .show();
        
				
			}
      
      if (_accordian.hasClass("standalone-accordian")) {
        $(_rows.get(0)).removeClass("open").find('.accordian-collapsable').hide();
      }
		});
	});
})
(jQuery);

/**
 * Fancybox lightboxes for content images
 */
(function($){
	$(function() {

		// Shadowboxes
		$('a[rel=shadowbox]').fancybox({
			hideOnContentClick: false,
			frameHeight: 365,
			frameWidth: 639,
			titlePosition: 'over'
		});
		$("a[rel=shadowbox_iframe]").fancybox({
			'width' : '70%',
			'height' : '55%',
			'autoScale' : false,
			'transitionIn' : 'elastic',
			'transitionOut' : 'elastic',
			'type' : 'iframe',
			'showNavArrows' : false
		}); 

		// added embiggen icon to indicate images are zoomable
		var zoom_icon = $("<span/>")
			.addClass("zoom")
			.html("&nbsp;");
		$('a[rel=shadowbox], a[rel=shadowbox_iframe]')
			.css("position", "relative")
			.append(zoom_icon);

		//fancybox with iframe for oncourse pledge
		
		$("a.iframe").fancybox({
			'width' : 850,
			'height' : '90%',
			'autoScale' : false
		});

	});
})(jQuery);


/**
 * Tab container
 */
(function($)
{
	$(document).ready(function()
	{
		$('.tab-container').each(function()
		{
			var container = $(this);
			var tabs = $('> .tab-item', container);
			
			var tabStripWrapper = $('<div></div>')
				.addClass('tab-strip')
				.prependTo(container);
				
			var tabStrip = $('<ul></ul>')
				.appendTo(tabStripWrapper);
				
			var defaultTab = false;
			
			var requestHash = document.location.hash.replace(new RegExp("^[#]+", "g"), '');
			
			tabs.each(function()
			{
				var currentTab = $(this);
				var heading = $('.tab-heading', currentTab).hide();
				
				var _li = $('<li />')
					.appendTo(tabStrip);
				
				var _a = $('<a href="#"></a>')
					.appendTo(_li)
					.click(_selectTab);
					
				var _span = $('<span />')
					.html(heading.html())
					.appendTo(_a);
					
				var headingId = heading.attr('id');
					
				/* set defaultTab if required */
				if(!defaultTab && requestHash == headingId)
				{
					defaultTab = _a;
				}
					
				function _selectTab(e)
				{
					e.preventDefault();
					
					showTab(currentTab, _li);
						
					/* update url hash */
					if(headingId)
					{
						// remove id to prevent page jump
						heading.attr('id', '');
						
						// set hash
						document.location.hash = headingId;
						
						// reset element id
						heading.attr('id', headingId);
					}
				}
			});
			
			function showTab(tab, li)
			{
				/* show correct tab-item, hide others */
				tab
					.addClass('active')
					.show()
					.siblings()
					.filter('.tab-item')
					.removeClass('active')
					.hide();
					
				/* add active state to tabStrip, remove from others */
				li
					.addClass('active')
					.siblings()
					.removeClass('active');
					
				$('.image-carousel').columnCarousel();
			}
			
			/* if defaultTab is set, we need to preselect that tab, otherwise just show the first one */
			if(defaultTab)
			{
				defaultTab.click();
			}
			else
			{
				// show first tab by default
				showTab($(tabs.get(0)), tabStrip.find('li:first-child'));
			}
		});
	});
})
(jQuery);

/**
 * Homepage article carousel
 */
(function($)
{
	$.fn.columnCarousel = function(options)
	{
		var defaults = {
			//numToShow: 2
			numToScroll: false,
			autoControls: false // determines if we should calculate the control positions automatically (not suitable for hidden elements)
		};
		options = $.extend(defaults, options);
		
		return this.each(function()
		{
			var el = $(this);
			
			if(el.data('is_init') == true)
			{
				return;
			}
			
			var containerIsUl = el.is('ul');
			
			var items = el.find('> *');
			
			items.filter(':last-child').addClass('last');
			
			if(containerIsUl)
			{
				var d_el = $('<div />')
					.attr('class', el.attr('class'))
					.insertBefore(el)
					.append(items);
				
				el.remove();
			
				el = d_el;
			}
			
			var itemsWidth = 0;
			
			var numSlides = 0;
			
			var currentSlide = 1;
			
			var controls = $('<div />')
				.addClass('carousel-controls');
			
			var ctrlHolder = $('<' + (containerIsUl ? 'ul' : 'div') + ' />')
				.addClass('column-carousel-slider')
				.append(items)
				.appendTo(el);
				
			ctrlHolder.wrap('<div class="column-carousel-container" />');
				
			// loop through the items and get the width to set parent container
			$(window).ready(function()
			{
				items.each(function()
				{
					var item = $(this);
					var mr = parseInt(item.css('margin-right'));
					var ml = parseInt(item.css('margin-left'));

					itemsWidth+= parseInt(item.css('width'));
					itemsWidth+= isNaN(mr) ? 0 : mr;
					itemsWidth+= isNaN(ml) ? 0 : ml;
				});
				ctrlHolder.width(itemsWidth);
				
				// get the number of 'slides'
				//numSlides = Math.round(itemsWidth / el.width());
				numSlides = itemsWidth / parseInt(el.css('width'));
				
				// only add controls if slider container is wider than its container
				if(numSlides > 1)
				{
					// add controls
					var next = $('<span><span class="control-bg"></span><span class="control-text">Next</span></span>')
						.addClass('control')
						.addClass('next')
						.click(slideNext)
						.appendTo(controls);

					var prev = $('<span><span class="control-bg"></span><span class="control-text">Previous</span></span>')
						.addClass('control')
						.addClass('prev')
						.click(slidePrev)
						.appendTo(controls);
					
					controls.appendTo(el);
					
					// set opacity
					controls.find('.control-text').css({opacity:0.8});
					controls.find('.control-bg').css({opacity:0.4});
					
					controls.find('control').hover(function()
					{
						if(!$(this).hasClass('disabled'))
							$(this).find('.control-bg').animate({opacity: 0.6}, 200);
					},
					function()
					{
						if(!$(this).hasClass('disabled'))
							$(this).find('.control-bg').animate({opacity: 0.4}, 200);
					});
					
					prev.addClass('disabled').css({cursor: 'default'});
					next.css({cursor: 'pointer'});
					
					if(options.autoControls)
						calcControlOffset();
				}
				
				el.data('is_init', true);
			});
			
			/**
			 * Slides the carousel to a particular 'frame'
			 */
			function slideNext()
			{
				if(!hasNext())
					return false;
				
				if(ctrlHolder.is(':animated'))
					return;
				
				currentSlide++;

				ctrlHolder.stop().animate({
					left: '-=' + getSlideAmount() + 'px' 
				},
				500,
				setControlState);
			}
			
			function slidePrev()
			{
				if(!hasPrev())
					return false;
				
				if(ctrlHolder.is(':animated'))
					return;
				
				currentSlide--;
				
				ctrlHolder.stop().animate({
					left: '+=' + getSlideAmount() + 'px'
				}, 
				500, 
				setControlState);
			}
			
			function getSlideAmount()
			{
				var amt = el.width() + getSlideMargin();
				var numSlide = options.numToSlide;

				if(numSlide)
				{
					amt = (el.find('> * > * > *').first().width() * numSlide) + getSlideMargin();
				}
				
				return amt;
			}
			
			function setControlState()
			{
				if(!hasPrev())
				{
					controls.find('.prev').addClass('disabled').css({cursor: 'default'}).siblings().removeClass('disabled').css({cursor: 'pointer'});
				}
				else
				{
					controls.find('.prev').removeClass('disabled').css({cursor: 'pointer'});
				}
				
				if(!hasNext())
				{
					controls.find('.next').addClass('disabled').css({cursor: 'default'}).siblings().removeClass('disabled').css({cursor: 'pointer'});
				}
				else
				{
					controls.find('.next').removeClass('disabled').css({cursor: 'pointer'});
				}
			}
			
			function getSlideMargin()
			{
				return parseInt($(items.get(0)).css('margin-right'));
			}
			
			function hasNext()
			{
				var _l = (parseInt(ctrlHolder.css('left')) * -1);
				var l = isNaN(_l) ? 0 : _l;
				var outOfBounds = (l + el.width()) >= itemsWidth;
				
				var itemWidth = el.find('>*>*>*').first().width();
				
				if(options.numToSlide)
				{
					outOfBounds = (l + itemWidth) >= itemsWidth;
				}

				if(outOfBounds)
					return false;
				
				if(_l >= ((ctrlHolder.width() - itemWidth - (itemWidth * options.numToSlide) - getSlideMargin())))
				{
					return false;
				}
				
				return true;
				
				//return numSlides > currentSlide;
			}
			
			function hasPrev()
			{
				return currentSlide > 1;
			}
			
			/**
			 * Aligns controls to correct top offset
			 */
			function calcControlOffset()
			{
				var aligningElement = el.find('.image').length
					? el.find('.image')
					: el;
					
				var halfwayPoint = (aligningElement.height() / 2) + aligningElement.position().top;
				
				var buttons = controls.find('> *');
				
				return buttons.css('top', Math.floor(halfwayPoint) + 'px');
			}
			
			
		});
	}
})
(jQuery);

(function($)
{
	/* contains the path to the current background image */
	var bg_url;
	var container;
	var content;
	var bg_thumb;
	var bg_thumb_overlay;
	var show_text = 'Show Image';
	var close_text = 'Show Content';

	/**
	* Handle the scaling background image
	*/
	$(document).ready(function()
	{	
		/* check if browser actually supports fixed positioning before trying to apply this */
		if(supportsFixedPosition())
		{
			var el = $('#background-container');
			//var el = $('<div />').appendTo('body')

			if(typeof el.attr('id') !== 'undefined')
			{
				/* get background image location */
				bg_url = el.css('background-image');

				bg_url = bg_url.replace('url(', '');
				bg_url = bg_url.replace(')', '');
				bg_url = bg_url.replace('"', '').replace('"', '');

				/* create image container */
				container = $('<div />')
					.attr('id', 'lineScalingImage');

				/* create image element */
				var _img = $('<img />')
					.attr('src', bg_url);

				/* append image to container */
				_img.appendTo(container);

				/* *prepend* image container to body element */
				container.prependTo('body');

				/* set the '#main' background to be transparent */
				el.css('background', 'transparent');
			}
		}
		else
		{
			// move the background to the body element for fixed position
			var el = $('#background-container');
			var fixedBg = el.css('background-image');

			el.css({
				backgroundImage: 'none',
				backgroundColor: 'transparent'
			});

			$('body').css({backgroundImage: fixedBg, backgroundAttachment: 'fixed', backgroundPosition: 'center 0'});
		}
	});
	
	function configureOverlay(el)
	{
		if(el instanceof jQuery)
		{
			var title_text = el.find('dt').html();
			var text = el.find('p');
			
			//.attr('id', 'background-container').appendTo('body')
			
			var overlay = $('<div />')
				.attr('id', 'background-overlay-container')
				.addClass('background-overlay');
				
			var overlay_inner = $('<div />').addClass('overlay-inner').appendTo(overlay);
			
			$('<div />').addClass('geo-logo').prependTo(overlay);
			$('<h1>'+title_text+'</h1>').appendTo(overlay_inner);
			text.each(function() { $(this).appendTo(overlay_inner); });

			return overlay.hide();
		}
		return jQuery;
	}

	/**
	* add the fullscreen background functionality
	* n.b. we're creating this element once all content has loaded
	* to prevent dimensions being incorrectly set, if clicked early
	*/
	$(document).ready(function()
	{
		bg_thumb = $('#background-puff');
		bg_thumb_overlay = configureOverlay($('#background-puff'));

		if(supportsFixedPosition())
		{
			bg_thumb_overlay.css('position', 'fixed');
		}
		else
		{
			bg_thumb_overlay.css('position', 'absolute');
		}

		bg_thumb_overlay.css({
			width: 361,
			marginLeft: '599px'
		});

		content = $('#main');

		var close = $('<span class="toggle"/>');
		
		$('<a />')
		/* bind click for close button */
		.click(function(e)
		{
			e.preventDefault();
			
			$('> div', content).fadeIn(200, function()
			{
				bg_thumb_overlay.hide();
				$('#main').removeClass('hidden');
			});
			
			$('#footer, #header').show();
		})
		.addClass('btn')
		.attr('href', '#')
		.text(close_text)
		.appendTo(close);

		bg_thumb_overlay
			.append(close)
			.prependTo($('#main'));

		var a = $('<a />').attr('href', '#').text(show_text);
		var p = $('<span class="toggle" />').append(a);

		p.appendTo(bg_thumb);

		var pad = parseInt(bg_thumb.css('padding-top'));

		/* bind event for open button */
		bg_thumb.click(function(e)
		{
			e.preventDefault();

			/* fix the element height */
			content.height(content.height());

			
			$('#footer, #header').hide();
			$('> div', content).hide(0, function()
			{
				bg_thumb_overlay.fadeIn(600, function()
				{
					$(window).resize();
				});
				
				$('#main').addClass('hidden');
			});
		});
		
		$(window).resize(function()
		{
			var winHeight = $(this).height();
			var overlayXMax = bg_thumb_overlay.height() + p.height() + bg_thumb_overlay.offset().top;
			var bottomLimitX = 20;
			var topLimitX = 40;
			var el = bg_thumb_overlay.find('.copyright');
			var copyrightHeight = el.height();
			
			var margin = winHeight - overlayXMax;
			
			if(margin + overlayXMax > (winHeight - overlayXMax - bottomLimitX))
			{
				margin = margin - 10 - bottomLimitX;
			}
			
			if(margin < topLimitX + topLimitX)
				margin = topLimitX + topLimitX;
			

			el.css({
				marginTop: margin + 'px'
			});
		});

		/* trigger window resize event */
		$(window).resize();
	});
	
	$(window).resize(function()
	{
		var el = $('#lineScalingImage img').css({
			width: '100%', 
			height: 'auto', 
			position: 'relative', 
			left: '50%', 
			marginLeft: '-100%'
		});

		if($(window).height() >= el.height())
		{
			el.height($(window).height())
				.css({
					width: 'auto'
				});
		}
		else
		{
			el.css({
				width: '100%',
				height: 'auto'
			});
		}
	}).resize();
	
	$(window).load(_triggerResize);
	$(document).ready(_triggerResize);

	function _triggerResize()
	{
		$(this).resize();
	}
})
(jQuery);


(function($){
	$(document).ready(function () {
		// CLICK CHECKBOX TO TOGGLE TEXTAREAS
	    $(".toggle_textarea").change(function() {
	      checkbox = $(this);
	      console.log(checkbox.is(":checked"));
	      // FIND THE DIV WITH THE TEXTREA

	      // console.log(checkbox.parent().parent().parent().find("." + checkbox.attr("id")));
	      checkbox.parent().parent().parent().find("." + checkbox.attr("id")).each(function() {
	      	
	        my_div = $(this);
	       	if (checkbox.is(":checked")) {
		       	my_div.show();
		    } else {
			    my_div.hide();
			}

	        // IF THE ELEMENT IS HIDDEN THE DISABLE THE INPUT
	        // NEED THIS TO MAKE SURE VALUES ARE NOT SAVED ON SUBMIT BUT ARE VALUES ARE
	        // STILL AVAILABLE IF USE RE-CLICKS BEFORE SAVING
	        my_div.find("input,textarea").each(function() {
	          $(this).attr('disabled', !my_div.is(':visible'));
	        });
	      });

	    });
	});
})(jQuery);



/**
 * Footer rotating logo's
 * 
 * @author Brian Coit <brian@line.uk.com>
 */
(function($)
{
	$.fn.partnerImageRotate = function(options)
	{
		options = $.extend({
			interval: 5000,
			transitionSpeed: 300
		},
		options);
		
		return this.each(function()
		{
			var container = $(this);
			var sections = container.find('> *');
			var sectionSlices = new Array();
			var j = 0;
			var hovered = false;
			var maxHeight = 0;
			var to;
			
			//_load();
			
			// position element off page
			container.css({
				position: 'absolute',
				top: '-9999px',
				left: '-9999px'
			});
			
			$(window).load(function()
			{
				// move back into display
				container.css({
					position: 'static', 
					top: 0, 
					left: 0
				});
				
				_setWidthData();
				
				_groupByWidth();
				
				_moveToBackgroundImages();
				
				_bindHover();
				
				
				sections.hide().first().show(); // show the first section
				sections.find('.row').hide(); // hide all items
				sections.first().find('.row:first-child').show();
				
				//_cycleSection();
				_cycleRow();
			});
			
			function _bindHover()
			{
				sections.find('a').hover(function()
				{
					clearTimeout(to);
				},
				function()
				{
					_cycleRow();
				});
			}
			
			function _moveToBackgroundImages()
			{
				container.find('img').each(function()
				{
					var img = $(this);
					
					if(img.parent().is('a'))
					{
						img.parent().css({
							backgroundImage: 'url("' + img.attr('src') + '")',
							width: img.data('item-width'),
							height: img.data('item-height') / 2
						});
						
						img.remove();
					}
				});
			}
			
//			function _getMaxHeight()
//			{
//				var h = 0;
//				
//				container.find('.section').each(function()
//				{
//					if($(this).height() > h)
//					{
//						h = $(this).height();
//					}
//				});
//				
//				return h;
//			}
			
			function _setWidthData()
			{
				container.find('img').each(function()
				{
					// set the width as data
					$(this).data('item-width', $(this).width());
					$(this).data('item-height', $(this).height());
				});
			}
			
			/** Group logos by width */
			function _groupByWidth()
			{
				var maxWidth = container.width();
				
				sections.each(function()
				{
					var section = $(this);
					var w = 0;

					section.each(function()
					{
						var _itemContainer = $('ul', this);
						var _item = $('li', _itemContainer);
						var maxWidth = _itemContainer.width();
						var curWidth = 0;
						var rowUl = _createRow();
						
						_item.each(function(i)
						{
							curWidth+= _item.find('img').data('item-width') + parseInt(_item.css('margin-right'));
							
							if(curWidth > maxWidth)
							{
								rowUl = _createRow();
								curWidth = 0;
							}
							
							$(this).appendTo(rowUl)
						});
						
						function _createRow()
						{
							return $('<ul />')
								.addClass('row')
								.css({display: 'none'})
								.appendTo(_itemContainer.parent());
						}
						
						_itemContainer.remove();
					});
				});
				
				$('li:last-child', sections).each(function()
				{
					$(this).addClass('last')
				});
			}
			
			function _cycleRow()
			{
				to = setTimeout(function()
				{
					var rows = sections.find('.row');
					var rowsArray = $.makeArray(rows);

					rows.filter(':visible').fadeOut(options.transitionSpeed, function()
					{
						var _row = $(this);
						var _nextRow = _row.next();


							if(_nextRow.length)
							{
								_nextRow.fadeIn(options.transitionSpeed, _cycleRow);
							}
							else
							{
								var section = _cycleSection();

								section.find('.row:first-child').fadeIn(options.transitionSpeed, _cycleRow);
							}

					});
				},
				options.interval);
			}

			function _cycleSection()
			{
				var currentSection = sections.filter(':visible');
				currentSection.hide();
				
				if(currentSection.next().length)
				{
					return currentSection.next().show();
				}
				else
				{
					return sections.first().show();
				}
			}
			
		});
	};
})
(jQuery);

/**
 * Generic equalHeights
 * 
 * @author Brian Coit <brian@line.uk.com>
 */
(function($)
{
	$.fn.equalHeights = function(options)
	{
		options = $.extend({
			cols: 2
		}, options);
		
		var split = {};
		var j = 0;
		
		this.each(function(i)
		{	
			if(i % options.cols === 0)
				j++;
			
			if(!split[j])
				split[j] = new Array();
			
			split[j].push($(this));
		});
		
		for(var k in split)
		{
			var z = 0;
			var el = split[k];
			
			for(var l in el)
			{
				if(el[l].height() > z)
				{
					z = el[l].height();
				}
			}
			for(var l in el)
			{
				//el[l].height(z);
				
				if($.support.minHeight)
				{
					el[l].css({
						minHeight: z + 'px'
					});
				}
				else
				{
					el[l].css({
						height: z + 'px'
					});
				}
			}
		}
	}
})
(jQuery);

/**
 * GEOSA table expander/collapser
 */
(function($)
{
	$(document).ready(function()
	{
		var sections = $('.expander dd, #geosa-experience .tab-item li');
		var itemsToDisplay = 3;
		
		sections.each(function()
		{
			var section = $(this);
			
			var table = $('table', section);
			
			var trs = $('tbody tr', section);
			
			var numElements = trs.length;
			
			if(numElements > itemsToDisplay)
			{
				var visible = trs.slice(0, itemsToDisplay);
				
				var hidden = trs.not(visible);
				
				var controls = $('<div />').addClass('view-more')
					.insertAfter(table);
				
				var ctrlHolder = $('<div />').addClass('view-more-tbl')
					.insertAfter(controls)
					.hide();
					
				var controls_count = $('<span />').text('And ' + (numElements - itemsToDisplay) + ' more')
					.addClass('num-extra')
					.appendTo(controls);
					
				var controls_expand = $('<span />').text('See all')
					.addClass('expand-collapse')
					.appendTo(controls)
					.css({
						cursor: 'pointer'
					})
					.click(function()
					{
						if(ctrlHolder.is(':visible'))
						{
							$(this).removeClass('open');
							
							controls_expand.text('See all');
							
							// hide
							ctrlHolder
								.slideUp(400);
						}
						else
						{
							$(this).addClass('open');
							
							controls_expand.text('Close');
							
							// show
							ctrlHolder
								.slideDown(400);
						}
					});
				
				var tableClone = table.clone()
					.appendTo(ctrlHolder);
				
				tableClone.find('tbody tr, thead')
					.remove();
				
				tableClone.find('tbody')
					.append(hidden);
			}
		});
		
	});
})
(jQuery);

/**
 * Social links strip
 */
(function($)
{
	$(document).ready(function()
	{
		var _open = function(e)
		{
			e.preventDefault();
			
			if(!overlay.is(':visible'))
			{
				overlay
					.stop()
					.fadeIn(150, function()
					{
						$(document).keyup(function(e)
						{
							if(e.keyCode == 27)
							{
								_close();
							}
						});
					});
			}
		};
		
		var _close = function(e)
		{
			if(e)
				e.preventDefault();
			
			if(overlay.is(':visible'))
			{
				overlay.fadeOut(200);
			}
		};
		
		$('.social-popup-overlay')
			.removeClass('social-popup-overlay-no-js');
		
		var socialStrip = $('#social-footer');
		var btn = $('.social-popup', socialStrip);
		var btn_a = btn.find('> a');
		var overlay = btn_a.siblings().hide();
		
		var bar = $('<div />')
			.addClass('title-bar');
			
		var close = $('<a />')
			.text('Close')
			.attr('href', '#')
			.attr('title', 'close')
			.addClass('close-button')
			.prependTo(bar)
			.click(_close);
		
		overlay.find('.social-popup-overlay-is')
			.prepend(bar);
		
		btn_a.click(_open);
	});
})
(jQuery);

/**
 * jQuery plugin function calls
 */
(function($)
{
	// add inner-wrapping divs to td's on #geosa-experience
	$(document).ready(function() 
	{
		$('#geosa-experience tbody td').wrapInner('<div class="overline" />');
		
		//$('#footer .puff-key-partners ul').imageRotate();
		$('#footer .puff-key-partners > dl > dd > ul').partnerImageRotate();
		
		$('.carousel-columns').columnCarousel({numToSlide: 1, autoControls: false});
		$('.image-carousel').columnCarousel({autoControls: false});
	});
	
	$(window).ready(function()
	{
		$('.grid-3 figcaption div').equalHeights({cols: 3});
		$('.block-puffs-list li').equalHeights({cols: 3});
		
		$('#login-register .equal').equalHeights({cols: 2});
		
		//$('.carousel-columns .half-col > h2:first-child').equalHeights({cols: 2});
		$('.self_assessment_q label').equalHeights({cols: 3});
	});
	
	/* <ie7 css3 selector fixes */
	$(document).ready(function()
	{
		if($.browser.msie && parseInt($.browser.version) <= 7)
		{
			$('h2 + .subtext').addClass('subtext-fix');
		}
		
		// ie6 accordian table fix
		if($.browser.msie && parseInt($.browser.version) <= 6)
		{
			$('table.grid').wrap('<div style="zoom:1; display: block; width: 100%; float: left;" />');
		}
	});
})
(jQuery);

$(document).ready(function()
{
	$('form .helper-helptext')
		.each(function()
		{
			var _this = $(this);
			var html = _this.html();
			var fieldParent = $(this).parent().parent();

			_this.tipTip({
				content: html,
				maxWidth: '235px',
				defaultPosition: 'right',
				edgeOffset: 4
			});
			
			fieldParent.find(':input')
				.focus(function()
				{
					_this.trigger('mouseover');
				})
				.blur(function()
				{
					_this.trigger('mouseout');
				});
		});
});



  // Self assesment forms radio button functionality

  (function($){
  	function applyRadio(radio)
		{
		  var selected = radio;

		  radio.parents('ul.self_assessment_q').find('li div input:radio').each(function() {
		    if($(this).val() != selected.val()) {
		      $(this).parents('div').removeClass('selected');
		    } else {
		      $(this).parents('div').addClass('selected');
		    }
		  });
		}

	$(document).ready(function(){
		// FIX FOR SELF-ASSESSMENT CLICK ON IE6
    $("ul.self_assessment_q label").click(function() {
      applyRadio($(this).find('input:radio'));
    });

    $("ul.self_assessment_q input:radio").click(function() {
      applyRadio($(this));
    });
	});

  })(jQuery);


  // certification process dynamic unit label change functionality

  (function($){

	$(document).ready(function(){

		if ($("#facility_measurement_course_length").length > 0 && $("#facility_measurement_area").length > 0) {
			// do it the first time
			$(".unitLength").html($("#facility_measurement_course_length option:first").text());
			$(".unitArea").html($("#facility_measurement_area option:first").text());
		
			// check for changes there after
			$("#facility_measurement_course_length").change(function(){
        var unit = $(this).find('option:selected').text();
				$(".unitLength").html(unit);
			});
			$("#facility_measurement_area").change(function(){
        var unit = $(this).find('option:selected').text();
				$(".unitArea").html(unit);
			});
		}
	});

  })(jQuery);

  (function($){
	$(document).ready(function(){
		// help tabs collapse and exands on click
		$("form .help > *:not(span)").hide().parent().addClass("collapsed");
		$("form .help span").click( function () {
			var _thisitem = $(this);
			$(this).parent().find('> *:not(span)').slideToggle('slow');
			_thisitem.parent().toggleClass("collapsed");
		});
	});
  })(jQuery);


  (function($){
	$(document).ready(function(){

		var oer_recommendation = $("#onsite_evaluation_report_recommendation");
		var oer_body = $(".evaluation_report_body");
		var oer_pending = $(".pending_only");

		if (oer_recommendation.length > 0) {

			oer_recommendation.val() == "" ? oer_body.hide() : oer_body.show();
			oer_recommendation.change(function() {

				var _this = $(this);

				if (_this.val() == "AWARD") {
					oer_body.show();
					oer_pending.hide();
				} else if (_this.val() == "PENDING") {
					oer_body.show();
					oer_pending.show();
				} else {
					oer_body.hide();
				}
			});
		}
	});
  })(jQuery);
  
  
var originalHeight;

function resetPageMinHeight()
{
	var header = $('#header');
	var content = $('#content');
	var footer = $('#footer');
	
	if(!originalHeight)
		originalHeight = parseInt(content.css('min-height'));
	
	content.css({
		minHeight: 0
	});
	
	var minHeight = 0;
	
	var win_height = $(window).height();
	var total_height_before = header.outerHeight() + content.outerHeight() + footer.outerHeight();
	
	var padding_y = parseInt(content.css('padding-top')) + parseInt(content.css('padding-bottom'));
	var newHeight = win_height - (header.outerHeight() + footer.outerHeight());
	
	if(win_height > total_height_before - padding_y)
	{
		content.css({
			minHeight: newHeight - padding_y
		});
	}
//	

	var s = $('#sidebar').outerHeight() - padding_y;
	
	if(content.outerHeight() < $('#main').height())
		$('#content').css('min-height', s + 'px');
}

$(window).load(resetPageMinHeight);
$(window).resize(resetPageMinHeight);

/**
 * 	Datepicker for all the fields with class name:"date"
 */
function applyDatepicker(el)
{
	el.live('focus', function()
	{
		$(this).datepicker(
		{			
			changeMonth: true,
			changeYear: true,
			dateFormat: 'yy-mm-dd',
			minDate: new Date(1990, 1 - 1, 1),
			maxDate: new Date(),
			yearRange: "-30:+0"
		});
	});
}


/*
 * Some functionality for the quickstart invitation forms...
 */
$(function() {
	applyDatepicker($('.datepicker'));
});

  /**
   * Toggle for mass action select / unselect table
   */
  $(document).ready(function() {
    
    $('.massaction-all-chk').click(function() {
      $this = $(this);
      $this.closest('table').find('td:last-child input').prop("checked", $this.prop("checked"));
      
      toggleInvitations($this.closest('table'));
    });
    
    $('.massaction-all-chk').closest('table').find('td:last-child input').click(function() {
      
      var collectionLength = $('.massaction-all-chk').closest('table').find('td:last-child input').length;
      var checkedLength = $('.massaction-all-chk').closest('table').find('td:last-child input:checked').length;

      if (checkedLength != collectionLength) {
         $('.massaction-all-chk').prop("checked", false);
      }
      else {
          $('.massaction-all-chk').prop("checked", true);
          $(this).closest('form').children('button').removeClass('disabled');
      }
      
      toggleInvitations($(this).closest('table'));
    });
    
    function toggleInvitations($table)
    {
      var checkedLength = $table.find('td:last-child input:checked').length;
      
      if (checkedLength !== 0) {
        $table.parent('form').children('button').removeClass('disabled');
      }
      else {
       $table.parent('form').children('button').addClass('disabled');
      }
    }
    
 
    $('.disabled').live('click', function() {
     return false;
    });
  });
