
/**
 * 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(){
	$(".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"));
		}
	});
});


/**
 * Add NowPlaying to the page
 */
var np_targetid = 'np-song-info';
$(function(){
	if( FLAG_NOWPLAYING ) {
		$.getScript("http://nowplaying.rogersradiointernet.com/public/widgets/nowplaying.json.js");
	}
	npframe = document.createElement("iframe");
	npframe.src = NOWPLAYING_IFRAME_URL;
	$npframe = $(npframe);
	// frameBorder <- camel casing mandatory for Internet Explorer
	$npframe.attr( {scrolling: "no", marginWidth: "0", marginHeight:"0", frameBorder:"0", border:"0", allowtransparency: "true", align: "center"} );
	$npframe.width("100%");
	$npframe.height(25);
	$npframe.css({width:"100%",height:"25px"});
	$("#bds-np").append(npframe);
});

/**
 * 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);
	}
});

/**
 * Login input fields behaviour. 
 */
 
var loyaltyUsr = "loyaltyUsr";
var loyaltyPwd = "loyaltyPwd";
var loyaltyRem = "loyaltyRem";
var loyaltyParam = { expires: 60 };

function initLoyaltyForm() {
		
	$("#username-field").click(function(){ hideFieldLabel("username",true); });
	$("#username").focus(function(){ hideFieldLabel("username",false); });
	$("#username").blur(function(){	checkFieldLabel("username");});
	
	$("#password-field").click(function(){ hideFieldLabel("password",true); });
	$("#password").focus(function(){ hideFieldLabel("password",false); });
	$("#password").blur(function(){	checkFieldLabel("password");});
	
	if($.cookie(loyaltyRem)) {
		$("#rememberme").attr('checked','checked');	
		$("#username").val($.cookie(loyaltyUsr));
		$("#password").val($.cookie(loyaltyPwd));
	}
	
	$("#loyalty").submit(function(){
		
		var usr = $("#username").val();
		var pwd = $("#password").val();
		var empty = usr == "" || pwd == "";
		
		if(!empty) {
			
			if($("#rememberme").attr('checked')) {
				$.cookie(loyaltyUsr,usr,loyaltyParam);
				$.cookie(loyaltyPwd,pwd,loyaltyParam);
				$.cookie(loyaltyRem,"true",loyaltyParam);
			} else {
				$.cookie(loyaltyUsr,null);
				$.cookie(loyaltyPwd,null);
				$.cookie(loyaltyRem,null);
			}
			
			return true;
			
		// Display an error message prompting for username and password
		} else {
			
			if(usr == "") {
				flashFieldLabel("username");
			}
			if(pwd == "") {
				flashFieldLabel("password");
			}
			
			return false;
		}
		
	});

	setTimeout("checkFieldLabels()", 5000);

}

function flashFieldLabel(field) {
	$("#"+field+"-field").addClass("flash");
	setTimeout("$('#"+field+"-field').removeClass('flash')",100);
	setTimeout("$('#"+field+"-field').addClass('flash')",200);
}

function hideFieldLabel(field, setfocus) {
		$("#"+field+"-field label").hide();
		if(setfocus) {
			$("#"+field+"").focus();
			$("#"+field+"").select();	
		}
}

function checkFieldLabel(field) {
		if($("#"+field+"").val() == "") {
			$("#"+field+"-field label").show();
		} else {
			$("#"+field+"-field").removeClass("flash");
			hideFieldLabel(field);
		}
}
function checkFieldLabels() {
	checkFieldLabel("password");
	checkFieldLabel("username");
}

/**
 * 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();
		}
	});
});


/**
* 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_GATEWAY == true) ? MEDIAPLAYER_GATEWAY_URL : MEDIAPLAYER_URL;
	}
	var win = window.open(location,'ListenLiveWindow','width='+MEDIAPLAYER_WIDTH+',height='+MEDIAPLAYER_HEIGHT+',status=0,scrollbars=0,resizable=0');
	win.focus();
	return false;
}