var _DEBUG = true;


var __onloadFired = false;
var __domreadyFired = false;

function CheckInitEvents()
{
	if (__onloadFired && __domreadyFired)
	{
		window.fireEvent('pagecomplete');
	}
}
//
window.addEvent('domready', function(){ __domreadyFired=true; CheckInitEvents(); });
window.addEvent('load', function(){ __onloadFired=true; CheckInitEvents(); });	

/*
	runs initialization tasks that are common to all pages; code put here should 
	be safe to run once all DOM elements are addressable and images have loaded
	*/
function common_Init()
{
	try
	{
		// the following initialiation methods can run right now because they
		// just attach various effects to elements that aren't visible yet:
		setup_link_hover_effects();
		setup_main_menu_effects();
		attach_window_resize_handlers();
		
		// most content is hidden until the browser has fully loaded the images 
		// and other resources; attach an initialization function to run and display
		// this hidden content once our custom "layoutcomplete" event is fired:
		//window.addEvent('layoutcomplete', init_sIFR);
		window.addEvent('layoutcomplete', show_delayed_content );

		var f1 = function() { adjust_content_size(); adjust_footer_size(); };
		var f3 = function(){ window.fireEvent('layoutcomplete'); };

		var myChain = new Chain();
		myChain.chain(f1, f3);
		myChain.callChain();
		myChain.callChain();

	}
	catch (ex)
	{
		if (_DEBUG) alert("exception in common_load_Init: "  + ex.message);
	}
}


/*
	adjusts the size of the 'content' area;  should only be called AFTER the 
	onload event; i.e. document.readyState should equal 'complete', which means
	we can rely on the fact that all external images are loaded so we know how 
	much space they take up
	*/
function adjust_content_size()
{
	try
	{
		var eContentHeader = $$('DIV.contentHeader')[0];
		var eTextHolder = $$('DIV.textHolder')[0];
		
		if ((eContentHeader == null) || (eTextHolder == null)) return;

		eTextHolder.setStyle('height', eTextHolder.getStyle('scroll-height'));
		eTextHolder.setStyle('margin-top',  -(eTextHolder.getStyle('height').toInt() + 60 + 20)); // A. MUST BE SAME as "C"
		eContentHeader.setStyle('height', eTextHolder.getStyle('height').toInt() + 60 + 90 + 20); // B. MUST BE SAME as "D"

		var footerTop = $('footerInt').getPosition().y;
		var textHolderTop = eTextHolder.getPosition().y;		
		var textHolderHeight = eTextHolder.getStyle('height').toInt();
		
		// if the text container is not tall enough to reach the footer...
		if (textHolderHeight < (footerTop-textHolderTop))
		{
			// artifically extend its height to make it reach the bottom of the page:
			eTextHolder.setStyle('height', (footerTop-textHolderTop)-60);
			// re-run the other two sizing assignments:
			eTextHolder.setStyle('margin-top',  -(eTextHolder.getStyle('height').toInt() + 60 + 20)); // C. MUST BE SAME as "A"
			eContentHeader.setStyle('height', eTextHolder.getStyle('height').toInt() + 60 + 90 + 20); // D. MUST BE SAME as "B"
		}
	}
	catch (ex)
	{
		if (_DEBUG) alert("exception in adjust_content_size: "  + ex.message);
	}	
}

/*
	adjusts the width of the 'footer' element based on the size of the window
	*/
function adjust_footer_size()
{
	try
	{
		var el = $('footerBorderInt');
		if (el != null) // (default.html does not have a 'footerBorderInt' element)
		{
			var myWidth = 0;
			if( typeof( window.innerWidth ) == 'number' ) {
				//Non-IE
				myWidth = window.innerWidth;
			} else if( document.documentElement && ( document.documentElement.clientWidth ) ) {
				//IE 6+ in 'standards compliant mode'
				myWidth = document.documentElement.clientWidth;
			} else if( document.body && ( document.body.clientWidth ) ) {
				//IE 4 compatible
				myWidth = document.body.clientWidth; 
			}
			el.style.width = (myWidth-730 > 0 ? myWidth-730 : 0) + "px";
		}
	}
	catch (ex)
	{
		if (_DEBUG) alert("exception in adjust_footer_size: "  + ex.message);
	}
}



/*
	sets up an effect that fades an element to 0 opacity when it has a mouseover, and 
	back to 1 when the mouse leaves.
	*/
function setup_link_hover_effects()
{
	try
	{
		var links = $$('li.mainLink A.imgLink, A.learnMore, li.subMenuLink A.subMenuLinkItem');
		var imgs = $$('li.mainLink A.imgLink IMG, A.learnMore IMG, li.subMenuLink A.subMenuLinkItem IMG');
		if ((links.length == 0) || (imgs.length == 0))
		{
			// for debugging
			return; // fail silently
		}

		links.each(function(link, i){
			var mouseFx = new Fx.Tween(imgs[i], {property: 'opacity', wait: false, duration: 400, fps: 25});
			link.addEvents({
				'mouseover': function(){
					mouseFx.start( '0' );
				},
				'mouseout': function(){
					mouseFx.start( '1' );
				}
			});
		});
	}
	catch (ex)
	{
		if (_DEBUG) alert("exception in setup_link_hover_effects: "  + ex.message);
	}	
}

/*
	sets up mouse-hover effects for the main menu
*/
function setup_main_menu_effects()
{
	try
	{
		var menuLinks = $$('.mainLinks LI.mainLink');
		var _CLOSED_MENU_HEIGHT = 21; // (height in pixels of closed menu)
		
		menuLinks.each(function(m, i){

			// hide the menu item, and configure it to fade in half a second after the 'layoutcomplete' event fires:
			var fadeInFx = new Fx.Tween(m, {property: 'opacity', duration: 1500, transition: Fx.Transitions.Quad.easeOut, link: 'cancel', fps: 25});
			fadeInFx.set(0.01);
			//m.setStyle('visibility', 'visible');
			var fFadeIn = function() { 
				(function() { fadeInFx.start(1); }).delay(500);
			};
			window.addEvent('layoutcomplete', fFadeIn);

			// collapse the menu, then set up functions to open/collapse it during mouse events:
			var mouseFx = new Fx.Tween(m, {property: 'height', duration: 500, transition: Fx.Transitions.Sine.easeOut, link: 'cancel', fps: 25});
			mouseFx.set(_CLOSED_MENU_HEIGHT);	
			m.addEvents({
				'mouseenter': function(){
					mouseFx.start(m.scrollHeight);
				},
				'mouseleave': function(){
					mouseFx.start(_CLOSED_MENU_HEIGHT);
				}
			}); // ends mLink.addEvents
		}); // ends menuLinks.each
	}
	catch (ex)
	{
		if (_DEBUG) alert("exception in setup_main_menu_effects");
	}
}

var _fadeDelayedContent = false;
function show_delayed_content()
{
	try
	{
		var allDelayedContent = $$('.hide-until-ready, .sIFR-replaced');
		
		allDelayedContent.each(function(el) {
			if (_fadeDelayedContent)
			{
				var tw = new Fx.Tween(el, {property: 'opacity', duration: 1000, transition: Fx.Transitions.Sine.easeOut, link: 'cancel', fps: 25});
				tw.set(0);
				el.setStyle('visibility', 'visible');
				tw.start(1);
			}
			else
			{
				el.setStyle('visibility', 'visible');
			}
		});
	}
	catch (ex)
	{
		if (_DEBUG) alert("exception in show_delayed_content: "  + ex.message);
	}
}

var _resizeTolerance = 99; // lets us process every [x]th resize event; otherwise we crush the CPU
var _resizeTimeout = null;
var _resizeEventCount = -1;



/*
	attaches the functions to resize content and footer when the window is resized
*/
function attach_window_resize_handlers()
{
	try
	{
		var windowResized = function() {
		//this function is executed when resizing is over 
			// reset the counter to -1; this ensures that the next time 'prepareResize' is called,
			// it will increment to zero; zero mod [_resizeTolerance] == 0 so prepareResize will run 
			// at least once and set up another windowResized.delay(500) to execute.
			_resizeEventCount = -1; 
			adjust_footer_size();
			adjust_content_size();
		};
		var prepareResize = function(){ 
			_resizeEventCount++; 
			if (_resizeEventCount % _resizeTolerance != 0) return; 
			$clear(_resizeTimeout); 
			_resizeTimeout = windowResized.delay(500);
		}; 
		window.addEvent('resize', prepareResize); 
	}
	catch (ex)
	{
		if (_DEBUG) alert("exception in attach_window_resize_handlers: "  + ex.message);
	}
}

