$(function() {

	//console.log(decks);
	
	// Mode changed
	$('input[name=mode_id]').live('click', updateMode);	// IE HACK
	$('input[name=mode_id]').live('change', updateMode);
	$('input[name=mode_id]').live('keyup', function(){
		$(this).change();
	});
	updateMode();
	
	// SuperCollection changed
	$('#super_collection_id').live('click', updateSuperCollection);	// IE HACK
	$('#super_collection_id').live('change', updateSuperCollection);
	$('#super_collection_id').live('keyup', function(){
		$(this).change();
	});
	updateSuperCollection();
	
	// Shared changed
	$('input[name=share_deck]').live('click', updateShareDeck);	// IE HACK
	$('input[name=share_deck]').live('change', updateShareDeck);
	$('input[name=share_deck]').live('keyup', function(){
		$(this).change();
	});
	updateShareDeck();
	
	// Individual Super Collection changed
	$('.superCollectionSelect').live('click', updateIndividualSuperCollection);	// IE HACK
	$('.superCollectionSelect').live('change', updateIndividualSuperCollection);
	$('.superCollectionSelect').live('keyup', function(){
		$(this).change();
	});

	// Number of Opponents changed
	$('#numberOfOpponents').change(updateTeamNames);
	updateTeamNames();

	// Retain collection preferences
	for (i in player_super_collections) {
		var val = player_super_collections[i];
		$('.superCollectionSelect:eq('+i+')').val(val);
		$('.superCollectionSelect:eq('+i+')').change();
	}
	for (i in player_collections) {
		var val = player_collections[i];
		$('.collectionSelect:eq('+i+')').val(val);
	}

	$('.addTeam').live('click', function(e) {
		e.preventDefault();
		var numberOfOpponents = $('#numberOfOpponents').val();
		numberOfOpponents++;
		
		$('#numberOfOpponents').val(numberOfOpponents);
		$('#numberOfOpponents').change();
	});
	$('.removeTeam').live('click', function(e) {
		e.preventDefault();
		var numberOfOpponents = $('#numberOfOpponents').val();
		numberOfOpponents--;

		$('#numberOfOpponents').val(numberOfOpponents);
		$('#numberOfOpponents').change();
	});

});

function updateMode() {
	//console.log('updateMode');
	var mode_id = $('input[name=mode_id]:checked').val();
	var mode = decks[mode_id];
	var super_collections = mode.super_collections;
	var opts = '';
	for (j in super_collections) {
		var super_collection = super_collections[j];
		opts += '<option value="'+j+'">'+super_collection.name+'</option>';
	}
	$('#super_collection_id').html(opts);
	
	// WARNING! HACK HACK HACK. Removes "Current Squads" SuperCollection from SharePane
	$('.sharedeckPane #super_collection_id option[value=6]').remove();	// HACK! Removes "Current Squads" SuperCollection from SharePane (ODI)
	$('.sharedeckPane #super_collection_id option[value=7]').remove();	// HACK! Removes "Current Squads" SuperCollection from SharePane (Test)
	
	$('.includeUserCardsPane').hide();
	if (mode_id == 1) {
		// One Day
		$('.includeUserCardsPane').show();
	}
	$('.superCollectionSelect').each(function() {
		$(this).html(opts);
	});
	
	// WARNING! HACK HACK HACK. Removes "Top 50 by Statistic" SuperCollection from IndividualPanes
	$('.individualPane .superCollectionSelect option[value=2]').remove();	// HACK! Removes "Top 50 by Statistic" SuperCollection from IndividualPanes (ODI)
	$('.individualPane .superCollectionSelect option[value=3]').remove();	// HACK! Removes "Top 50 by Statistic" SuperCollection from IndividualPanes (Test)

	updateSuperCollection();
}

function updateSuperCollection() {
	var mode_id = $('input[name=mode_id]:checked').val();
	var super_collection_id = $('#super_collection_id').val();
	var collections = decks[mode_id].super_collections[super_collection_id].collections;
	var sharedOpts = '';
	var individualOpts = '';
	for (k in collections) {
		var collection = collections[k];
		if (!collection.only_allow_shared) {
			individualOpts += '<option value="'+k+'">'+collection.name+'</option>';
		}
		if (collection.allow_shared) {
			sharedOpts += '<option value="'+k+'">'+collection.name+'</option>';
		}
	}
	$('#collection_id').html(sharedOpts);
	
	// Works, BUT, requires you select the option which has no children first.
	//	Also, MIGHT not work for "shared decks", which defeats the purpose .... ugh ...
	//if (!individualOpts.length) {
	//	$('#super_collection_id option[value='+super_collection_id+']').remove();
	//}

	$('.collectionSelect').each(function() {
		$(this).html(individualOpts);
	});

	$('.superCollectionSelect').each(updateIndividualSuperCollection);
}

function updateIndividualSuperCollection() {
	var mode_id = $('input[name=mode_id]:checked').val();
	var super_collection_id = $(this).val();	// note THIS
	var collections = decks[mode_id].super_collections[super_collection_id].collections;
	var individualOpts = '';
	for (k in collections) {
		var collection = collections[k];
		individualOpts += '<option value="'+k+'">'+collection.name+'</option>';
	}
	$(this).next('.collectionSelect').each(function() {
		$(this).html(individualOpts);
	});
}

function updateShareDeck() {
	//console.log('updateShareDeck');
	var numberOfOpponents = $('#numberOfOpponents').val();
	numberOfOpponents++;
	if ($('input[name=share_deck]:checked').val() == 1) {
		$('.sharedeckPane').show();
		$('.individualPane').hide();
	}
	else {
		$('.sharedeckPane').hide();
		$('.individualPane:lt('+numberOfOpponents+')').show();
	}
	$('.superCollectionSelect').each(function() {
		$(this).change();
	});
}

function updateTeamNames() {
	var numberOfOpponents = $('#numberOfOpponents').val();
	numberOfOpponents++;
	$('input[name*=team]').hide();
	$('label[for*=team]').hide();
	$('.individualPane').hide();
	$('input[name*=team]:lt('+numberOfOpponents+')').show();
	$('label[for*=team]:lt('+numberOfOpponents+')').show();

	$('.teams br').hide();
	$('.teams br:lt('+numberOfOpponents+')').show();
	
	if ($('input[name=share_deck]:checked').val() == 0) {
		$('.individualPane:lt('+numberOfOpponents+')').show();
	}

	if (numberOfOpponents == 4) {
		$('.addTeam').hide();
	}
	else {
		$('.addTeam').show();
	}
	if (numberOfOpponents == 2) {
		$('.removeTeam').hide();
	}
	else {
		$('.removeTeam').show();
	}

}

