// JavaScript Document

//This does image rotator
var isVisible = true;
var images = new Array();
var count = Math.floor(Math.random()*4);
if (count == 4) {count = 0}

images[0] = "HomeRotator1EmbraceVirtue.jpg";
images[1] = "HomeRotator2CherishLearning.jpg";
images[2] = "HomeRotator3DiscoverTruth.jpg";
images[3] = "HomeRotator4All.jpg";

$(function() {

// start preloading
var imageObj = new Image();
for(i=0; i<=3; i++) {imageObj.src=FWResource + images[i];}

	$("#ImgHolder2").contents().attr("src", FWResource + images[count]);
	if (count+2 > images.length){
		$("#ImgHolder1").contents().attr("src", FWResource + images[0]);
	}
	else {
		$("#ImgHolder1").contents().attr("src", FWResource + images[count+1]);
	}
});
function toggleVisible() {
	
	
	if (isVisible)
	{
		$("#ImgHolder2").fadeOut(3000, function () {
			$(this).contents().attr("src", FWResource + images[count]);
		});
		isVisible = !isVisible;
		count += 1;
		if (count > images.length-1)
		{
			count = 0;
		}		
	}
	else
	{
		$("#ImgHolder2").fadeIn(3000, function () {
			$("#ImgHolder1").contents().attr("src", FWResource + images[count]);
		});
		isVisible = !isVisible;
		count += 1;
		if (count > images.length-1)
		{
			count = 0;
		}
	}
};
setInterval("toggleVisible()", 6000);


//Functions to toggle news and events viewing
function eventsToggle(){
	$('#eventsButton').attr('src', FWResource + 'HomeEventsButtonOn.jpg');
	$('#newsButton').attr('src', FWResource + 'HomeNewsButtonOff.jpg');
	$('#NewsEvents #News, #NewsShadow #News').fadeOut('fast',
		function () {
			$('#Events').fadeIn('fast',
				function () {
					//this.style.removeAttribute('filter');
				}
			);
		}
	);

}

function newsToggle(){
	$('#newsButton').attr('src', FWResource + 'HomeNewsButtonOn.jpg');
	$('#eventsButton').attr('src', FWResource + 'HomeEventsButtonOff.jpg');
	$('#NewsEvents #Events, #NewsShadow #Events').fadeOut('fast',
		function () {
			$('#News').fadeIn('fast',
				function () {
					//this.style.removeAttribute('filter');
				}
			);			
		}
	);
}
//Maybe this will auto toggle the news and events stuff
var isNews = 1;

function automateToggle() {
	if (isNews) {
		eventsToggle();
		isNews = 0;
	}
	else {
		newsToggle();
		isNews = 1;
	}
}
var IntervalId = self.setInterval("automateToggle()",7000);

//Functions to toggle news and events viewing form microsites
var MEventsOn = function MeventsToggle(){
	$('#MeventsButton').attr('src', FWResource + 'MicrositeEventsTop.png').unbind("click",MEventsOn);
	$('#MnewsButton').attr('src', FWResource + 'MicrositeNewsTopOff.png').bind("click",MNewsOn);
	$('#News').fadeOut('fast',
		function () {
			$('#Events').fadeIn('fast');	
		}
	);

}

var MNewsOn = function MnewsToggle(){
	$('#MnewsButton').attr('src', FWResource + 'MicrositeNewsTop.png').unbind("click",MNewsOn);
	$('#MeventsButton').attr('src', FWResource + 'MicrositeEventsTopOff.png').bind("click",MEventsOn);
	$('#Events').fadeOut('fast',
		function () {
			$('#News').fadeIn('fast');	
		}
	);
}
//This stuff runs at page load
$(function() {
	//Here be a login box animation
	
	//New click only
	$('.SignInOption').click(
		function () {
			$('.AnimSignin').filter('#SignInPopup').slideDown('slow').end().filter('#GreyFade').css({'height':$(document).height(), 'display':'block', 'opacity':0}).fadeTo('slow', 0.42);
		}
	);
	$('#GreyFade').click(
		function () {
			$('.AnimSignin').filter('#SignInPopup').slideUp('slow').end().filter('#GreyFade').fadeOut('slow');
		}
	);
	$('#CancelSignin').click(
		function () {
			$('.AnimSignin').filter('#SignInPopup').slideUp('slow').end().filter('#GreyFade').fadeOut('slow');
		}
	);
	//for the news and events toggle
	$('#eventsButton').click(function () {
		clearInterval(IntervalId);
		eventsToggle();
	});
	$('#newsButton').click(function () {
		clearInterval(IntervalId);
		newsToggle();
	});
	
	//This colors the news/events stuff
	$('#News').contents().find('.DialogBodyLabelFont').each(
		function (i) {
			$(this).removeClass('DialogBodyLabelFont').addClass('NewsColor' + i);
		}
	);
	$('#Events').contents().find('.DialogBodyLabelFont').each(
		function (i) {
			$(this).removeClass('DialogBodyLabelFont').addClass('Events' + i);
		}
	);
	
	//for the microsite news and events toggle
	$('#MeventsButton').click(MEventsOn);
	
	//Eh, fixa da nav bar!
	var topNavLength = $('#nav #NavigationCustom > ul').width();
	var numberOfChildren = $('#nav #NavigationCustom > ul').children().size();
	var menuItemWidth = (topNavLength/numberOfChildren)-1;
	var roundedWidth = Math.floor(menuItemWidth);
	//var menuItemWidth = (1/topNavLength)*100;
	//var widthAsPercent = menuItemWidth.toFixed(2);
	//$('#nav #NavigationCustom > ul').children().css({'width': widthAsPercent + '%'});
	$('#nav #NavigationCustom > ul').children().css({'width': roundedWidth + 'px'});
	
	//MyUD links expander
	$('#MyUDSectionList > li').click(
		function() {
			$('.MyUDSection > div').each(
				function() {
					$(this).fadeOut('fast').css({'display':'block','visibility':'hidden'});
				}			
			);
			$('.MyUDSection').each(
				function() {
					$(this).css('background','#fff');
				}
			);
			var StartingWidth = $(this).children().filter('span').attr('offsetWidth');
			var ListHeight = $(this).children().filter('div').attr('offsetHeight');
			var MarginHeight = (ListHeight/2+8)*-1;
			//alert(MarginHeight);
			$(this).css({'background':'url(' + FWResource + '/BracketRepeat.jpg) repeat-x center','width':StartingWidth}).animate({width:'278px'},'normal',
				function() {
					$(this).children().filter('div').css({'marginTop':MarginHeight,'visibility':'visible','display':'none'}).fadeIn('normal');
				}
			);
		}
	);
	var BiggestDiv = 0;
	$('.MyUDSection').children().filter('div').each(
		function() {
			if ($(this).attr('offsetHeight') > BiggestDiv) {
				BiggestDiv = $(this).attr('offsetHeight');
			}
		}
	);
	NewPadding = Math.floor(BiggestDiv/2)-30;
	$('#MyUDLists').css({'paddingTop':NewPadding,'paddingBottom':NewPadding});
	
	//Fun side menu thing
	var MenuNum = $('#SubPageNav > #NavigationCustom > ul > li').length;
	var MenuIndex = 1;
	var StepFade = function() {
			if (MenuIndex < MenuNum) {	
				$('#SubPageNav > #NavigationCustom > ul > li').eq(MenuIndex).animate({marginLeft:0}, 30,StepFade).fadeIn('normal');
				MenuIndex ++;
			}
	}
	$('#SubPageNav > #NavigationCustom > ul > li').css({'display':'none'}).eq(0).animate({marginLeft:0}, 30,StepFade).fadeIn('normal');
	
	//This one goes out to IE6!
	//4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)'
	var Version = navigator.appVersion;
	var isIE6 = Version.indexOf('MSIE 6.0');
	if(navigator.appName == 'Microsoft Internet Explorer' && isIE6 > 0)
	{
		$('.SignInField > div').addClass('SignInFieldDiv');
		$('.SignInField > div > input').addClass('SignInFieldDivInput');
		$('#nav #NavigationCustom > ul').addClass('navNavigationCustomUl');
		$('#nav #NavigationCustom > ul > li').addClass('navNavigationCustomUlLi');
		$('#nav #NavigationCustom > ul > li:first-child').addClass('navNavigationCustomUlLiFirstChild');
		$('#nav #NavigationCustom > ul > li').hover(
			function() {
				$(this).addClass('navNavigationCustomUlLiHover');
			},
			function() {
				$(this).removeClass('navNavigationCustomUlLiHover');
			}
		);
		$('#nav #NavigationCustom > ul > .CurrentPage').addClass('navNavigationCustomUlCurrentPage');
		$('#nav #NavigationCustom > ul > .ParentCurrentPage').addClass('navNavigationCustomUlParentCurrentPage');
		$('#nav #NavigationCustom > ul > li > span').addClass('navNavigationCustomUlLiSpan');
		$('#nav #NavigationCustom > ul > li > a').addClass('navNavigationCustomUlLiA');
		$('#nav #NavigationCustom > ul > li > ul').addClass('navNavigationCustomUlLiUl');
		$('#nav #NavigationCustom').css({'position':'fixed'});
		$('#nav #NavigationCustom > ul > li').hover(
			function() {
				$(this).children().filter('ul').addClass('navNavigationCustomUlLiHoverUl').css({'marginLeft':'0px'});
			},
			function() {
				$(this).children().filter('ul').removeClass('navNavigationCustomUlLiHoverUl').css({'marginLeft':'-10000px'});
			}
		);
		$('#nav #NavigationCustom > ul > li > ul > li').addClass('navNavigationCustomUlLiUlLi');
		$('.SigninMenu').hover(
			function() {
				$(this).children().filter('ul').css({'marginLeft':'0px'});
			},
			function() {
				$(this).children().filter('ul').css({'marginLeft':'-10000px'});
			}
		);
		$('#SignInOptions').css({'width':'266px','marginLeft':'90px'});
		$('#SignInButton').css({'width':'95px'});
		$('#SignInPopup > div').css({'paddintgLeft':'40px','paddingRight':'40px','paddingTop':'20px'});
		$('#PageContainer').css({'width':'901px'});
		$('#SubPageNav > #NavigationCustom > ul > li.BackNavLink').addClass('SubPageNavNavigationCustomUlLiBackNavLink');
		$('#SubPageNav #NavigationCustom ul li').css({'textIndent':'0px'});
		$('#MicrositeBottom > div').css({'position':'relative'});
		$('#MicrositePhotoGallery').css({'paddingLeft':'80px','paddingTop':'13px'});
		$('.MyUDSection > div').addClass('MyUDSectionDiv').css({'background-image':'url(' + FWResource + 'bracketRepeat.jpg)','background-repeat':'repeat-y','backgrount-position':'left'});
		$('.MyUDSection > div > div a').addClass('MyUDSectionDivDivA');
		NewPadding = Math.floor(BiggestDiv/2);
		$('#MyUDLists').css({'paddingTop':NewPadding,'paddingBottom':NewPadding});
		$('#MyUDTop').css({'overflow':'hidden'});
		$('.AltMicrositeContent > div > div:first-child').css({'overflow':'hidden'});
	}
//Because of IE6 funkyness we now have to hide things with incompatible css by default and then show it at page load
$('#nav').css({'display':'block'});
$('#SignInOptions').css({'display':'block'});
	
});
