$(document).ready(function() {
	

	
	//Initialize Weekly Schedule Scroller
	(function($){$.fn.scrolling_carousel = function() {
		return this.each(function(){
			$(this).addClass("scrolling-carousel");
		    
			//put all the children into a  set of containers and append it back to the parent object
			var viewer = $('<div class="viewing-window"></div>');
			var panes =  $('<div class="pane-container"></div>').append($(this).children());
			viewer.append(panes).appendTo($(this));
			
			//add the scroll buttons
			$(this).prepend('<div class="left-scroll-button"/><div class="right-scroll-button"/>');
			//add the regulating class to the first element
			$(this).find(".pane-container").children().first().addClass("at-left-margin");
			
			//attach the event listeners
			$(this).find(".right-scroll-button").click({carousel: this}, function(e){
				$('#time-12am, #time-4am, #time-8am, #time-12pm, #time-4pm, #time-8pm').show(); 
				var sliding_element = $(e.data.carousel).find(".pane-container").children(".at-left-margin"); 

				if(sliding_element.next().width() != null){
					$(e.data.carousel).find(".pane-container").animate({"right": "+=" + (964)}, 500);
					sliding_element.removeClass("at-left-margin").next().addClass("at-left-margin");
				}
			});
			$(this).find(".left-scroll-button").click({carousel: this}, function(e){
				$('#time-12am, #time-4am, #time-8am, #time-12pm, #time-4pm, #time-8pm').show();
				var sliding_element = $(e.data.carousel).find(".pane-container").children(".at-left-margin"); 
				//var highlight-time = $(e.data.carousel).find(".pane-container").children(".grid");
				
				if(sliding_element.prev().width() != null){
					$(e.data.carousel).find(".pane-container").animate({"right": "-=" + (964)}, 500);
					sliding_element.removeClass("at-left-margin").prev().addClass("at-left-margin");
				}
			});
			
			
			// Switch active link styles when scrolling
			$(this).find(".right-scroll-button").click({carousel: this}, function(e){
				var $timeblock = null;
				$timeblock = $(".at-left-margin").attr("id").split("-")[1];
				$('a.time-chooser').removeClass('red-button red-underline').addClass('red-underline');
				$('a#' + $timeblock).removeClass('red-underline').addClass('red-button');
			});
			$(this).find(".left-scroll-button").click({carousel: this}, function(e){
				var $timeblock = null;
				$timeblock = $(".at-left-margin").attr("id").split("-")[1];
				$('a.time-chooser').removeClass('red-button red-underline').addClass('red-underline');
				$('a#' + $timeblock).removeClass('red-underline').addClass('red-button');
			});
			
			
			// Activate time-block links (weekly schedule only)
			$('a.time-chooser').click({carousel: this}, function(e) {
				var sliding_element = $(e.data.carousel).find(".pane-container").children(".at-left-margin"); 
				sliding_element.removeClass("at-left-margin");
				$('#time-' + this.id).addClass("at-left-margin");				
				$('a.time-chooser').removeClass('red-button red-underline').addClass('red-underline');
				$('a#' + this.id).removeClass('red-underline').addClass('red-button');
				e.preventDefault();
				if(this.id == '12am'){$(e.data.carousel).find(".pane-container").animate({"right": "0"}, 1000);}
				if(this.id == '4am'){$(e.data.carousel).find(".pane-container").animate({"right": "964"}, 1000);}
				if(this.id == '8am'){$(e.data.carousel).find(".pane-container").animate({"right": "1928"}, 1000);}
				if(this.id == '12pm'){$(e.data.carousel).find(".pane-container").animate({"right": "2892"}, 1000);}
				if(this.id == '4pm'){$(e.data.carousel).find(".pane-container").animate({"right": "3856"}, 1000);}
				if(this.id == '8pm'){$(e.data.carousel).find(".pane-container").animate({"right": "4820"}, 1000);}
			});
			
		});
	};})(jQuery);

	$("#showschedule-carousel").scrolling_carousel();
	$("a#8pm").click();
	
	var weeklySchedule = ($('body#tv-schedule-weekly').length != 0);
	var maxDate = "2w";
	if(weeklySchedule) {
		// Reduce how far you can look in the future on the weekly schedule
		// (since the page shows you the date you choose + the next 6 days)
		maxDate = "8d";
	}
	// Activate date pickers
	$( "#datepicker" ).datepicker({
		dateFormat: "yy-mm-dd",
		altField: "#datepicker",
		altFormat: "MM d",
		showOn: "button",
		minDate: "-2w",
		maxDate: maxDate,
		buttonImage: "/assets/images/common/calendar-icon.png",
		buttonImageOnly: true,
		onSelect: function(dateText) { window.location.href = "?date=" + dateText; }
	});

	// Activate time zone dropdowns
	$('select.timezone').change(function() {
		window.location = overrideUrlParameters(window.location.href, {tz: $(this).val()});
	});
	
	
});





