
/**
 * Format all dates identified by the date class.
 *  - Expects dates in the format: YYYY-MM-DD
 *
 * BEFORE:
 * <span class="date">2007-08-16</span>
 *
 * AFTER:
 * <span class="date">Thursday, August 16th, 2007</span> 
 */
$(function(){
	if( FLAG_CONVERT_DATES ) {
		$(".dates li").each(function(){
			var date = $(this).text();
			if(date != null && date != ""){
				var d = new Date(date.substring(0,4),date.substring(5,7)-1,date.substring(8,10));
				$(this).text(d.formatDate("l, F jS, Y"));
			}
		});
	}
});

$(function(){
		$(".datetime").each(function(){
			var date = $(this).text();
			if(date != null && date != ""){
				var d = new Date(date);
				$(this).text(d.formatDate("F jS"));
				$(this).show();
			}
		});
});

/**
 * Schedule Display
 *
 */
var schedule;
var currentDate = new Date();
function showSchedule(view) {
	var target;
	if(schedule) {
		schedule.options.view = view;
		schedule.display("#"+view);
	} else {
		$.get( "/shared/schedule.xml", function(xml, statusText){ 
			if(statusText == "success")	{
				schedule = new Schedule(xml, { now: currentDate });
				showSchedule(view);
			} else {
				// XML not successfully retrieved
			}
		});
	}
}
				

/**
 * Look for media - convert to tabs and appropriate display
 */
$(function(){
	var tabinst = 0;
	$('ul.media').each(function(){
		
		$links = $(this).find('li a');
		if($links.length == 0) return;
		
		var count = 0;
		var mediatabs = $('<ul id="#mediatabs-'+tabinst+'"></ul>');
		tabinst++;
		
		if($links.length > 1) {
			$links.each(function(){
				count++;
				var tabid = 'tab-'+tabinst+'-'+count;
				$(this).parent().attr("id",tabid);
				$(this).media();
				mediatabs.append('<li><a href="#'+tabid+'" title="'+this.text+'">&nbsp;'+count+'&nbsp;</a></li>');
			});
			$(this).before(mediatabs);
			mediatabs.tabs({ fxFade: true });
		} else {
			var src = $links.attr("href");
			$links.media();
			$('ul.media').append("<p class='download'><a href='"+src+"' title='Having problems? Click here to play.'>[download]</a> <small>(To download: Right click-&lt;Save as..)</small></p>");
		}
	});
});

/**
 * Archive Tabs
 */
//$(function(){ archiveTabs() });
function archiveTabs(opts) {
	
	var tabinst = 0;
	$('.archive-tabs .archive').each(function(){
		
		$records = $(this).find('.record');
		if($records.length == 0) return;
		
		var count = 0;
		var archiveNav = $('<ul id="#archive-nav-'+tabinst+'" class="archive-nav"></ul>');
		tabinst++;
		
		if($records.length > 1) {
			$records.each(function(){
				count++;
				var tabid = $(this).attr("id");
				if(typeof(tabid) == 'undefined') {
					tabid = 'archive-tab-'+tabinst+'-'+count
					$(this).attr("id",tabid);
				}
				archiveNav.append('<li><a href="#'+tabid+'">&nbsp;'+count+'&nbsp;</a></li>');
			});
			$(this).append(archiveNav);
			archiveNav.tabs(opts);
		}
	});
	
}
 
/**
* Fancify pagination
*/
var PAGINATION_BUFFER = 3;
var PAGINATION_CLEANUP_THRESHOLD = 5;
function fancifyPagination(id) {
	
	$(id+" .archivePagination").each(function(){
	
		var $pages = $(this).find('li');
		var $curr = $(this).find('.current');
		var $next = $curr.next().clone();
		var $prev = $curr.prev().clone();
		
		// Reduce the navigation if there are more than 5 pages.
		if($pages.length > PAGINATION_CLEANUP_THRESHOLD) {
			$nextAll = $curr.nextAll();
			$prevAll = $curr.prevAll();
			
			if($nextAll.length > PAGINATION_BUFFER) {
				$nextAll.slice(PAGINATION_BUFFER).hide();
			}
			
			if($prevAll.length > PAGINATION_BUFFER) {
				$prevAll.slice(PAGINATION_BUFFER).hide();
			}
		}
		
		// Add Next page navigation
		if($next.length) {
			$('a',$next).html("Next &raquo;");
			$next.addClass("last");
			$(this).append($next);
		}
		
		// Add Previous page navigation
		if($prev.length) {
			$('a',$prev).html("&laquo; Prev");
			$prev.addClass("first");
			$(this).prepend($prev);
		}
		
																								 
  });
	
}



/**
* Greybox links (aka Lightbox)
*
*/
var GB_ANIMATION = true;
$(function(){
	$("a.greybox").click(function(){
		var t = this.title || $(this).text() || this.href;
		var meta = $.metadata.get(this);
    var h = meta.h || 470;
    var w = meta.w || 600;
		GB_show(t,this.href,h,w);
		return false;
	});
});

/*
$(function(){
	$(".aintro .title").hoverIntent({
		 sensitivity: 1, // number = sensitivity threshold (must be 1 or higher)
		 interval: 50, // number = milliseconds for onMouseOver polling interval
		 over: showSub, // function = onMouseOver callback (required)
		 timeout: 150, // number = milliseconds delay before onMouseOut
		 out: hideSub // function = onMouseOut callback (required)
	});
});

function showSub() {
	$(this).find(".subnav").fadeIn("fast");
}

function hideSub() {
	$(this).find(".subnav").fadeOut("fast");
}*/

function openListenLive(file) {
	var location = "";
	if(file != null && file.length > 0) {
		location = MEDIAPLAYER_URL + "?file=" + file;
	} else {
		location = MEDIAPLAYER_URL;
	}
	var win = window.open(location,'ListenLiveWindow','width='+MEDIAPLAYER_WIDTH+',height='+MEDIAPLAYER_HEIGHT+',status=0,scrollbars=0,resizable=0');
	win.focus();
	return false;
}

function openListenLive2(file) {
	var location = "";
	if(file != null && file.length > 0) {
		location = MEDIAPLAYER2_URL + "?file=" + file;
	} else {
		location = MEDIAPLAYER2_URL;
	}
	var win = window.open(location,'ListenLiveWindow','width='+MEDIAPLAYER2_WIDTH+',height='+MEDIAPLAYER2_HEIGHT+',status=0,scrollbars=0,resizable=0');
	win.focus();
	return false;
}

/**
 * sIFR - Flash Text Replacement
 * Replaces all page titles (h2) with flash
 * text using an appealing font face.
 */
var sifr_white_opts = { src: '/css/skin/sifr/sifr.swf', 
												flashvars: { 
														textcolor: "#FFFFFF",
														linkcolor: "#FFFFFF",
														hovercolor: "#FFFFFF"
												}
											};
var sifr_dark_opts = { src: '/css/skin/sifr/sifr.swf', 
												flashvars: { 
														textcolor: "#474747",
														linkcolor: "#474747",
														hovercolor: "#474747"
												}
											};

function sifr_helper(htmlOptions) {
	htmlOptions.flashvars.txt = this.innerHTML.toUpperCase();
	this.innerHTML = '<div>'+this.innerHTML+'</div>';
	var $alt = $(this.firstChild);
	htmlOptions.height = 23; //$alt.height();
	htmlOptions.flashvars.h = 23; //$alt.height();
	htmlOptions.width = $alt.width();
	htmlOptions.flashvars.w = $alt.width();
	htmlOptions.wmode = "transparent";
	$alt.addClass('alt');
	$(this)
			.addClass('flash-replaced')
			.prepend($.fn.flash.transform(htmlOptions));
}

$(function(){
    //$('#hp-audio h2').flash(sifr_dark_opts, { version: 7 } ,sifr_helper);
    $('#page .drk h2').flash(sifr_dark_opts, { version: 7 } ,sifr_helper);
    $('#page h2:not(.flash-replaced)').flash(sifr_white_opts, { version: 7 } ,sifr_helper);
    $('#page .fancy-title h3:not(.flash-replaced)').flash(sifr_white_opts, { version: 7 } ,sifr_helper);
});

/* Scroll to named anchors with the class="scroll" */
$(document).ready(function() {
  $('a.scroll').each(function() {
	  var $target = $(this.hash);
	  if ($target) {
			var targetOffset = $target.offset().top;
			$(this).click(function() {
		  	$('html, body').animate({scrollTop: targetOffset}, 400);
		  	return false;
			});
	  }
  });
});

