	var playerid = '716';
	$(document).ready(function(){
		$.ifixpng(base_url+'images/pixel.gif');		
		
		if(team1 != "" && team2 != "" && position1 != "" && position2 != "" && player1 != "" && player2 != ""){
			getPlayerData($('#grid_player1'),'1',player1,true);
			getPlayerData($('#grid_player2'),'2',player2,true);
			getCRITERIA($('#grid_averagestats'),'1',player1+'/'+position1+'/'+team1);
			getPlayerData($('#grid_player1stats'),'1',player1,false);	
			getPlayerData($('#grid_player2stats'),'2',player2,false);			
			getHotSpot($('#hotspots_player_2'),player2);
			getTournament($('#tournamentgraph'),player1,player2);
			
			$('select[name=team2] option[value='+team2+']').attr('selected','selected');
			getPlayerList($('#playerlist2'),team2,'',player2);
			$('select[name=player2] option[value='+player2+']').attr('selected','selected');
			
		}else{
			getPlayerData($('#grid_player1'),'1','716',true);
			getPlayerData($('#grid_player2'),'2','',true);
			getCRITERIA($('#grid_averagestats'),'1',"716/1/15");
			getPlayerData($('#grid_player1stats'),'1','716',false);		
			getTournament($('#tournamentgraph'),"716/1/15","");
		}
		
		
		
		$('select[name=team1]').change(function(){
			var team1 = $(this).val();
			
			reset_select($('select[name=position1] option:selected'));
			reset_select($('select[name=player1] option:selected'));			
			
			disable($('select[name=team2]'));
			disable($('select[name=player2]'));
			
			empty($('#criteria'));
			empty($('#grid_player2stats'));
			empty($('#grid_player1stats'));
			/*empty($('#hotspots_player_1'));*/
			empty($('#tournamentgraph'));	
			
			getPlayerList($('#playerlist1'),team1,'');
			/*$.get(base_url+'worldcup/select_country/'+team1,function(data){
				$('#playerlist1').html(data);
			})*/
		});		
		
		$('select[name=team2]').change(function(){
			reset_select($('select[name=player2] option:selected'));			
			
			var tm = $(this).val();			
			var ps = $('select[name=position1] option:selected').val();
			
			getPlayerList($('#playerlist2'),tm,ps);		
						
		});
		
		$('select[name=player1]').change(function(){
			player1id = $(this).children('option:selected').val();
			player2id = $('select[name=player2] option:selected').val();
			
			if(player1id != ""){
				$('select[name=team2]').removeAttr("disabled");
				$('select[name=player2]').removeAttr("disabled");			
				
				getPlayerData($('#grid_player1'),'1',player1id,true);
				getPlayerData($('#grid_player2'),'2',player2id,true);
				getCRITERIA($('#grid_averagestats'),'1',player1id);
				getPlayerData($('#grid_player1stats'),'1',player1id,false);	
				getHotSpot($('#hotspots_player_1'),player1id);
				getTournament($('#tournamentgraph'),player1id,"");
				
				player1id = player1id.split("/");
				
				ps = player1id[1];
				tm = $('select[name=team2] option:selected').val();
				
				$('select[name=position1] option[value='+ps+']').attr('selected','selected');
				getPlayerList($('#playerlist2'),tm,ps);
				
			}else{
				disable($('select[name=team2]'));
				disable($('select[name=player2]'));
				empty($('#grid_averagestats'));
				empty($('#grid_player2stats'));
				empty($('#grid_player1stats'));
				empty($('#grid_player1'));
				empty($('#grid_player2'));
				empty($('#hotspots_player_2'));
			}
				
		});
		
		$('select[name=position1]').change(function(){
			var ps = $(this).children('option:selected').val();
			
			if(ps == ""){
				disable($('select[name=team2]'));
				disable($('select[name=player2]'));
			}
			
			var tm = $('select[name=team1] option:selected').val();			
			getPlayerList($('select[name=player1]'),tm,ps);
			
			var tm2 = $('select[name=team2] option:selected').val();
			getPlayerList($('select[name=player2]'),tm2,ps);
		});
		
		$('select[name=player2]').change(function(){			
			
			player2id = $(this).children('option:selected').val();
			getPlayerData($('#grid_player2'),'1',player2id,true);
			/*getCRITERIA($('#grid_averagestats'),'1',player1id);*/
			getPlayerData($('#grid_player2stats'),'2',player2id,false);	
			getHotSpot($('#hotspots_player_2'),player2id);
			getTournament($('#tournamentgraph'),playerid,player2id);
			
			/*if(player2id != ""){
				$('select[name=team2]').removeAttr("disabled");
				$('select[name=player2]').removeAttr("disabled");
			}else{
				disable($('select[name=team2]'));
				disable($('select[name=player2]'));
			}*/			
		});
	});
	
	function getJSonPlayerList(obj,tm,ps){
		if(tm == ""){tm = 0;}
		if(ps == ""){ps = 0;}
		
		$.getJSON(base_url+'worldcup/getPlayerList/'+ps+'/'+tm,function(data){	
			if(data){
				options = '<option value="" selected="selected">-</option>';
			 $.each(data, function(i,item){
				options += '<option value="'+item.id+'/'+item.position_id+'/'+item.team_id+'">'+item.full_name+'<\/option>';
			  });
			  
			  option = options;
			}
			obj.html(option);
		});
	}
	
	function getPlayerList(obj,tm,ps,playerid){
		if(tm == ""){tm = 0;}
		if(ps == ""){ps = 0;}
		
		$.get(base_url+'worldcup/getPlayerList/'+ps+'/'+tm,function(data){
			if(data != ""){
				obj.html(data);
				if(playerid != ""){
					$(this).children('option[value="'+playerid+'"]').attr('selected','selected');
				}
			}
		});
	}
	
	function reset_select(name){
		name.attr('selected','');
	}	
	
	function disable(name){		
		name.attr('disabled','disabled');
	}
	
	function empty(name){name.html('');}
	
	function getCRITERIA(obj,p,id){
		id = id.split('/');		
		$.get(base_url+'worldcup/getCRITERIA/'+id[1]+'/'+id[0],function(data){
			obj.html(data);				
		});		
	}
	
	function getHotSpot(obj,id){
		obj.html('<img src="http://www.castrolfootball.com/resources/graphs/hotspots.php?id='+id+'" />');
		/*obj.children('img').attr('src','http://www.castrolfootball.com/resources/graphs/hotspots.php?id='+id);*/
	}
	
	function getPlayerData(obj,n,id,summary){
		$('.kit img').iunfixpng();
		id = id.split('/');
		if(summary){
			$.get(base_url+'worldcup/getPlayerSummary/'+n+'/'+id[0],function(data){
					obj.html(data);	
				    $('.kit img').ifixpng();
			});

		}else{
			$.get(base_url+'worldcup/getPlayerData/'+n+'/'+id[0],function(data){
					obj.html(data);
					$('.kit img').ifixpng();
			});
		}
	}
	
	
	function getTournament(obj,player1id,player2id){
		if(player1id == ""){
			player1id = playerid;
		}else{
			player1id = player1id.split("/");
			player1id = player1id[0];		}
		
		
		if(player2id != ""){
			player2id = player2id.split("/");
			$.get(base_url+'worldcup/getTourment/'+player1id+'/'+player2id[0],function(data){						
				obj.html(data);				
			});
		}else{
			$.get(base_url+'worldcup/getTourment/'+player1id,function(data){		
				obj.html(data);				
			});
		}		
		playerid = player1id;
	}