$(function(){
	$(".select input").unbind('click').click(function(){
		$(this).siblings('div').slideToggle();
	});

	$(".select ul li a").unbind('click').click(function(){
	
		var url = $(this).attr("href");
		
		var input = $(this).parent().parent().parent().siblings('input');
		
		$(input).attr("readonly","");
		
		$(input).val($(this).text());
		
		$(input).attr("readonly","readonly");
		
		$(this).parent().parent().parent().slideUp();
	
		$("#DaTControl").submit();
					
		return false;
	});
});

$(document).ready(function() {

	$(".program-details").click(function(event) {
		var popup = $(this).parent().children(".schedule-pop");

		var top = parseInt($(document).height()) - Math.abs(event.pageY);

		$(this).parent().addClass("active").addClass("hover");

		$(popup).css("position", "relative");
		$(popup).css("top", -1 * top + "px");
		$(popup).css("left", parseInt($(this).offset().left) - parseInt($(this).parent().width())/2 + "px");

		$(popup).addClass("activePopup");

		$("body").append($(popup).clone().slideToggle());

		$(".activePopup").hover(function() { $(this).addClass("mouseOver")}, function() { $(this).removeClass("mouseOver");});
		
		$(".activePopup .close").click(function () {
			killActivePopups();
		});

		$(popup).removeClass("activePopup");
		$(popup).addClass("activePopupSource");

	});

	$(window).resize(function(event) {
		$(".activePopup").css("left", $(".activePopupSource").parent().offset().left - parseInt($(".activePopupSource").parent().parent().css("width")) / 2 + "px");
	});

	$("#schedule").scroll(function() {
		killActivePopups();
	});

	$(".close").click(function() {
		killActivePopups();
		return false;
	});

	$(document).click(function() {
		if($(".activePopup").attr("rel") != "done")
		{
			$(".activePopup").attr("rel", "done");
			return true;
		}

		if($(".activePopup").hasClass("mouseOver"))
			return true;

		killActivePopups();
	});

	function killActivePopups() {
		$(".activePopup").remove();
		$(".activePopupSource").removeClass("activePopupSource");

		$(".hover").removeClass("hover");
	}


	$(".program").hover(function() { $(this).addClass("hover") }, function() { if($(this).hasClass("active")) return false; $(this).removeClass("hover") });
	
	$(".schedule #date input").datepicker({
		showButtonPanel: true,
		changeMonth: true,
		changeYear: true,
		dayNamesMin: ['S', 'M', 'T', 'W', 'T', 'F', 'S'],
		closeText: 'Close',
		currentText: 'Current Month',
		minDate: new Date(1979,0,1),
		maxDate: '+1m',
		yearRange: '1979:' + (new Date().getFullYear() + 1).toString(),
		dateFormat: 'M d, yy',
		onSelect: function(dateText, inst){
			$("#DaTControl").submit();
		}
	});

});

// Prepase a "reverse" function to aid in backwards .each's
jQuery.fn.reverse = function() {
	return this.pushStack(this.get().reverse());
}

$(function() {
	var $totalHeight = $("#schedule #times").height();

	$("#schedule").scroll(function() {
	
		var $pos = $("#schedule").scrollTop();

		$(".program").each(function () {
			var $top = parseInt($(this).css("top")) + parseInt($(this).css("padding-top")) + parseInt($(this).children(".program-details").css("padding-top"));
			var $bottom = $top + parseInt($(this).height());
			var $toMove = $(this).children(".program-details").children(".scrollMove");

			// Adjust for images
			if($(this).children(".program-details").children(".video-wrapper").length > 0)
				$top += parseInt($(this).children(".program-details").children(".video-wrapper").height()) + parseInt($(this).children(".program-details").children(".video-wrapper").css("margin-bottom"));
			
			// See if we are in this program
			if($top <= $pos && $bottom >= $pos)
					moveTo($toMove,  $pos - $top, $bottom - $top);
			else if($top > $pos)
				moveTo($toMove, 0, 0); 
		});	
	});
		
	function moveTo(elm, pos, h)
	{
		if(pos == 0)
		{
			if($(elm).children(".before").length)
				$(elm).children(".before").remove();

		} else {
                	if(!$(elm).children(".before").length)
	                	$(elm).children("h3").before("<div class='before'>^</div>");
			
			var tot = 0;
			$(elm).children("*").each(function() {
				if(this.tagName != "H3")
				{
					tot += parseInt($(this).css("padding-top")) + parseInt($(this).height());
				} else {
					if((tot + $(this).height()) >= h)
			//			pos = parseInt($(this).css("top")); 
					return false;
				}	
			});			
        	}
		$(elm).css("top", pos + "px");
	}
});

/*
$(function() {

	$("#schedule").scroll(function() {

		var $offset = $("#schedule #times").height() - $("#schedule")[0].scrollHeight;
		var $sh = $("#schedule").scrollTop();
		var $pos = $sh;// + $offset;

		$(".program-details").each(function() {
			
			// box to move in, in px
			var $top = parseInt($(this).parent().css("top")) + parseInt($(this).parent().css("padding-top"));
			var $bottom = $top + $(this).height();
			
			if($(this).children(".video-wrapper").length != 0)
				$top += parseInt($(this).children(".video-wrapper").css("margin-bottom")) + parseInt($(this).children(".video-wrapper").height());

			// Break is before this box to move in
			if($pos <= $top)
			{
				removeBeforeMark($(this));
				$(this).children(".scrollMove").css("top", "0px");

				$(this).children(".scrollMove").children("div:last").css("display", "block").removeAttr("rel");
				$(this).children(".scrollMove").children("h4:last").css("display", "block");
				$(this).children(".scrollMove").children(".inprogress").css("display", "block").removeAttr("rel");
				$(this).children(".scrollMove").children(".live").css("display", "block").removeAttr("rel");
			} else {
				$currentSmTop = parseInt($(this).children(".scrollMove").css("top"));
				$smTop = $currentSmTop + $top;				
				$smBottom = $smTop + $(this).children(".scrollMove").height(); 
				
				$delta = $pos - $smTop;

				if(($smBottom + $delta) < $bottom)
				{
					$(this).children(".scrollMove").children().each(function() {
						if($(this).attr("rel") >= $currentSmTop + $delta)
						{
							$(this).removeAttr("rel");
							$(this).css("display", "block");
						}
					});

					addBeforeMark($(this));
					$(this).children(".scrollMove").css("top", $currentSmTop + $delta);
				} else {
					if($(this).children(".scrollMove").children("div:last").css("display") != "none")
					{
						$(this).children(".scrollMove").children("div:last").attr("rel", $smTop - $top);
						$(this).children(".scrollMove").children("div:last").css("display", "none");	
						$(this).children(".scrollMove").children("h4:last").attr("rel", $smTop - $top);
						$(this).children(".scrollMove").children("h4:last").css("display", "none");

					} else if($(this).children(".scrollMove").children(".live").length > 0) {

						$(this).children(".scrollMove").children(".live").attr("rel", $smTop - $top);
						$(this).children(".scrollMove").children(".live").css("display", "none");

						$(this).children(".scrollMove").children(".inprogress").attr("rel", $smTop - $top +1);
						$(this).children(".scrollMove").children(".inprogress").css("display", "none");

					} else if($(this).children(".scrollMove").children(".inprogress").length > 0) {

						$(this).children(".scrollMove").children(".inprogress").attr("rel", $smTop - $top +1);
						$(this).children(".scrollMove").children(".inprogress").css("display", "none");
					}

					addBeforeMark($(this));
					$(this).children(".scrollMove").css("top", $smTop - $top);
				}
			}
		});
		
	});


	function addBeforeMark(elem) {
		if($(elem).children("div").children(".before").length)
			return;

		$(elem).children("div").children("h3").before("<div class='before'>^</div>");	
	}

	function removeBeforeMark(elem) {
		if(!$(elem).children("div").children(".before").length)
			return;

		$(elem).children("div").children(".before").remove();
	}	
});
*/

$(document).ready(function() {
	var spp = parseInt($("#spp").html());
	var seconds = parseInt($("#seconds").html());

	var scroll = Math.round(seconds/spp);

	$("#schedule").scrollTo(scroll + "px", 200);
});

