$(document).ready( onDocumentReady );

var currentType 	= 0;

var oldCurrentType 	= 0;

var autoUpdateInterval = null;

var currentCompId = 0;



var archive = new Array();



function onDocumentReady()

{

	autoUpdateInterval = setInterval( bbUpdateOdds, 15 * 1000 );

	

	$('#tab-nav-1x2 a').click(function (){

		tabHighlightBB('#tab-nav-1x2');

		bbGetOdds( 69, currentCompId);

		return false;

	});



	$('#tab-nav-asian a').click(function (){

		tabHighlightBB('#tab-nav-asian');

		bbGetOdds( 48, currentCompId);

		return false;

	});



	$('#tab-nav-over a').click(function (){

		tabHighlightBB('#tab-nav-over');

		bbGetOdds( 47, currentCompId);

		return false;

	});

}



function bbUpdateOdds()

{

	doBbGetOdds( currentType, currentCompId, false );

}



function bbGetOdds( typeId, compId )

{

	if( autoUpdateInterval != null )

		clearInterval( autoUpdateInterval );

	

	autoUpdateInterval = setInterval( bbUpdateOdds, 15 * 1000 );

	

	doBbGetOdds( typeId, compId, true );

}



function doBbGetOdds( typeId, compId, showLoading )

{

	currentCompId = compId;

	

	if( currentType != 0 ) oldCurrentType = currentType;

	currentType = typeId;

	if( currentType != 0 ) bbMenuOff( oldCurrentType );

	bbMenuOn( currentType );

	callVars = {

		typeId: typeId,

		compId: compId,

		ajax: true

	};

	

	if( showLoading )

		bbLoadingOn();

	$.getJSON( "http://www.soccerpunter.com/competitionGetOdds.php", callVars, onGetOddsSuccess );

}



function onGetOddsSuccess( dataJSON )

{

	var type = dataJSON.type;

	var data = dataJSON.rows;

	bbLoadingOff();

	

	displayBB( data, type );

	

	transitionsLeft = 60;

	blink();

}



function displayBB( data, type )

{

	switch( type )

	{

		case '69': displayBB1x2( data ); break;

		case '47': displayBBOverUnder( data ); break;

		case '48': displayBBAsian( data ); break;

	}

}



function displayBB1x2( data )

{

	$('#bbResults').empty();

	

	$.each( 

		data, 

		function( date, rows )

		{

			th	= $('<tr></tr>').addClass( "header" );

			

			

			th.append( $('<td></td>').attr( "colspan", 2 ).css("color", "white").html( date ) );

			th.append( $('<td></td>').addClass("odds").css("color", "white").html( 'Home' ) );

			th.append( $('<td></td>').addClass("odds").css("color", "white").html( 'Draw' ) );

			th.append( $('<td></td>').addClass("odds").css("color", "white").html( 'Away' ) );

			

			$('#bbResults').append( th );

			

			for( i=0; i < rows.length; i++ )

			{

				

				var tr = $('<tr></tr>');

				

				if( i % 2 == 0)

					tr.addClass( "odd" );

				else

					tr.addClass( "even" );

				

				tr.append( $('<td></td>').html( rows[i].match.time ));

				tr.append( $('<td></td>').append(

					$('<a></a>')

					.attr( "href", liveSoccerOddsBaseURL + "home=" + escape( rows[i].match.home ) + "&away=" + escape( rows[i].match.away ) + "&date=" + escape( rows[i].match.date_match ) )

					.html( rows[i].match.home + ' vs ' + rows[i].match.away )

				) );

					

				

				if( rows[i].odds == null ) continue;

				

				if( typeof( archive[rows[i].match.id_match] ) == "undefined" )

				{

					archive[rows[i].match.id_match] = new Array();

				}

				

				if( typeof(archive[rows[i].match.id_match][69] ) == "undefined" )

				{

					archive[rows[i].match.id_match][69] = new Array();

				}

				

				

				td1 = $('<td></td>').addClass("odds").append( $('<a></a>').attr( "target", "_blank").attr( "href", rows[i].odds[1].url ).html( rows[i].odds[1].odds ) );

				td0 = $('<td></td>').addClass("odds").append( $('<a></a>').attr( "target", "_blank").attr( "href", rows[i].odds[0].url ).html( rows[i].odds[0].odds ) );

				td2 = $('<td></td>').addClass("odds").append( $('<a></a>').attr( "target", "_blank").attr( "href", rows[i].odds[2].url ).html( rows[i].odds[2].odds ) );

				

				if( typeof( archive[rows[i].match.id_match][69][1] ) == "undefined" )

				{

					archive[rows[i].match.id_match][69][1] = rows[i].odds[1].odds;

					archive[rows[i].match.id_match][69][0] = rows[i].odds[0].odds;

					archive[rows[i].match.id_match][69][2] = rows[i].odds[2].odds;

				}

				

				

				

				//58935588; 58571759 - 2010-06-11 12:23:26

				if( archive[rows[i].match.id_match][69][1] != rows[i].odds[1].odds )

				{

					td1.addClass( "blink" );

				}

				

				if( archive[rows[i].match.id_match][69][0] != rows[i].odds[0].odds )

				{

					td0.addClass( "blink" );

				}

				

				if( archive[rows[i].match.id_match][69][2] != rows[i].odds[2].odds )

				{

					td2.addClass( "blink" );

				}

				

				archive[rows[i].match.id_match][69][1] = rows[i].odds[1].odds;

				archive[rows[i].match.id_match][69][0] = rows[i].odds[0].odds;

				archive[rows[i].match.id_match][69][2] = rows[i].odds[2].odds;

				

				

				tr.append( td1 );

				tr.append( td0 );

				tr.append( td2 );

				

				$('#bbResults').append( tr );

			}

		}

	);

}





function displayBBAsian( data )

{

	$('#bbResults').empty();

	

	$.each( 

			data, 

			function( date, rows )

			{

				th	= $('<tr></tr>').addClass( "header" );

				

				th.append( $('<td></td>').attr( "colspan", 2 ).css("color", "white").html( date ) );

				th.append( $('<td></td>').addClass("odds").css("color", "white").html( 'Handicap' ) );

				th.append( $('<td></td>').addClass("odds").css("color", "white").html( 'Home' ) );

				th.append( $('<td></td>').addClass("odds").css("color", "white").html( 'Away' ) );

				

				$('#bbResults').append( th );

				

				for( i=0; i < rows.length; i++ )

				{

					

					if( rows[i].odds == null ) continue;

					

					var j=0;

					$.each(

						rows[i].odds, 

						function(key, value) 

						{ 

							var tr = $('<tr></tr>');

							

							if( j % 2 == 0)

								tr.addClass( "odd" );

							else

								tr.addClass( "even" );

							j++;

							tr.append( $('<td></td>').html( rows[i].match.time ));

							tr.append( $('<td></td>').html( $('<a></a>')

								.attr( "href", liveSoccerOddsBaseURL + "home=" + escape( rows[i].match.home ) + "&away=" + escape( rows[i].match.away ) + "&date=" + escape( rows[i].match.date_match ) )

								.html( rows[i].match.home + ' vs ' + rows[i].match.away ) ) );

							tr.append( $('<td></td>').addClass("odds").html( key ) );


							if( typeof( archive[rows[i].match.id_match] ) == "undefined" )

							{

								archive[rows[i].match.id_match] = new Array();

							}

							

							if( typeof(archive[rows[i].match.id_match][48] ) == "undefined" )

							{

								archive[rows[i].match.id_match][48] = new Array();

							}

							

							if( typeof(archive[rows[i].match.id_match][48][key] ) == "undefined" )

							{

								archive[rows[i].match.id_match][48][key] = new Array();

							}

							

							tdHome = $('<td></td>').addClass("odds").append( $('<a></a>').attr( "target", "_blank").attr( "href", value.home.url ).html( value.home.odds ) );

							tdAway = $('<td></td>').addClass("odds").append( $('<a></a>').attr( "target", "_blank").attr( "href", value.away.url ).html( value.away.odds ) );

							

							if( typeof( archive[rows[i].match.id_match][48][key][0] ) == "undefined" )

							{

								archive[rows[i].match.id_match][48][key][0] = value.home.odds;

								archive[rows[i].match.id_match][48][key][1] = value.away.odds;

							}

							

							if( archive[rows[i].match.id_match][48][key][0] != value.home.odds )

							{

								tdHome.addClass( "blink" );

							}

							

							if( archive[rows[i].match.id_match][48][key][1] != value.away.odds )

							{

								tdAway.addClass( "blink" );

							}

							

							archive[rows[i].match.id_match][48][key][0] = value.home.odds;

							archive[rows[i].match.id_match][48][key][1] = value.away.odds;

							

							tr.append( tdHome );

							tr.append( tdAway );

							$('#bbResults').append( tr );

						}

					);

					

				}

			}

		);

}



function displayBBOverUnder( data )

{

	$('#bbResults').empty();

	

	

	$.each( 

			data, 

			function( date, rows )

			{

				th	= $('<tr></tr>').addClass( "header" );

				

				th.append( $('<td></td>').attr( "colspan", 2 ).css("color", "white").html( date ) );

				th.append( $('<td></td>').addClass("odds").addClass("odds").css("color", "white").html( 'Total' ) );

				th.append( $('<td></td>').addClass("odds").addClass("odds").css("color", "white").html( 'Under' ) );

				th.append( $('<td></td>').addClass("odds").addClass("odds").css("color", "white").html( 'Over' ) );

				

				$('#bbResults').append( th );

				

				for( i=0; i < rows.length; i++ )

				{

					if( rows[i].odds == null ) continue;

					var j=0;

					

					$.each(

						rows[i].odds, 

						function(key, value) 

						{ 

							var tr = $('<tr></tr>');

							

							if( j % 2 == 0)

								tr.addClass( "odd" );

							else

								tr.addClass( "even" );

							j++;

							

							tr.append( $('<td></td>').html( rows[i].match.time ));

							tr.append( $('<td></td>').html( $('<a></a>')

								.attr( "href", liveSoccerOddsBaseURL + "home=" + escape( rows[i].match.home ) + "&away=" + escape( rows[i].match.away ) + "&date=" + escape( rows[i].match.date_match ) )

								.html( rows[i].match.home + ' vs ' + rows[i].match.away ) ) );

							tr.append( $('<td></td>').addClass("odds").html( key ) );

							

							if( typeof( archive[rows[i].match.id_match] ) == "undefined" )

							{

								archive[rows[i].match.id_match] = new Array();

							}

							

							if( typeof(archive[rows[i].match.id_match][47] ) == "undefined" )

							{

								archive[rows[i].match.id_match][47] = new Array();

							}

							

							if( typeof(archive[rows[i].match.id_match][47][key] ) == "undefined" )

							{

								archive[rows[i].match.id_match][47][key] = new Array();

							}

							

							

							tdUnder =  $('<td></td>').addClass("odds").append( $('<a></a>').attr( "target", "_blank").attr( "href", value.under.url ).html( value.under.odds ) );

							tdOver  =  $('<td></td>').addClass("odds").append( $('<a></a>').attr( "target", "_blank").attr( "href", value.over.url ).html( value.over.odds ) );

							

							if( typeof( archive[rows[i].match.id_match][47][key][0] ) == "undefined" )

							{

								archive[rows[i].match.id_match][47][key][0] = value.under.odds;

								archive[rows[i].match.id_match][47][key][1] = value.over.odds;

							}

							

							if( archive[rows[i].match.id_match][47][key][0] != value.under.odds )

							{

								tdUnder.addClass( "blink" );

							}

							

							if( archive[rows[i].match.id_match][47][key][1] != value.over.odds )

							{

								tdOver.addClass( "blink" );

							}

							

							archive[rows[i].match.id_match][47][key][0] = value.under.odds;

							archive[rows[i].match.id_match][47][key][1] = value.over.odds;

							

							tr.append( tdUnder );

							tr.append( tdOver );

							$('#bbResults').append( tr );

						}

					);

					

				}

			}

		);

}





function bbMenuOn( id )

{

	$('#bbHeader' + id ).addClass( 'on' );

}





function bbMenuOff( id )

{

	if( currentType != id )

	$('#bbHeader' + id ).removeClass( 'on' );

}



function bbLoadingOn( )

{

	$('#bbResults').hide();

	$('#bbLoading').show();

}



function bbLoadingOff( )

{

	$('#bbResults').show();

	$('#bbLoading').hide();

}



var transitionsLeft = 10; 



function blink()

{

	var period = 500;

	if( transitionsLeft % 2 == 0 )

		bg = 'yellow';

	else

		bg = 'white';

	

	$('#bbResults td.blink').animate(

			{

				backgroundColor: bg

			},

			period

	);

	transitionsLeft--;

	if( transitionsLeft > 0 )

	{

		setTimeout( blink, period + 10 );

	}

}



var curTab;

function tabHighlightBB(tabselector){

	$('#tab-nav-bb li').removeClass('active').filter(tabselector).addClass('active');

}









