/* * * * * Misc. jQuery application of design * * * * */

/* HOME PAGE TEMPLATE STYLES */

(function ($) {
	$.fn.copySlide = function () {

		return this.each(function () {
			var $wrapper = $(this).css('overflow', 'hidden'),
						$slider = $wrapper.find('> ul').width(9999),
						$items = $slider.find('> li'),
						$single = $items.filter(':first'),
						singleWidth = 278,
						currentPage = 1,
						pages = $items.length,
						$navigation = $wrapper.siblings('ul');

			$items.filter(':first').before($items.slice(-1).clone().addClass('cloned'));
			$items.filter(':last').after($items.slice(0, 1).clone().addClass('cloned'));
			$items = $slider.find('> li');

			$wrapper.scrollLeft(singleWidth);

			function gotoPage(page) {
				var dir = page < currentPage ? -1 : 1,
						n = Math.abs(currentPage - page),
						left = singleWidth * dir * n;

				$wrapper.filter(':not(:animated)').animate({
					scrollLeft: '+=' + left
				}, 500, function () {
					if (page > pages) {
						$wrapper.scrollLeft(singleWidth);
						page = 1;
					} else if (page == 0) {
						page = pages;
						$wrapper.scrollLeft(singleWidth * pages);
					}

					currentPage = page;
					$('li', $navigation).removeClass('active');
					$('li', $navigation).eq(page - 1).addClass('active');
				});
			}

			$(this).bind('goto', function (event, page) {
				gotoPage(page);
			});
			$(this).bind('next', function () {
				gotoPage(currentPage + 1);
			});
		});
	};
})(jQuery);

$(document).ready(function () {

	// Video Host stuff

	var autoscrolling = true,
		introPlaying = false,
		copyInterval = false,
		$flashWrapper = $('div#theFlash');

	$('div.copySlides').copySlide();
	$('div.copySlides, ul.slideIndicator').mouseover(function () {
		autoscrolling = false;
	}).mouseout(function () {
		autoscrolling = true;
	});

	if ($('html').hasClass('ltIE9')) {
		setTimeout("$('div.silent-intro.hostVideo object')[0].playMovie()", 1000);
	} else {
		setTimeout("$('div.silent-intro.hostVideo video')[0].play()", 1000);
	}

	function switchVideo($video) {
		$('div.hostVideo').removeClass('active');
		clearInterval(copyInterval);

		if (introPlaying && $('html').hasClass('ltIE9')) {
			$('div.intro.hostVideo object')[0].stopMovie();
		} else if (introPlaying) {
			$('div.intro.hostVideo video')[0].pause();
		}
		if ($flashWrapper.hasClass('loading')) {
			$flashWrapper.removeClass('loading');
		}

		if ($('html').hasClass('ltIE9')) {
			$video.addClass('active').find('object')[0].playMovie();
		} else {
			$video.addClass('active').children('video')[0].play();
		}
	}
	
	$('div.silent-intro.hostVideo, div.silent-intro.hostVideo object, a.flashIntroLink').click(function () {
		introPlaying = true;
		switchVideo($('div.intro.hostVideo'));
		return false;
	});
	$('div.intro.hostVideo video').bind('ended', function () {
		introPlaying = false;
		switchVideo($('div.introLoop.hostVideo'));
	});
	$('div.introLoop.hostVideo a.replayHost').click(function () {
		introPlaying = true;
		switchVideo($('div.intro.hostVideo'));
		return false;
	});
	$('a.playIntro').click(function () {
		introPlaying = true;
		switchVideo($('div.intro.hostVideo'));
		return false;
	});
	$('a.hostMute').click(function () {
		if ($(this).hasClass('active')) {
			$(this).removeClass('active').parent().children('video')[0].muted = false;
			return false;
		}
		else {
			$(this).addClass('active').parent().children('video')[0].muted = true;
			return false;
		}
	});

	function copySlide($target) {
		copyInterval = setInterval(function () {
			if (autoscrolling) {
				$target.trigger('next');
			}
		}, 8000);
	}
	$('a.respondQuickly').click(function () {
		switchVideo($('div.respondVideo.hostVideo'));

		copySlide($('div.respondVideo div.copySlides'));

		return false;
	});
	$('a.needsMatter').click(function () {
		switchVideo($('div.needsVideo.hostVideo'));

		copySlide($('div.needsVideo div.copySlides'));

		return false;
	});
	$('a.createBelongs').click(function () {
		switchVideo($('div.createVideo.hostVideo'));

		copySlide($('div.createVideo div.copySlides'));

		return false;
	});

	$('ul.slideIndicator a').click(function () {
		var clickedSlide = $(this).parent().index() + 1;
		$(this).parent().parent().siblings('div.copySlides').trigger('goto', clickedSlide);
		return false;
	});

	// Set CSS class for each last (bottom) list item in the targeted element.
	$('div.threeColumnBox div.columnContent ul.linkList li:last-child').addClass('last');
	// Set CSS class for each last (bottom) list item in the targeted element (News/Press Home Page & Inner Page Listings).
	$('ul#np-newsStoryList li:last-child').addClass('last');
	// Set CSS class for each last (bottom) list item in the targeted element.
	$('ul#blog-newsStoryList li:last-child').addClass('last');
	// Turning on Static Header on static page (where corresponding div will be displayed).
	if ($('div.staticHeaderContent').length) {
		$('body.homePage div.headerBox').addClass('staticHeader');
	}
	// Toggle Flash Video Header ON and remove "staticHeader" specific styling when "View Intro" button is clicked.
	$('a.staticHeaderLinkViewIntro').click(function () {
		$('body.homePage div.headerBox').removeClass('staticHeader');
		$('div.staticHeaderContent').toggle();
		$('div#theFlash').toggle();
	});
	/* HOME TABBED BOXES */

	// INITIAL SETUP - Apply CSS to "turn on" a random tab.
	var tabCount = 2;
	var selectedTab = Math.floor(Math.random() * tabCount) + 1;
	$('div#homeTabsSelector').addClass('tab' + selectedTab);

	// CHANGING TABS ON CLICK - Apply CSS to "turn off" all tabs then apply CSS to "turn on" the selected tab.
	$('div#homeTabsSelector div.homeTabs a').click(function () {
		var tabName = $(this).attr('name');
		$('div#homeTabsSelector').removeClass();
		$('div#homeTabsSelector').addClass(tabName);
	});

	// Initialize home page slideshows.
	$('#tab1 div.homeSlideshow')
	.after('<div class="homeSlideshowNav">')
	.cycle({
		fx: 'fade',
		height: 288,
		speed: 400,
		timeout: 10000, // 10 seconds 
		pager: '#tab1 div.homeSlideshowNav',
		pause: true,
		random: true,
		cleartype: true,
		cleartypeNoBg: true
	});
	

});
