var now;
var then = new Date("12:00 pm 4/30/2011");
var counterclock = true;
function strm(str, f){
	return (new Array(f + 1)).join(str);
}
function pad(n, nop){
	return strm("0", nop - String(n).length) + n;
}
function tick(){
	now = new Date("9:00 am 4/30/2011");
	var srd, mrd, hrd, trd, ttx;
	var hour = document.getElementById("hour");
	var minute = document.getElementById("minute");
	var second = document.getElementById("second");
        var ttw = document.getElementById("ttw");
	var diff = Math.floor((then.getTime() - now.getTime()) / 1000);
	if(diff > 43200){
	srd = (diff / 86400) * (360/29);
        if(!counterclock){
        hrd = (hrd === undefined) ? undefined : 360 - hrd;
        mrd = (mrd === undefined) ? undefined : 360 - mrd;
        srd = (srd === undefined) ? undefined : 360 - srd;
        }
        trd = srd;// + 92;
	//alert(trd);
        ttx = Math.ceil(diff / 86400) + " days";
	if((trd < 0 || trd > 180) && trd < 360){
		trd += 89;
		ttw.setAttribute("x", "39");
	}else{
		trd -= 92;
		ttw.setAttribute("x", "49");
	}
	setTimeout(tick, 60000);
	}else{
	hrd = diff / 120; // (diff / 3600) * 30;
	mrd = ((diff % 3600) / 60) * 6;
	srd = (diff % 60) * 6;
        if(!counterclock){
        hrd = (hrd === undefined) ? undefined : 360 - hrd;
        mrd = (mrd === undefined) ? undefined : 360 - mrd;
        srd = (srd === undefined) ? undefined : 360 - srd;
        }
	trd = hrd;// - 90;
	ttx = pad(Math.floor(diff / 3600), 2) + ":" + pad(Math.floor((diff % 3600) / 60), 2) + ":" + pad(Math.floor(diff % 60), 2);
//        ttw.setAttribute("x", "48");
        if((trd < 0 || trd> 180) && trd < 360){
                trd += 89;
                ttw.setAttribute("x", "41");
        }else{
                trd -= 92;
                ttw.setAttribute("x", "48");
        }
	setTimeout(tick, 999);
	}
	if(diff <= 0){
		document.getElementById("ubuntu").setAttribute("xlink:href", "http://www.ubuntu.com/desktop/get-ubuntu/download");
		ttx = "It's here!";
	}
/*	if(!counterclock){
	hrd = (hrd === undefined) ? undefined : 360 - hrd;
	mrd = (mrd === undefined) ? undefined : 360 - mrd;
	srd = (srd === undefined) ? undefined : 360 - srd;
	}*/
	if(diff <= 0){
		trd = hrd = mrd = srd = undefined;
	}
	ttw.textContent = ttx;
	second.style.display = (srd === undefined) ? "none" : "block";
        minute.style.display = (mrd === undefined) ? "none" : "block";
        hour.style.display = (hrd === undefined) ? "none" : "block";
	second.setAttribute("transform", "rotate(" + srd + " 50 50)");
	minute.setAttribute("transform", "rotate(" + mrd + " 50 50)");
	hour.setAttribute("transform", "rotate(" + hrd + " 50 50)");
        ttw.setAttribute("transform", "rotate(" + trd + " 50 50)");
}
function load(){
	var l = location.href;
	var c = l.substr(l.lastIndexOf("?") + 1);
	var i = c.indexOf("cc=");
	if(i != -1){
	var tf = c.substr(i + 3);
	counterclock = (tf == "false") ? false : true;
	}
	tick();
	setTimeout(tick, 1000);
}
window.onload = load;
//load();
