$(document).ready( function(){
	
	$("#search_but").click( function(){
		//var url = $(this).attr('action') + '/' + $("#top_search").val();
		var url = '/Search/' + $("#top_search").val();
		
		window.location = url;
	});
	
	$("#top_search").keypress( function(e){
		if(e.which == 13){
			//var url = $(this).attr('action') + '/' + $("#top_search").val();
			var url = '/Search/' + $(this).val();
			
			window.location = url;
		}
	});
		
	$(".cal_next").live("click", function(){
		changeCal($(this).attr('id'), false);
	});
	$(".cal_back").live("click", function(){
		changeCal($(this).attr('id'), false);
	});
	
	/*
	$("#cal_month").live("change", function(){
		changeCal($(this).find(':selected').val());
	});
		
	var evt = $.browser.msie ? "click" : "change";
	
	if(evt == 'click'){
		$("#cal_sport > option").live("click", function(){	
			alert('here');
			var date = $("#cal_month option:selected").val();
			changeCal(date);
		});
	}*/
	
	$(".change_cal").live("click", function(){
		
		var month = $("#cal_month > option:selected").val();
				
		changeCal(month, true);
		
	});
	
	/*
	$("#cal_sport").live("change", function(){	
		alert('here');
		var date = $("#cal_month option:selected").val();
		changeCal(date);
	});
	*/
	if (!$.browser.msie || ($.browser.msie && $.browser.version.substr(0,1)>6)) {
		$(".cal_info").live('mouseover', function(){		
			$("#cal_num_events").html('There are <strong>'+ $(this).attr('data') +'</strong> event/s on this day');		
		}).live('mouseout', function(){
			$("#cal_num_events").html('Select a highlighted date');
		});
	}
	
	
	/******
	show and hide the event listing and sports calendar
	******/
	$('.cal_row_show').live("click", function(){
		showRows($(this), 'cal_row');		
	});
	
	$('.cal_row_hide').live("click", function(){
		hideRows($(this), 'cal_row');		
	});
	
	$('.events_show').live("click", function(){
		showRows($(this), 'events');		
	});
	
	$('.events_hide').live("click", function(){
		hideRows($(this), 'events');		
	});
	
});

function showRows(element, name){
	var rowval = element.attr('id');
	var rowcount = 1;
	$('.'+ name +'_'+rowval).each( function(){			
		var rowclass = rowcount%2 ? ' grey ' : ' ';
		$(this).attr('class',name +'_'+rowval+rowclass);
		rowcount++;
	});
	
	element.attr('class',name+'_hide').html('hide all');
	
}

function hideRows(element, name){
	var thecount = 1;	
	var rowval = element.attr('id');
	
	$('.'+name+'_'+rowval).each( function(){
		if(thecount > 5){
			//$(this).slideUp();
			$(this).attr('class','hidden '+name+'_'+rowval);
		}
		thecount++;
	});	
	element.attr('class',name+'_show').html('show more');
	/*
	var rowval = element.attr('id');
	var rowcount = 1;
	$('.'+ name +'_'+rowval).each( function(){			
		var rowclass = rowcount%2 ? ' grey ' : ' ';
		$(this).attr('class',name +'_'+rowval+rowclass);
		rowcount++;
	});
	
	element.attr('class',name+'_hide').html('hide all');
	*/
}

//filter is to detect if filter is pressed
function changeCal(thedate, filter){
	
	var sport = '';
	var pg_sport = '';
	
	if(filter == true){
		sport = $("#cal_sport option:selected").val();
	}else{
		pg_sport = $("#pg_sports").val();
	}
				
	$("#calender_overlay").fadeIn(1000, function(){
		
		var theHeight = parseInt($("#calendar_table").height());
		$(this).height(theHeight - 72);
					
		//wait a while
		setTimeout( function(){
			//$("#calendar_table").html('&nbsp;');
			$("#calendar_table").load(
				"/scripts/ajax/refresh_calendar.php",
				{ month: thedate, sports: sport, pgsports: pg_sport }
			);
			
			$("#calender_overlay").fadeOut(1000);
			
		}, 500);
		
		
	});
	
	
}
