﻿var Home = {
	noAnimate: true, // this gets changed

	Poller: {
		Poll: function() {
			Home.Poller.__counter++;
			if (Site.Settings.hasValue('homeTabStart')) {
				Home.Poller.Stop();
				Home.Show(Site.Settings.Values['homeTabStart']);
			}
			if (Home.Poller.__counter > 4) {
				Home.Poller.TimeOut();
			}
		},

		Stop: function() {
			clearInterval(Home.Poller.__PollerInterval);
		},

		Start: function() {
			Home.Poller.__counter = 0
			Home.Poller.__PollerInterval = setInterval(Home.Poller.Poll, 500);
		},

		TimeOut: function() {
			Home.Poller.Stop();
			Home.Show(Site.Settings.useDefault('homeTabStart'));
		},

		Init: function() {
			Home.Poller.Start();
		}
	},

	Init: function() {
		Home.Poller.Init();

		Site.PreLoadImages.LoadImages('/Assets/Images/home-portrait-philosophy.png', '/Assets/Images/home-portrait-approach.png', '/Assets/Images/home-portrait-programs.png', '/Assets/Images/home-portrait-cause.png', '/Assets/Images/home-portrait-student.png');
	},

	Show: function(startTab) {
		Home.HomeNav.Init(startTab);
		Home.HomeVid.Init();
		$('#homeTheme').fadeIn(400);
	},

	HomeNav: {
		On: function() { $(this).addClass('hover'); },

		Off: function() { $(this).removeClass('hover'); },

		HideTab: function(key) {
			if (key !== '')
				key = '.' + key;

			if (!Home.noAnimate) {
				$('ul.homeNav').find('li' + key + ' a')
                    .stop()
                    .animate(
                        {
                        	marginLeft: '0',
                        	left: '0',
                        	width: '128px' /* , Set width back to default */
                        }, 200, function() {
                        	$(this)
                                .removeClass('hover')
                                .removeClass('selected');
                        }
                    );
			} else {
				$('ul.homeNav').find('li' + key + ' a')
                    .css(
                        {
                        	marginLeft: '0',
                        	left: '0',
                        	width: '128px' /* , Set width back to default */
                        })
                    .removeClass('hover')
                    .removeClass('selected');
			}
		},

		ShowTab: function(key) {
			if (!Home.noAnimate) {
				$('ul.homeNav').find('li.' + key + ' a')
                    .addClass('selected')
                    .stop()
                    .animate(
                    {
                    	marginLeft: '-90px',
                    	left: '50%',
                    	width: '148px'
                    }, 200);
			}
			else {
				$('ul.homeNav').find('li.' + key + ' a')
                    .css(
                    {
                    	marginLeft: '-90px',
                    	left: '50%',
                    	width: '148px'
                    })
                    .addClass('selected');
			}
		},

		HideHeadline: function(key) {
			if (key !== '')
				key = '.' + key;

			if (!Home.noAnimate) {
				$('.headlineViewPort img' + key)
                    .animate({ left: '-924px', opacity: 0 }, 300, function() {
                    	$(this).css({ 'left': '924px' });
                    });
			}
			else {
				$('.headlineViewPort img' + key).css({ 'left': '924px' });
			}
		},

		ShowHeadline: function(key) {
			if (!Home.noAnimate) {
				$('.headlineViewPort img.' + key).animate({ left: '0', opacity: 1 }, 300);
			}
			else {
				$('.headlineViewPort img.' + key).css({ left: '0', opacity: 1 }, 300);
			}
		},

		HideContent: function(key) {
			if (key !== '')
				key = '#' + key;
			else
				key = '.home';

			if (!Home.noAnimate) {
				$(key + 'Content')
                    .animate({ left: '-558px', opacity: 0 }, 300, function() {
                    	$(this).css({ 'left': '558px' });
                    });
			}
			else {
				$(key + 'Content').css({ 'left': '558px' });
			}
		},

		ShowContent: function(key) {
			if (!Home.noAnimate) {
				$('#' + key + 'Content')
                    .css({ opacity: 0 })
                    .animate({ left: '0', opacity: 1 }, 300);
			}
			else {
				$('#' + key + 'Content').css({ left: '0' }, 300).fadeIn();
			}
		},

		AnimatePortrait: function(key, current) {
			var duration = 0;
			if (jQuery.support.opacity) { // if not ie then make duration longer
				duration = 350;
			}
			if ($("html").hasClass("ie6")) {
				duration = 0;
			}

			$('.portrait')
                .fadeOut(duration, function() {

                	//higher level container class
                	var previousState = 'home-state-' + current;
                	var currentState = 'home-state-' + key;
                	$('.wrapper').removeClass(previousState).addClass(currentState);
                	$(this)
                        .css({ 'background-image': 'url(/Assets/Images/home-portrait-' + key + '.png)' });

                	//hack to fix ie6 issue. only do fade if no IE6
                	if ($("html").hasClass("ie6")) {
                		$(this).show();
                	} else {
                		$(this).fadeIn(duration);
                	}

                });
		},

		HidePortrait: function() {
			$('.portrait').hide();
		},

		ShowPortrait: function(key) {
			$('.portrait')
                .css({ 'background-image': 'url(/Assets/Images/home-portrait-' + key + '.png)' })
                .show();
		},

		Show: function(key) {
			// don't animate if already selected
			if (Home.HomeNav.__CURRENT !== key) {

				/* change the size of the tab */
				Home.HomeNav.HideTab(Home.HomeNav.__CURRENT);
				Home.HomeNav.ShowTab(key);

				Home.HomeNav.AnimatePortrait(key, Home.HomeNav.__CURRENT);


				/* change the headline, content and portrait */
				Home.HomeNav.HideHeadline(Home.HomeNav.__CURRENT);
				Home.HomeNav.ShowHeadline(key);

				Home.HomeNav.HideContent(Home.HomeNav.__CURRENT);
				Home.HomeNav.ShowContent(key);

				// change the outter most box class to match the 'animated-to' state
				$('#homeTheme').removeClass(Home.HomeNav.__CURRENT).addClass(key);

			}

			Home.HomeNav.__CURRENT = key;
		},

		HideAll: function() {
			Home.HomeNav.HideTab('', true);
			Home.HomeNav.HideHeadline('', true);
			Home.HomeNav.HideContent('', true);
			Home.HomeNav.HidePortrait();
		},

		ShowAll: function(key) {
			Home.HomeNav.ShowTab(key, true);
			Home.HomeNav.ShowHeadline(key, true);
			Home.HomeNav.ShowContent(key, true);
			Home.HomeNav.ShowPortrait(key);
			Home.HomeNav.__CURRENT = key;

			// if not ie6, the DO animation, otherwise, don't EVER (ie6)
			if ($("html").hasClass("ie6") === false) {
				Home.noAnimate = false;
			}

		},

		Click: function(e) {
			/* dont' do anything if tab is already selected */
			if (!$(this).hasClass('selected')) {
				var strClass = $(this).parent().attr('class');
				Home.HomeNav.Show(strClass);
			}
		},

		Next: function() {
			if (Home.HomeNav.__CURRENT === Home.HomeNav.__START)
				Home.HomeNav.__LOOPCOUNT++;

			if (Home.HomeNav.__LOOPCOUNT === Home.HomeNav.__ANI_CYCLES) {
				Home.HomeNav.Stop();
			}
			else {
				switch (Home.HomeNav.__CURRENT) {
					case 'philosophy':
						Home.HomeNav.Show('approach');
						break;
					case 'approach':
						Home.HomeNav.Show('programs');
						break;
					case 'programs':
						Home.HomeNav.Show('cause');
						break;
					case 'cause':
						Home.HomeNav.Show('student');
						break;
					case 'student':
						Home.HomeNav.Show('philosophy');
						break;
					default:
						Home.HomeNav.HideAll()
						Home.HomeNav.Show(Home.HomeNav.__START);
						break;
				}
			}
		},

		Animate: function() {
			Home.HomeNav.__LOOP = setInterval(Home.HomeNav.Next, 5000);
		},

		Stop: function() {
			clearInterval(Home.HomeNav.__LOOP);
			Home.HomeNav.__LOOPCOUNT = -1;
		},

		Init: function(startTab) {


			// stop any animations and set state back to default
			Home.HomeNav.Stop();
			Home.HomeNav.HideAll();
			switch (startTab) {
				case 'philosophy':
					Home.HomeNav.__START = startTab;
					break;
				case 'approach':
					Home.HomeNav.__START = startTab;
					break;
				case 'programs':
					Home.HomeNav.__START = startTab;
					break;
				case 'cause':
					Home.HomeNav.__START = startTab;
					break;
				case 'student':
					Home.HomeNav.__START = startTab;
					break;
				default:
					Home.HomeNav.__START = 'philosophy';
					break;
			}

			$('.wrapper').addClass('home-state-' + Home.HomeNav.__START);

			Home.HomeNav.ShowAll(Home.HomeNav.__START);

			var $homeNav = $('ul.homeNav li a');

			//console.log($homeNav);

			if ($homeNav.length > 0) {
				$homeNav
                    .voidLink()
                    .click(Home.HomeNav.Click)
                    .hover(Home.HomeNav.On, Home.HomeNav.Off);

				// start the looping animation
				if (Site.Settings.hasValue('tabCycles'))
					Home.HomeNav.__ANI_CYCLES = Site.Settings.Values['tabCycles'];
				else
					Home.HomeNav.__ANI_CYCLES = Site.Settings.useDefault('tabCycles');

				Home.HomeNav.Animate();

				$('.home').click(function(e) { Home.HomeNav.Stop(); });
			}
		}
	},

	HomeVid:
	{
		Init: function() {
			$('.home .launchVid').click(function(e) {
				Home.HomeNav.Stop();
				Home.HomeVid.Show();
				return false;
			});
			$('a.closeVid').click(function(e) {
				Home.HomeVid.Close();
				return false;
			});
		},

		Show: function() {
			$('html').animate({ scrollTop: 0 });

			$('body').append($('.flashOverlay'));
			$('body').append($('.flashMsg'));

			$('.flashOverlay').fadeIn('normal', function() { $('.flashMsg').show('normal', function() { $('.addThisButton').show(); $('#flashIntroContent').fadeIn('fast'); }); });

		},

		Close: function() {
			$('.addThisButton').hide();
			$('#flashIntroContent').fadeOut('fast', function() { $('.flashMsg').hide('normal', function() { $('.flashOverlay').fadeOut('fast') }) });
			setTimeout(function() {
				$('#flashIntroContent').html('')
			}, 2000);
		}
	}
};

/* add Home.Init() to jQuery document.ready */
$(function() {
   Home.Init();
});


