/* PRETTY PHOTO */
$(document).ready(function() {
	$("a[href*='.flv']").each(function(i, o) { $(o).attr('rel', 'prettyPhoto'); });
	$("a[rel^='prettyPhoto']").prettyPhoto();
});


/* SEARCH */
$(document).ready(function() {
	$('#search-box').val('Search')
	$('#search-box').attr('class', 'untouched')
	$('#search-box').click(function() {
		if ($('#search-box').attr('class') == 'untouched') {
			$('#search-box').attr('class', 'touched')
			$('#search-box').val('');
		}
	})
	$('#search-box').keydown(function(event) {
		if (event.keyCode == '13') {
			SiteSearch($('#search-box').val(), $('#site-code').val());
		} else {
			return true;
		}
	})
	$('#search-button').click(function(event) { event.preventDefault(); SiteSearch($('#search-box').val(), $('#site-code').val()) })
});

function SiteSearch(sSearch, iCode) {
	window.location = '/search-results.asp?search=' + sSearch + '&site-code=' + iCode;
}

/* SITE TOGGLE */
$(document).ready(function() {
	$('#site-selector a').click(function(event) {
		event.preventDefault();
		$(this).blur();
		($(this).attr('href') == '#') ? $('#site-selector').toggleClass('open') : window.location = $(this).attr('href');
	})
});

/* NEWS */
$(document).ready(function() {
	sSite = sSite === undefined ? '' : '&sSite=' + sSite;
	oNews.GetNews('news_feed.asp?feedname=News' + sSite);
	oNews.AssignControls($('#news-previous'), $('#news-next'));
	oNews.Automate();
});

var oNews = {
	iLimit: 10,
	iInterval: 8,
	GetNews: function(sURL) {
		this.AddSpinner();
		$.get('/' + sURL, function(xml) { oNews.BuildNews(xml) })
	},
	BuildNews: function(xml) {
		try {
			var asNews = [];
			var iCount = 0;
			$(xml).find('item').each(function() {
				iCount++
				if (iCount > oNews.iLimit) { return; }
				asNews[asNews.length] = '<li><a href="' + $(this).find('link').text() + '">' + $(this).find('title').text() + '</a></li>';
			});
			this.oDiv.append($('<ul>' + asNews.join('\n') + '</ul>'));
			this.oDiv.find('ul').hover(function() { oNews.Stop() }, function() { oNews.Go(true, true) })
			this.KillSpinner();
		} catch (e) {
			return;
		}
	},
	AddSpinner: function() {
		this.oDiv = $('#news');
		var oSpinner = $('<div class="spinner"></div>');
		oSpinner.css('background-color', this.oDiv.css('background-color'));
		oSpinner.css('display', 'block')
		this.oDiv.append(oSpinner);
	},
	KillSpinner: function() {
		this.oDiv.find('div.spinner').fadeOut(1000, function() { oNews.oDiv.find('div.spinner').remove(); });
	},
	AssignControls: function(oPrevious, oNext) {
		oNext.click(function(event) { event.preventDefault(); this.blur(); oNews.Go(true) });
		oPrevious.click(function(event) { event.preventDefault(); this.blur(); oNews.Go(false) });
		/* -- PAUSE -- */
		oNext.hover(function() { oNews.Stop() }, function() { oNews.Go(true, true) })
		oPrevious.hover(function() { oNews.Stop() }, function() { oNews.Go(true, true) })
	},
	Automate: function() { this.Go(true, true); },
	Go: function() {
		oFunctions = [
			function(bWhere) {
				sWhere = bWhere ? 'first' : 'last';
				var oRemove = oNews.oDiv.find('ul li:' + sWhere + '-child');
				var oFade = bWhere ? oRemove : oNews.oDiv.find('ul li:first-child')
				oFade.fadeTo(oNews.iInterval * 100, 0, function() {
					oRemove.remove();
					oNews.oDiv.find('ul')[(bWhere ? 'ap' : 'pre') + 'pend'](oRemove);
					oFade.fadeTo(1, 1);
				});
			},
			function(bWhere, bRepeat) {
				if (bRepeat) {
					oNews.iRepeat = setInterval('oNews.Go(' + bWhere + ');', oNews.iInterval * 1000);
				} else {
					oNews.Stop();
				}
			}
		];
		if (arguments.length > oFunctions.length) { return; }
		oFunctions[arguments.length - 1].apply(this, arguments)
	},
	Stop: function() { clearTimeout(oNews.iRepeat); }
}

/* HOME MOVIE */
$(document).ready(function() {
	if ($('#home-feature-area').length && $('#placeholder').length) {
		swfobject.embedSWF('/graphics/content/flash/' + $('#placeholder').attr('title'), 'placeholder', ($('#placeholder').width()).toString(), ($('#placeholder').height()).toString(), "9.0.0", '', {}, { wmode: "opaque", allowscriptaccess: "always" });
		$('#placeholder').css('z-index', 2);
	};
});

$(document).ready(function() {
	try {
		if ($.browser.msie && parseInt($.browser.version) < 8) {
			$('ol#primary-navigation>li').addClass('top');
			$('ol#primary-navigation').fadeIn(300);
		}
	} catch (e) { }
});
