var PP_LOGO = "http://propublica.org/images/pp_clock_logo.gif";
var PP_HREF = "http://propublica.org/";

var propublica_countdown_array = [
									{id:0, desc:"Sen. Christopher Dodd promises to release his Countrywide mortgage documents", link:"http://www.propublica.org/article/promises-clock#dodd", date:[2008,7,24,15,11,0]} , 
									{id:1, desc:"Treasury promises to un-redact TARP-related contracts", link:"http://www.propublica.org/article/promises-clock#mellon", date:[2008,10,15,0,0,0], doneDate:[2009, 3,14,0,0,0]}, 
									{id:2, desc:"End no-bid contracts above $25,000", link:"http://www.propublica.org/article/promises-clock#obama", date:[2009,1,20,12,0,0]},
									{id:3, desc:"End the \"Stop-Loss\" program of forcing troops to stay in service beyond their expected commitments", link:"http://www.propublica.org/article/promises-clock#obama", date:[2009,1,20,12,0,0]},
									{id:4, desc:"End the \"Don't Ask, Don't Tell\" policy", link:"http://www.propublica.org/article/promises-clock#obama", date:[2009,1,20,12,0,0]},
									{id:5, desc:"Close the Guantanamo Bay Detention Center", link:"http://www.propublica.org/article/promises-clock#obama", date:[2009,1,20,12,0,0]},
									{id:6, desc:"Create a public \"Contracts and Influence\" database", link:"http://www.propublica.org/article/promises-clock#obama", date:[2009,1,20,12,0,0]},						
									{id:7, desc:"Sen. Roland Burris promises a &quot;concise document&quot; explaining his conflicting statements on his Senate seat", link:"http://www.propublica.org/article/promises-clock#burris", date:[2009,2,17,14,25,0]},
									{id:8, desc:"President Obama ordered agencies to post any communications they have with lobbyists regarding stimulus projects.", link:"http://www.propublica.org/article/promises-clock#stimlobbyists", date:[2009,5,29,17,35,0]},
									{id:9, desc:"FOIA to Middlesex County (Mass.) district attorney for budget documents", link:"http://www.propublica.org/article/promises-clock#middlesex_da", date:[2008,12,16,0,0,0], doneDate:[2009, 4,20,0,0,0]},
									{id:10, desc:"All TARP contracts to be put on Treasury website", link:"http://www.propublica.org/article/promises-clock#tarp-contracts", date:[2009,1,28,0,0,0]}
									
													
									];
									

function PP_Promise_Clock(id, params){

	var data = propublica_countdown_array[id];
	var params = (pp_isEmpty(params)) ? new Object() : params;
	if(pp_isEmpty(data)){
	
		document.write("Error in reading data for Promise Clock. Visit http://propublica.org");
		return;
	}
	this.isDone = !pp_isEmpty(data.doneDate);
	this.desc = data.desc;
	this.link = data.link;
	this.id = data.id;	
	this.date = data.date;
		var year = pp_isEmpty(this.date[0]) ? 0 : this.date[0];
		var month = pp_isEmpty(this.date[1]) ? 0 : this.date[1];
		var day = pp_isEmpty(this.date[2]) ? 0 : this.date[2];
		var hour = pp_isEmpty(this.date[3]) ? 0 : this.date[3];
		var min = pp_isEmpty(this.date[4]) ? 0 : this.date[4];
		var sec = pp_isEmpty(this.date[5]) ? 0 : this.date[5];
	
	
	if(!this.isDone){
		this.utc = Date.parse(new Date()) - Date.UTC(year, (month-1), day, hour, min, sec);
	}else{
		var d1 = pp_isEmpty(data.doneDate[2]) ? 0 : data.doneDate[2];
		var h1 = pp_isEmpty(data.doneDate[3]) ? 0 : data.doneDate[3];
		var m1 = pp_isEmpty(data.doneDate[4]) ? 0 : data.doneDate[4];
		var s1 = pp_isEmpty(data.doneDate[5]) ? 0 : data.doneDate[5];
		this.utc = Date.UTC(data.doneDate[0], data.doneDate[1]-1, d1,h1-1,m1,s1) - Date.UTC(year, (month-1), day, hour, min, sec) + 5*3600000;
	}
	
	
	
	var p_eoffset = (this.isDone) ? 0 : -5*3600000;//east coast time zone
	this.cdays = Math.floor((this.utc+p_eoffset)/86400000);
	var p1 = new Date();
	p1.setTime(this.utc);
	this.chours = p1.getHours();
	this.cmin = p1.getMinutes();
	this.csec = p1.getSeconds();
	
	
	var dId = "pp_promise_clock_"+this.id;
	
	var bcolor = this.isDone ? "#ecedec" : "#fffeeb";
	var st="style='background: "+bcolor+"; padding: 6px; width:225px; font: 9pt Arial;'";
	
	this.div= "<div class=\"pp_promise_clock\" id=\""+dId+"\"" + st;
	this.div+= ">";

	this.div += "<div class='pp_clock_desc' style='margin-bottom: 5px;'>";
	if(params.isInside !== true){
		this.div += "<a href=\""+this.link+"\">"+this.desc+"</a>";
	}
	this.div += "</div>";
	
	this.div += "<div class='pp_clock_timewrap' style='margin: 0px auto;'>"+this.drawTime()+"</div>";
	if(params.sL !== false){
		this.div += "<div style='text-align: right' class=\"pp_clock_logo\"><a href='"+PP_HREF+"'><img src=\""+PP_LOGO+"\" alt=\"ProPublica\" /></a></div>";
//		var dstr = '<script type="text/javascript" src="counter.js"></script><script>var ppclock_'+this.id+' = new PP_Promise_Clock('+this.id+');</script>';
//		var dstr = this.id;
//		this.div += "<div class=\"pp_embed_link\"><a style='cursor:pointer' onclick=\"showEmbed('"+dstr+"')>Embed</a></div>";
	}
	this.div += "</div>";
	

	
	document.write(this.div);
	
	this.tDiv = document.getElementById(dId).childNodes[1];
	
	var thispp = this;
	function doTimeUpdate(){
		thispp.inc_seconds();
		thispp.tDiv.innerHTML = thispp.drawTime();
	}

	this.causeInterval = function(){
		setInterval(doTimeUpdate, 1000);
	}
	
	if(!this.isDone){
		this.causeInterval();
	}
}




PP_Promise_Clock.prototype.drawTime = function(){

	var txt = "";

	if(this.isDone){
		txt += "<div class='pp_clock_done' style='font-weight:bold; text-align: center; color:white; background-color:black'>KEPT</div>";
	}
	
	var sth = "<style>.ppcountdown_tb th{ font-size: 8pt; text-align: center; font-weight: bold; font-variant: small-caps; color: #222;}";
	sth += ".ppcountdown_tb td{	background: #222; padding: 4px 1px; vertical-align: middle; border-right: 1px solid #fff;}";
	sth += "div.pp_p{ font-size: 10pt; line-height: 14pt; font-weight: bold; color: white; text-align:center;}";
	sth += "div.p_days{ color: #f33}";
	sth += "</style>";
	txt += sth;
	txt += "<table class='ppcountdown_tb' id='pptable_"+this.id+"' ";
	var st = "style='text-align:center; width: 100%'";
	txt += st + ">";
	var h = "<thead><tr>";
	var c = "<tbody><tr class='pp_tr'>";
	if(!pp_isEmpty(this.date[2])){
		h += "<th>Days</th>";
		c += "<td class='pp_td'><div class='pp_p p_days'>"+this.cdays+"</div></td>";
	}
	if(!pp_isEmpty(this.date[3])){
		h += "<th>Hours</th>";
		c += "<td class='pp_td'><div class='pp_p p_hours'>"+this.chours+"</div></td>";
	}	
	if(!pp_isEmpty(this.date[4])){
		h += "<th>Min.</th>";
		c += "<td class='pp_td'><div class='pp_p p_minutes'>"+this.cmin+"</div></td>";
	}	
	if(!pp_isEmpty(this.date[5])){
		h += "<th>Sec.</th>";
		c += "<td class='pp_td'><div class='pp_p p_seconds'>"+this.csec+"</div></td>";
		
	
	}			
	
	h +="</tr></thead>";
	c +="</tr></tbody>";
	
	txt += h + c + "</table>";
	
	return txt;

}

PP_Promise_Clock.prototype.inc_seconds = function(){
	
	this.csec++;
	if(this.csec >= 60){
		this.csec = 0;
		this.cmin++;
		if(this.cmin >= 60){
			this.cmin = 0;
			this.chours++;
			if(this.chours >= 24){
				this.chours = 0;				
				this.cdays++;
			}
		}
	}
}

function pp_isEmpty(val){

	if(val===null || val===undefined || val==="")
		return true;
	else
		return false;

}

function trace(txt){
	document.getElementById("trace").innerHTML += "<br />"+txt;
}