// Get xml
function fetchXml(aType){

	type = aType.toUpperCase();
	
	if(type == 'SEN'){
		type = 'Sen';
		typeLetter = 'S';
		$('#hs_header').text('Senate Races');
	}
	else if(type == 'HOUSE'){
		type = 'House';
		typeLetter = 'H';
		$('#hs_header').text('Key House Races');
	}

	// if we don't have the params we need... forward to home	
	if(!type){
		document.location.href = 'http://election.cbsnews.com';
	}
	
	$.ajax({
		url: '/election2008/xmlfiles/Races_' + type + '.xml',
		success: loadXml
	});
}

// Load up the xml
function loadXml(feed){
	xml = feed;
	
	if(this.url.indexOf('Races_Sen') != -1){
		var type = 'Sen';
	}
	else if(this.url.indexOf('Races_House') != -1){
		var type = 'House';
	}

	var $main = $('xTab', xml);
	
	// Populate header variables
	
	// Clear before we start displaying
	$('#output').html('');

	if($('#bop-' + type).size() == 1){
		loadBalanceOfPower(type, xml, 'bop-' + type);
	}

	if($('#main').size() == 1){
		loadBalanceOfPower(type, xml, 'main');
	}

	if(loadTableBool){
		$('Race', xml).each(function(){
			loadTable(this);
		});
	}


	// Show last updated dialog
	lastUpdated('el08_lastUpdated', 1);

	// We set the interval at the end so it's X seconds from completion
	setTimeout(function(){ fetchXml(type); }, interval*1000);
}

function loadBalanceOfPower(type, xml, table){
	var demWon = 0;
	var repWon = 0;

	$xml = $(xml);
	$table = $('#'+table);

	// Store old values in temp
	var demWonOld = demWon;
	var repWonOld = repWon;

	// General data
	var $racesWon = $('Table[Name=Makeup] ', $xml);
	demWon = parseInt($racesWon.attr('DemNew'));
	repWon = parseInt($racesWon.attr('RepNew'));
	repWon = parseInt($racesWon.attr('RepNew'));

	var indWon = parseInt($racesWon.attr('OtherNew'));
	var demOld = parseInt($racesWon.attr('DemOld'));
	var repOld = parseInt($racesWon.attr('RepOld'));
	var indOld = parseInt($racesWon.attr('OtherOld'));
	
	// Meta Info
	if(type == 'Sen'){
		$('.atStake', $table).html( 35 );
		$('.majority', $table).html( 51 );
		$('.controlOf', $table).html('Control<br />of Senate');
	}
	if(type == 'House'){
		$('.atStake', $table).html( 435 );
		$('.majority', $table).html( 218 );
		$('.controlOf', $table).html('Control<br />of House');
	}
	
	// Graph only if values changed
	if(demWon != demWonOld || repWon != repWonOld){

		// For senate, add 2 to democrats since 2 independents caucus with dems.
		if(type == 'Sen'){
			var demWonGraph = demWon + 2;
		} else {
			var demWonGraph = demWon;
		}
		var repWonGraph = repWon;

		// Set Values
		$('.balanceOfPowerGraphNumLeft', $table).html(demWonGraph.toString());
		$('.balanceOfPowerGraphNumRight', $table).html(repWonGraph.toString());	
		
		if(type == 'Sen'){
			var demBarWidth = Math.floor((demWonGraph)*2.18);
			var repBarWidth = Math.floor(repWonGraph*2.18);
		} else {
			var demBarWidth = Math.floor(demWonGraph/2);
			var repBarWidth = Math.floor(repWonGraph/2);
		}
	
		$('.balanceOfPowerGraphLeft', $table).animate({width: demBarWidth}, 2500);
		$('.balanceOfPowerGraphRight', $table).animate({width: repBarWidth}, 2500);

	
	}
	// Graph Net Gain
	var $turnovers = $('Table[Name=Turnovers] ', $xml);
	var netGains = $turnovers.attr('NetGains');

	var $netGainsElem = $('.balanceOfPowerGraphNetChangeVal', $table);
	if($netGainsElem.size() > 0){
		$netGainsElem.html(netGains);
	}

	if(netGains.indexOf('DEM') != -1){
		$netGainsElem.css('color', '#5678B6');
	} else {
		$netGainsElem.css('color', '#A05353');
	}

	// Table
	$bopTable = $('.balanceOfPowerTable', $table);
	
	populateBOPTable($bopTable, 1, demOld, demWon);
	populateBOPTable($bopTable, 2, repOld, repWon);
	populateBOPTable($bopTable, 3, indOld, indWon);
}

function populateBOPTable($table, row, oldVal, newVal){
	var $row = $('tr:eq('+row+')', $table);
	if(oldVal == 0) { oldVal = '-'}
	if(newVal == 0) { newVal = '-'}
	$('td:eq(1)', $row).html(oldVal);
	$('td:eq(2)', $row).html(newVal);
}
