$(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");

		$(".to_program").click(function() {
			var url = "/videoLibrary/schedule.php?time=" + $("#px_position").html();
			$.ajax({
				type: "GET",
				url: url
			});
	});

	});

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

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

		
		$("#px_position").html(parseInt($(this).scrollTop()) * parseInt($("#spp").html()));
	});

	$(".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") });

	$(".to_program").click(function() {
		var url = "/videoLibrary/schedule.php?time=" + $("#px_position").html();
		$.ajax({
			type: "GET",
			url: url
		});
	});

	$("#timezone a").click(function() {
		var url = "/videoLibrary/schedule.php?time=" + $("#px_position").html();
		$.ajax({
			type: "GET",
			url: url
		});
	});
	
	$(".schedule #date input").datepicker({
		showButtonPanel: true,
		changeMonth: true,
		changeYear: true,
		dayNamesMin: ['S', 'M', 'T', 'W', 'T', 'F', 'S'],
		closeText: 'Close',
		currentText: 'Today',
		minDate: new Date(1979,0,1),
		maxDate: '+1m',
		yearRange: '1979:' + (new Date().getFullYear() + 1).toString(),
		dateFormat: 'M d, yy',
		onSelect: function(dateText, inst){
			var url = "/videoLibrary/schedule.php?time=" + $("#px_position").html();
			$.ajax({
				type: "GET",
				url: url
			});
			$("#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");
	}
});

$(document).ready(function() {
	if(parseInt($("#px_position").html()) != 0)
	{
		var spp = parseInt($("#spp").html());
		var scroll = Math.round(parseInt($("#px_position").html()) / spp);
		$("#schedule").scrollTo(scroll +  "px", 200);
	} else {
		var spp = parseInt($("#spp").html());
		var seconds = parseInt($("#seconds").html());
		var scroll = Math.round(seconds/spp);
	
		$("#schedule").scrollTo(scroll + "px", 200);
	}
});


