var Summary = {
	
	initialize: function(match_id, base_url, is_race )
	{
		Summary.baseUrl = base_url;
		Summary.isUpdating = false;
		Summary.matchId = match_id;
		Summary.isRace = is_race;
		
		Summary.templates = {
				match_score: '| счет: <span id="match_score"></span>',
				goals_first_team: '<p id="goals_first_team_block">голы хозяев: <span id="goals_first_team"></span></p>',
				goals_second_team: '<p id="goals_second_team_block">голы гостей: <span id="goals_second_team"></span></p>',
				penalties_first_team: '<p id="penalties_first_team_block">пенальти хозяев: <span id="penalties_first_team"></span></p>',
				penalties_second_team: '<p id="penalties_second_team_block">пенальти гостей: <span id="penalties_second_team"></span></p>',
				hockey_match_score: '<p id="hockey_match_score">счет по периодам:</p>',
				list_hockey_goals: '<div class="title"><p>голы</p></div><table class="stat-table"><colgroup><col width="70" /><col width="70" /><col width="480" /></colgroup><tr><th>время</th><th>счет</th><th class="lcol">авторы голов и ассистенты</th></tr><tbody id="list_hockey_goals">',
				teaser_dl: '<img src="http://www.sports.ru/stat/skin/images/teaserdl.gif" width="1" height="12">'
		}
	},
	
	update: function()
	{
		if(Summary.isUpdating )
		{
			return false;
		}
		Summary.isUpdating = true;
		if (Summary.isRace == 0)
		{
			var match_url = Summary.baseUrl + '/match/summary/' + Summary.matchId + '.json';
		}
		else if (Summary.isRace == 1)
		{
			var match_url = Summary.baseUrl + '/race/summary/' + Summary.matchId + '.json';
		}
		var updateRequest = new Ajax.Request(match_url, 
			{
				method: 'get',
				requestHeaders: {Accept: 'application/json'},
				onSuccess: Summary.fillSummary,
				onComplete: Summary.fireUpdating
			}
		);
		return true;
	},
	
	fillSummary: function(response )
	{
		var data = response.responseText.evalJSON();
		if(data.length )
		{			
			data = data[0];
			
			/* Заполняем счет и статус */
			if($('goal_first_team' ) && data.goal_first_team )
			{
				$('goal_first_team').update(data.goal_first_team );
			}
			if($('goal_second_team' ) && data.goal_second_team )
			{
				$('goal_second_team').update(data.goal_second_team );
			}
			
			$('match_status').update(data.match_status);

			if($('match_status_' + Summary.matchId ) )
			{
				$('match_status_' + Summary.matchId ).update(data.match_status );
			}

			if(!$('match_score') && data.match_score )
			{
				$('match_status').insert({after: Summary.templates.match_score } );
			}
			if(data.match_score )
			{
				$('match_score').update(data.match_score );
				if($('match_teaser_' + Summary.matchId ) )
				{
					$('match_teaser_' + Summary.matchId ).update(data.match_score );
				}
				if($('match_gadget_' + Summary.matchId ) )
				{
					$('match_gadget_' + Summary.matchId ).update(data.match_score );
				}
			}
			if(!$('hockey_match_score') && data.hockey_match_score )
			{
				$('hockey_match_score_box').insert({after: Summary.templates.hockey_match_score } );
			}
			if(data.hockey_match_score )
			{
				$('hockey_match_score').update(data.hockey_match_score );
				if($('match_teaser_' + Summary.matchId ) )
				{
					$('match_teaser_' + Summary.matchId ).update(data.goal_first_team + ' : ' + data.goal_second_team );
				}
			}
			
			/* Заполняем голы хозяев */
			if(!$('goals_first_team' ) && data.goals_first_team )
			{
				if($('match_referee_block' ) )
				{
					$('match_referee_block').insert({after: Summary.templates.goals_first_team } );
				}
				else
				{
					$('match_date_block').insert({after: Summary.templates.goals_first_team } );
				}
			}
			if(data.goals_first_team )
			{
				$('goals_first_team').update(data.goals_first_team );
			}
			
			/* Заполняем голы гостей */
			if(!$('goals_second_team' ) && data.goals_second_team )
			{
				if($('goals_first_team_block') )
				{
					$('goals_first_team_block').insert({after: Summary.templates.goals_second_team } );
				}
				else if($('match_referee_block' ) )
				{
					$('match_referee_block').insert({after: Summary.templates.goals_second_team } );
				}
				else
				{
					$('match_date_block').insert({after: Summary.templates.goals_second_team } );
				}
			}
			if(data.goals_second_team )
			{
				$('goals_second_team').update(data.goals_second_team );
			}
			
			/* Заполняем пенальти хозяев */
			if(!$('penalties_first_team' ) && data.penalties_first_team )
			{
				if($('goals_second_team_block') )
				{
					$('goals_second_team_block').insert({after: Summary.templates.penalties_first_team } );
				}
				else if($('goals_first_team_block') )
				{
					$('goals_first_team_block').insert({after: Summary.templates.penalties_first_team } );
				}
				else if($('match_referee_block' ) )
				{
					$('match_referee_block').insert({after: Summary.templates.penalties_first_team } );
				}
				else
				{
					$('match_date_block').insert({after: Summary.templates.penalties_first_team } );
				}
			}
			if(data.penalties_first_team )
			{
				$('penalties_first_team').update(data.penalties_first_team );
			}
			
			/* Заполняем пенальти гостей */
			if(!$('penalties_second_team' ) && data.penalties_second_team )
			{
				if($('penalties_first_team_block') )
				{
					$('penalties_first_team_block').insert({after: Summary.templates.penalties_second_team } );
				}
				else if($('goals_second_team_block') )
				{
					$('goals_second_team_block').insert({after: Summary.templates.penalties_second_team } );
				}
				else if($('goals_first_team_block') )
				{
					$('goals_first_team_block').insert({after: Summary.templates.penalties_second_team } );
				}
				else if($('match_referee_block' ) )
				{
					$('match_referee_block').insert({after: Summary.templates.penalties_second_team } );
				}
				else
				{
					$('match_date_block').insert({after: Summary.templates.penalties_second_team } );
				}
			}
			if(data.penalties_second_team )
			{
				$('penalties_second_team').update(data.penalties_second_team );
			}
			
			if (!$('list_hockey_goals' ) && data.list_hockey_goals )
			{
				$('hockey_goals_box').insert({after: Summary.templates.list_hockey_goals } );
			}
			if (data.list_hockey_goals )
			{
				$('list_hockey_goals').update(data.list_hockey_goals );
			}
			
			/* Заполняем теннисный тизер */
			if (data.score_5)
			{
				$('match_teaser_' + Summary.matchId + '_1').update(data.score_1);
				$('match_teaser_' + Summary.matchId + '_2').update(data.score_2);
				$('match_teaser_' + Summary.matchId + '_3').update(data.score_3);
				$('match_teaser_' + Summary.matchId + '_4').update(data.score_4);
				$('match_teaser_' + Summary.matchId + '_5').update(data.score_5);
				
				$('dl_' + Summary.matchId + '_1').addClassName('ltd');
				$('dl_' + Summary.matchId + '_2').addClassName('ltd');
				$('dl_' + Summary.matchId + '_3').addClassName('ltd');
				$('dl_' + Summary.matchId + '_4').addClassName('ltd');
			}
			else if (data.score_4)
			{
				$('match_teaser_' + Summary.matchId + '_1').update('');
				$('match_teaser_' + Summary.matchId + '_2').update(data.score_1);
				$('match_teaser_' + Summary.matchId + '_3').update(data.score_2);
				$('match_teaser_' + Summary.matchId + '_4').update(data.score_3);
				$('match_teaser_' + Summary.matchId + '_5').update(data.score_4);
				
				$('dl_' + Summary.matchId + '_1').removeClassName('ltd');
				$('dl_' + Summary.matchId + '_2').addClassName('ltd');
				$('dl_' + Summary.matchId + '_3').addClassName('ltd');
				$('dl_' + Summary.matchId + '_4').addClassName('ltd');
			}
			else if (data.score_3)
			{
				$('match_teaser_' + Summary.matchId + '_1').update('');
				$('match_teaser_' + Summary.matchId + '_2').update('');
				$('match_teaser_' + Summary.matchId + '_3').update(data.score_1);
				$('match_teaser_' + Summary.matchId + '_4').update(data.score_2);
				$('match_teaser_' + Summary.matchId + '_5').update(data.score_3);
				
				$('dl_' + Summary.matchId + '_1').removeClassName('ltd');
				$('dl_' + Summary.matchId + '_2').removeClassName('ltd');
				$('dl_' + Summary.matchId + '_3').addClassName('ltd');
				$('dl_' + Summary.matchId + '_4').addClassName('ltd');
			}
			else if (data.score_2)
			{
				$('match_teaser_' + Summary.matchId + '_1').update('');
				$('match_teaser_' + Summary.matchId + '_2').update('');
				$('match_teaser_' + Summary.matchId + '_3').update('');
				$('match_teaser_' + Summary.matchId + '_4').update(data.score_1);
				$('match_teaser_' + Summary.matchId + '_5').update(data.score_2);
				
				$('dl_' + Summary.matchId + '_1').removeClassName('ltd');
				$('dl_' + Summary.matchId + '_2').removeClassName('ltd');
				$('dl_' + Summary.matchId + '_3').removeClassName('ltd');
				$('dl_' + Summary.matchId + '_4').addClassName('ltd');
			}
			else if (data.score_1)
			{
				$('match_teaser_' + Summary.matchId + '_1').update('');
				$('match_teaser_' + Summary.matchId + '_2').update('');
				$('match_teaser_' + Summary.matchId + '_3').update('');
				$('match_teaser_' + Summary.matchId + '_4').update('');
				$('match_teaser_' + Summary.matchId + '_5').update(data.score_1);
				
				$('dl_' + Summary.matchId + '_1').removeClassName('ltd');
				$('dl_' + Summary.matchId + '_2').removeClassName('ltd');
				$('dl_' + Summary.matchId + '_3').removeClassName('ltd');
				$('dl_' + Summary.matchId + '_4').removeClassName('ltd');
			}
			else
			{
				$('match_teaser_' + Summary.matchId + '_1').update('');
				$('match_teaser_' + Summary.matchId + '_2').update('');
				$('match_teaser_' + Summary.matchId + '_3').update('');
				$('match_teaser_' + Summary.matchId + '_4').update('');
				$('match_teaser_' + Summary.matchId + '_5').update('- : -');
				
				$('dl_' + Summary.matchId + '_1').removeClassName('ltd');
				$('dl_' + Summary.matchId + '_2').removeClassName('ltd');
				$('dl_' + Summary.matchId + '_3').removeClassName('ltd');
				$('dl_' + Summary.matchId + '_4').removeClassName('ltd');
			}
			
			if (data.team_staff )
			{
				$('staff_' + Summary.matchId).update(data.team_staff );
			}
		}
	},
	
	fireUpdating: function( )
	{
		Summary.isUpdating = false;
	}
}
