﻿$(function() {
	// rotate home page banner images
	var images = $('.topBannerImg');
	if (images.length > 0) {
		var imgCount = images.length;
		count = 0;
		function changeImg() {
			if (count == imgCount - 1) {
				count = 0;
			} else {
				count = count + 1;
			}
			$('.topBannerImg').hide();
			$('.topBannerImg:eq(' + count + ')').fadeIn();
		}
		setInterval(function() { changeImg(); }, 8000);
	};

	// tooltip on the staff photos page - no longer using plugin, Damien Code follows
	//$("#staff-list a.staff-photo").tooltip({position: 'top center', effect: 'slide' });

	var SL = $("#staff-list");
	if (SL.length > 0) {
		var SL_M = SL.find('.staff-member');
		SL_M.find('.tooltip').hide();
		CONTACT_INTERVAL = false;
		SL_M.find('a.staff-photo, .tooltip').mouseover(function() {

			$(this).parent().css({ 'z-index': 1000 }).find('.tooltip').show()//.animate({ top: '55px' }, 300, "swing");
		});

		SL_M.find('.staff-photo').focus(function() {
			$(this).parent().css({ 'z-index': 1000 }).find('.tooltip').show()
		}).focusout(function() {
			$(this).parent().css({ 'z-index': 400 }).find('.tooltip').hide();
		});

		SL_M.find('a, .tooltip').mouseout(function() {
			$(this).parent().css({ 'z-index': 400 }).find('.tooltip').hide()
			//var that = $(this);
			//CONTACT_HOVER = setTimeout(function(){ $('.tooltip').hide().css({ top: '70px' }) }, 5000);
		});
	}

	// rotate the mission statements
	var ms = $('.missionStatement p');
	if (ms.length > 0) {
		ms.not(':first').hide();
		// add change icons
		var div = $('<div class="msIcons"></div>').prependTo('.missionStatement');
		$('<a id="ms1" class="roundedIcon msSelect" href="javascript:void(0);"><span>First</span></a>').appendTo(div).click(function() {
			changeMsTo($(this));
		});
		$('<a id="ms2" class="roundedIcon" href="javascript:void(0);"><span>Second</span></a>').appendTo(div).click(function() {
			changeMsTo($(this));
		});
		$('<a id="ms3" class="roundedIcon" href="javascript:void(0);"><span>Third</span></a>').appendTo(div).click(function() {
			changeMsTo($(this));
		});

		var msLength = ms.length;
		msCount = 0;

		function msStart() {
			msInterval = setInterval(function() { changeMs(); }, 5000);
	}

		function changeMsTo(cmp) {
			var id = $(cmp).attr('id').split('s')[1];
			clearInterval(msInterval);
			ms.hide();
			$('.msSelect').removeClass('msSelect');
			$('#ms' + id).addClass('msSelect');
			$('.missionStatement p:eq(' + (id - 1) + ')').fadeIn(1000);
			msStart();
		};

		function changeMs() {
			if (msCount == msLength - 1) {
				msCount = 0;
			} else {
				msCount = msCount + 1;
			}
			ms.hide();
			$('.msSelect').removeClass('msSelect');
			$('#ms' + (msCount + 1)).addClass('msSelect');
			$('.missionStatement p:eq(' + (msCount) + ')').fadeIn(1000);
		}
		msStart();
	};

	// allow scrolling on top new stories
	var news = $('.topItem');
	if (news.length > 0) {
		var news_count = 0;
		var news_length = news.length;
		// hide all except the first
		news.not(':first').hide();
		// add next/previous
		var news_next = $('<a class="nextNews">Next</a>').prependTo('.innerBodyContainer');
		var news_prev = $('<a class="prevNews">Previous</a>').prependTo('.innerBodyContainer');
		news_next.click(function() {
			news.hide();
			if (news_count == news_length - 1) {
				news_count = 0;
			} else {
				news_count++;
			}
			news.eq(news_count).fadeIn()
		});
		news_prev.click(function() {
			news.hide();
			if (news_count == 0) {
				news_count = news_length - 1;
			} else {
				news_count--;
			}
			news.eq(news_count).fadeIn()
		});

	};

	// choose a random quote
	var quote = $('.quote');
	if (quote.length > 0) {
		var length = quote.length;
		var randomQ = Math.floor(Math.random() * length);
		quote.hide();
		$('.quote:eq(' + randomQ + ')').show();
	};


	// script to apply rounded images.
	$("img.rounded-img").each(function() {
		$(this).wrap(function() {
			return '<span class="' + $(this).attr('class') + '" style=\'display: block; background-image: #ccc url("' + $(this).attr('src') + '") no-repeat center center; \'></span>';
		});
		if ($.browser.msie) {
			if ($.browser.version == '8.0');
			$(this).parent().parent().find('span').css('background-image', 'url(' + $(this).attr('src') + ') center center no-repeat');
		}
		//$(this).remove();
	});

	$(".channelSummaryContainer").find('img').each(function() {
		$(this).wrap(function() {
			return '<span class="rounded-img" style=\'display: block; background: #ccc url("' + $(this).attr('src') + '") no-repeat center center; \' ></span>';
		});
		if ($.browser.msie) {
			if ($.browser.version == '8.0');
			$(this).parent().parent().find('span').css('background', 'url(' + $(this).attr('src') + ') center center no-repeat');
		}
		$(this).remove();
	});

	$('<div id="modal" style="text-align: left;"></div>').appendTo(document.body);
	var modal = $('#modal');
	$('.quote').find('.moreLink').click(function() {
		var href = $(this).attr('href');
		var title = $(this).parent().find('h5').text();
		modal.empty();
		modal.load(href + '?modal=true' + ' .htmlContentContainer', function() {
			$(this).css({
				display: 'block'
			});
			$(this).prepend('<h3>' + title + '</h3>');
			$(this).append('<p><a href="' + href + '">View this page</a></p>');
			var that = this;
			$.colorbox({
				width: 700,
				inline: true,
				href: "#modal",
				onClosed: function() {
					$(that).hide();
				}
			});

		});

		return false;
	});

});
