$(document).ready(function(){
	var month_names = new Array ( );
	month_names[0] = "January";
	month_names[1] = "February";
	month_names[2] = "March";
	month_names[3] = "April";
	month_names[4] = "May";
	month_names[5] = "June";
	month_names[6] = "July";
	month_names[7] = "August";
	month_names[8] = "September";
	month_names[9] = "October";
	month_names[10] = "November";
	month_names[11] = "December";
	
	$('table.calendar').each(function(){
		var $thisTable = $(this);
		var $thisCells = $(this).find('tbody td');
		var tournaments = {};
		$thisCells.each(function(){
			var $thisCell = $(this);
			var cell_id = $thisCell.attr('id');
			var cell_date = parseInt($thisCell.attr('id').replace(/-/g, '').replace(/calendarDay/gi, ''), 10);
			tournaments[cell_date] = {};
			tournaments[cell_date]['tuid'] = [];
			tournaments[cell_date]['flag'] = [];
			for (var i=0, l=tournBase.current.length; i<l; i++) {
				var start = parseInt(tournBase.current[i].start, 10);
				var end = parseInt(tournBase.current[i].end, 10);
				
				if (start <= cell_date && end >= cell_date) {
					if (start == cell_date) {
						tournaments[cell_date]['flag'].push('flagged');
					}
					tournaments[cell_date]['tuid'].push(i);
				}
			};
			if (tournaments[cell_date]['tuid'].length>0) {
				
				var data_upcoming = [];
				var data_started = [];
				var prizes = [];
				var prizes_filter = /^\$/;
				for (var i = 0, l=tournaments[cell_date]['tuid'].length; i<l; i++) {
					var tn_string = '<ul><li class="tna-title"><strong>'+tournBase.current[tournaments[cell_date]['tuid'][i]].name+'</strong> - '+tournBase.current[tournaments[cell_date]['tuid'][i]].prize+' prize</li><li><a href="play/download/'+tournBase.current[tournaments[cell_date]['tuid'][i]].casino+'" target="_blank">Participate</a></li></ul>';
					if (tournBase.current[tournaments[cell_date]['tuid'][i]].start == cell_date) {
						data_upcoming.push(tn_string);
					} else {
						data_started.push(tn_string);
					}
					if (tournBase.current[tournaments[cell_date]['tuid'][i]].start == cell_date) {
						var prize = tournBase.current[tournaments[cell_date]['tuid'][i]].prize;
						if (prizes_filter.test(prize)) {
							prize = parseInt(prize.replace(/[$,]/g, ''), 10);
							prizes.push(prize);
						}
					}
				}
				var max_prize = Math.max.apply(Math, prizes);
				
				
				function calamityTrigger(q){
					$('.tournament-appendix').remove();	
					$('body').unbind('mousemove click');
					function showAppendix(e) {
						if ($(e.target).is('#'+cell_id+'')) {
							var cell_date_str = new String(cell_date);
							cell_date_str = ''+cell_date_str.substring(4, 6)+'/'+cell_date_str.substring(6)+'/'+cell_date_str.substring(0, 4)+'';
							
							var cell_jsdate = new Date(cell_date_str);
							var data_str = '<div class="tournament-appendix" id="ta-'+cell_date+'">';
							data_str += '<h3>'+cell_jsdate.getDate()+' '+month_names[cell_jsdate.getMonth()]+', '+cell_jsdate.getFullYear()+' Tournaments</h3>';
							data_str += '<h4>Starting this day:</h4>'+data_upcoming.join('')+'';
							if (data_started.length>0) {
								data_str += '<h4>In progress:</h4>'+data_started.join('')+'';
							}
							data_str += '<a class="tna-schedule-lnk" href="tournaments-schedule-day.htm">Full schedule for this day</a>';
							data_str += '</div>';
							$('body').append(data_str);
							$('.tournament-appendix').css({'top':''+(q.pageY+1)+'px', 'left':''+(q.pageX+1)+'px'});
							$('.tna-schedule-lnk').click(function(f){
								f.preventDefault();
								$('#tn-date-post input').val(cell_date);
								$('#tn-date-post').submit();
							});
						};
					};
					if ($('body#main_page').length>0) {
						$('body').bind('click', showAppendix);
					} else {
						$('body').bind('mousemove', showAppendix);
					}
					$('body').mousemove(function(e){
						if (!$(e.target).is('#ta-'+cell_date+', #ta-'+cell_date+' *') && !$(e.target).is('#'+cell_id+'')) {
							$('.tournament-appendix').remove();	
							$('body').unbind('mousemove click');
						}
					});
				};
				
				if ($('body#main_page').length>0) {
					$(this).bind('click', calamityTrigger);
				} else {
					$(this).bind('mouseenter', calamityTrigger);
					$(this).bind('click', function(){
						$('#tn-date-post input').val(cell_date);
						$('#tn-date-post').submit();
					});
				};
				
			};
			if (tournaments[cell_date]['flag'].length>0) {
					$(this).addClass('flagged');
					if (max_prize >= 100000) {
						$(this).addClass('prizea');
					} else if (max_prize >= 40000) {
						$(this).addClass('prizeb');
					} else if (max_prize >= 1000) {
						$(this).addClass('prizec');
					};
			};
			
		});
		
	});
	
});