$(document).ready(function() {
	epgTimeScroll();
	epgCycle('#epgTimeSelect');
	epgCarousel('#epgCalendar');
	showMoreInfo();
	showModalDialog('#signinDialog', '#nav-account-signin a, #cvn-tip a, #account-cancel');
	showModalDialog('#infoDialog', '.epgInfo a.full');
	showModalDialog('#helpDialog', '#epgSearch a');
	$('#notification a.close').click(function(){
		$('#notification').slideUp(150);
		return false;
	});
	planBubbles();
});
function growGraph(q) {
	var w = q+'%';
	$('.bar').animate({width: w}, 800, 'easeOutCubic');
}
function epgTimeScroll() {
	$("#epgPrograms").scroll(function() {
		$("#epgTimes").scrollLeft($(this).scrollLeft());
	});
}
function epgCycle(element) {
	var startingSlide = 6;

	if(typeof(today) != "undefined" && today == true) {
		currentDate = new Date();
		startingSlide = Math.floor(currentDate.getHours() / 3);
	}

	$(element + ' ul').cycle({
		timeout:		0,     // (in ms)
		speed:			600,   // (in ms)
		fx:				'scrollHorz',
		easing:			'easeInOutCubic', 
		next:			element + ' .next a', 
		prev:			element + ' .prev a',
		before:			cycleToTime,
		startingSlide:	startingSlide
	});	
}
function epgCarousel(element) {
	$(element + ' .slider').jCarouselLite({
		speed:			400,   // (in ms)
		easing:			'easeInOutCubic',
		circular:		false,
		visible:		7,
		scroll:			1,
		start:			1,
        btnNext: 		element + '#epgCalendar .next a',
        btnPrev: 		element + '#epgCalendar .prev a'
    });
}
function cycleToTime() {
	cycleToBlock($('#epgTimeSelect ul li').index(this));
}

function cycleToBlock(block) {
	 $("#epgPrograms").animate({
		scrollLeft: 	block * 900
	},
	{
		duration: 		600,
		easing:			'easeInOutCubic'
	});	
}

function cycleToNow() {
	currentDate = new Date();
	now = Math.floor(currentDate.getHours() / 3);

	$('#epgTimeSelect ul').cycle(now);
}

function showMoreInfo() {
	//Hide descriptions on load - recordings page only
	$(".recItem .description").hide(); 
				   
	$(".more").click(function(){
		$(this).parent().siblings(".description").slideToggle({
			speed:400,
    		easing:'easeInOutCubic'
  		});
	});
	$(".more").toggle(function(){
		$(this).find("b").text("Less info");
	}, function () {
		$(this).find("b").text("More info");
	});
}
function showModalDialog(holder, trig) {
	$(holder).jqm({
		ajax: '@href',
		ajaxText: '<div class="wait"><p>Loading&hellip;</p></div>',
		trigger: trig,
		target: 'div.jqmAjaxContent',
		onShow: function(h) {
			h.w.fadeIn(200);
        },
		overlay:0
	});
}
function planBubbles() {
	var bubble;
	var pos;
	var linkHeight;
	var linkWidth;
	var linkCenter;
	var bubbleHeight;
	var bubbleWidth;

	// Find features that have extra info
	var bubbleLink = $('.plan-content li a');

	// Show bubble on hover
	$(bubbleLink).hover(function(e) {
		// Find bubble sitting next to feature link						 
		bubble = $(this).next('.plan-bubble');
		// Get link position
		pos = $(this).position();
		// Get bubble dimensions
		bubbleHeight = bubble.outerHeight();
		bubbleWidth = bubble.outerWidth();
		// Get link dimensions
		linkHeight = $(this).outerHeight();
		linkWidth = $(this).outerWidth();
		
		// Position bubble in middle of link
		linkCenter = pos.left + (linkWidth/2);
		bubbleCenter = bubbleWidth/2;
		// Show bubble to force display:block, but set opacity to 0, then position it.
		bubble.show();
		bubble.css('top', pos.top - bubbleHeight + 5);
		bubble.css('left', linkCenter-bubbleCenter);
		// Check if not IE with crap opacity support
		if (jQuery.support.opacity) {
			bubble.css('opacity', 0);
			bubble.animate({
				opacity:1,
				top: pos.top - bubbleHeight
			}, 300, 'linear');
		}				
	},
	function() {
		bubble.hide();
		bubble.css('top', pos.top - bubbleHeight + 5);	
	});
	$(bubbleLink).click(function() {
		return false;				 
	});
}
