window.addEvent('domready', function(){

	// Resize images to fill space
	function resize()
	{
		if ($('backgroundfill'))
		{
			if ($('backgroundfill').hasClass('half'))
			{
				var widthoffset = 410;
				var minw = 670;
				var minh = 749;
			}
			else
			{
				var widthoffset = 0;
				var minw = 1080;
				var minh = 749;
			}
				
			if (window.innerHeight) { // Firefox
				var docheight = window.innerHeight;
				var docwidth = window.innerWidth - widthoffset;
			} else if (document.all) { // IE
				var docheight = document.body.clientHeight;
				var docwidth = document.body.clientWidth - widthoffset;
			};
					
			var docratio = docwidth/docheight;
			var ratio = minw/minh;
			
			// Propose scaling to full width
			propw = docwidth;
			proph = docwidth / ratio;
			
			// If scaling to full width causes height cut...
			if (proph < docheight)
			{
				// Propose scaling to full height instead
				propw = docheight * ratio;
				proph = docheight;
			}
			
			// If proposed width is smaller than minimum
			if (propw < minw || proph < minh)
			{
				propw = minw;
				proph = minh;
				
				// But make sure width and height aren't cropped
				if (propw < docwidth)
				{
					// Propose scaling to full width
					propw = docwidth;
					proph = docwidth / ratio;
				}
				if (proph < docheight)
				{
					// Propose scaling to full height instead
					propw = docheight * ratio;
					proph = docheight;
				}
			}
			$('backgroundfill').setStyle('width', propw);
			$('backgroundfill').setStyle('height', proph);
			
			// Prevent browser window from going to narrow
			if (docwidth+widthoffset < 860) {
/* 				$('container').setStyle('width', 880); */
				$('yourontariocollegefaculty').setStyles({'right':'auto', 'left':580});
			}
			else 
			{
/* 				$('container').setStyle('width', '100%'); */
				$('yourontariocollegefaculty').setStyles({'right':45, 'left':'auto'});
			}
			
		}
		
	}
	
	// Bind the function to window on resize & then run it on DOM load
	window.onresize = resize;
	resize();
		
	// Set focus for scrolling at load (mostly for IE)
	document.getElementById("container").focus();
	
	// Menu interactivity animations
	if ($('mainmenu'))
	{	
		$('mainmenu').getElements('li').each(function(item)
		{
			if (item.hasClass('selected')) item.getElement('a').setStyles( {top: -22, cursor: 'default'} );
			else
			{
				item.getElement('a').set('tween' , { duration: 250 });
				item.addEvents({
					'mouseenter': function()
					{
						item.getElement('a').get('tween').cancel();
						item.getElement('a').tween('top', -22);
					},
					'mouseleave': function()
					{
						item.getElement('a').get('tween').cancel();
						item.getElement('a').tween('top', 0);
					}
				});
			}
		});
	}
	
	// Homepage animation
	$('backgroundfill').set('tween' , { duration: 2000 });
	if ($('backgroundfill').hasClass('half'))
	{
		$('backgroundfill').fade('hide');
		$('backgroundfill').fade('in');
	}
	else
	{
		$('backgroundfill').getElements('div').each(function(and)
		{
			and.set('tween' , { duration: 1000, delay: 800 });
			and.fade('hide');
		});
		
		$('backgroundfill').getElement('div.and1').fade('in').get('tween').chain(function()
		{
			$('backgroundfill').getElement('div.and2').fade('in').get('tween').chain(function()
			{
				$('backgroundfill').getElement('div.and3').fade('in').get('tween').chain(function()
				{
					$('backgroundfill').getElement('div.and4').fade('in');
				});
			});
		});
	}
	
	// Fix the menubar in place during scrolling
	$('container').addEvent('scroll', function() { $('menubar').setStyle('top', $('container').getScrollTop()-20); });
	
	// Allow smooth scrolling functionality
	var scroll = new Fx.Scroll($('container'), { duration: 800, transition: Fx.Transitions.Quad.easeInOut });
	
	// Expandable content
	if ($('copy'))
	{
		$('copy').getElements('div.expandable').each(function(block)
		{
			var toggler = block.getElement('h2');
			toggler.content = block.getElement('div.hidden');
			toggler.contenth = toggler.content.getSize().y;
			toggler.content.setStyles({'height':1,'overflow':'hidden','visiblity':'visible','opacity':0});
			toggler.content.set('tween' , { duration: 1000 });
			toggler.addEvents({
				'mouseenter': function()
				{
					this.get('tween').cancel();
					this.tween('color', '#999');
				},
				'mouseleave': function()
				{
					this.get('tween').cancel();
					this.tween('color', '#ed1c24');
				},
				'click': function()
				{
					this.get('tween').cancel();
					
					if (!this.content.hasClass('showing'))
					{
						this.morph({'color': '#ed1c24', 'border-bottom-color': '#333'});
						this.content.addClass('showing');
						this.content.setStyles({'height': this.contenth+30, 'padding-top': '1em' });
						this.content.get('tween').cancel();
						this.content.fade('in');
						scroll.toElement(this.content);
					}
					else
					{
						this.setStyle('border-bottom-color', '#000');
						this.content.removeClass('showing');
						this.content.get('tween').cancel();
						this.content.fade('hide');
						this.content.setStyles({'height': 1, 'padding-top': 0 });
						
						// In IE the menubar sometimes jumps out of place. Force it back in place
						$('menubar').setStyle('top', $('container').getScrollTop()-20);
					}
				}
			});
		});
	}
});