$(function(){
	$('.kjo-link').remove();

	$('#player-spotlight h3 a').player_spotlight_refresh({ image: '/wordpress/wp-content/themes/scottish/images/silhouette.png' });
	
	$('#stats').setup_tabs();
	
	$('#filter a[href="#schedule"]').toggle_schedule();
	
	$('.gag').swap_images({
		hover: '/wordpress/wp-content/themes/scottish/images/silhouette-stache.png',
		normal: '/wordpress/wp-content/themes/scottish/images/silhouette.png'
	});
});

$.fn.player_spotlight_refresh = function(settings) {
	settings = $.extend({
		image: 'normal.png'
	}, settings);

	$(this)
		.show()
		.click(function(e) {
			e.preventDefault();
			$.getJSON(
				'/players/spotlight/?player='+$('input[name=current_player_spotlight]').val(),
				function(data){
					var spotlight = $('#player-spotlight');
					spotlight.find('input[name=current_player_spotlight]').val(data.player.id);
					
					if(data.player.image)
						spotlight.find('.profile').attr('src', '/wordpress/wp-content/uploads/players/'+data.player.image);
					else
						spotlight.find('.profile').attr('src', settings.image);
					spotlight.find('h4').html(data.player.first_name + ' ' + data.player.last_name);
					spotlight.find('.height').html('<strong>Height:</strong> ' + data.player.height);
					spotlight.find('.weight').html('<strong>Weight:</strong> ' + data.player.weight);
					spotlight.find('.century').html('<strong>Century Club:</strong> ' + data.player.century_club + ' games played');
					spotlight.find('p a').attr('href', '/players/profile/?player=' + data.player.id);
				}
			);
		});
}

$.fn.setup_tabs = function() {
	$(this).each(function(t) {
		var tabsDiv = $(this);
		var list = '';
	
		//for the h2 in each tab div
		$(tabsDiv).find("h3").each(
			function(h){
				var tabId = "tab" + t + "-" + h;
				$(this).html('<a id="'+tabId+'" tabindex="-1">'+ $(this).text() + '</a>');
				list += '<li><a href="#' + tabId + '">' + $(this).text() + '</a></li>';
			}
		);
		$(tabsDiv).prepend('<ul class="tabsMenu">' + list + '</ul>').find(">div").addClass("tab").hide();
		var current = $('input[name="team"]').val();

		$(tabsDiv).find(".tab").eq(current).show();
		$(tabsDiv).find(".tabsMenu>li").eq(current).toggleClass("current");
		//for each tabs menu link
		$(tabsDiv).find(">ul>li>a").each(
			function(a){
				$(this).click(
					function(e){	
						e.preventDefault();
						var target = $(this).attr("href");
						$(tabsDiv).find(">ul>li.current").removeClass("current").find(">a>span").remove();
						$(this).blur();
						$(tabsDiv).find(".tab:visible").hide();
						$(tabsDiv).find(".tab").eq(a).show();
						$('input[name="team"]').val(a);
						$(this).parent().addClass("current");
						//set focus to target h2 anchor inside the relevant, previously hidden tab -- NOTE: focus is being set AFTER the span is written to the tabs menu li anchor
						$($(this).attr("href")).focus();
					}
				);
			}
		);
	});
}

$.fn.swap_images = function(images) {
	images = $.extend({
		hover: 'hover.png',
		normal: 'normal.png'
	}, images);
	
	$(this).hover(
		function() {
			$(this).attr('src', images.hover);
		},
		function() {
			$(this).attr('src', images.normal);
		}
	);
}

jQuery.fn.toggleText = function(needles) {
	return $(this).each(function() {
		if (typeof needles == 'string') { needles = [needles]; }
		
		for (var needleKey=0; len=needles.length,needleKey<len; needleKey++) {
			var text = $(this).text();
			var regex = new RegExp(needles[needleKey], 'i')
			
			if (text.match(regex)) {
				$(this).text(text.replace(regex, ''));
			} else {
				$(this).text(text+' '+needles[needleKey]);
			}
		}
	});
};

$.fn.toggle_schedule = function(e) {
	$('tr.past').toggle();
	$(this).click(function(e) {
		$('tr.past').toggle();
		$(this).toggleText(['Hide Past Matches','Show Past Matches']);
		e.preventDefault();
	});
}