
/**
 * 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
			}
		});
	}
}
				

/**
 * Google Analytics
 * Depends on jquery.ga.js and urchin.js
 */
$(function(){
	if( FLAG_GOOGLE_ANALYTICS ) {
		options = {
			domains: ANALYTICS_TRACKED_DOMAINS,
			restricted: ['ad.doubleclick.net'],
			fileTypes : ['zip','wmv','gz','mov','txt','ppt','doc','xls','mpg','mpeg','mp3','pdf','avi'],
			trackAnchors : true,
			trackFiles : true,
			trackForms : true,
			debug: FLAG_DEBUG
		};
		$(document).ga(_uacct,options);
	}
});

/**
 * 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 {
			$links.media();
		}
	});
});

/**
 * 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);
		}
	});
	
}
 


/**
* 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 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;
}

/**
 * sIFR - Flash Text Replacement
 * Replaces all page titles (h2) with flash
 * text using an appealing font face.
 */
var sifr_white_opts = { src: 'http://www.fan590.com/images/skin/sifr/sifr.swf', 
												flashvars: { 
														textcolor: "#FFFFFF",
														linkcolor: "#FFFFFF",
														hovercolor: "#FFFFFF"
												}
											};
var sifr_dark_opts = { src: 'http://www.fan590.com/images/skin/sifr/sifr.swf', 
												flashvars: { 
														textcolor: "#474747",
														linkcolor: "#474747",
														hovercolor: "#474747"
												}
											};

function sifr_helper(htmlOptions) {
	var flashvarstxt;
	var href = $("a",this).attr("href");
	if(href != null && typeof href != 'undefined') {
		href = $("a",this).attr("href");
		flashvarstxt = "<a href=\""+href+"\">"+$("a",this).text().toUpperCase()+"</a>";
	} else {
		flashvarstxt = this.innerHTML.toUpperCase();
	}
	htmlOptions.flashvars.txt = flashvarstxt;
	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);
    $('.extended h3').flash(sifr_dark_opts, { version: 7 } ,sifr_helper);
    $('#sidebar 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;
			});
	  }
  });
});

