var staticRaceInfo = {
	"ALDEM" : {
		"state" : "Alabama",
		"electiondate" : "20080205"
	},
	"ALREP" : {
		"state" : "Alabama",
		"electiondate" : "20080205"
	},
	"AKDEM" : {
		"state" : "Alaska",
		"electiondate" : "20080205"
	},
	"AKREP" : {
		"state" : "Alaska",
		"electiondate" : "20080205"
	},
	"ARDEM" : {
		"state" : "Arkansas",
		"electiondate" : "20080205"
	},
	"ARREP" : {
		"state" : "Arkansas",
		"electiondate" : "20080205"
	},
	"ASDEM" : {
		"state" : "American Samoa",
		"electiondate" : "20080205",
		"skipDatePage" : true
	},
	"ASREP" : {
		"state" : "American Samoa",
		"electiondate" : "20080223",
		"skipDatePage" : true
	},
	"AZDEM" : {
		"state" : "Arizona",
		"electiondate" : "20080205"
	},
	"AZREP" : {
		"state" : "Arizona",
		"electiondate" : "20080205"
	},
	"CADEM" : {
		"state" : "California",
		"electiondate" : "20080205"
	},
	"CAREP" : {
		"state" : "California",
		"electiondate" : "20080205"
	},
	"CODEM" : {
		"state" : "Colorado",
		"electiondate" : "20080205"
	},
	"COREP" : {
		"state" : "Colorado",
		"electiondate" : "20080205"
	},
	"CTDEM" : {
		"state" : "Connecticut",
		"electiondate" : "20080205"
	},
	"CTREP" : {
		"state" : "Connecticut",
		"electiondate" : "20080205"
	},
	"DCDEM" : {
		"state" : "District of Columbia",
		"electiondate" : "20080212"
	},
	"DCREP" : {
		"state" : "District of Columbia",
		"electiondate" : "20080212"
	},
	"DADEM" : {
		"state" : "Democrats Abroad",
		"electiondate" : "20080205",
		"skipDatePage": true
	},
	"DEDEM" : {
		"state" : "Delaware",
		"electiondate" : "20080205"
	},
	"DEREP" : {
		"state" : "Delaware",
		"electiondate" : "20080205"
	},
	"FLDEM" : {
		"state" : "Florida",
		"electiondate" : "20080129"
	},
	"FLREP" : {
		"state" : "Florida",
		"electiondate" : "20080129"
	},
	"GADEM" : {
		"state" : "Georgia",
		"electiondate" : "20080205"
	},
	"GAREP" : {
		"state" : "Georgia",
		"electiondate" : "20080205"
	},
	"GUDEM" : {
		"state" : "Guam",
		"electiondate" : "20080503",
		"skipDatePage" : true
	},
	"GUREP" : {
		"state" : "Guam",
		"electiondate" : "20080308",
		"skipDatePage" : true
	},
	"HIREP" : {
		"state" : "Hawaii",
		"electiondate" : "20080518"
	},
	"HIDEM" : {
		"state" : "Hawaii",
		"electiondate" : "20080219"
	},
	"IADEM" : {
		"state" : "Iowa",
		"electiondate" : "20080103"
	},
	"IAREP" : {
		"state" : "Iowa",
		"electiondate" : "20080103"
	},
	"IDDEM" : {
		"state" : "Idaho",
		"electiondate" : "20080205"
	},
	"IDREP" : {
		"state" : "Idaho",
		"electiondate" : "20080527"
	},
	"INDEM" : {
		"state" : "Indiana",
		"electiondate" : "20080506"
	},
	"INREP" : {
		"state" : "Indiana",
		"electiondate" : "20080506"
	},
	"ILDEM" : {
		"state" : "Illinois",
		"electiondate" : "20080205"
	},
	"ILREP" : {
		"state" : "Illinois",
		"electiondate" : "20080205"
	},
	"KSDEM" : {
		"state" : "Kansas",
		"electiondate" : "20080205"
	},
	"KSREP" : {
		"state" : "Kansas",
		"electiondate" : "20080209"
	},
	"KYDEM" : {
		"state" : "Kentucky",
		"electiondate" : "20080520"
	},
	"KYREP" : {
		"state" : "Kentucky",
		"electiondate" : "20080520"
	},
	"LADEM" : {
		"state" : "Louisiana",
		"electiondate" : "20080209"
	},
	"LAREP" : {
		"state" : "Louisiana",
		"electiondate" : "20080209"
	},
	"MADEM" : {
		"state" : "Massachusetts",
		"electiondate" : "20080205"
	},
	"MAREP" : {
		"state" : "Massachusetts",
		"electiondate" : "20080205"
	},
	"MDDEM" : {
		"state" : "Maryland",
		"electiondate" : "20080212"
	},
	"MDREP" : {
		"state" : "Maryland",
		"electiondate" : "20080212"
	},
	"MEREP" : {
		"state" : "Maine",
		"electiondate" : "20080202"
	},
	"MEDEM" : {
		"state" : "Maine",
		"electiondate" : "20080210"
	},
	"MIDEM" : {
		"state" : "Michigan",
		"electiondate" : "20080115"
	},
	"MIREP" : {
		"state" : "Michigan",
		"electiondate" : "20080115"
	},
	"MNDEM" : {
		"state" : "Minnesota",
		"electiondate" : "20080205"
	},
	"MNREP" : {
		"state" : "Minnesota",
		"electiondate" : "20080205"
	},
	"MODEM" : {
		"state" : "Missouri",
		"electiondate" : "20080205"
	},
	"MOREP" : {
		"state" : "Missouri",
		"electiondate" : "20080205"
	},
	"MPREP" : {
		"state" : "Northern Marianas",
		"electiondate" : "20080223",
		"skipDatePage": true
	},
	"MSDEM" : {
		"state" : "Mississippi",
		"electiondate" : "20080311"
	},
	"MSREP" : {
		"state" : "Mississippi",
		"electiondate" : "20080311"
	},
	"MTDEM" : {
		"state" : "Montana",
		"electiondate" : "20080603"
	},
	"MTREP" : {
		"state" : "Montana",
		"electiondate" : "20080205"
	},
	"NCDEM" : {
		"state" : "North Carolina",
		"electiondate" : "20080506"
	},
	"NCREP" : {
		"state" : "North Carolina",
		"electiondate" : "20080506"
	},
	"NDDEM" : {
		"state" : "North Dakota",
		"electiondate" : "20080205"
	},
	"NDREP" : {
		"state" : "North Dakota",
		"electiondate" : "20080205"
	},
	"NEDEM" : {
		"state" : "Nebraska",
		"electiondate" : "20080209"
	},
	/*"NEREP" : {
		"state" : "Nebraska",
		"electiondate" : "20080513"
	},*/
	"NHDEM" : {
		"state" : "New Hampshire",
		"electiondate" : "20080108"
	},
	"NHREP" : {
		"state" : "New Hampshire",
		"electiondate" : "20080108"
	},
	"NJDEM" : {
		"state" : "New Jersey",
		"electiondate" : "20080205"
	},
	"NJREP" : {
		"state" : "New Jersey",
		"electiondate" : "20080205"
	},
	"NMDEM" : {
		"state" : "New Mexico",
		"electiondate" : "20080205"
	},
	"NMREP" : {
		"state" : "New Mexico",
		"electiondate" : "20080603"
	},
	"NYDEM" : {
		"state" : "New York",
		"electiondate" : "20080205"
	},
	"NYREP" : {
		"state" : "New York",
		"electiondate" : "20080205"
	},
	"NVDEM" : {
		"state" : "Nevada",
		"electiondate" : "20080119"
	},
	"NVREP" : {
		"state" : "Nevada",
		"electiondate" : "20080119"
	},
	"OHDEM" : {
		"state" : "Ohio",
		"electiondate" : "20080304"
	},
	"OHREP" : {
		"state" : "Ohio",
		"electiondate" : "20080304"
	},
	"OKDEM" : {
		"state" : "Oklahoma",
		"electiondate" : "20080205"
	},
	"OKREP" : {
		"state" : "Oklahoma",
		"electiondate" : "20080205"
	},
	"ORDEM" : {
		"state" : "Oregon",
		"electiondate" : "20080520"
	},
	"ORREP" : {
		"state" : "Oregon",
		"electiondate" : "20080520"
	},
	"PADEM" : {
		"state" : "Pennsylvania",
		"electiondate" : "20080422"
	},
	"PAREP" : {
		"state" : "Pennsylvania",
		"electiondate" : "20080422"
	},
	"PRDEM" : {
		"state" : "Puerto Rico",
		"electiondate" : "20080601"
		//"skipDatePage" : true
	},
	"PRREP" : {
		"state" : "Puerto Rico",
		"electiondate" : "20080224",
		"skipDatePage" : true
	},
	"RIDEM" : {
		"state" : "Rhode Island",
		"electiondate" : "20080304"
	},
	"RIREP" : {
		"state" : "Rhode Island",
		"electiondate" : "20080304"
	},
	"SCDEM" : {
		"state" : "South Carolina",
		"electiondate" : "20080126"
	},
	"SCREP" : {
		"state" : "South Carolina",
		"electiondate" : "20080119"
	},
	"SDDEM" : {
		"state" : "South Dakota",
		"electiondate" : "20080603"
	},
	"SDREP" : {
		"state" : "South Dakota",
		"electiondate" : "20080603"
	},
	"TNDEM" : {
		"state" : "Tennessee",
		"electiondate" : "20080205"
	},
	"TNREP" : {
		"state" : "Tennessee",
		"electiondate" : "20080205"
	},
	"TXDEM" : {
		"state" : "Texas",
		"electiondate" : "20080304",
		"type": "primary",
		"sortOrder":1
	},
	"TXDEM2" : {
		"state" : "Texas",
		"electiondate" : "20080304",
		"type": "caucuses",
		"sortOrder":2
	},
	"TXREP" : {
		"state" : "Texas",
		"electiondate" : "20080304",
		"type": "primary",
		"sortOrder":1
	},
	"TXREP2" : {
		"state" : "Texas",
		"electiondate" : "19700101",
		"type": "caucuses",
		"sortOrder":2
	},
	"UTDEM" : {
		"state" : "Utah",
		"electiondate" : "20080205"
	},
	"UTREP" : {
		"state" : "Utah",
		"electiondate" : "20080205"
	},
	"VADEM" : {
		"state" : "Virginia",
		"electiondate" : "20080212"
	},
	"VAREP" : {
		"state" : "Virginia",
		"electiondate" : "20080212"
	},
	"VIDEM" : {
		"state" : "U.S. Virgin Islands",
		"electiondate" : "20080209",
		"skipDatePage": true
	},
	"VIREP" : {
		"state" : "U.S. Virgin Islands",
		"electiondate" : "20080405",
		"skipDatePage": true
	},
	"VTDEM" : {
		"state" : "Vermont",
		"electiondate" : "20080304"
	},
	"VTREP" : {
		"state" : "Vermont",
		"electiondate" : "20080304"
	},
	"WADEM" : {
		"state" : "Washington",
		"electiondate" : "20080209"
	},
	"WADEM2" : {
		"state" : "Washington",
		"electiondate" : "20080219"
	},
	"WAREP" : {
		"state" : "Washington",
		"electiondate" : "20080209"
	},
	"WAREP2" : {
		"state" : "Washington",
		"electiondate" : "20080219"
	},
	"WIDEM" : {
		"state" : "Wisconsin",
		"electiondate" : "20080219"
	},
	"WIREP" : {
		"state" : "Wisconsin",
		"electiondate" : "20080219"
	},
	"WVDEM" : {
		"state" : "West Virginia",
		"electiondate" : "20080513",
		"type" : "primary",
		"sortOrder":1
	},/*
	"WVDEM2" : {
		"state" : "West Virginia",
		"electiondate" : "19700101",
		"type": "caucuses",
		"sortOrder":2
	},*/
	"WVREP" : {
		"state" : "West Virginia",
		"electiondate" : "20080513",
		"type":"primary",
		"sortOrder":1
	},
	"WVREP2" : {
		"state" : "West Virginia",
		"electiondate" : "20080205",
		"sortOrder":2,
		"type":"caucuses"
	},
	"WYDEM" : {
		"state" : "Wyoming",
		"electiondate" : "20080308"
	},
	"WYREP" : {
		"state" : "Wyoming",
		"electiondate" : "20080105"
	}
};

var staticMapInfo = {
	"AL" : { "fips" : "01000" },
	"AK" : { 
		"fips" : "02000",
		"party" : {
			"DEM": {
				"skipCountyMap" : true
			},
			"REP": {
				"skipCountyMap" : true
			}
		}
	},
	"AZ" : { "fips" : "04000" },
	"AR" : { "fips" : "05000" },
	"CA" : { "fips" : "06000" },
	"CO" : { "fips" : "08000" },
	"CT" : { "fips" : "09000" },
	"DE" : { "fips" : "10000" },
	"DC" : { "fips" : "11000" },
	"FL" : { "fips" : "12000" },
	"GA" : { "fips" : "13000" },
	"HI" : {
		"fips" : "15000",
		"party" : {
			"DEM": { "skipCountyMap" : false },
			"REP": { "skipCountyMap" : true }
		}
	},
	"ID" : { "fips" : "16000" },
	"IL" : { "fips" : "17000" },
	"IN" : { "fips" : "18000" },
	"IA" : { "fips" : "19000" },
	"KS" : { 
		"fips" : "20000",
		"party" : {
			"DEM": {
				"skipCountyMap" : true
			},
			"REP": {
				"skipCountyMap" : true
			}
		}
	},
	"KY" : { "fips" : "21000" },
	"LA" : { "fips" : "22000" },
	"ME" : { 
		"fips" : "23000",
		"party" : {
			"DEM": {
				"skipCountyMap" : true
			},
			"REP": {
				"skipCountyMap" : true
			}
		}
	},
	"MD" : { "fips" : "24000" },
	"MA" : { "fips" : "25000" },
	"MI" : { "fips" : "26000" },
	"MN" : { "fips" : "27000" },
	"MS" : { "fips" : "28000" },
	"MO" : { "fips" : "29000" },
	"MT" : { "fips" : "30000" },
	"NE" : { 
		"fips" : "31000",
		"party" : {
			"DEM": {
				"skipCountyMap" : true
			},
			"REP": {
				"skipCountyMap" : true
			}
		}
	},
	"NV" : { "fips" : "32000" },
	"NH" : { "fips" : "33000" },
	"NJ" : { "fips" : "34000" },
	"NM" : { 
		"fips" : "35000",
		"party" : {
			"DEM": {
				"skipCountyMap" : true
			},
			"REP": {
				"skipCountyMap" : true
			}
		}
	},
	"NY" : { "fips" : "36000" },
	"NC" : { "fips" : "37000" },
	"ND" : { 
		"fips" : "38000",
		"party" : {
			"DEM": {
				"skipCountyMap" : true
			},
			"REP": {
				"skipCountyMap" : true
			}
		}
	},
	"OH" : { "fips" : "39000" },
	"OK" : { "fips" : "40000" },
	"OR" : { "fips" : "41000" },
	"PA" : { "fips" : "42000" },
	"PR" : {
		"fips" : "72000",
		"party": {
			"DEM": { "skipCountyMap" : false },
			"REP": { "skipCountyMap" : true }
		}
	},
	"RI" : { "fips" : "44000" },
	"SC" : { "fips" : "45000" },
	"SD" : { "fips" : "46000" },
	"TN" : { "fips" : "47000" },
	"TX" : { 
		"fips" : "48000",
		"party" : {
			"DEM": {
				"mapRaceTypeOnState" : "primary"
			},
			"REP": {
				"mapRaceTypeOnState" : "primary"
			}
		}
	},
	"UT" : { "fips" : "49000" },
	"VT" : { "fips" : "50000" },
	"VA" : { "fips" : "51000" },
	"WA" : { 
		"fips" : "53000",
		"party" : {
			"DEM": {
				"mapRaceTypeOnState" : "caucus"
			},
			"REP": {
				"mapRaceTypeOnState" : "caucus"
			}
		}
	},
	"WV" : { "fips" : "54000",
		"party" : {
			"DEM": {
				"mapRaceTypeOnState" : "primary"
			},
			"REP": {
				"mapRaceTypeOnState" : "primary"
			}
		}
	},
	"WI" : { "fips" : "55000" },
	"WY" : { "fips" : "56000",
		"party" : {
			"DEM": {
				"skipCountyMap" : false
			},
			"REP": {
				"skipCountyMap" : true
			}
		}
	}
}

//gets array of race objects with electiondate of given yyyymmdd
function getRacesByDate(yyyymmdd){
	var results = new Array();
	for(key in staticRaceInfo){
		if(staticRaceInfo[key].electiondate == yyyymmdd){
			var theLength = results.length;
			results[theLength] = {
				"code" : key.substring(0,2),
				"party" : key.substring(2).replace(/2/,''),
				"state" : staticRaceInfo[key].state,
				"electiondate" : staticRaceInfo[key].electiondate
			};
			if(typeof(staticRaceInfo[key].skipDatePage) != 'undefined'){
				results[theLength].skipDatePage = staticRaceInfo[key].skipDatePage;
			}
			if(typeof(staticRaceInfo[key].type) != 'undefined'){
				results[theLength].type = staticRaceInfo[key].type;
			}
			if(typeof(staticRaceInfo[key].sortOrder) != 'undefined'){
				results[theLength].sortOrder = staticRaceInfo[key].sortOrder;
			}
		}
	}
	//sort by state, then by sort order (if it exists) then by party
	results.sort(
		function(race1,race2){
			var thisCode = race1.state;
			var thatCode = race2.state;
			
			thisCode += (typeof(race1.sortOrder) != "undefined") ? race1.sortOrder : 1;
			thatCode += (typeof(race2.sortOrder) != "undefined") ? race2.sortOrder : 1;
			
			thisCode += race1.party;
			thatCode += race2.party;			
			
			
			return (thisCode > thatCode) ? 1 : (thisCode < thatCode) ? -1 : 0;
		}
	);
	return results;
}

var repTX = 0;
var demTX=0;

function raceIsOnThisDate(stateCode,party,yyyymmdd){try{
	var key = stateCode.toUpperCase()+party.toUpperCase();

	if( stateCode.toUpperCase() == 'WA' && yyyymmdd == '20080219' )
	{
		key += "2";
	}
	else if( stateCode.toUpperCase() == 'TX' )
	{
		if( party.toUpperCase() == 'DEM' )
		{
			demTX++;
			if( demTX > 1 )
			{
				key += demTX.toString();
			}
		}
		else if( party.toUpperCase() == 'REP' )
		{
			repTX++;
			if( repTX > 1 )
			{
				key += repTX.toString();
			}
		}
	}
	var theRaceDate = staticRaceInfo[key].electiondate;
	return (theRaceDate == yyyymmdd);
}catch(e){return false;}}

//gets the electiondate for a given statecode/party combination
function getRaceDate(stateCode,party){try{
	var key = stateCode.toUpperCase()+party.toUpperCase();
	var result = staticRaceInfo[key].electiondate;
	return (result !== undefined) ? result : '';
}catch(e){return '';}}

// Determines if we can display the county for a particulr state/party or not
function getHasCountyMap(stateCode, party)
{
	var theStateCode = stateCode.toUpperCase();
	var theParty = party.toUpperCase();
	var hasMap = true;
	if(staticMapInfo[theStateCode] && staticMapInfo[theStateCode].party && staticMapInfo[theStateCode].party[theParty] && staticMapInfo[theStateCode].party[theParty].skipCountyMap == true){
		hasMap = false;
	}
	return hasMap;
}

// Determines if we can display the county for a particulr state/party or not
function getFIPS(stateCode)
{
	var key = stateCode.toUpperCase();
	var fips = staticMapInfo[key].fips;
	return fips;
}

//Used for JSON from the state page to decide which map to display when there is more that the usual 2 contests of the same race type
function getRaceTypeForMapOnState(cnnStateRaces, stateCode, party){
	var theStateCode = stateCode.toUpperCase();
	var theParty = party.toUpperCase();
	var raceToDisplay = 'primary';
	for(var i=0; i<cnnStateRaces.length; i++){
		if(cnnStateRaces[i].party == party){
			raceToDisplay = cnnStateRaces[i].primarytype;
		}
	}
	if(staticMapInfo[theStateCode] && staticMapInfo[theStateCode].party && staticMapInfo[theStateCode].party[theParty] && staticMapInfo[theStateCode].party[theParty].mapRaceTypeOnState){
		raceToDisplay = staticMapInfo[theStateCode].party[theParty].mapRaceTypeOnState;
	}
	return raceToDisplay;
}
