(function($){

	var heroSlideShowActive = true;
	var noticesActive = true;
	var modalCntr;

	initUIDateTime = function() {
	
		var d = new Date();
		$('#yearNow').html(d.getFullYear());
	
	}

	initUILayout = function() {
		
		// If there's only one image and it isn't 'active' make it active
		if($('.heroImg').length == 1 && !$('.heroImg').hasClass("active")){
			$('.heroImg').addClass("active");
		}
		
		var minDocSize;
		
		// Sets minDocSize so that the layout isn't resized to anything smaller than the fixed content of the page
		if(location.href.indexOf("Home.aspx") > -1){
			minDocSize = 1080;
		}else{
			minDocSize = 940;
		}

		var hCntBx = $('.heroBox');
		var hImg;
		var hImgH;
		var hCntBxH;
		var hCntBxP;		
	
		// Adjusts layout
		function updateHero() {
			if($(window).width() < minDocSize){
				$('#wrpOutr_main').width($(document).width());
			}else{
				$('#wrpOutr_main').css('width','100%');			
			}
			hImgH = hImg.outerHeight();
			hCntBxH = hCntBx.outerHeight();
			hCntBxP = Math.round((hImgH/2)-(hCntBxH/2));
			hCntBx.css('top',hCntBxP + 'px');		
		}
		
		// Adjust layout when browser is resized
		$(window).resize(function(){
			updateHero();		
		});
	
		// Adjust layout once first image is loaded
		var firstHImg;
		
		if($('.heroImg').length > 0){
			firstHImg = $('.heroImg').first();
			hImg = $('.heroImg');
		}else if($('.heroInnerImg').length > 0){
			//alert('.heroInnerImg loaded');
			firstHImg = $('.heroInnerImg').first();
			hImg = $('.heroInnerImg');
		}
	
		firstHImg.load(function(){
			updateHero();
		});
			
	}
	
	initUIHeroSlideShow = function() {
	
		if($('.heroImg').length > 1){
	
			var heroSlides = new Array();
		
			$('.heroImg').each(function(i) {
				heroSlides.push($(this));
			});		
			
			var slideSpeed = 5000;
			var curIndex = 0;
			var nextIndex = 0;
			var indexes = heroSlides.length - 1;
						
			updateUIHeroSlideShow = function(){
			
				if(heroSlideShowActive){
			
					if(curIndex < indexes){
						nextIndex = curIndex + 1;
					}else{
						nextIndex = 0;
					}		
												
					var curSlide = heroSlides[curIndex];
					var nextSlide = heroSlides[nextIndex];
					
					nextSlide.css('display','block');
							
					curSlide.fadeOut('slow', function() {
				
						nextSlide.addClass('active');
						curSlide.removeClass('active');
				
						curIndex = nextIndex;
						setTimeout("updateUIHeroSlideShow()", slideSpeed);

					});
					
				}

			}
					
			setTimeout("updateUIHeroSlideShow()", slideSpeed);
			
		}
	
	}
	
	initUINotices = function() {
	
		var ticker = $('#noticeTicker');
		var	tickerWdth = 940;
		var tickerCover = $('#noticeTickerCover');
		var tickerCoverWdth = 0;
		var tickerSpeedRatio = 0.05;
		var tickerSpeed = 0;
		
		$('#noticeTickerCover .tickerNotice').each(function(i) {
			tickerCoverWdth += $(this).outerWidth(true);
		});
	
		tickerCover.width(tickerCoverWdth);
		tickerCover.draggable({ axis: "x" });

		tickerSpeed = Math.round((tickerCoverWdth)/tickerSpeedRatio);
	
		animateUINotices = function() {
		
			if(noticesActive){

				tickerCover.stop(true,true).animate({left: -tickerCoverWdth}, tickerSpeed, 'linear', function() {
					tickerCover.css('left', tickerWdth);
					setTimeout("animateUINotices()",250);
				});
				
			}
				
		}
		
		animateUINotices();
		
		ticker.mouseenter(function() {
			tickerCover.stop(true,false);
		});

		ticker.mouseleave(function() {
			animateUINotices();
		});		
	
	}

	initUIModal = function() {
		
		//modalCntr is a global var(see top)
		var overlay = $('#overlay');
		var docH = $(document).height();
		
		$('.modal_link').each(function(index) {

			$(this).click(function() {
					
				modalCntrSlctStr = '#modalContainer_' + $(this).attr('id').substr(11,2);
				modalCntr = $(modalCntrSlctStr);
				
				heroSlideShowActive = false;
				noticesActive = false;
				$('#noticeTickerCover').stop();	
			
				overlay.height(docH);
			
				overlay.fadeIn(function() {
					modalCntr.fadeIn();
				});
		
			});	
		
		});
				
		overlay.click(function() {
		
			modalCntr.hide();
			overlay.hide();
			heroSlideShowActive  = true;
			noticesActive = true;
			updateUIHeroSlideShow();
			animateUINotices();
		
		});
	
	}

	initUIPortalNavs = function(_portalItem) {
	
		var portalItems = $('.cntSub_prtl_itm');
		
		if(_portalItem){
			_portalItem.find('.cntSub_prtlNav_hdr .cntSub_prtlNav_lnk').click(function() {showPortalNav(_portalItem);});
			_portalItem.find('.cntSub_prtlNavTab_hdr .cntSub_prtlNav_lnk').click(function() {hidePortalNav(_portalItem);});				
		}else{
		
			portalItems.each(function (i) {
				var	portalItem = $(this);
				portalItem.find('.cntSub_prtlNav_hdr .cntSub_prtlNav_lnk').click(function() {showPortalNav(portalItem);});
				portalItem.find('.cntSub_prtlNavTab_hdr .cntSub_prtlNav_lnk').click(function() {hidePortalNav(portalItem);});		
			});
			
		}
		
	}
	
	showPortalNav = function(_portalItem) {
	
		clearTimeout(t);
	
		var portalNav = _portalItem.find('.cntSub_prtlNav');
		
		if(_portalItem.attr('id') == 'cntSub_prtl_itm_01'){
			$('#cntSub_prtl_sldr').css('height', '460');
		}
	
		scrollPageDown();
		portalNav.fadeIn();
		
	}
	
	hidePortalNav = function(_portalItem) {
	
		clearTimeout(t);
	
		var portalNav = _portalItem.find('.cntSub_prtlNav');	
	
		if(_portalItem.attr('id') == 'cntSub_prtl_itm_01'){
			$('#cntSub_prtl_sldr').animate({height: '305'},500);
		}
		portalNav.fadeOut();
	
	}
	
	initUIPortalSlider = function() {
	
		var sldrCover = $('#cntSub_prtl_sldrCover');
		var sldrCoverStrtPos = 20;
		var numSldrItems = $('.cntSub_prtl_itm').length;
		var sldrItemWdth = 324;
		var sldrPrevBtn = $('#cntSub_prtl_btn_prev');
		var sldrNxtBtn = $('#cntSub_prtl_btn_next');
		
		var sldrSpeed = 5000; // Should be > than sldrAnimSpeed to function correctly
		var sldrAnimSpeed = 600;
		
		var sldrBtnAnimPos = '-40';
		var sldrBtnAnimToPos = '0';
		var sldrBtnAnimSpeed = 100;
		var sldrCoverWdth = sldrItemWdth*numSldrItems;
		var sldrBusy = false;
		
		sldrCover.width(sldrCoverWdth);
		
		sldrPrevBtn.click(function() {
			$(this).addClass('active');
			$(this).stop(true,false).animate({right: sldrBtnAnimToPos}, sldrBtnAnimSpeed, function() {
				$(this).animate({right: sldrBtnAnimPos}, sldrAnimSpeed, function() {
					$(this).removeClass('active');
				});				
				prevSlide();
			});
		});
		
		sldrNxtBtn.click(function() {
			$(this).addClass('active');
			$(this).stop(true,false).animate({left: sldrBtnAnimToPos}, sldrBtnAnimSpeed, function() {
				$(this).animate({left: sldrBtnAnimPos}, sldrAnimSpeed, function() {
					$(this).removeClass('active');
				});		
				nextSlide();
			});
		});
		
		prevSlide = function() {
		
			clearTimeout(t);
			
			if(!sldrBusy){
			
				var slideEnd = $('.cntSub_prtl_itm:last');
				var slideOnetoEnd = $('.cntSub_prtl_itm:nth-child(3)');
				var slideEndCnt = slideEnd.wrap('<div></div>').parent().html();
				sldrBusy = true;

				hidePortalNav(slideOnetoEnd);				
				
				slideEnd.parent().remove();
				slideEnd.prependTo(sldrCover);
				sldrCover.css('left', '-' + (sldrItemWdth - sldrCoverStrtPos) + 'px');
				sldrCover.animate({left: sldrCoverStrtPos}, sldrAnimSpeed, function() {
					sldrBusy = false;
					initUIPortalNavs(slideEnd);
				});
			
			}
			
		}

		nextSlide = function() {
		
			clearTimeout(t);
			
			if(!sldrBusy){
			
				var slideEnd = $('.cntSub_prtl_itm:first');
				var slideEndCnt = slideEnd.wrap('<div></div>').parent().html();	
				sldrBusy = true;

				hidePortalNav(slideEnd);
				
				sldrCover.animate({left: '-' + (sldrItemWdth - sldrCoverStrtPos)}, sldrAnimSpeed, function() {
					sldrCover.css('left', sldrCoverStrtPos);
					slideEnd.parent().remove();
					slideEnd.appendTo(sldrCover);
					sldrBusy = false;
					initUIPortalNavs(slideEnd);					
				});
			
			}
			
		}
				
		updatePortalSlider = function() {
		
			nextSlide();		
			t = setTimeout("updatePortalSlider()", sldrSpeed);
			
		}
		
		t = setTimeout("updatePortalSlider()", sldrSpeed);
		
	}

	initUIFooter = function() {

		$('#ftr').addClass('js');
	
		var fTab = $('#ftrTabLnk');
		var fSitemap = $('#pageSiteMap');
		
		fTab.click(function() {
		
			if(!fSitemap.hasClass('open')){scrollPageDown();}
			
			fSitemap.slideToggle(function() {
				fSitemap.toggleClass('open');
			});
			
		});
		
	}
	
	scrollPageDown = function() {
		var bH = $('html,body');
		var bScrl = $(document).height();
		bH.stop(true,true).animate({scrollTop:bScrl}, 1000);		
	}
		
})(jQuery);
