var Countdown = (function(){
	function constructor(){
		this.container = document.getElementById('count-down-timer');
		this.counter;
		this.interval;
		this.t = new Date();
		this.year = this.t.getFullYear();
		if(this.t.getMonth()>1){
			this.year = this.t.getFullYear()+1;//next year
		}
		this.month = 1;
		this.day = !(this.year%4)&&(this.year%100)||!(this.year%400)?29:28;
		this.rdd = new Date(this.year,this.month,this.day);
		//this.rdd = new Date(new Date().getTime()+5000);//rdd 5 seconds in the future
		this.st = new Date(this.t.getFullYear(),this.t.getMonth(),this.t.getDate());
		this.isToday = this.rdd.getTime() == this.st.getTime();
		this.checkDate();
	}
	
	constructor.prototype.clear = function(){
		while(this.container.hasChildNodes()){
			this.container.removeChild(this.container.lastChild);	
		}	
	}
	
	constructor.prototype.checkDate = function(){
		if(!this.isToday){
			this.update();
			startCountdown(this);
		} else {
			this.end();
		}
	}
	constructor.prototype.end = function(){
		clearInterval(this.interval);
		this.container.appendChild(document.createTextNode('It\'s Today!'));
	}
	
	constructor.prototype.update = function(){
		this.clear();
		this.t = new Date();
		this.d = (this.rdd.getTime()-this.t);
		if(this.d <= 0){this.end(); return;}
		var seconds = Math.floor(((this.d%(1000*60*60))%(1000*60))/1000);
		var minutes = Math.floor((this.d%(1000*60*60))/(1000*60));
		var hours = Math.floor(this.d/(1000*60*60)%24);
		var days = Math.floor(this.d/(1000*60*60*24));
		this.container.appendChild(document.createElement('strong')).appendChild(document.createTextNode(days));
		this.container.appendChild(document.createTextNode(" "+(days==1?"Day":"Days")+", "));
		this.container.appendChild(document.createElement('strong')).appendChild(document.createTextNode(hours));
		this.container.appendChild(document.createTextNode(" "+(hours==1?" Hr":"Hrs")+", "));
		this.container.appendChild(document.createElement('strong')).appendChild(document.createTextNode(minutes));
		this.container.appendChild(document.createTextNode(" Min, "));
		this.container.appendChild(document.createElement('strong')).appendChild(document.createTextNode(seconds));
		this.container.appendChild(document.createTextNode(" Sec"));
	}
	
	function startCountdown(instance){
		instance.interval = setInterval(function(){instance.update()},1000);
	}
	
	return constructor; 
})();

//banner rotation
//requires jQuery 1.3.2
var BannerController = (function(){
	function constructor(jqElement, timeout){
		this.element = jqElement;
		this.timeout = timeout;
		this.interval;
		this.banners = this.element.children();
		if (this.banners.length==1){return;} //stop animating if only one banner
		play(this);
	}
	
	function swap(){
		var p = $(this).parent();
		$(this).remove().css("margin-top","0").appendTo(p);
	}
	
	constructor.prototype.next = function(){
		this.element.children(":first").animate({marginTop:-this.element.children(":first").height()},600,"swing",swap);
	}
	
	function play(instance){
		instance.interval = setInterval(function(){instance.next()},instance.timeout);
	}
					
	return constructor;
})();



function homePageImages(){
	if(document.getElementById('stories')){
		var i = document.getElementById('stories').getElementsByTagName('img');
		for(var k=0;k<i.length;k++){
			if(i[k].width > 274){
				scale = 274/i[k].width;
				width = 274;
				height = i[k].height*scale;
			}
			var img = i[k].cloneNode(true);
			img.width = width;
			img.height = height;
			var p = i[k].parentNode;
			p.removeChild(i[k]);
			while(!/entry/.test(p.className)){p = p.parentNode;}
			w = document.createElement('div');
			w.className = "wrapper"
			p.insertBefore(w,p.firstChild);
			w.appendChild(img);
		}
	}
}

function ieActions(){
	
	if('$' in window && /MSIE 6/.test(navigator.userAgent)){
		$('#nav>ul>li').each(function(i){$(this).bind("mouseenter mouseleave",function(){$(this).toggleClass("show");});});
		$('#header #header-content #nav ul li ul li:first-child').each(function(i){$(this).addClass('first');});
		$('#highlights #stories li:first-child').each(function(i){$(this).addClass('first');});
		$('#left-nav ul li:first-child').each(function(i){$(this).addClass('first');});
	}
}

function initShareThis(){
	if(document.getElementById("share-this-link")){
		var st = SHARETHIS.addEntry({title:'Rare Disease Day U.S.',
					summary: 'Alone we are are Rare. Together we are strong.'},{button:false}
				)
		st.attachButton(document.getElementById("share-this-link"));
/*		var a = document.getElementById('st-links').getElementsByTagName('a');
		for(var k=0;k<a.length;k++){
			st.attachButton(a[k]);	
		} */
	}
}

function init(){
	if(arguments.callee.doOnce){return;}
	arguments.callee.doOnce=true;
	ieActions();
	if(document.getElementById('count-down-timer')){
		var c = new Countdown();
	}
	if('$' in window && document.getElementById('banner-container')){
		var b = new BannerController($('#banner-container'),8000);
	}
	
	initShareThis();
	homePageImages();
}
if(document.addEventListener){document.addEventListener("DOMContentLoaded",init,null);}
window.onload=init;



























