
jQuery(function ($) {

	// Carousel Actions

	var MARKER_OFFSET_TOP = 10,
		oCurrent = $('#carousel_1'),
		tTimer, sMenuID, sCurrent = '1';

	$('#carouselItems li').click(function (e) {
		
		var oSelf	= $(this);
			
		// Remove "menu_" from the front
		sMenuID = oSelf.attr('id').split('menu_')[1];
	
		if (sMenuID === sCurrent) {
			return;
		}
		
		sCurrent = sMenuID;

		var iSlideToo = oSelf.position().top - MARKER_OFFSET_TOP;
	
		// Animate marker
		$('#carouselMarker').animate({
			top: iSlideToo
		}, 'fast', function () {
	
		});
	
		// Turn slides on and off
		$('#carouselItems li.selected').removeClass('selected');
		oSelf.addClass('selected');
	
		if ($.browser.msie && $.browser.version <= 8) {
			oCurrent.hide();
			$('#carousel_' + sMenuID).show();
		}
		else {
			oCurrent.fadeOut('slow');
			$('#carousel_' + sMenuID).fadeIn('slow');
		}
		
		oCurrent = $('#carousel_' + sMenuID);
		
		// User interacted, stop.
		if (e.originalEvent) {
			clearInterval(tTimer);
		}
		else {
			restartTimer();
		}
		
	});
	
	// Restarts the carousel timer
	function restartTimer () {

		clearInterval(tTimer);
	
		tTimer = setInterval(function () {
	
			var oNextMenuItem = $('#carouselItems li.selected').next();

			// Reset
			if (oNextMenuItem.length === 0) {
				$('#carouselMarker').css('top', -MARKER_OFFSET_TOP);
				oCurrent = $('#carousel div:first');
				$('#carouselItems li.selected').removeClass('selected');
				$('#carouselItems li:first').addClass('selected');
				
				// IE can't handle the pace
				if ($.browser.msie && $.browser.version <= 8) {
					$('#carousel div:last').hide();
					oCurrent.show();
				}
				else {
					$('#carousel div:last').fadeOut('slow')
					oCurrent.fadeIn('slow');
				}
				restartTimer();
				return;
			}
	
			// Click it
			oNextMenuItem.trigger('click');
		
		}, 7000);
	
	}
	
	restartTimer();

	$('#carousel .details').click(function (e) {
	
		e.preventDefault();
		window.open($(this).attr('href'), "mywindow", "menubar=0,resizable=0,width=450,height=500");
	
	});
	
	
	// New / Exisiting Customer actions
	var $tabContent = $('#homepage div.tab-content');	
	
	$('#homepage ul.homepage-tabs li a').click(function(e) {
		e.preventDefault();
		var $this = $(this);
		var $li = $this.parents('li');
		if ($li.is('.active')) return false;
		
		$('#homepage-main ul.homepage-tabs li.active').removeClass('active');
		$li.addClass('active');
		
		var tabName = $this.attr('rel');
		$tabContent.hide();
		$('#' + tabName).show();
		
		$('#homepage-tabbed-content').attr('class', tabName);
		
	});
	
	// Service advistory actions
	$('#serviceAdvisory .close, #serviceAdvisoryMild .close').click(function () {
		$(this).parent().parent().hide();
	});
	
	$('#carousel_countdown').click(function () {
	
		 window.location = 'http://www.comcast.com/Corporate/Learn/Bundles/bundles.html';
	
	});

});
