
/****
** Teams
****/
var typeshown = 'team';
function teamsInit() {
	
	var visible_leagues = $('#leagues-container a:visible');
	if (visible_leagues.length == 1)
		visible_leagues.addClass('current');
	
	$('#handle_allteams').click(function() {
		typeshown = 'team';
		$('#teams-inner a.team').show();
		$('ul#teams-type a.current').removeClass('current');
		$(this).addClass('current');
		
		$('#leagues-container a.league').show();
		$('#leagues-container a.federation').show();
		$('#leagues-container a').removeClass('current');
		
		var visible_leagues = $('#leagues-container a:visible');
		if (visible_leagues.length == 1) {
			visible_leagues.addClass('current');
			$('#leagues-container div.noneshown').hide();
			$('#teams-inner div.noneshown').hide();
		} else if (visible_leagues.length > 1) {
			$('#leagues-container div.noneshown').hide();
			$('#teams-inner div.noneshown').hide();
		}
		
		$('#filter-title').html('All Teams');
		return false;
	});
	$('#handle_clubteams').click(function() {
		typeshown = 'club';
		$('#teams-inner a.team').hide();
		$('#teams-inner a.club').show();
		$('ul#teams-type a.current').removeClass('current');
		$(this).addClass('current');
		
		$('#leagues-container a.league').show();
		$('#leagues-container a.federation').hide();
		
		$('#leagues-container a').removeClass('current');
		
		var visible_leagues = $('#leagues-container a:visible');
		if (visible_leagues.length == 1) {
			visible_leagues.addClass('current');
			$('#leagues-container div.noneshown').hide();
		} else if (visible_leagues.length == 0) {
			////
			// Show No Leagues
			$('#leagues-container div.noneshown').show();
		} else {
			////
			// Hide No Leagues
			$('#leagues-container div.noneshown').hide();
		}
		
		var visible_teams = $('#teams-inner a:visible');
		if (visible_teams.length == 0) {
			////
			// Show No Leagues
			$('#teams-inner div.noneshown').show();
		} else {
			////
			// Hide No Leagues
			$('#teams-inner div.noneshown').hide();
		}
		
		$('#filter-title').html('League Teams');
		return false;
	});
	$('#handle_nationalteams').click(function() {
		typeshown = 'national';
		$('#teams-inner a.team').hide();
		$('#teams-inner a.national').show();
		$('ul#teams-type a.current').removeClass('current');
		$(this).addClass('current');
		
		$('#leagues-container a.federation').show();
		$('#leagues-container a.league').hide();
		
		$('#leagues-container a').removeClass('current');
		var visible_leagues = $('#leagues-container a:visible');

		if (visible_leagues.length == 1) {
			visible_leagues.addClass('current');
			$('#leagues-container div.noneshown').hide();
		} else if (visible_leagues.length == 0) {
			////
			// Show No Leagues
			$('#leagues-container div.noneshown').show();
		} else {
			////
			// Hide No Leagues
			$('#leagues-container div.noneshown').hide();
		}
		
		var visible_teams = $('#teams-inner a:visible');
		if (visible_teams.length == 0) {
			////
			// Show No Leagues
			$('#teams-inner div.noneshown').show();
		} else {
			////
			// Hide No Leagues
			$('#teams-inner div.noneshown').hide();
		}
		
		$('#filter-title').html('Federation Teams');
		return false;
	});
	$('#leagues-container a').each(function() {
		$(this).bind('click', {title: $(this).attr('title')}, function(e) {
			if ($(this).hasClass('current')) {
				$(this).removeClass('current');
				$('#teams-inner a.'+typeshown).show();
			} else {
				$('#leagues-container a').removeClass('current');
				$('#teams-inner a').hide();
				$('#teams-inner a.'+e.data.title).show();
				$(this).addClass('current');
			}
			
			var visible_teams = $('#teams-inner a:visible');
			if (visible_teams.length == 0) {
				////
				// Show No Leagues
				$('#teams-inner div.noneshown').show();
			} else {
				////
				// Hide No Leagues
				$('#teams-inner div.noneshown').hide();
			}
		
			return false;
		});
		$(this).attr('title', '');
	});
}

/****
** Products
****/
function changeProductImage(el, imghome, imgaway) {
	if (imghome == '' && imgaway == '') return;
	
	var img;
	
	if (el.attr('selectedIndex') == 0) {
		if (imghome != '') {
			img = imghome;
		} else {
			img = imgaway;
		}
	} else if (el.attr('selectedIndex') == 1) {
		if (imgaway != '') {
			img = imgaway;
		} else {
			img = imghome;
		}
	}
	
	$('#small-header div').empty();
	
	var img_src = $('<img>')
		.load(function() {
			$('#small-header div').append(this);
		})
		.attr('src', img);
}

function createCustomPlayer() {
	if ($('#customnamerow').css('display')=='none') {
		$('#customnamerow').show();
		$($('#selectplayer option')[0]).attr('selected', 'selected');
		$('#selectplayer').trigger('change');
		$('td.selectplayername div.replacement-header').html('Custom Player');
		$('#product_custom_player_shown').attr('value', '1');
		$('#selectplayer').bind('change', removeCustomPlayer);
	}
	return false;
}
function removeCustomPlayer() {
	$('#customnamerow').css('display', 'none');
	$('#product_custom_player_shown').attr('value', '0');
	$('#selectplayer').unbind('change', removeCustomPlayer);
}

/****
** Create Account
****/
function changeRegion(country) {
	$('#staterow').hide();
	$('#provincerow').hide();
	$('#countyrow').hide();
	$('#regionrow').hide();
	
	country = $(country);
	
	if (country.val() == '38') {
		$('#provincerow').show();
	} else if (country.val() == '222') {
		$('#countyrow').show();
	} else if (country.val() == '223') {
		$('#staterow').show();
	} else {
		$('#regionrow').show();
	}
}

function showBusinessOwner(position) {
	if ($(position).val() != '1') {
		$('#row_owner_firstname').show();
		$('#row_owner_lastname').show();
	} else {
		$('#row_owner_firstname').hide();
		$('#row_owner_lastname').hide();
	}
}

/****
** Quick Order Form
****/

function qoChangeTeam() {
	
	$('#row-qo-year').hide();
	$('#row-qo-player').hide();
	$('#row-qo-customname').hide();
	$('#row-qo-customnumber').hide();
	$('#row-qo-size').hide();
	$('#row-qo-type').hide();
	$('#row-qo-quantity').hide();
	$('#row-qo-submit').hide();
	
	var team = $('#qo-team').val();
	
	if (team == '') return;
	
	$.post(BASE_HREF+"ajax.php", { method: "get_team_years", team: team },
	  function(data){
		$('#qo-year-container').html(data);
		start_custom_selects();
		$('#row-qo-year').show();
	  }, "html");
}

function qoGetTeamInfo() {

	$('#row-qo-player').hide();
	$('#row-qo-customname').hide();
	$('#row-qo-customnumber').hide();
	$('#row-qo-size').hide();
	$('#row-qo-type').hide();
	$('#row-qo-quantity').hide();
	$('#row-qo-submit').hide();
	
	var team = $('#qo-team').val();
	var year = $('#qo-year').val();
	
	if (year == '') return;
	
	$.post(BASE_HREF+"ajax.php", { method: "get_team_info", team: team, year: year },
	  function(data){
		$('#qo-products_id').val(data.product);
		$('#qo-player-container').html(data.player);
		if (data.custom_players > 0) {
			$('#qo-handle-customplayer').show();
		} else {
			$('#qo-handle-customplayer').hide();
		}
		$('#qo-size-container').html(data.size);	
		start_custom_selects();
		
		$('#row-qo-player').show();
		$('#row-qo-size').show();
		$('#row-qo-type').show();
		$('#row-qo-quantity').show();
		$('#row-qo-submit').show();

	  }, "json");
	
}

function qoCreateCustomPlayer() {
	if ($('#row-qo-customname').css('display')=='none') {
		$('#row-qo-customname').show();
		$('#row-qo-customnumber').show();
		$($('#qo-player option')[0]).attr('selected', 'selected');
		$('#qo-player').trigger('change');
		$('#qo-player-container div.replacement-header').html('Custom Player');
		$('#product_custom_player_shown').attr('value', '1');
		$('#qo-player').bind('change', qoRemoveCustomPlayer);
	}
	return false;
}

function qoRemoveCustomPlayer() {
	$('#row-qo-customname').hide();
	$('#row-qo-customnumber').hide();
	$('#product_custom_player_shown').attr('value', '0');
	$('#qo-player').unbind('change', qoRemoveCustomPlayer);
}


function initChangeHeatPressCat() {
	
	jQuery.each($('#teams-type a'), function(i, el) {
		
		var span = $(el).children('span')[0];
		var cat = $(el).attr('title');
		$(el).attr('title', $(span).html());
		
		$(el).bind('click', { cat: cat }, changeHeatPressCat);
		
	});
	
}

function changeHeatPressCat(info) {
	var cat = info.data.cat;
	
	$('.heat-press-cat').hide();
	$('#heat-press-cat-'+cat).show();
	
	$('#teams-type a').removeClass('current');
	$(this).addClass('current');
	
	return false;
}

function getFontSizeQuantities() {

	var size = $('#selectsize').val();
	var color = $('#selectcolor').val();
	
	if (size == '' || color == '') return;
	
	$('#selectquantity').html('<span class="title">Loading Quantities...</span>');
	
	
	$.post(BASE_HREF+"ajax.php", { method: "get_font_size_quantities", size: size, color: color },
	  function(data){
		$('#selectquantity').html(data);
	  });
	
}


