$(document).ready(function(){
	// Seasons tabs hover
	$(".seasonsTabs li").hover(
		function(){
			$(this).addClass("ui-state-hover");
		},
		function(){
			$(this).removeClass("ui-state-hover");
		}
	);
	
	// News archive
	$('#showNewsArchive').click(function(){
		$(this).hide();
		$('#newsArchive').show();
	});
	$('#newsArchive').hide().removeClass('hidden');
	
	// Sortable tables
    
    // Standings
	$(".standings").tablesorter({
        sortList: [[8, 1]],
		sortMultiSortKey: 'ctrlKey',
		debug: true,
		// BUG FIX!
		headers: {
			1: {sorter: 'digit'},
			2: {sorter: 'digit'},
			3: {sorter: 'digit'},
			4: {sorter: 'digit'},
			5: {sorter: 'digit'},
			6: {sorter: 'digit'},
			7: {sorter: 'digit'},
			8: {sorter: 'digit'}
		}
    });
    // Player stats
	$(".playerStats").each(function(index){
    	$(this).tablesorter({
	        sortList: [[3, 1], [1, 1]],
			sortMultiSortKey: 'ctrlKey',
			// BUG FIX!
			headers: {
				1: {sorter: 'digit'},
				2: {sorter: 'digit'},
				3: {sorter: 'digit'},
				4: {sorter: 'digit'}
			}
		});
    });
    // Goalies
    $(".goalies").tablesorter({
        sortList: [[6, 0]],
		sortMultiSortKey: 'ctrlKey',
		// BUG FIX!
		headers: {
			1: {sorter: 'digit'},
			2: {sorter: 'digit'},
			3: {sorter: 'digit'},
			4: {sorter: 'digit'},
			5: {sorter: 'digit'},
			6: {sorter: 'digit'}
		}
    });
    
    // Create new player button
    $("#createPlayerButton").button();
    
    // Create new match button
    $("#createMatchButton").button();
    
    // Delete admin stats item
    $(".adminStatsItem .deleteAdminStatsItem").each(initializeDeleteAdminStatsItemButton);
    
    // Add admin stats item
    $(".addAdminStatsItem").each(function(){
    	$(this)
	    	.button({
	    		icons: {
	    			primary: "ui-icon-plusthick"
	    		}
	    	})
	    	.click(function(){
	    		$table = $(this).next().clone();
	    		$table.find("button").each(initializeDeleteAdminStatsItemButton);
	    		$(this).prev().append($table.show());
	    	});
    });
    
    // Submit admin stats
    $("#saveMatchStats")
    	.button()
    	.click(function(){
    		// Remove all templates
    		$(".adminStatsTable:hidden").remove();
    		// Format times
    		$('[name="time"]').each(function(){
    			$(this).val("00:" + $(this).val());
    		});
    		// Rename all "name" fields in order to save every field
    		$(".adminStatsGoal").each(function(i){
    			$(this).find("[name]").each(function(j){
    				$(this).attr("name", "goals[" + i + "][" + $(this).attr("name") + "]");
    			});
    		});
    		$(".adminStatsPenalty").each(function(i){
    			$(this).find("[name]").each(function(j){
    				$(this).attr("name", "penalties[" + i + "][" + $(this).attr("name") + "]");
    			});
    		});
    	});
    	
	function initializeDeleteAdminStatsItemButton(){
		$(this)
	    	.button({
	    		icons: {
	    			primary: "ui-icon-closethick"
	    		},
	    		text: false
	    	})
	    	.click(function(){
	    		$(this).parents(".adminStatsTable").remove();
	    	});
	}
});

