	/* Schedule bar */
	var sched_size = 2;
	function AddScheduleItems(xml, cur_id, cur_time, cur_date, gcount, next) {
		
		xml.find('schedule').each(function(index){
			
			if($(this).attr('id') == cur_id || next == true) {
				
				$(this).find('show').each(function(index){
					
					var s_time, si, cls, len, d_time									
					s_time = new Date(cur_date + ' ' + $(this).find('time').text());
					len = $(this).find('length').text();
					s2_time = new Date(cur_date + ' ' + $(this).find('time').text());					
					
					
					if( (len % 1) > 0 ) { 
						s2_time.setHours( s2_time.getHours() + parseInt(len), 30 );
					}
					else {
						s2_time.setHours( s2_time.getHours() + parseInt(len) );
					}
					
					d_time = $(this).find('display-time').text();

					if(gcount == sched_size) {
						return false;
					}
					
					if( (s_time <= cur_time && cur_time < s2_time) || cur_time <= s_time || next == true ) {
						si = $('<li></li>');
						url = $(this).find('link').text();
						cls = "scht";
						
						if( s_time <= cur_time && cur_time <= s2_time ) {
							si.attr("class", "onair");
							cls = "onairtxt";
							d_time = "ON AIR";
						}						
						
						si.html('<a href="'+url+'" class="' + cls + '" title="' + d_time + '">' + 	d_time +': </a><a href="'+url+'" class="schi" title="' + $(this).find('title').text() + '">' + $(this).find('title').text()+'</a>').appendTo('#schlist');
						gcount++;
						
					}
					
				});										
			}
		
		});
		
		return gcount;
	}
	
	$(document).ready(function(){
		$.ajax({
			type: "GET",
			url: "http://www.wbur.org/wp-content/themes/wbur/data/schedule.php",
			dataType: "xml",
			success: function(xml) {						
				
				var cur_time, cur_date, cur_id, next_id, gcount;
				cur_date = $(xml).find('schedules').attr('current-date');
				//cur_time = new Date(cur_date + ' ' + '18:00'); 
				cur_time = new Date(cur_date + ' ' + $(xml).find('schedules').attr('current-hour'));
				cur_id = $(xml).find('schedules').attr('current-id');	
				next_id = $(xml).find('schedules').attr('next-id');	
				gcount = 0;

				gcount = AddScheduleItems( $(xml), cur_id, cur_time, cur_date, gcount, false);				
				if(gcount < sched_size) {
					gcount = AddScheduleItems( $(xml), cur_id, cur_time, cur_date, gcount, true);
				}
			}
		});
	});
	
	/* Mez */
	function mez_itemLoadCallback(carousel, state)
	{
		// Check if the requested items already exist
		if (carousel.has(carousel.first, carousel.last)) {
			return;
		}
	
		jQuery.get(
			'http://www.wbur.org/wp-content/themes/wbur/data/mez.xml',
			{
				first: carousel.first,
				last: carousel.last
			},
			function(xml) {
				mez_itemAddCallback(carousel, carousel.first, carousel.last, xml);
			},
			'xml'
		);
	};
	
	function mez_itemAddCallback(carousel, first, last, xml)
	{
		var tot = parseInt($('item', xml).find('total').text())
		carousel.size();
	
		$('item', xml).each(function(i) {											  
			carousel.add(first + i, mez_getItemHTML(jQuery(this)));
		});
	};

	function mez_getItemHTML(item)
	{
		
		return '<div class="ar slitem-txt">' +
		'<div class="slitem-src">' + item.find('program').text() + '</div><a href="' + item.find('link').text() + '">' + item.find('title').text() + '</a>' +
		'</div>' +
		'<div class="slitem-img"><a href="' + item.find('link').text() + '"><img src="' + item.find('url').text() + '" width="206" height="92" text="' + item.find('title').text() + '" alt="' + item.find('title').text() + '" /></a></div>';						
	};
	
	//$(document).ready(function() {
		//$('#featdum').remove();
		//$('#mez').jcarousel({
			//scroll: 1,
			//auto: 7,
			//itemLoadCallback: {onBeforeAnimation: mez_itemLoadCallback}
		//});
	//});


	/* Tabbed view */
	$(document).ready(function () {
		var tabContainers = $('div.tabs > div');
		tabContainers.hide().filter(':first').show();
		
		$('div.tabs ul.tabNavigation a').click(function () {
				tabContainers.hide();
				tabContainers.filter(this.hash).show();
				$('div.tabs ul.tabNavigation a').removeClass('selected');
				$(this).addClass('selected');
				return false;
		}).filter(':first').click();
	});

	/* program sub nav */
	$(document).ready(function(){
		
		$("#nprograms").click(function(event) { //When trigger is clicked...
			
			event.preventDefault();

			//Following events are applied to the subnav itself (moving subnav up and down)
			$("UL.progsublist").slideDown('fast').show(); //Drop down the subnav on click
			
			$("UL.progsublist LI A").click(function(event) {
				window.location.href = $(this).attr("href");
			});
										   
			$(this).parent().hover(function() {
			}, function(){
				$("UL.progsublist").slideUp('slow'); //When the mouse hovers out of the subnav, move it back up
			});
			
			return true;
		});
	
	});
	
	/* Transcript slide toggle */
	
	$(document).ready(function(){	
		$("DIV.transcripthead").click(function () {
			$("DIV.transcript").slideToggle("fast");
			$("DIV.transcripthead DIV").toggle();
		});	
	});


	/* Pop-Up AHREF handler */
	$(document).ready( function() {
									  
		$("a[rel='pop-up-mediaplayer']").click(function () {
			var features = "height=350,width=660,scrollTo,resizable=0,scrollbars=0,location=0,top=100,left=200";
			newwindow=window.open(this.href, 'MediaPlayer', features);
			return false;
		});
		
		$("#main a[rel='pop-up-emtf']").click(function () {
			var features = "height=530,width=440,scrollTo,resizable=0,scrollbars=0,location=0,top=200,left=200";
			newwindow=window.open(this.href, 'EmailToFriend', features);
			return false;
		});

		$("#main a[rel='pop-up-media']").click(function () {
			var features = "height=650,width=800,scrollTo,resizable=1,scrollbars=1,location=1,top=200,left=200";
			newwindow=window.open(this.href, 'MediaGeneral', features);
			return false;
		});

		$("#main a[rel='pop-up-nprvideo']").click(function () {
			var features = "height=390,width=640,scrollTo,resizable=0,scrollbars=0,location=0,top=100,left=200";
			newwindow=window.open(this.href, 'NprVideo', features);
			return false;
		});

		$("#main a[rel='pop-up-special']").click(function () {
			var features = "height=544,width=816,scrollTo,resizable=0,scrollbars=0,location=0,top=200,left=200";
			newwindow=window.open(this.href, 'Special', features);
			return false;
		});

		$(".article a[rel='npr-enlargement']").click(function () {
			var features = "height=500,width=640,scrollTo,resizable=0,scrollbars=0,location=0,top=200,left=200";
			newwindow=window.open(this.href, 'NprEnlargement', features);
			return false;
		});
		
		$("a[rel='ext']").click( function() {
			window.open( $(this).attr('href') );
			return false;
		});
		
		$("a[rel='external']").click( function() {
			window.open( $(this).attr('href') );
			return false;
		})		
	});
	
	/* Boston.com Sports News */
	
	$(document).ready(function(){
		$.ajax({
			type: "GET",
			url: "http://api.wbur.org/bostondotcom/sports.aspx?callback=?",
			dataType: "json",
			success: function(data) {
				var allowedCats = ["Basketball","Football","Baseball"];
				$.each(data.items, function(i,item){
						
						var categories = item.categories;
						
						for (var cat in categories) {
							//$("#test").append(" " + categories[cat]);
						} 
						
						$("<li/>").html("<a href=\"" + item.link + "\">" + item.title + "</a>").appendTo("#bdc-sports");
						if ( i == 5 ) return false;
				});
			},
			error:function (xhr, ajaxOptions, thrownError){
				alert(xhr.status);
			} 

		});

	});