Event.observe(window, 'load', init);

var scores = null;
var sports = ["nba", "ncb", "ncf", "nfl", "nhl"];
var _sport = undefined;
var _click = false;
var _intvl = null;

function init() {
	//getting scores via ajax call
	//alert(document.cumulativeOffset());
	scoreBoard();
	_createLeagueDisplay();
	_intvl = setInterval("scoreBoard()",5000);
}

function scoreBoard () {	
	if(this._sport !== undefined) {
		_sport = this._sport;
		var div_position = $$('div [class="sport ' + _sport + '"]')[0].cumulativeOffset();
		var box_position = $$('div [class="box_long_bg"]')[0].cumulativeOffset();
		var left = div_position.left - box_position.left - 1;

		
		//if sport button is pressed move blue hover bg
		if(this.click === true) {
			_click = true;
			new Effect.Move($('hover'), {
				x	 : left,
				y	 : 33,
				mode : 'absolute'
			});
			
			//moving old data out of the way
			new Effect.Parallel(
				[
					new Effect.Move('score_board', { sync: true, x: 200, y: 0, mode: 'absolute' }), 
					new Effect.Opacity('score_board', { sync: true, from: 1, to: 0 })
				], 
				{ 
					duration: 1.0
				}
			);
			
			clearTimeout(_intvl);
			_intvl = setInterval("scoreBoard()",5000);
		}
		else {
			_click = false;	
		}
	}
	
	//getting scores form espn.com
	new Ajax.Request('/ajax/scores/', {
		evalJS	  : true,
		onSuccess : function(transport) {
			scores = transport.responseJSON;
			_setScores();
		}
	});
	
	
}

function _createLeagueDisplay() {	
	for(var x in sports) {
		if(typeof(sports[x]) == 'string') {
			var div_sports = new Element('div', {
				'class' : 'sport ' + sports[x]
			}).update(sports[x]).observe('click', scoreBoard.bind({'_sport' : sports[x], 'click' : true}));
			
			if($('league')) {
				$('league').insert(div_sports);
			}
		}
	}
}

function in_array (needle, haystack) {
	for (key in haystack) {
		if (haystack[key] == needle) {
			return true;
		}
	}

    return false;
}

function _setScores() {
	for (var i in scores.sports) {
		if(typeof(scores.sports[i]) == 'object' && in_array(scores.sports[i].sport, sports)) {
			_prepareScores(scores.sports[i]);
		}
	}
}

function _prepareScores(league) {
	var sport = (_sport === undefined ? sports[0] : _sport);
	
	if(league.sport == sport) {
		var games = league.leagues[0].games;
		_setGames(games);
	}
}

function _setGames(games) {
	$('score_board').innerHTML = '';
	
	var _max  = 9;
	var count = 0;
	
	//if sport button was clicked bring back the scoreboard via zoom motion
	if(_click === true) {
		setTimeout("_showGames()",500);
	}
	
	for(var c in games) {
		if(count < _max) {
			_createValues(games[c]);
			count++;
		}
	}
}

function _showGames() {
	//restoring scoreboard
	new Effect.Parallel(
		[
			new Effect.Move('score_board', { sync: true, x: 0, y: 0, mode: 'absolute' }), 
			new Effect.Opacity('score_board', { sync: true, from: 0, to: 1 })
		], 
		{ 
			duration: 1.0
		}
	);
}

function _createValues(game) {
	var div_game = new Element('div', { 'class': 'game' });
	
	//showing period and clock
	var a = game.statusText.length - 2;
	var time = new Element('div', { 'class': 'time' }).update(game.statusText.substring(a) == 'PM' || game.statusText.substring(a) == 'AM' ? game.statusText : '' );
	
	if(game.statusText.substring(a) != 'PM' && game.statusText.substring(a) != 'AM') {
		var quarter = new Element('div', { 'class': 'quarter' }).update(game.statusText == 'F' ? 'Final' : game.statusText);
		var clock   = new Element('div', { 'class': 'clock' }).update(game.clock == '' ? ' &nbsp;' : game.clock);
		time.insert(quarter);
		time.insert(clock);
	}
	//showing away team and score
	var away  = new Element('div', { 'class': 'away' });
	var team  = new Element('div', { 'class': 'team' }).update(game.away.name);
	var score = new Element('div', { 'class': 'score' }).update(game.away.score);
	away.insert(team);
	away.insert(score);
	
	//showing home team and score
	var home  = new Element('div', { 'class': 'home' });
	var team  = new Element('div', { 'class': 'team' }).update(game.home.name);
	var score = new Element('div', { 'class': 'score' }).update(game.home.score);
	home.insert(team);
	home.insert(score);
	
	div_game.insert(time);
	div_game.insert(away);
	div_game.insert(home);
	
	$('score_board').insert(div_game);
}