function show_info_help()
{
	$('#info_help').toggle();
}

var GAME = {
	SETLINEUP : 
	{
		GetLineup : function(url)
		{
			UI.AjaxLoad();
			if($('#gameid_select').attr('value') != undefined)
			{
				var data = {teamid : $('#team_id').attr('value'),period : $('#period').attr('value'), gameid : $('#gameid_select').attr('value')};
			}
			else
			{
				var data = {teamid : $('#team_id').attr('value'),period : $('#period').attr('value')};
			}
			$.post(url,data,GAME.SETLINEUP.InsertLineup);
			
			$('#inputteamid').attr('value',$('#team_id').attr('value'));
			$('#inputperiod').attr('value',$('#period').attr('value'));
			return false;
		},
	
		InsertLineup : function(result)
		{
			$('#ajaxcontainer').html(result);
			$('#ajaxcontainer').show();
			UI.AjaxDone();
		},
		
		SubmitRoster : function(url,copydouble)
		{
			UI.AjaxLoad();
			//grab all the players[] and make each part of the data string
			var data = {
				teamid: $('#inputteamid').attr('value'),
				period: $('#inputperiod').attr('value'),
				gameid: $('#inputgameid').attr('value')
			}
			
			if(copydouble == 2)
			{
				data['submit_copy'] = 1;
			}
			$('.lineuppos').each(function()
			{
				data[$(this).attr('name')] = $(this).attr('value');
			});
				
			$.post(url,data,GAME.SETLINEUP.AfterSubmitLineup,'json');
			UI.AjaxDone();
		},
		
		AfterSubmitLineup : function(result)
		{
			if (result.update == 1) {
				$('#success').html('Roster has been saved.');
				$('#success').show();
				GAME.SETLINEUP.GetLineup(result.reloadurl);
			}
			else
			{
				UI.AjaxDone();
				$('#success').html('');
				$('#success').hide();
				$('#post_errors').html(result.error);
				$('#error_holder_hidden').html(result.error);
				GAME.SETLINEUP.GetLineup(result.reloadurl);
				UI.ALERT.SetErrorPopup('post_errors');
			}
		}
	},
	ADDPLAYER :
	{
		search : {},
		default_contract : '#NONE',
		default_salary : '#NONE',
		
		GetRoster : function(url)
		{
			UI.AjaxLoad();
			if($('#gameid_select').attr('value') != undefined)
			{
				var data = {teamid : $('#team_id').attr('value'),period : $('#period').attr('value'), gameid : $('#gameid_select').attr('value')};
			}
			else
			{
				var data = {teamid : $('#team_id').attr('value'),period : $('#period').attr('value')};
			}
			$.post(url,data,GAME.ADDPLAYER.InsertRoster,"json");
			
			$('#inputteamid').attr('value',$('#team_id').attr('value'));
			$('#inputperiod').attr('value',$('#period').attr('value'));
			return false;
		},
		
		SubmitRoster : function(url)
		{
			UI.AjaxLoad();
			//grab all the players[] and make each part of the data string
			var data = {
				teamid: $('#inputteamid').attr('value'),
				period: $('#inputperiod').attr('value'),
				'players[]' : $.map($('#setup_roster').find('input[name="players[]"]'),function (n) {return $(n).val();})
			}
			
			if($('#markdrafted').is(':checked'))
			{
				data['markdrafted'] = 1;
			}
			
			$('.contract-holder').each(function()
			{
				if ($(this).attr('name').match('#PLAYERID') == null) {
					data[$(this).attr('name')] = $(this).attr('value');
				}
			});
			
			$('.salary-holder').each(function()
			{
				if ($(this).attr('name').match('#PLAYERID') == null) {
					data[$(this).attr('name')] = $(this).attr('value');
				}
			});
			
			$('#setup_roster').find('select').each(function()
			{
				data[$(this).attr('name')] = $(this).attr('value');
			});

			
			$.post(url,data,GAME.ADDPLAYER.AfterSubmitRoster,'json');
		},
		
		AfterSubmitRoster : function(result)
		{
			if (result.update == 1) {
				$('#add_roster_body').html('');
				$('#roster_body').html('');
				
				$.each(GAME.ADDPLAYER.search,function(posid,players)
				{
					$.each(result['info']['roster'],function(index,playerid)
					{	
						if(players[playerid] != undefined)
						{
							var html = $('#roster_row_clone > table > tbody:first-child').clone().html();
							html = html.replace(/#PLAYERID#/g,playerid);
							html = html.replace(/#POSITION#/g,players[playerid].p);
							html = html.replace(/#NAME#/g,players[playerid].nl);
							html = html.replace(/#TEAM#/g,players[playerid].t);
							
							if(result['info']['value'][playerid] != undefined)
							{
								html = html.replace(/#CONTRACT#/g,result['info']['value'][playerid].c);
							}
							else if(GAME.ADDPLAYER.default_contract != '#NONE#')
							{
								html = html.replace(/#CONTRACT#/g,GAME.ADDPLAYER.default_contract);
							}
							
							if(result['info']['value'][playerid] != undefined)
							{
								html = html.replace(/#SALARY#/g,result['info']['value'][playerid].s);
							}
							else if(GAME.ADDPLAYER.default_salary != '#NONE#')
							{
								html = html.replace(/#SALARY#/g,GAME.ADDPLAYER.default_salary);
							}
							
							$('#roster_body').append(html);
						}
					});
				});
				
				if(result.error != undefined)
				{
					$('#post_errors').html(result.error);
					$('#error_holder_hidden').html(result.error);
					UI.ALERT.SetErrorPopup('post_errors');
				}
				UI.AjaxDone();
			}
			else
			{
				UI.AjaxDone();
				$('#post_errors').html(result.error);
				$('#error_holder_hidden').html(result.error);
				UI.ALERT.SetErrorPopup('post_errors');
			}
		},
		
		InsertRoster: function(result)
		{
			$('#add_roster_body').html('');
			$('#roster_body').html('');
			$('#setup-addplayer').find('tr.player-hidden').each(function(){
				$(this).show();
			});
			
			$.each(GAME.ADDPLAYER.search,function(posid,players)
			{
				$.each(result.roster,function(index,playerid)
				{	
					if(players[playerid] != undefined)
					{
						var html = $('#roster_row_clone > table > tbody:first-child').clone().html();
						html = html.replace(/#PLAYERID#/g,playerid);
						html = html.replace(/#POSITION#/g,players[playerid].p);
						html = html.replace(/#NAME#/g,players[playerid].nl);
						html = html.replace(/#TEAM#/g,players[playerid].t);
						
						if(result['value'][playerid] != undefined)
						{
							html = html.replace(/#CONTRACT#/g,result['value'][playerid].c);
						}
						else if(GAME.ADDPLAYER.default_contract != '#NONE#')
						{
							html = html.replace(/#CONTRACT#/g,GAME.ADDPLAYER.default_contract);
						}
						
						if(result['value'][playerid] != undefined)
						{
							html = html.replace(/#SALARY#/g,result['value'][playerid].s);
						}
						else if(GAME.ADDPLAYER.default_salary != '#NONE#')
						{
							html = html.replace(/#SALARY#/g,GAME.ADDPLAYER.default_salary);
						}
						
						$('#roster_body').append(html);
					}
				});
			});
			
			$.each(result.rem,function(index,playerid)
			{
				$('#player-'+playerid).addClass('player-hidden');
				$('#player-'+playerid).hide();
			});
			
			$('#ajaxcontainer').show();
			$('body').css('cursor', 'auto'); 
			UI.AjaxDone();
		},
		
		Select : function(playerid)
		{
			var name = $('#playername-' + playerid).html();
			var pos = $('#playerpos-' + playerid).html();
			var team = $('#playerteam-' + playerid).html();
			
			$('#player-'+playerid).addClass('player-hidden');
			$('#player-'+playerid).hide();
			
			var html = $('#roster_row_clone > table > tbody:first-child').clone().html();
			html = html.replace(/#PLAYERID#/g,playerid);
			html = html.replace(/#POSITION#/g,pos);
			html = html.replace(/#NAME#/g,name);
			html = html.replace(/#TEAM#/g,team);
			
			if(GAME.ADDPLAYER.default_contract != '#NONE#')
			{
				html = html.replace(/#CONTRACT#/g,GAME.ADDPLAYER.default_contract);
			}
			
			if(GAME.ADDPLAYER.default_salary != '#NONE#')
			{
				html = html.replace(/#SALARY#/g,GAME.ADDPLAYER.default_salary);
			}
			
			$('#add_roster_body').append(html);
		},
		
		Remove : function(removeelm,playerid)
		{
			$(removeelm).parent().parent().remove();
			$('#player-'+playerid).show();
			return false;
		},
		
		PlayerSearchInit : function(box,position)
		{
			$(box).focus(function()
			{
				$(box).select();	
			});
			
			$(box).keyup(function()
			{
				GAME.ADDPLAYER.PlayerKeyup(this,position);
			});
		},
		
		PlayerKeyup : function(elm,position)
		{
			var len = $(elm).attr('value').length;
			var str = $(elm).attr('value').toLowerCase();
			$.each(GAME.ADDPLAYER.search[position],function(id,info)
			{
				if(!(info.n.toLowerCase().match(str)) &&
					!(info.nl.toLowerCase().match(str)) &&
					!(info.t.toLowerCase().match(str)))
				{
					$('#player-' + id).hide();
				}
				else if($('#player-' + id).hasClass('player-hidden') == false)
				{
					$('#player-' + id).show();
				}
			});
		}
	},
	
	CLAIMS :
	{
		GetRoster : function(url,playerid)
		{
			UI.AjaxLoad();
			$.post(url,{'playerid' : playerid},GAME.CLAIMS.InsertRoster);
		},
		
		InsertRoster : function(result)
		{
			$('#ajaxcontainer').html(result);
			eval($('#ajaxcontainer'));
			UI.AjaxDone();
		},
		
		Select : function(playerid)
		{
			var select = $('#playerselectbox-' + playerid);
			var name = $('#playername-' + playerid).html();
			var pos = $('#playerpos-' + playerid).html();
			var team = $('#playerteam-' + playerid).html();
			var cln = $('#roster_row_clone').clone();
			$(cln).find('span.cln-pos').html(pos);
			$(cln).find('span.cln-name').html(name);
			$(cln).find('span.cln-team').html(team);
			$('#transaction_maker > ul').append(cln.html().replace('#PLAYERID',playerid));
		},
		
		Remove : function(removeelm)
		{
			$(removeelm).parent().parent().parent().parent().parent().remove();
			return false;
		}
	},
	
	WISHLIST : 
	{
		Add : function(trigger,url)
		{
			$(trigger).click(function()
			{
				
				var data = {
					'players[]' : $.map($('#player-wish-adds > span'),function (n) {return $(n).html();})
				};

				$.post(url,data,function(result)
				{
					jQuery(".profileHeader_btn").hide();
					alert("The player has been added to your watchlist");
				});
				return false;
			});
		}
	},
	
	TRADE :
	{
		teams : new Array(),
		leagueid : 0,
		suburl : '',
		returl : '',
		
		LoadTeam : function(leagueid,teamid,container)
		{
			UI.AjaxLoad();

			var url = '/ajax/team_current_roster/' + leagueid;
			$.post(url,{'teamid':teamid},function(result){
				$(container).html(result);
				UI.AjaxDone();
			});
		},
		
		Propose : function()
		{
			count = 1;
			var cln = $('#confirm-trade').clone().html();
			$('input[name="team[]"]').each(function()
			{
				$('#confirm-trade > form > div > ul.team' + count + '-give').html('');
				var teamid = $(this).attr('value');
				var players = new Array();
				var picks = new Array();
				$('#teamroster-' + teamid + ' > tbody > tr > td > input[type="checkbox"]').each(function()
				{
					if($(this).is(':checked') && $(this).attr('name') != 'makepick[]'){
						var info = $(this).attr('value').split(':');
						players[info[0]] = info[1]; 
					}
				});
				$('#picks-' + teamid + ' > tbody > tr > td > input[type="checkbox"]').each(function()
				{
					if($(this).is(':checked') && $(this).attr('name') == 'makepick[]'){
						var selname = 'tradepick-' + $(this).attr('value');
						var selpick = $(this).parent().parent().find('select[name="'+selname+'"]');
						var seltext = $(selpick).find('option:selected').text();
						picks[$(selpick).attr('value')] = seltext;
					}
				}); 
				
				
				$('#confirm-trade > form > div > p > span.team'+count+'name').html('<input id="team'+count+'" type="hidden" name="team'+count+'" value="'+teamid+'" />' + GAME.TRADE.teams[teamid]);
				for(var p in players)
				{
					$('#confirm-trade > form > div > ul.team' + count + '-give').append('<li><input class="tradeplayer" type="hidden" name="trade['+teamid+'][]" value="'+p+'" />'+players[p]+'</li>');
				}
				for(var p in picks)
				{
					$('#confirm-trade > form > div > ul.team' + count + '-give').append('<li><input class="tradepick" type="hidden" name="tradepick['+teamid+'][]" value="'+p+'" />'+picks[p]+'</li>');
				}
				count++;
			});	
			
			$("#confirm-trade").dialog({
				bgiframe: true,
				modal: true,
				height:300,
				width:400,
				buttons : {'Continue' : function()
									{											
										$(this).dialog('close');
										GAME.TRADE.MakePropose();
									},
							Cancel	: function()
									{	
										$(this).dialog('close');
									}
						}
			});	
			
			$("#confirm-trade").dialog('open');
		},
		
		MakePropose : function()
		{
			//UI.AjaxLoad();
			var t1 = $('#team1').attr('value');
			var t2 = $('#team2').attr('value');
			var note = $('#emailnote').attr('value');
			data = {'team1' : $('#team1').attr('value'),
					'team2' : $('#team2').attr('value'),
					'emailnote' : note
					};

			data['trade['+t1+'][]'] = $.map($('input[name="trade['+t1+'][]"]'),function (n) {return $(n).attr('value');});
			data['trade['+t2+'][]'] = $.map($('input[name="trade['+t2+'][]"]'),function (n) {return $(n).attr('value');});
			data['tradepick['+t1+'][]'] = $.map($('input[name="tradepick['+t1+'][]"]'),function (n) {return $(n).attr('value');});
			data['tradepick['+t2+'][]'] = $.map($('input[name="tradepick['+t2+'][]"]'),function (n) {return $(n).attr('value');});
			$.post(GAME.TRADE.suburl,data,
					function(result){
						//UI.AjaxDone();
						if(result.success == 1)
						{
							window.location.href=GAME.TRADE.returl + '/' + result.tradeid;
						}
						else
						{
							$('#post_errors').html(result.error);
							$('#error_holder_hidden').append(result.error);
							UI.ALERT.SetErrorPopup('post_errors');
						}
					},
				'json'
			);
		}
	},
	
	Playernews: function(){
		$('#myPlayersTab_news').click(function(){
			$('#myPlayersTab_news').parent().addClass('on');
			$('#allPlayersTab_news').parent().removeClass('on');
			$('#myPlayersNews').show();
			$('#allPlayersNews').hide();
			return false;
		});
		
		$('#allPlayersTab_news').click(function(){
			$('#myPlayersTab_news').parent().removeClass('on');
			$('#allPlayersTab_news').parent().addClass('on');
			$('#myPlayersNews').hide();
			$('#allPlayersNews').show();
			return false;
		});
	},
	
	ELIGIBILITY :{
		Select : function(playerid)
		{			
			$('#player-'+playerid).addClass('player-hidden');
			$('#player-'+playerid).hide();
		
			var name = $('#playername-' + playerid).html().replace(/^\s*|\s*$/g,'');
			var pos = $('#playerpos-' + playerid).html().replace(/^\s*|\s*$/g,'');
			var team = $('#playerteam-' + playerid).html().replace(/^\s*|\s*$/g,'');
			var cln = $('#roster_row_clone').clone();
			html = $(cln).html();
			
			if(pos == 'DL'){
				html = html.replace(/#CHECKED-DL#/gi,'checked="true"');
				html = html.replace(/#DEFAULT-DL#/gi,'- Default Position');
			}
			else if(pos == 'LB'){
				html = html.replace(/#CHECKED-LB#/gi,'checked="true"');
				html = html.replace(/#DEFAULT-LB#/gi,'- Default Position');
			}
			else if(pos == 'DB'){
				html = html.replace(/#CHECKED-DB#/gi,'checked="true"');
				html = html.replace(/#DEFAULT-DB#/gi,'- Default Position');
			}
			
			if(pos != 'DL'){
				html = html.replace(/#CHECKED-DL#/gi,'');
				html = html.replace(/#DEFAULT-DL#/gi,'');
			}
			if(pos != 'LB'){
				html = html.replace(/#CHECKED-LB#/gi,'');
				html = html.replace(/#DEFAULT-LB#/gi,'');
			}
			if(pos != 'DB'){
				html = html.replace(/#CHECKED-DB#/gi,'');
				html = html.replace(/#DEFAULT-DB#/gi,'');
			}

			html = html.replace(/#PLAYERID#/g,playerid);
			html = html.replace(/#POSITION#/g,pos);
			html = html.replace(/#NAME#/g,name);
			html = html.replace(/#TEAM#/g,team);

			$('#transaction_maker > ul').append(html);
		}
	}
}
