// jQuery scripts
var winH;
var timeDelay;
var timerOn = 0;

$(document).ready(function() {

	// Pop-ups
	$('.emailPopup').colorbox({ innerWidth: "350px", innerHeight: "250px", iframe: true });
	// $('.popupSignUp .SubscriptionButton').onClick();

	// Transition effects home.
	// affect runs once on page load, revealing content
	var baseDelay = 2500;
	$('.flipper1').delay(baseDelay).fadeOut(1);
	$('.flipper2').delay(baseDelay + 600).fadeOut(1);
	$('.flipper3').delay(baseDelay + 400).fadeOut(1);
	$('.flipper4').delay(baseDelay + 200).fadeOut(1);
	$('.flipper5').delay(baseDelay + 1000).fadeOut(1);
	$('.flipper6').delay(baseDelay + 800).fadeOut(1);

	$('.homeRotator1').delay(baseDelay + 20).queue(function() {
		var childCount = $(this).children().length;
		if (childCount < 2) { $('.homeRotator1Item').css('display', 'block') };
		$(this).cycle({
			fx: 'fade',
			speed: 2500,
			delay: 3000,
			pause: 1
		});
	});
	$('.homeRotator2').delay(baseDelay + 200).queue(function() {
		var childCount = $(this).children().length;
		if (childCount < 2) { $('.homeRotator2Item').css('display', 'block') };
		$(this).cycle({
			fx: 'fade',
			speed: 2500,
			delay: 3500,
			pause: 1
		});
	});
	$('.template4 .homeBottomBox').delay(baseDelay + 200).queue(function() {
		var childCount = $(this).children().length;
		if (childCount < 2) { $('.homeRotator3Item').css('display', 'block') };
		$(this).cycle({
			fx: 'fade',
			speed: 2500,
			delay: 4000,
			pause: 1
		});
	});
	$('.homeNewsRotator').delay(baseDelay + 800).queue(function() {
		$('.homeNewsRotatorItems').cycle({
			pause: 1,
			fx: 'scrollUp',
			speed: 2000
		});
	});
	/*	ORIGINAL FADING TRANSITIONS - RETIAIN FOR POSSIBLE REINSTATEMENT
	$('.flipper1').cycle({
	delay: baseDelay,
	fx: 'fade',
	speed: '250',
	continuous: 1,
	autostop: 1,
	autostopCount: 2,
	end: onEnd1
	});
	$('.flipper2').cycle({
	delay: (baseDelay + 1000),
	fx: 'fade',
	speed: '250',
	continuous: 1,
	autostop: 1,
	autostopCount: 2,
	end: onEnd2
	});
	$('.flipper3').cycle({
	delay: baseDelay + 500,
	fx: 'fade',
	speed: '250',
	continuous: 1,
	autostop: 1,
	autostopCount: 2,
	end: onEnd3
	});
	$('.flipper4').cycle({
	delay: baseDelay + 250,
	fx: 'fade',
	speed: '250',
	continuous: 1,
	autostop: 1,
	autostopCount: 2,
	end: onEnd4
	});
	$('.flipper5').cycle({
	delay: baseDelay + 1250,
	fx: 'fade',
	speed: '250',
	continuous: 1,
	autostop: 1,
	autostopCount: 2,
	end: onEnd5
	});
	$('.flipper6').cycle({
	delay: baseDelay + 750,
	fx: 'fade',
	speed: '250',
	continuous: 1,
	autostop: 1,
	autostopCount: 2,
	end: onEnd6
	});
	function onEnd1() {
	$('.flipper1').fadeOut('slow', function() {
	$(this).remove();
	});
	};
	function onEnd2() {
	$('.flipper2').fadeOut('slow', function() {
	$(this).remove();
	});
	};
	function onEnd3() {
	$('.flipper3').fadeOut('slow', function() {
	$(this).remove();
	});
	};
	function onEnd4() {
	$('.flipper4').fadeOut('slow', function() {
	$(this).remove();
	});
	};
	function onEnd5() {
	$('.flipper5').fadeOut('slow', function() {
	$(this).remove();
	});
	};
	function onEnd6() {
	$('.flipper6').fadeOut('slow', function() {
	$(this).remove();
	});
	};
	*/
	//fade in content rotators after flip reveal
	/*	
	$('.homeRotator1').delay(baseDelay).fadeIn('slow', function() {
	var childCount = $(this).children().length;
	if (childCount < 2) { $('.homeRotator1Item').fadeIn('slow') };
	$(this).cycle({
	fx: 'fade',
	speed: 2500,
	delay: 4000,
	pause: 1
	});
	});
	$('.homeRotator2').delay(baseDelay + 100).fadeIn('slow', function() {
	var childCount = $(this).children().length;
	if (childCount < 2) { $('.homeRotator2Item').fadeIn('slow') };
	$(this).cycle({
	fx: 'fade',
	speed: 2500,
	delay: 5000,
	pause: 1
	});
	});
	$('.template4 .homeBottomBox').delay(baseDelay + 500).fadeIn('slow', function() {
	var childCount = $(this).children().length;
	if (childCount < 2) { $('.homeRotator3Item').fadeIn('slow') };
	$(this).cycle({
	fx: 'fade',
	speed: 2500,
	delay: 5000,
	pause: 1
	});
	});
	$('.NewsBox').delay(baseDelay + 500).fadeIn('slow'),
	$('.InvestBox').delay(baseDelay + 250).fadeIn('slow'),
	$('.EventsBox').delay(baseDelay + 1250).fadeIn('slow'),
	$('.homeNewsRotator').delay(baseDelay + 750).fadeIn('slow', function() {
	$('.homeNewsRotatorItems').cycle({
	pause: 1,
	fx: 'scrollUp',
	speed: 2000
	});
	});
	// END ARCHIVED HOME PAGE FADE TRANSITIONS
	*/
	$('.NewsBox').children('.boxLink').hide();
	$('.NewsBox').hover(
	  function() {
	  	$(this).children('.boxTitle').fadeOut('fast');
	  	$(this).children('img').fadeTo('fast', 0.5);
	  	$(this).children('.boxLink').fadeIn('fast');
	  },
	  function() {
	  	$(this).children('.boxTitle').fadeIn('fast');
	  	$(this).children('img').fadeTo('fast', 1.0);
	  	$(this).children('.boxLink').fadeOut('fast');
	  });
	$('.InvestBox').children('.boxLink').hide();
	$('.InvestBox').hover(
	  function() {
	  	$(this).children('.boxTitle').fadeOut('fast');
	  	$(this).children('img').fadeTo('fast', 0.5);
	  	$(this).children('.boxLink').fadeIn('fast');
	  },
	  function() {
	  	$(this).children('.boxTitle').fadeIn('fast');
	  	$(this).children('img').fadeTo('fast', 1.0);
	  	$(this).children('.boxLink').fadeOut('fast');
	  });
	$('.homeRotator1').hover(
	  function() {
	  	$(this).find('ul').fadeTo('fast', 1.0);
	  },
	  function() {
	  	$(this).find('ul').fadeTo('fast', 0.9);
	  });
	$('.homeRotator2').hover(
	  function() {
	  	$(this).find('ul').fadeTo('fast', 1.0);
	  },
	  function() {
	  	$(this).find('ul').fadeTo('fast', 0.9);
	  });
	$('.articleTbl table').attr('cellspacing', '10');

	//News Rotator
	$('.newsRotator, .whatsNew-rotator').cycle({
		fx: 'fade',
		speed: 2000,
		pause: 1
	});

	//Footer Rotator
	$('.footerBox1').cycle({
		pause: 1,
		fx: 'scrollUp',
		speed: 3000,
		timeout: 6000
	});

	// Fixed Frame when scrolling
	fixFrame();

	// FAQ click killer
	$('.faqQ').click(function() {
		return false;
	});

	// IMG Headers
	if ($('.imgHeading img').length) { $('body').addClass('hasImgHdr'); };

});

function fixFrame() {
	//alert('fix frame ran');
	var EditMode = $('body').hasClass('EditMode');
	var DesignMode = $('body').hasClass('DesignMode');
	var minH = 550; // minimum content area height
	var winInHeight = window.innerHeight; // used for non-IE & IE9+
	var ieHeight = document.documentElement.clientHeight; // used for IE8-
	if (EditMode == true || DesignMode == true) {
		// do nothing in Edit or Design modes
	} else if ($('.rightSidebar').length) {
		// when right sidebar is present fix the page height.
		// when length of right sidebar has been scrolled:
		// set header, left sidebar, right sidebar & footer to fixed
		$(window).load(function() {
			$(window).scrollTop(0); // fixes issues when AJAX reloads the page in the middle
			// wait on last sidebar image to load for accurate measurements
			var rsbarO = $('.rightSidebar').offset().top;
			var rsbarH = $('.rightSidebar').height();
			var contH = $('.pageContent').height();
			var rootH = $('.rootPageContent').height();
			var lsbarH = 0;
			if ($('.leftSidebar').length) { lsbarH = $('.leftSidebar').height(); };
			var frameRH = rsbarO + rsbarH; // Y coord of bottom R sidebar
			var frameLH = rsbarO + lsbarH; // Y coord of bottom L sidebar
			var frameH = Math.max(frameRH, frameLH, minH); // find largest value betwee Min, Left & Right
			var winH = $(window).height();
			if (contH > rsbarH || rootH > rsbarH) {
				// if conent > sidebar
				$('.globalContainer').addClass('fixedFooter');
				$('.globalFooter-links').css('top', frameH);
				var scrollDif = (frameH + 50) - winH;
				if (scrollDif < 0) {
					// window is larger than content frame
					// add bottom border to hide trailing content
					var btmBrdr = Math.abs(scrollDif) + 'px solid #fff';
					var contPad = (Math.abs(scrollDif) + 41) + 'px';
					$('.globalFooter-links').css('border-bottom', btmBrdr);
					$('body').addClass('scrollCrazy');
					$('.subPageContent, .rootPageContent').css('padding-bottom', contPad);
					var i = 0;
					window.onresize = function() {
						i++;
						if (i > 1) {
							// doc resizes once when padding content
							// on second+ resize reload the page
							var newIeHeight = document.documentElement.clientHeight;
							if (winInHeight != null) {
								window.location = window.location;
							} else if (ieHeight != newIeHeight) {
								window.location = window.location;
							};
						};
					};
				} else if (scrollDif > 0) {
					// window is smaller than content frame
					// allow scrolling until reaching scrollDiff then Fix positions
					var fixHead = $('.header').offset().top - scrollDif;
					var fixCtop = $('.contentTop').offset().top - scrollDif;
					var fixLsdb;
					if ($('.leftSidebar').length) { fixLsdb = $('.leftSidebar').offset().top - scrollDif; };
					var fixRsdb = $('.rightSidebar').offset().top - scrollDif;
					var fixFoot = $('.globalFooter-links').offset().top - scrollDif;
					var initFoot = $('.globalFooter-links').offset().top;
					$(window).scroll(function() {
						var newScroll = $(window).scrollTop();
						if (newScroll >= scrollDif) {
							$('body').addClass('scrollFixed');
							$('.header').css('top', fixHead);
							$('.contentTop').css('top', fixCtop);
							$('.leftSidebar').css('top', fixLsdb);
							$('.rightSidebar').css('top', fixRsdb);
							$('.globalFooter-links').css('top', fixFoot);
						} else {
							$('body').removeClass('scrollFixed');
							$('.globalFooter-links').css('top', initFoot);
						};
					});
					window.onresize = function() {
						var newIeHeight = document.documentElement.clientHeight;
						if (winInHeight != null) {
							window.location = window.location;
						} else if (ieHeight != newIeHeight) {
							window.location = window.location;
						};
					};
				};
			} else {
				$('body').removeClass('scrollCrazy');
				$('body').removeClass('scrollFixed');
				$('.globalContainer').removeClass('fixedFooter')
			};
		});
	}
}
