var currentMonth;
var currentYear;
var minCalendarDate;
var maxCalendarDate;
var defaultMinCalendarDate = "000000";
var defaultMaxCalendarDate = "999999";
var rowCount = 1;
var tempCal = '';

var electionCalData = {
	"DEM" : {},
	"REP" : {},
	"NAV" : {}
};

electionCalData['REP'] = new Array();
electionCalData['DEM'] = new Array();

electionCalData["REP"][20080103] = new Array({ "party":"REP","state":"Iowa","code":"IA","primarytype":"caucus" });
electionCalData["DEM"][20080103] = new Array({ "party":"DEM","state":"Iowa","code":"IA","primarytype":"caucus" });
electionCalData["NAV"][20080103] = {"left" : '', "right" : '20080105', "right_month" : '0'};
electionCalData["REP"][20080105] = new Array({ "party":"REP","state":"Wyoming","code":"WY","primarytype":"caucus" });
electionCalData["DEM"][20080105] = new Array();
electionCalData["NAV"][20080105] = {"left" : '20080103', "left_month" : '0', "right" : '20080108', "right_month" : '0' };
electionCalData["REP"][20080108] = new Array({ "party":"REP","state":"New Hampshire","code":"NH","primarytype":"primary" });
electionCalData["DEM"][20080108] = new Array({ "party":"DEM","state":"New Hampshire","code":"NH","primarytype":"primary" });
electionCalData["NAV"][20080108] = {"left" : '20080105', "left_month" : '0', "right" : '20080115', "right_month" : '0' };
electionCalData["REP"][20080115] = new Array({ "party":"REP","state":"Michigan","code":"MI","primarytype":"primary" });
electionCalData["DEM"][20080115] = new Array({ "party":"DEM","state":"Michigan","code":"MI","primarytype":"primary" });
electionCalData["NAV"][20080115] = {"left" : '20080108', "left_month" : '0', "right" : '20080119', "right_month" : '0' };
electionCalData["REP"][20080119] = new Array({ "party":"REP","state":"Nevada","code":"NV","primarytype":"caucus" }, { "party":"REP","electiondate":"20080119","state":"South Carolina","code":"SC","primarytype":"primary" });
electionCalData["DEM"][20080119] = new Array({ "party":"DEM","state":"Nevada","code":"NV","primarytype":"caucus" });
electionCalData["NAV"][20080119] = {"left" : '20080115', "left_month" : '0', "right" : '20080126', "right_month" : '0' };
electionCalData["REP"][20080126] = new Array();
electionCalData["DEM"][20080126] = new Array({ "party":"DEM","state":"South Carolina","code":"SC","primarytype":"primary" });
electionCalData["NAV"][20080126] = {"left" : '20080119', "left_month" : '0', "right" : '20080129', "right_month" : '0' };
electionCalData["REP"][20080129] = new Array({ "party":"REP","state":"Florida","code":"FL","primarytype":"primary" });
electionCalData["DEM"][20080129] = new Array({ "party":"DEM","state":"Florida","code":"FL","primarytype":"primary" });
electionCalData["NAV"][20080129] = {"left" : '20080126', "left_month" : '0', "right" : '20080202', "right_month" : '1' };
electionCalData["REP"][20080202] = new Array({ "party":"REP","state":"Maine","code":"ME","primarytype":"caucus" });
electionCalData["DEM"][20080202] = new Array();
electionCalData["NAV"][20080202] = {"left" : '20080129', "left_month" : '0', "right" : '20080205', "right_month" : '1' };
electionCalData["REP"][20080205] = new Array(
{ "party":"REP","state":"Alabama","code":"AL","primarytype":"primary" },
{ "party":"REP","state":"Alaska","code":"AK","primarytype":"caucus" },
{ "party":"REP","state":"Arizona","code":"AZ","primarytype":"primary" },
{ "party":"REP","state":"Arkansas","code":"AR","primarytype":"primary" },
{ "party":"REP","state":"California","code":"CA","primarytype":"primary" },
{ "party":"REP","state":"Colorado","code":"CO","primarytype":"caucus" },
{ "party":"REP","state":"Connecticut","code":"CT","primarytype":"primary" },
{ "party":"REP","state":"Delaware","code":"DE","primarytype":"primary" },
{ "party":"REP","state":"Georgia","code":"GA","primarytype":"primary" },
{ "party":"REP","state":"Montana","code":"MT","primarytype":"caucus" },
{ "party":"REP","state":"West Virginia","code":"WV","primarytype":"convention" },
{ "party":"REP","state":"Illinois","code":"IL","primarytype":"primary" },
{ "party":"REP","state":"Massachusetts","code":"MA","primarytype":"primary" },
{ "party":"REP","state":"Minnesota","code":"MN","primarytype":"caucus" },
{ "party":"REP","state":"Missouri","code":"MO","primarytype":"primary" },
{ "party":"REP","state":"New Jersey","code":"NJ","primarytype":"primary" },
{ "party":"REP","state":"New York","code":"NY","primarytype":"primary" },
{ "party":"REP","state":"North Dakota","code":"ND","primarytype":"caucus" },
{ "party":"REP","state":"Oklahoma","code":"OK","primarytype":"primary" },
{ "party":"REP","state":"Tennessee","code":"TN","primarytype":"primary" },
{ "party":"REP","state":"Utah","code":"UT","primarytype":"primary" }
);
electionCalData["DEM"][20080205] = new Array(
{ "party":"DEM","state":"Alabama","code":"AL","primarytype":"primary" },
{ "party":"DEM","state":"Alaska","code":"AK","primarytype":"caucus" },
{ "party":"DEM","state":"Arizona","code":"AZ","primarytype":"primary" },
{ "party":"DEM","state":"Arkansas","code":"AR","primarytype":"primary" },
{ "party":"DEM","state":"California","code":"CA","primarytype":"primary" },
{ "party":"DEM","state":"Colorado","code":"CO","primarytype":"caucus" },
{ "party":"DEM","state":"Connecticut","code":"CT","primarytype":"primary" },
{ "party":"DEM","state":"Delaware","code":"DE","primarytype":"primary" },
{ "party":"DEM","state":"Georgia","code":"GA","primarytype":"primary" },
{ "party":"DEM","state":"Idaho","code":"ID","primarytype":"caucus" },
{ "party":"DEM","state":"Kansas","code":"KS","primarytype":"caucus" },
{ "party":"DEM","state":"New Mexico","code":"NM","primarytype":"primary" },
{ "party":"DEM","state":"Illinois","code":"IL","primarytype":"primary" },
{ "party":"DEM","state":"Massachusetts","code":"MA","primarytype":"primary" },
{ "party":"DEM","state":"Minnesota","code":"MN","primarytype":"caucus" },
{ "party":"DEM","state":"Missouri","code":"MO","primarytype":"primary" },
{ "party":"DEM","state":"New Jersey","code":"NJ","primarytype":"primary" },
{ "party":"DEM","state":"New York","code":"NY","primarytype":"primary" },
{ "party":"DEM","state":"North Dakota","code":"ND","primarytype":"caucus" },
{ "party":"DEM","state":"Oklahoma","code":"OK","primarytype":"primary" },
{ "party":"DEM","state":"Tennessee","code":"TN","primarytype":"primary" },
{ "party":"DEM","state":"Utah","code":"UT","primarytype":"primary" }
);
electionCalData["NAV"][20080205] = {"left" : '20080202', "left_month" : '1', "right" : '20080209', "right_month" : '1' };
electionCalData["REP"][20080209] = new Array(
{ "party":"REP","state":"Kansas","code":"KS","primarytype":"caucus" },
{ "party":"REP","state":"Louisiana","code":"LA","primarytype":"primary" },
{ "party":"REP","state":"Washington","code":"WA","primarytype":"caucus" }
);
electionCalData["DEM"][20080209] = new Array(
{ "party":"DEM","state":"Nebraska","code":"NE","primarytype":"caucus" },
{ "party":"DEM","state":"Louisiana","code":"LA","primarytype":"primary" },
{ "party":"DEM","state":"Washington","code":"WA","primarytype":"caucus" }
);
electionCalData["NAV"][20080209] = {"left" : '20080205', "left_month" : '1', "right" : '20080210', "right_month" : '1' };
electionCalData["REP"][20080210] = new Array();
electionCalData["DEM"][20080210] = new Array({ "party":"DEM","state":"Maine","code":"ME","primarytype":"caucus" });
electionCalData["NAV"][20080210] = {"left" : '20080209', "left_month" : '1', "right" : '20080212', "right_month" : '1' };
electionCalData["REP"][20080212] = new Array(
{ "party":"REP","state":"District of Columbia","code":"DC","primarytype":"primary" },
{ "party":"REP","state":"Maryland","code":"MD","primarytype":"primary" },
{ "party":"REP","state":"Virginia","code":"VA","primarytype":"primary" }
);
electionCalData["DEM"][20080212] = new Array(
{ "party":"DEM","state":"District of Columbia","code":"DC","primarytype":"primary" },
{ "party":"DEM","state":"Maryland","code":"MD","primarytype":"primary" },
{ "party":"DEM","state":"Virginia","code":"VA","primarytype":"primary" }
);
electionCalData["NAV"][20080212] = {"left" : '20080210', "left_month" : '1', "right" : '20080219', "right_month" : '1' };
electionCalData["REP"][20080219] = new Array(
{ "party":"REP","state":"Washington","code":"WA","primarytype":"primary" },
{ "party":"REP","state":"Wisconsin","code":"WI","primarytype":"primary" }
);
electionCalData["DEM"][20080219] = new Array(
{ "party":"DEM","state":"Hawaii","code":"HI","primarytype":"caucus" },
{ "party":"DEM","state":"Washington","code":"WA","primarytype":"primary" },
{ "party":"DEM","state":"Wisconsin","code":"WI","primarytype":"primary" }
);
electionCalData["NAV"][20080219] = {"left" : '20080212', "left_month" : '1', "right" : '20080304', "right_month" : '2' };
electionCalData["REP"][20080304] = new Array(
{ "party":"REP","state":"Ohio","code":"OH","primarytype":"primary" },
{ "party":"REP","state":"Rhode Island","code":"RI","primarytype":"primary" },
{ "party":"REP","state":"Texas","code":"TX","primarytype":"primary" },
{ "party":"REP","state":"Vermont","code":"VT","primarytype":"primary" }
);
electionCalData["DEM"][20080304] = new Array(
{ "party":"DEM","state":"Ohio","code":"OH","primarytype":"primary" },
{ "party":"DEM","state":"Rhode Island","code":"RI","primarytype":"primary" },
{ "party":"DEM","state":"Texas","code":"TX","primarytype":"primary" },
{ "party":"DEM","state":"Vermont","code":"VT","primarytype":"primary" }
);
electionCalData["NAV"][20080304] = {"left" : '20080219', "left_month" : '1', "right" : '20080308', "right_month" : '2' };
electionCalData["REP"][20080308] = new Array();
electionCalData["DEM"][20080308] = new Array({ "party":"DEM","state":"Wyoming","code":"WY","primarytype":"caucus" });
electionCalData["NAV"][20080308] = {"left" : '20080304', "left_month" : '2', "right" : '20080311', "right_month" : '2' };
electionCalData["REP"][20080311] = new Array({ "party":"REP","state":"Mississippi","code":"MS","primarytype":"primary" });
electionCalData["DEM"][20080311] = new Array({ "party":"DEM","state":"Mississippi","code":"MS","primarytype":"primary" });
electionCalData["NAV"][20080311] = {"left" : '20080308', "left_month" : '2', "right" : '20080422', "right_month" : '3' };
electionCalData["REP"][20080422] = new Array({ "party":"REP","state":"Pennsylvania","code":"PA","primarytype":"primary" });
electionCalData["DEM"][20080422] = new Array({ "party":"DEM","electiondate":"20080422","state":"Pennsylvania","code":"PA","primarytype":"primary" });
electionCalData["NAV"][20080422] = {"left" : '20080311', "left_month" : '2', "right" : '20080506', "right_month" : '4' };
electionCalData["REP"][20080506] = new Array(
{ "party":"REP","state":"Indiana","code":"IN","primarytype":"primary" },
{ "party":"REP","state":"North Carolina","code":"NC","primarytype":"primary" }
);
electionCalData["DEM"][20080506] = new Array(
{ "party":"DEM","state":"Indiana","code":"IN","primarytype":"primary" },
{ "party":"DEM","state":"North Carolina","code":"NC","primarytype":"primary" }
);
electionCalData["NAV"][20080506] = {"left" : '20080422', "left_month" : '3', "right" : '20080513', "right_month" : '4' };
electionCalData["REP"][20080513] = new Array(
{ "party":"REP","state":"West Virginia","code":"WV","primarytype":"primary" }
);
electionCalData["DEM"][20080513] = new Array(
{ "party":"DEM","state":"West Virginia","code":"WV","primarytype":"primary" }
);
electionCalData["NAV"][20080513] = {"left" : '20080506', "left_month" : '4', "right" : '20080520', "right_month" : '4' };
//electionCalData["REP"][20080518] = new Array({ "party":"REP","state":"Hawaii","code":"HI","primarytype":"convention" });
//electionCalData["DEM"][20080518] = new Array();
//electionCalData["NAV"][20080518] = {"left" : '20080513', "left_month" : '4', "right" : '20080520', "right_month" : '4' };
electionCalData["REP"][20080520] = new Array(
{ "party":"REP","state":"Kentucky","code":"KY","primarytype":"primary" },
{ "party":"REP","state":"Oregon","code":"OR","primarytype":"primary" }
);
electionCalData["DEM"][20080520] = new Array(
{ "party":"DEM","state":"Kentucky","code":"KY","primarytype":"primary" },
{ "party":"DEM","state":"Oregon","code":"OR","primarytype":"primary" }
);
electionCalData["NAV"][20080520] = {"left" : '20080513', "left_month" : '4', "right" : '20080527', "right_month" : '4' };
electionCalData["REP"][20080527] = new Array({ "party":"REP","state":"Idaho","code":"ID","primarytype":"primary" });
electionCalData["DEM"][20080527] = new Array();
electionCalData["NAV"][20080527] = {"left" : '20080520', "left_month" : '4', "right" : '20080601', "right_month" : '5' };

electionCalData["REP"][20080601] = new Array();
electionCalData["DEM"][20080601] = new Array({ "party":"DEM","state":"Puerto Rico","code":"PR","primarytype":"primary" });
electionCalData["NAV"][20080601] = {"left" : '20080527', "left_month" : '4', "right" : '20080603', "right_month" : '5' };


electionCalData["REP"][20080603] = new Array({ "party":"REP","state":"New Mexico","code":"NM","primarytype":"primary" }, { "party":"REP","state":"South Dakota","code":"SD","primarytype":"primary" });
electionCalData["DEM"][20080603] = new Array({ "party":"DEM","state":"Montana","code":"MT","primarytype":"primary" }, { "party":"DEM","state":"South Dakota","code":"SD","primarytype":"primary" });
electionCalData["NAV"][20080603] = {"left" : '20080601', "left_month" : '5', "right" : '', "right_month" : '' };


//initial load of calendar - with span defined
function initCalendar(y,x) { initElexCal('012008','062008'); }

function initElexCal(mmyyyyMIN,mmyyyyMAX){
	minCalendarDate = (mmyyyyMIN === undefined) ? defaultMinCalendarDate : mmyyyyMIN;
	maxCalendarDate = (mmyyyyMAX === undefined) ? defaultMaxCalendarDate : mmyyyyMAX;

	//dont allow invalid min/max
	if(getMinYear() == getMaxYear()){
		if(getMinMonth()>getMaxMonth()){
			clearMinMax();
		}
	}else if(getMinYear() > getMaxYear()){
		clearMinMax();
	}
	if(isAnElection(getToday())) {
		var thisDate = getToday();
		currentMonth = thisDate.getMonth();
		currentYear = thisDate.getFullYear();
		calDayView(dateToYYYYMMDD(thisDate), currentMonth);
	}
	else { getThisMonth(); }

}

//clears constraints when invalid
function clearMinMax(){
	minCalendarDate = defaultMinCalendarDate;
	maxCalendarDate = defaultMaxCalendarDate;
}

//builds calendar starting at today - unless its out of range - then start at first legal month
function getThisMonth(){
	var thisDate = getToday();
	currentMonth = thisDate.getMonth();
	currentYear = thisDate.getFullYear();
	if(isOutOfRange()){
		currentYear = getMinYear();
		currentMonth = getMinMonth();
	}
	cnnBuildCalendar(currentMonth,currentYear);
}

//tests if a month/year combination is out of min/max range
function isOutOfRange(){
	return (currentYear < getMinYear() ||
		currentYear > getMaxYear() ||
                (currentYear == getMinYear() && currentMonth < getMinMonth()) ||
                (currentYear == getMaxYear() && currentMonth > getMaxMonth()));
}

//sets the calendar to the next month
function getNextMonth(){
	if(currentMonth == getMaxMonth() && currentYear == getMaxYear()){ return; }
	currentMonth++;
	if(currentMonth > 11){
		currentMonth=0;
		currentYear++;
	}
	cnnBuildCalendar(currentMonth, currentYear);
}

//sets the calendar to the previous month
function getPrevMonth(){
	if(currentMonth == getMinMonth() && currentYear == getMinYear()){ return; }
	currentMonth--;
	if(currentMonth < 0){
		currentMonth = 11;
		currentYear--;
	}
	cnnBuildCalendar(currentMonth, currentYear);
}

//gets minimum year allowed
function getMinYear(){
	var minimumYear = 0;
	try{
		minimumYear = parseInt(minCalendarDate.substring(2));
	}catch(e){
		minimumYear = 0;
	}
	return minimumYear;
}

//gets maximum year allowed
function getMaxYear(){
        var maximumYear = 0;
        try{
                maximumYear = parseInt(maxCalendarDate.substring(2));
        }catch(e){
                maximumYear = 0;
        }
        return maximumYear;
}

//gets minimum month allowed
function getMinMonth(){
        var minimumMonth = 0;
        try{
                minimumMonth = parseInt(minCalendarDate.substring(0,2));
        }catch(e){
                minimumMonth = 0;
        }
        return minimumMonth-1;
}

//gets maximum month allowed
function getMaxMonth(){
        var maximumMonth = 0;
        try{
                maximumMonth = parseInt(maxCalendarDate.substring(0,2));
        }catch(e){
                maximumMonth = 0;
        }
        return maximumMonth-1;
}

//are we on the border of minimum date? - used for arrow-enabling
function isTheMin(){
	return (currentMonth == getMinMonth() && currentYear == getMinYear());
}

//are we on the border of maximum date? - used for arrow-enabling
function isTheMax(){
	return (currentMonth == getMaxMonth() && currentYear == getMaxYear());
}


//constructs calendar html - via javascript date object calculations
function cnnBuildCalendar(month,year){

	rowCount = 0;
	var rollOverRight = (!isTheMax()) ? '\'this.src="http://i2.cdn.turner.com/cnn/.element/img/2.0/sect/election/primaries/results/main_page/calendar/right_red.gif"\'' : '';
	var rollOverLeft = (!isTheMin()) ? '\'this.src="http://i2.cdn.turner.com/cnn/.element/img/2.0/sect/election/primaries/results/main_page/calendar/left_red.gif"\'' : '';

	calHeader = '<table cellspacing="0" cellpadding="0" border="0" width="258" class="cnnelCalendar"><colgroup><col width="40"><col width="175"><col width="43"></colgroup><tr>';

if (!isTheMin()) {
	calHeader += '<td><img onclick="getPrevMonth();" onmouseover='+rollOverLeft+' onmouseout=\'this.src="http://i2.cdn.turner.com/cnn/.element/img/2.0/sect/election/primaries/results/main_page/calendar/left_white.gif"\' src="http://i2.cdn.turner.com/cnn/.element/img/2.0/sect/election/primaries/results/main_page/calendar/left_white.gif" width="25" height="25" alt="" border="0" hspace="9"></td>';
} else {
	calHeader += '<td><img src="http://i2.cdn.turner.com/cnn/.element/img/2.0/sect/election/primaries/results/main_page/calendar/left_gray.gif" width="25" height="25" alt="" border="0" hspace="9"></td>';
}

	calHeader += '<td class="cnnelCalendarMonth">'+months[month]+' '+year+'</td>';

if(!isTheMax()) {
	calHeader += '<td><img onclick="getNextMonth();" onmouseover='+rollOverRight+' onmouseout=\'this.src="http://i2.cdn.turner.com/cnn/.element/img/2.0/sect/election/primaries/results/main_page/calendar/right_white.gif"\' src="http://i2.cdn.turner.com/cnn/.element/img/2.0/sect/election/primaries/results/main_page/calendar/right_white.gif" width="25" height="25" alt="" border="0" hspace="9"></td>';
} else {
	calHeader += '<td><img src="http://i2.cdn.turner.com/cnn/.element/img/2.0/sect/election/primaries/results/main_page/calendar/right_gray.gif" width="25" height="25" alt="" border="0" hspace="9"></td>';
}

	calHeader += '</tr></table><div id="calContent"><table cellspacing="0" cellpadding="0" border="0" width="258" class="cnnElexMPCCalendar"><colgroup><col width="36"><col width="37"><col width="37"><col width="37"><col width="37"><col width="37"><col width="36"></colgroup><tr class="cnnElexMPCalDates"><td><div>S</div></td><td><div>M</div></td><td><div>T</div></td><td><div>W</div></td><td><div>T</div></td><td><div>F</div></td><td><div>S</div></td></tr><tr><td class="cnnElexCalGap" colspan="7"><img src="http://i2.cdn.turner.com/cnn/images/1.gif" width="1" height="6" alt="" border="0"><br></td></tr>';

	var calDate = new Date();
	calDate.setFullYear(year);
	calDate.setMonth(month);
	calDate.setDate(1);
	calDate.setMonth(month);
	var days = '';
	if(calDate.getDay() > 0 ){
		days += '<tr class="cnnElexMPCalDays">';
		for(var i=0; i<calDate.getDay(); i++){
			rowCount = 1;
			days += '<td><div></div></td>';
		}
	}
	var thisMonth = month;
	for(var i=1; thisMonth == month; i++){
		calDate.setDate(i);
		days+=getDay(calDate, month);
		calDate.setDate(i+1);
		thisMonth = calDate.getMonth();
	}
	calDate.setDate(calDate.getDate()-1);
	for(var i=6; i>calDate.getDay(); i--){
		days += '<td><div></div></td>';
	}
	days += '</tr>';
	for(var i=rowCount; i<6; i++){//different week #s shouldn't change the height of calendar
		days += '<tr class="cnnElexMPCalDays"><td><div>&nbsp;</div></td></tr>';
	}
	var calFooter = '<tr><td colspan="7" class="cnnelCalendarFooter">&nbsp;</td></tr></table></div>';

	$('elCalendar').innerHTML = calHeader+days+calFooter;
	tempCal = $('elCalendar').innerHTML;
}

//build html for a particular day
function getDay(dateObj, month){
	var html=(dateObj.getDay() == 0) ? '<tr class="cnnElexMPCalDays">' : '';
	if(dateObj.getDay() == 0){
		rowCount++;
	}
	var styleClass = (isToday(dateObj)) ? 'cal_today' : '';
	var dayNum = (isAnElection(dateObj)) ? '<a href="javascript:calDayView(\''+dateToYYYYMMDD(dateObj)+'\', \''+month+'\')">'+dateObj.getDate()+'</a>' : dateObj.getDate();
	html += '<td class="'+styleClass+'"><div>'+dayNum+'</div></td>';
	html += (dateObj.getDay() == 6) ? '</tr>' : '';
	return html;
}

//if dateObj is today, return true
function isToday(dateObj){
	var today = getToday();
	return ((today.getDate() == dateObj.getDate()) &&
		(today.getMonth() == dateObj.getMonth()) &&
		(today.getFullYear() == dateObj.getFullYear()));
}

//if dateObj is an election date - return true
function isAnElection(dateObj){
	//if(!electionByPartyDate.init){return false;}
	var yyyymmdd = dateToYYYYMMDD(dateObj);
	try{
		if(electionCalData["DEM"][yyyymmdd].length > 0){
			return true;
		}
	}catch(e){}
	try{
		if(electionCalData["REP"][yyyymmdd].length > 0){
			return true;
		}
	}catch(e){}
	return false;
}

//converts js date obj to yyyymmdd formatted string
function dateToYYYYMMDD(dateObj){
	var month 	= (dateObj.getMonth()+1);
	var date	= dateObj.getDate();
	var year	= dateObj.getFullYear();
	month = (month < 10) ? "0"+month : ""+month;
	var date = (date<10) ? "0"+date : ""+date;
	return year+month+date;
}

//converts yyyymmdd formatted string to date object
function YYYYMMDDtoDate(yyyymmdd){
	var year = parseInt(yyyymmdd.substring(0,4));
	var month = parseInt(yyyymmdd.substring(4,6));
	var date = parseInt(yyyymmdd.substring(6))
	if(yyyymmdd.substring(6,7) == '0'){
                date = parseInt(yyyymmdd.substring(7));
        }
	var theDate = new Date();
	theDate.setFullYear(year);
	theDate.setMonth(month-1);
	theDate.setDate(date);
	theDate.setMonth(month-1);
	return theDate;
}

//gets the 'day view' when the user clicks a clickable day (election day)
function calDayView(yyyymmdd, month){

	if(!tempCal) { tempCal = $('elCalendar').innerHTML; }

	var rollOverRight = (electionCalData['NAV'][yyyymmdd].right) ? '\'this.src="http://i2.cdn.turner.com/cnn/.element/img/2.0/sect/election/primaries/results/main_page/calendar/right_red.gif"\'' : '';
	var rollOverLeft = (electionCalData['NAV'][yyyymmdd].left) ? '\'this.src="http://i2.cdn.turner.com/cnn/.element/img/2.0/sect/election/primaries/results/main_page/calendar/left_red.gif"\'' : '';

	var calHeader = '<table cellspacing="0" cellpadding="0" border="0" width="258" class="cnnelCalendar"><colgroup><col width="40"><col width="175"><col width="43"></colgroup><tr>';

	if (electionCalData['NAV'][yyyymmdd].left) {
		calHeader += '<td><img onclick="calDayView(\''+electionCalData['NAV'][yyyymmdd].left+'\', \''+electionCalData['NAV'][yyyymmdd].left_month+'\');" onmouseover='+rollOverLeft+' onmouseout=\'this.src="http://i2.cdn.turner.com/cnn/.element/img/2.0/sect/election/primaries/results/main_page/calendar/left_white.gif"\' src="http://i2.cdn.turner.com/cnn/.element/img/2.0/sect/election/primaries/results/main_page/calendar/left_white.gif" width="25" height="25" alt="" border="0" hspace="9"></td>';
	} else {
		calHeader += '<td><img src="http://i2.cdn.turner.com/cnn/.element/img/2.0/sect/election/primaries/results/main_page/calendar/left_gray.gif" width="25" height="25" alt="" border="0" hspace="9"></td>';
	}

	calHeader += '<td class="cnnelCalendarMonth">'+months[month]+' 2008</td>';

	if(electionCalData['NAV'][yyyymmdd].right) {
		calHeader += '<td><img onclick="calDayView(\''+electionCalData['NAV'][yyyymmdd].right+'\', \''+electionCalData['NAV'][yyyymmdd].right_month+'\');" onmouseover='+rollOverRight+' onmouseout=\'this.src="http://i2.cdn.turner.com/cnn/.element/img/2.0/sect/election/primaries/results/main_page/calendar/right_white.gif"\' src="http://i2.cdn.turner.com/cnn/.element/img/2.0/sect/election/primaries/results/main_page/calendar/right_white.gif" width="25" height="25" alt="" border="0" hspace="9"></td>';
	} else {
		calHeader += '<td><img src="http://i2.cdn.turner.com/cnn/.element/img/2.0/sect/election/primaries/results/main_page/calendar/right_gray.gif" width="25" height="25" alt="" border="0" hspace="9"></td>';
	}

	calHeader += '</tr></table><div id="calContent">';

	var dayViewHeader = '<table cellspacing="0" cellpadding="0" border="0" width="258" class="cnnElexMPCCalendar"><colgroup><col width="36"><col width="37"><col width="37"><col width="37"><col width="37"><col width="37"><col width="36"></colgroup><tr class="cnnElexMPCalDates"><td><div></div></td><td><div></div></td><td><div></div></td><td><div></div></td><td><div></div></td><td><div></div></td><td><div></div></td></tr><tr><td class="cnnElexCalGap2" colspan="7"><img src="http://i2.cdn.turner.com/cnn/images/1.gif" width="1" height="4" alt="" border="0"><br></td></tr><tr><td colspan="7"><table cellspacing="0" cellpadding="0" border="0" width="256" class="cnnElexCalDayView"><colgroup><col width="94"></colgroup><tr><td><div class="cnnElexDayCalViewDate" style="padding-top:0px;height:144px;overflow:hidden;">';
	var dayViewContent = '';
	if(yyyymmdd == "20080205"){
		dayViewContent += '<span>SUPER</span><br><span>TUESDAY<br></span><div class="cnnElexDateStars"><img src="http://i2.cdn.turner.com/cnn/.element/img/2.0/sect/election/primaries/results/main_page/tuesday/stars.gif" alt="" height="7" width="72"><br></div>';
	}else{
		dayViewContent += "<span>"+dayNames[YYYYMMDDtoDate(yyyymmdd).getDay()].toUpperCase()+"</span><br/>";
	}
	var num = parseInt(yyyymmdd.substring(6))
	if(yyyymmdd.substring(6,7) == '0'){
		num = parseInt(yyyymmdd.substring(7));
	}
	var styleInfo = (yyyymmdd == "20080205") ? "margin: -3px;" : '';

	if(isInTheFuture(yyyymmdd)){
		dayViewContent += '<div class="cnnElexDate" style="font-size:72px;font-weight:bold;line-height:66px;">'+num+'</div>';
	} else {
		dayViewContent += '<div class="cnnElexDate"><a href="/ELECTION/2008/primaries/results/dates/#'+yyyymmdd+'">'+num+'</a></div>';
	}


	dayViewContent += '<div class="cnnElexCalIcon" style="'+styleInfo+'">';
	dayViewContent += '<a href="javascript:calMonthView(\''+month+'\', \'2008\');" onmouseover="cnnImgSwap(this,1)" onmouseout="cnnImgSwap(this,0)">';
	dayViewContent += '<img src="http://i2.cdn.turner.com/cnn/.element/img/2.0/sect/election/primaries/results/main_page/tuesday/calendar.gif" width="23" height="18" alt=""><br>';
	dayViewContent += 'Month View</a></div>';
	dayViewContent += '</div></td><td><div class="cnnElexDayCalLegend">';
	dayViewContent += '<img src="http://i2.cdn.turner.com/cnn/.element/img/2.0/sect/election/primaries/results/main_page/tuesday/key_DandR.gif" width="162" height="11" alt=""><br>';
	dayViewContent += '</div><div class="cnnElexDayCalEventsTitle">EVENTS:</div><div class="cnnElexDayCalEvents">';
	dayViewContent += ''+getUniqueListOfEvents(yyyymmdd);

	var dayViewFooter = '</div></td></tr></table></td></tr><tr><td colspan="7" class="cnnelCalendarFooter">&nbsp;</td></tr></table></div>';
	$('elCalendar').innerHTML = calHeader + dayViewHeader + dayViewContent + dayViewFooter;
	$('calContent').style.overflow = "hidden";
}

//gets list of events for day view
function getUniqueListOfEvents(yyyymmdd){
	var returnHTML = '';
	var returnPrimaryType = '';
	var demGraphic = '<img src="http://i2.cdn.turner.com/cnn/.element/img/2.0/sect/politics/democrat_legend.gif" width="11" height="11" alt="Dems Only">';
	var repGraphic = '<img src="http://i2.cdn.turner.com/cnn/.element/img/2.0/sect/politics/republican_legend.gif" width="11" height="11" alt="Reps Only">';
	var demElections = electionCalData["DEM"][yyyymmdd];
    var repElections = electionCalData["REP"][yyyymmdd];

	var allEvents = demElections.concat(repElections);
	allEvents.sort(byState);
	var alreadyUsed = '';
	for(var i=0; i<allEvents.length; i++){
		try{
		if(alreadyUsed.indexOf(allEvents[i].state)==-1){

			alreadyUsed += allEvents[i].state;

			arrayOfInstances = getElexByStateAndDate(allEvents[i].code,yyyymmdd);

			if(arrayOfInstances.length == 1){
				returnPrimaryType = allEvents[i].primarytype.replace(/caucus/,'caucuses');
			} else {
				returnPrimaryType = allEvents[i].primarytype.replace(/primary/,'primaries').replace(/caucus/,'caucuses');
			}
			if(isInTheFuture(yyyymmdd)){
				returnHTML += allEvents[i].state+" "+returnPrimaryType+" ";
			}else{
				returnHTML += '<a href="/ELECTION/2008/primaries/results/state/#'+allEvents[i].code+'">'+allEvents[i].state+' '+returnPrimaryType+'</a> ';
			}
			if(arrayOfInstances.length == 1){

				returnHTML += (allEvents[i].party == "DEM") ? demGraphic : repGraphic;

			}
			returnHTML+= (i<(allEvents.length-1)) ? '<br/>' : '';
		}
		}catch(e){} //ignore bad rows - they obviously weren't meant to be.
	}
	return returnHTML;
}

//sorts an array of objects by .state value
function byState(obj1, obj2){
	var state1 = obj1.state;
	var state2 = obj2.state;
	return (state1 > state2) ? 1 : (state1 < state2) ? -1 : 0;
}

//returns true if yyyymmdd is greater than now
function isInTheFuture(yyyymmdd){
	var today = dateToYYYYMMDD(getToday());
	return (parseInt(yyyymmdd) > today || today.indexOf("200712")>-1);
}

//puts back the calendar - returning from day view
function calMonthView(month, year){
	currentMonth = month;
	cnnBuildCalendar(month, year);
}

function getElexByStateAndDate(stateCode,yyyymmdd){
	var electionArray = new Array();
	demElections = electionCalData["DEM"][yyyymmdd];
	repElections = electionCalData["REP"][yyyymmdd];

	if(demElections != undefined){
		for(var i=0; i<demElections.length; i++){
			if(demElections[i].code == stateCode){
				electionArray[electionArray.length] = demElections[i];
			}
		}
	}
	if(repElections != undefined){
                for(var i=0; i<repElections.length; i++){
                        if(repElections[i].code == stateCode){
                                electionArray[electionArray.length] = repElections[i];
                        }
                }
        }

	return electionArray;
}
