$('html').removeClass('no-js').addClass('has-js');

var _speed = 500;
jQuery.easing.def = "easeInOutCubic";
var _currentNewsSection = null;
var _currentNewsArticle = null;
var _currentSpotlight = null;
var $_currentSlide = null;
var _slideshowInterval = null;


// Variables for news scrolling
var _viewportHeight = 0;
var _topOffset = 0;
var _articles = null;
var _topArticleIdx = 0;
var _listHeight = 0;
var _newsTop = 0;
var _scrollLocked = false;


function loadNewsArticle(sectionNum, articleNum) {
	var article = '';

	article += '<h3>' + $('#news-list li a').eq(articleNum).html() + '</h3>';
	article += $('#newsSection' + sectionNum + ' .article').eq(articleNum).html();
	if (article == "<h3>null</h3>null") {
		article = '';
	}
	$('#news-widget .article').html(article);
}

function loadNewsSection(sectionNum) {
	var articles = '';
	$('#newsSection' + sectionNum + ' > li > a').each(function (count) {
		articles += '<li><a href="' + $(this).attr('href') + '">' + $(this).html() + '</a></li>';
	});
	$('#news-list > ul').html(articles).css({ top: 0 });
	$('#news-list .get-more').css({ top: 0 });
	$('.news-nav').removeClass('visible');

	_newsTop = $('#news-list').offset().top;
	_viewportHeight = $('#news-list').outerHeight();
	_articles = $('#news-list ul li');
	_topArticleIdx = 0;
	_topOffset = 0;
	_listHeight = $('#news-list ul').outerHeight(true) + $('#news-list .get-more').outerHeight(true);
	_articles.eq(0).addClass('top');

	if (_listHeight > _viewportHeight) {
		$('#news-down').addClass('visible');
	}
}

function initNews() {
	$('#news-list a.get-more').html('Loading news...');
	var sections = '';
	$('#hidden-news .newsSection').each(function (count) {
		sections += '<li><a href="#">' + $(this).children('h3').html() + '</a></li>';
		$(this).children('ul').attr('id', 'newsSection' + count);
	});
	$('#news-widget .nav ul').html(sections);

	$('#news-list').after('<span id="news-up" class="news-nav">&uarr;</span><span id="news-down" class="news-nav">&darr;</span>');

	_currentNewsSection = 0;
	loadNewsSection(0);
	$('#news-widget .nav li').eq(0).addClass('selected');
	$('#news-list a.get-more').html('MORE NEWS');
}

function collapseNews() {
	$('#news-widget').removeClass('expanded').addClass('collapsed');
	$('#news-widget .section').stop().animate({ marginLeft: '-233px' }, _speed);
	$('#news-widget .article').stop().animate({ marginLeft: '-415px' }, _speed);
	$('#news-widget .selected').removeClass('selected');
}
function expandNews(sectionNum, articleNum) {
	collapseEvents();
	collapseSpotlight();
	$('#news-widget').removeClass('collapsed').addClass('expanded');

	if (_currentNewsSection != sectionNum) {
		loadNewsSection(sectionNum);
	}
	loadNewsArticle(sectionNum, articleNum);

	_currentNewsSection = sectionNum;
	_currentNewsArticle = articleNum;

	$('#news-widget li.selected').removeClass('selected');
	var section = $('#news-widget .nav li').eq(sectionNum);
	section.addClass('selected');
	var article = $('#news-widget .section li').eq(articleNum);
	article.addClass('selected');

	$('#news-widget .section').stop().animate({ marginLeft: '0' }, _speed);
	$('#news-widget .article').stop().animate({ marginLeft: '0' }, _speed);
}


function loadEvent(eventNum) {
	var details = '';

	details += '<h3>' + $('#hidden-events > li > a').eq(eventNum).clone().find('strong').remove().end().html() + '</h3>';
	details += $('#hidden-events .event-details').eq(eventNum).html();
	$('#events-widget .article').html(details);
}
function initEvents() {
	$('#events-widget .get-more').html('Loading events...');
	var events = '';
	$('#hidden-events > li > a').each(function () {
		events += '<li><a href="' + $(this).attr('href') + '">' + $(this).html() + '</a></li>';
	});

	$('#events-widget .nav ul').html(events);
	$('#events-widget .get-more').html('MORE EVENTS');
}
function collapseEvents() {
	$('#events-widget').removeClass('expanded').addClass('collapsed');
	$('#events-widget .nav').stop().animate({ width: '65px' }, _speed);
	$('#events-widget .article').stop().animate({ marginLeft: '-468px' }, _speed);
	$('#events-widget .selected').removeClass('selected');
}
function expandEvents(eventNum) {
	collapseNews();
	collapseSpotlight();
	$('#events-widget .nav').stop().animate({ width: '245px' }, _speed);
	$('#events-widget .article').stop().animate({ marginLeft: '0' }, _speed);

	loadEvent(eventNum);

	$('#events-widget .selected').removeClass('selected');
	$('#events-widget .nav li').eq(eventNum).addClass('selected');
	$('#events-widget').removeClass('collapsed').addClass('expanded');
}


function loadSpotlight(spotlightNum) {
	var section = "";
	var $s = $('#hidden-spotlight > li').eq(spotlightNum);
	section += '<a href="' + $s.children('a').attr('href') + '">';
	section += $s.children('.full-size').html();
	section += '<span>' + $s.children('a').html() + '</span>';
	section += '</a>';

	_currentSpotlight = spotlightNum;

	$('#spotlight-widget .section').html(section);
	$('#spotlight-widget .article').html('<div>' + $s.children('.spotlight-details').html() + '</div>');
}
function initSpotlight() {
	$('#spotlight-widget .get-more').html('Loading spotlight...');

	var spotlights = '';
	$('#hidden-spotlight > li').each(function (count) {
		spotlights += '<li><a href="' + $(this).children('a').attr('href') + '">' + $(this).children('.thumbnail').html() + '</a></li>';
	});

	$('#spotlight-widget .nav ul').html(spotlights);
	$('#spotlight-widget .nav li').eq(0).addClass('selected');
	loadSpotlight(0);
}
function collapseSpotlight() {
	$('#spotlight-widget').removeClass('expanded').addClass('collapsed');
	$('#spotlight-widget .section').stop().animate({ marginLeft: '-253px' }, _speed);
	$('#spotlight-widget .article').stop().animate({ marginLeft: '-412px' }, _speed);
	$('#spotlight-widget .nav .selected').removeClass('selected');
}
function expandSpotlight(spotlightNum) {
	collapseNews();
	collapseEvents();

	$('#spotlight-widget .section').stop().animate({ marginLeft: '0' }, _speed);
	$('#spotlight-widget .article').stop().animate({ marginLeft: '0' }, _speed);

	loadSpotlight(spotlightNum);

	$('#spotlight-widget .selected').removeClass('selected');
	$('#spotlight-widget .nav li').eq(spotlightNum).addClass('selected');
	$('#spotlight-widget').removeClass('collapsed').addClass('expanded');
}







function returnToInitialState() {
	$('#news-widget .section .selected').removeClass('selected');
	$('#news-widget .nav li').eq(_currentNewsSection).addClass('selected');
	$('#news-widget .section').stop().animate({ marginLeft: '0' }, _speed);
	$('#news-widget .article').stop().animate({ marginLeft: '-415px' }, _speed);

	$('#events-widget .selected').removeClass('selected');
	$('#events-widget .nav').stop().animate({ width: '245px' }, _speed);
	$('#events-widget .article').stop().animate({ marginLeft: '-468px' }, _speed);

	$('#spotlight-widget .nav li').eq(_currentSpotlight).addClass('selected');
	$('#spotlight-widget .section').stop().animate({ marginLeft: '0' }, _speed);
	$('#spotlight-widget .article').stop().animate({ marginLeft: '-412px' }, _speed);

	$('.widget').removeClass('expanded').removeClass('collapsed');
}





function initSlideshow() {
	var nav = '';
	$_currentSlide = $('#ss-images li').eq(0);

	$('#ss-images li').each(function (count) {
		nav += '<li><a>' + (count + 1) + '</a></li>';
	});
	$('#slideshow .nav ul').html(nav);
	$('#slideshow .nav a').eq(0).addClass('selected');
	$('#slideshow .nav').append('<span>' + $('#ss-images span').eq(0).html() + '</span>');

	_slideshowInterval = setInterval('advanceSlideshow()', 5000);
}

function loadSlide(n) {
	$s = $('#ss-images li').eq(n);
	$s.css({ left: '0', zIndex: '0' });
	$('#slideshow .nav span').html($s.children('span').html());
	$_currentSlide.stop().animate({ opacity: 0 }, _speed, function () {
		$(this).css({ left: '100%', opacity: 1, zIndex: '0' });
		$s.css('z-index', '1');
	});
	$('#slideshow .nav .selected').removeClass('selected');
	$('#slideshow .nav a').eq(n).addClass('selected');
	$_currentSlide = $s;
}


function advanceSlideshow() {
	var $slides = $('#ss-images li');
	loadSlide(($slides.index($_currentSlide) + 1) % $slides.length);
}



$(document).ready(function () {

	initSlideshow();
	initNews();
	initEvents();
	initSpotlight();


	if ($('.nav.secondary .selected').length > 0) {
		var offset = $("#nav-wrapper").offset().top;
		var startY = $(".nav.primary .selected").offset().top;
		var endY = $(".nav.secondary .selected").offset().top;

		$(".rule").css({ "top": (startY + 8 - offset) + "px",
			"height": +endY - startY + 15 + "px",
			"width": "7px"
		});
	}



	$('#slideshow .nav li a').live('click', function () {
		clearInterval(_slideshowInterval);
		if (!$(this).hasClass('selected')) {
			loadSlide($(this).html() - 1);
		}
		return false;
	});



	$('.widget h2').click(function () {
		if ($(this).parents('.widget').hasClass('collapsed')) {
			switch ($(this).parents('.widget').attr('id')) {
				case 'news-widget':
					expandNews(0, 0);
					break;
				case 'events-widget':
					expandEvents(0);
					break;
				case 'spotlight-widget':
					expandSpotlight(0);
					break;

			}
		} else {
			if ($(this).parents('.widget').hasClass('expanded')) {
				returnToInitialState();
			}
		}
	});

	$('#news-widget .nav li a').live('click', function () {
		expandNews($('#news-widget .nav a').index($(this)), 0);
		return false;
	});

	$('#news-widget .section li a').live('click', function () {
		expandNews(_currentNewsSection, $('#news-widget .section a').index($(this)));
		return false;
	});

	$('#news-down').live('click', function () {
		if (_scrollLocked) return false;
		_scrollLocked = true;

		var foundNext = false;
		do {
			_topArticleIdx++;
			article = _articles.eq(_topArticleIdx);
			if (article.offset().top + article.outerHeight() > _newsTop + _viewportHeight) {
				foundNext = true;
			}
		} while (!foundNext && (_topArticleIdx + 1) < _articles.length);

		_topOffset = _topOffset + article.offset().top - _newsTop;
		article.addClass('top');

		if (_listHeight - _topOffset < _viewportHeight) {
			// Hit the bottom
			$('#news-down').removeClass('visible');
		}

		$('#news-list ul').animate({ top: '-' + _topOffset + 'px' }, function () { _scrollLocked = false; });
		$('#news-list .get-more').animate({ top: '-' + _topOffset + 'px' });

		$('#news-up').addClass('visible');
	});

	$('#news-up').live('click', function () {
		if (_scrollLocked) return false;
		_scrollLocked = true;

		do {
			_topArticleIdx--;
			article = _articles.eq(_topArticleIdx);
			if (article.hasClass('top')) {
				break;
			}
		} while (_topArticleIdx >= 0);

		_topOffset = _topOffset - (_newsTop - article.offset().top);

		if (_topArticleIdx <= 0) {
			// Hit the top
			_topOffset = 0;
			$('#news-up').removeClass('visible');
		}

		$('#news-list ul').animate({ top: '-' + _topOffset + 'px' }, function () { _scrollLocked = false; });
		$('#news-list .get-more').animate({ top: '-' + _topOffset + 'px' });

		$('#news-down').addClass('visible');
	});

	$('#events-widget .nav li a').live('click', function () {
		expandEvents($('#events-widget .nav a').index($(this)));
		return false;
	});

	$('#spotlight-widget .nav li a').live('click', function () {
		expandSpotlight($('#spotlight-widget .nav a').index($(this)));
		return false;
	});

	$('#spotlight-widget .section a').live('click', function () {
		expandSpotlight(_currentSpotlight);
		return false;
	});

});
