function debug(message) {
	if(window.console) {
		console.log(message);
	}
}

jQuery(document).ready(function($) {  
	
	$(".read-more").live('click', function() {
		
		// Hide the read more link
		$(this).hide();
		
		// If there's more content to show, then show it
		if($('.more-content')) {
			$('.more-content').fadeIn('medium');
		}

	});
	
	// Day highlighter for the calendar
	$("#large_calendar").each(function() {
	  var d = new Date();
	
	  debug('Performing calendar CLOSED highlights.');	  
	  jQuery("td:contains('CLOSED')").addClass('closed');

	  // TODO: Make sure it's the proper month too!
 	  debug('Performing calendar CURRENT DAY highlight: Day = ' + d.getDate() + ' Month = ' + d.getMonth());	  
	  var todays_day = d.getDate();
	  var selector = "td > h5:contains('" + todays_day + "')";
	  window.day_highlighted = false;
	
	  // Give the containing cell the proper highlight class
	  var highlighted = false;
	  $(selector).each(function() {
		if(!highlighted) {
			debug('Evaluating day: ' + $(this).html());
	        if($(this).html() == todays_day) {
				$(this).parent().addClass('this-is-today');
			    debug('That\'s the one!');
				highlighted = true;
			}
	  	}
	  });
	});
	
  // Email collector
  $("#email_submitter").click(function(){

    var email = $('#email_address').val();
    if(is_valid_email(email)) {
  	  var submitURL = "http://sdtta.org/cms/wp-content/themes/inove/helpers/emailer.php?email=" + email + "&action=add";
      $('#email_submitter').attr('disabled','disabled');
	  $('#email_submitter').val('Adding...');
   	  $.get(submitURL, function() {

		$('#email_submitter').val('Added!');
	  });
    }
    else { 
	  alert('The email address you provided seems to be invalid. Please correct it and try again.')
    }
  });
	
	  /****** Handle the clicking of the player to reveal their graph ******/
	  var targetPlayer;
	  var targetHeight = 260;
	  var player_window_height = 0;
	  var player_data_loaded = new Array();
	  var player_graph_state = new Array();

	  $(".player td:not(.player_picture_thumb)").live('click', function() { 

		targetPlayer = $(this).parent().attr("id"); 
	    debug('Clicked player box: ' + targetPlayer);
		player_window_height = $("#row_ratings_" + targetPlayer).css("height");
		// Load the player's graph if it hasn't been loaded already
		if(player_data_loaded[targetPlayer] == undefined || !player_data_loaded[targetPlayer]) {
		    debug('Loading player graph - ' + targetPlayer);
			player_data_loaded[targetPlayer] = true;
			$("#ratings_" + targetPlayer).html("<iframe scrolling='no' frameborder='0' width='405' height='205' src='http://sdtta.org/swfcharts/ratings_chart.php?name=" + targetPlayer + "'></iframe>");
			player_graph_state[targetPlayer] = "up";
	    }

		// Show/Hide the graph window
		if(player_graph_state[targetPlayer] == "up") {
			debug("Opening Graph Window");
			$("#row_ratings_" + targetPlayer).css('width','100%');
		    $("#row_ratings_" + targetPlayer).animate({ height: targetHeight + "px" }, 500, function() { player_graph_state[targetPlayer] = "down"; });
		}
		else {
			debug("Closing!");
			$("#ratings_" + targetPlayer).html('');
			$("#row_ratings_" + targetPlayer).animate({ height: "0px" }, 250, function() { player_graph_state[targetPlayer] = "up"; });
			player_data_loaded[targetPlayer] = false;
		}    
	  });
	
	
});


function is_valid_email(email) {
	return (email.search(/^[^@]+@[^@]+.[a-z]{2,}$/i) != -1);
}




var ratings_chart = false;	
function get_rating_info(playerName) {
    	return;
	if (AC_FL_RunContent == 0 || DetectFlashVer == 0) {
		alert("This page requires AC_RunActiveContent.js.");
	} else {
		var hasRightVersion = DetectFlashVer(requiredMajorVersion, requiredMinorVersion, 45);
		if(hasRightVersion) { 
			return AC_FL_RunContent(
				'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,45,0',
				'width', '400',
				'height', '250',
				'scale', 'noscale',
				'salign', 'TL',
				'bgcolor', '#FFF',
				'wmode', 'transparent',
				'movie', 'charts',
				'src', 'charts',
				'FlashVars', 'xml_source=http://sdtta.org/cms/wp-content/themes/inove/helpers/player_data.php?name=' + playerName + 'unique_id=4', 
				'id', 'my_chart',
				'name', playerName + '_chart',
				'menu', 'true',
				'allowFullScreen', 'true',
				'allowScriptAccess','always',
				'quality', 'high',
				'align', 'middle',
				'pluginspage', 'http://www.macromedia.com/go/getflashplayer',
				'play', 'true',
				'devicefont', 'false'
				);
		} else { 
			var alternateContent = 'This content requires the Adobe Flash Player. '
			+ '<u><a href=http://www.macromedia.com/go/getflash/>Get Flash</a></u>.';
			alert(alternateContent); 
		}
	}

}

