// JS Document

var slWi = 659;
var slideWidth = 0;
var curVal = 0;
var score_track_sec = 30;
var score_track_int;

function toggleScore(val,wid) {
	curVal = val;
	if (curVal == undefined) {
		curVal = $('#curVal').val();
	}
	slideWidth = wid;
	if (slideWidth == undefined) {
		slideWidth = $('#slideWidth').val();
	}
	if (slideWidth <= slWi) {
		$('#lArr').hide();
		$('#rArr').hide();
	} else {
		$('#lArr').show();
		$('#rArr').show();
	}
	var obj = $('#titler div');
	for(var i = 0; i < obj.length; i++) {
	 	if (obj[i].id.substr(0,5) != 'clear') {
	 		if (obj[i].id.substr(4) == curVal) {
	 			document.getElementById('scoreBg').className = 'scoreBg scoreBg_'+obj[i].id.substr(4);
	 			$(obj[i]).addClass('activeSB');
	 			$('#sb_'+obj[i].id.substr(4)).show();
	 		} else {
	 			$(obj[i]).removeClass('activeSB');
	 			$('#sb_'+obj[i].id.substr(4)).hide();
	 		}
	 	}
	 }
    if (val == 8) {
    	$('#sponsorSpot').addClass('nbaS');
    	$('#sponsorSpot').removeClass('ncaaS');
    } else {
    	$('#sponsorSpot').removeClass('nbaS');
    	$('#sponsorSpot').addClass('ncaaS');
    }
    
    enable_score_ping();
    
}

var curSp = 0;
function moveScores(ty) {
    var windows = Math.ceil(slideWidth/660);
    if ((ty == 2) && (curSp < (windows-1))) { 	// Move Left
        curSp++;
    } else if ((ty == 1) && (curSp != 0)) { 	// Move Right
        curSp--;
    }
    $("#scorePocket_"+curVal).animate({marginLeft:'-'+(660*curSp)+'px'},'slow');
}

var resGetScores = function(msg) {
	//alert(msg); return;
    if (msg.substr(0, 5) == 'Error') {
    	$.facebox('<div class="padBtm10"><div class="errorFlag"></div></div><div>'+msg+'</div>');
    } else {
    	if (msg != '') {
			var arr = eval(msg);
			for (var i = 0; i < arr.length; i++) {
				var qtr_str = '';
				var tim_lef = '';
				if (arr[i]['status'] > 40) {
					qtr_str = 'Rain Delay';
					tim_lef = '&nbsp;';
				} else if (arr[i]['status'] == 2) {
					qtr_str = 'Final';
					tim_lef = '&nbsp;';
				} else if (arr[i]['status'] == 1) {
					var qtr = '';
					if (arr[i]['num'] == 1) {
						qtr = 'st';
					} else if (arr[i]['num'] == 2) {
						qtr = 'nd';
					} else if (arr[i]['num'] == 3) {
						qtr = 'rd';
					} else {
						qtr = 'th';
					}
					qtr_str = arr[i]['num']+qtr+' '+arr[i]['unitsAbbr'];
					tim_lef = arr[i]['time_left'];
				} else if (arr[i]['status'] == 0) {
					qtr_str = arr[i]['dateTime'];
					tim_lef = '&nbsp;';
				}
				$('#game_st_'+arr[i]['thisGID']+' div.tspot').html(qtr_str);
				$('#game_st_'+arr[i]['thisGID']+' div.tleft').html(tim_lef);
				$('#game_st_'+arr[i]['thisGID']+' div.aw_score').html(arr[i]['awayFinal']);
				$('#game_st_'+arr[i]['thisGID']+' div.ho_score').html(arr[i]['homeFinal']);
				
			}
		}
	}
}

function get_scores() {
	
	var sendVals = 'call=get-scores';
    
    $.ajax({
        type: "POST",
        url: "/classes/ScoreTracker.php",
        data: sendVals,
        success: resGetScores
    });
	
}

function enable_score_ping() {
	score_track_int = setInterval('get_scores()', (1000*score_track_sec));
}

$(document).ready(function(e) {
	toggleScore();
});
