var $portfolioName = null;
var $showMessage = false;
//**********
var $colors = new Array('total', 'monthly', 'quarterly', 'yearly', 'fbo', 'baseballonly', 'golfonly', 'racingonly');
//**********
$colors['total'] = new Array();
$colors['total']['name'] = '#CCC';
$colors['total']['row'] = '#DDD';
//**********
$colors['monthly'] = new Array();
$colors['monthly']['name'] = '#FF6464';
$colors['monthly']['row'] = '#FFA8A8';
//**********
$colors['quarterly'] = new Array();
$colors['quarterly']['name'] = '#FFFF00';
$colors['quarterly']['row'] = '#FFFFB9';
//**********
$colors['yearly'] = Array();
$colors['yearly']['name'] = '#C5C8E0';
$colors['yearly']['row'] = '#DFE0EE';
//**********
$colors['football'] = Array();
$colors['football']['name'] = '#C5C8E0';
$colors['football']['row'] = '#DFE0EE';
//**********
$colors['baseball'] = Array();
$colors['baseball']['name'] = '#C5C8E0';
$colors['baseball']['row'] = '#DFE0EE';
//**********
$colors['golf'] = Array();
$colors['golf']['name'] = '#C5C8E0';
$colors['golf']['row'] = '#DFE0EE';
//**********
$colors['racing'] = Array();
$colors['racing']['name'] = '#C5C8E0';
$colors['racing']['row'] = '#DFE0EE';
//********************
function updatePort( $data ) {
	//NOTE: Set the new portfolio text.
	$('#currentPortfolio').html('<strong>'+$data+'</strong>');
	//**********
	//NOTE: Grabs the user's balance.
	$.ajax({
		cache: false,
		url: 'updatebalance.php',
		success: function($data) {
			updateBalanceRow($data);
		},
		error: function() {
		}
	});
	//********************
	updateMarket();
}
//**********
function updateBalanceRow($data) {
	//NOTE: Set the new balance [visually].
	$('#balanceRow').html('<strong>'+$data+'</strong>');
}
//**********
function updateMarket() {
	//NOTE: Clear owned markers.
	clearOwnedMarkers();
	//**********
	$.ajax({
		cache: false,
		url: 'updatemarket.php',
		success: function($data) {
			updateMarketVisual($data);
		},
		error: function() {
		}
	});
}
function updateMarketVisual($data) {
	if( $portfolioName == null ) {
		$portfolioName = $('#selectPortfolio').val();
	}
	//**********
	$('.playerRow td:nth-child(1)').css('background-color', '#DEECFF');
	$('.playerRow td:nth-child(2)').css('background-color', '#EDF4FD');
	//**********
	$('.playerRow td a').attr('class', 'Jocklink1');
	//**********
	$('.playerRow').css('background-color', 'transparent');
	//********************
	$dataParsed = $.parseJSON($data);
	//**********
	$.each( $dataParsed, function( $i, $item ) {
		if( $('#id-'+$item.id).length ) {
			$player = $('#id-'+$item.id);
			//**********
			$children = $player.children('td');
			//**********
			$children.eq(3).html($item.fbuyvolume);
			$children.eq(4).html($item.fsellvolume);
			//**********
			if( $item.fmovementpercent > 0 ) {
				$children.eq(5).html('<span style="color:green;">'+$item.fmovementpercent+'%</span>');
				$children.eq(6).html('<span style="color:green;">$'+$item.fmovementamount+'</span>');
			} else if( $item.fmovementpercent < 0 ) {
				$children.eq(5).html('<span style="color:red;">'+$item.fmovementpercent+'%</span>');
				$children.eq(6).html('<span style="color:red;">$'+$item.fmovementamount+'</span>');
			} else {
				$children.eq(5).html($item.fmovementpercent+'%');
				$children.eq(6).html('$'+$item.fmovementamount);
			}
			//**********
			$children.eq(7).html('$'+$item.flastdiv);
			$children.eq(8).html('$'+$item.fvalue);
			//**********
			if( $item.plid != null ) {
				//**********
				$children.eq(0).css('background-color', $colors[$portfolioName]['name']);
				$children.eq(1).css('background-color', $colors[$portfolioName]['row']);
				//**********
				$children.children('a').attr('class', 'blacklink2');
				//**********
				$player.css('background-color', $colors[$portfolioName]['row']);
				//**********
				$player.removeClass('purchase');
				$player.addClass('owned');
			}
		}
	});
	//********************
	if( $showMessage ) {
		if( $portfolioName == 'total' ) {
			$('#updateBox').css({'background-color':'#DDD', 'border-color':'#999'});
		} else if( $portfolioName == 'monthly' ) {
			$('#updateBox').css({'background-color':'#FF9F9F', 'border-color':'#8A0000'});
		} else if( $portfolioName == 'quarterly' ) {
			$('#updateBox').css({'background-color':'#FFFFB7', 'border-color':'#FBFB00'});
		} else {
			$('#updateBox').css({'background-color':'#B7B7FF', 'border-color':'#000095'});
		}
		//**********
		$('#updateBox').html('Active portfolio changed!').show();
	}
	//********************
	/*
	//**********
	$('#market').tablesorter({
		sortList: [[6,1],[4,1]],
		headers: {0:{sorter: false}, 9:{sorter: false}}
    });
    //*/
}
//**********
function changePort( $portName ) {
    clearPurchaseMarkers();
    //**********
	$showMessage = true;
	//**********
	$('#updateBox').hide();
	//**********
	$portfolioName = $portName;
	//**********
	var imageLink = '';
	//NOTE: Changes the 'active port' image in the lower-right of the template.
	if( $portName == 'total' )
		imageLink = '/images/template/1/' + $portName + 'value.gif';
	else if( $portName == 'football' )
		imageLink = '/images/template/1/fbo.gif';
	else if( $portName == 'baseball' || $portName == 'golf' || $portName == 'racing' )
		imageLink = '/images/template/1/' + $portName + 'only.gif';
	else
		imageLink = '/images/template/1/' + $portName + '.gif';
	//**********
	$('#portImage').attr('src', imageLink );
	//********************
	// Changes the users current portfolio.
	$.ajax({
		cache: false,
		data: 'portfolio=' + escape($portName) + '&version=2',
		url: 'changeactiveport.php',
		success: function($data) {
			updatePort($data);
		},
		error: function() {
		}
	});
}
