//#################
//The size of your rotating images. should be a square for optimal results.
var SunMoonImagesize = 150; 
var WthrImagesize = 250;
//This is the brightest your clock can get. 255 being black.
var minDarkClk = 150;
//Set this to 1 if you prefer a 24 hour clock. Set to 2 to pad a 24 hour clock's hour with extra 0's
var H24 = 1; 
//This is your default zip code for weather.
var zip = 47906;
//This is the rate you will pull xml data in milliseconds
var refresh = 30000;
//#################

var curtime;

function mkRGB(red, green, blue)
{
var rgbline = 'rgb('+red+','+green+','+blue+')';
return rgbline;
}

function colors() {
var hour=curtime.getHours()-12+curtime.getMinutes()/60;
var shade=255-Math.round((Math.abs(hour))/12*255);
document.getElementById("body").style.backgroundColor=mkRGB(0,Math.max(shade-50,0),shade);
document.getElementById("thetime").style.color=mkRGB(255-Math.max(shade,minDarkClk),255-Math.max(shade,minDarkClk),255-Math.max(shade,minDarkClk));
document.getElementById("wthrvalues").style.color=mkRGB(255-Math.max(shade,0),255-Math.max(shade,0),255-Math.max(shade,0));
document.getElementById("control").style.color=mkRGB(255-Math.max(shade,0),255-Math.max(shade,0),255-Math.max(shade,0));
}

function placesunmoon() {
var hour=curtime.getHours()-12+curtime.getMinutes()/60;
var thesun=document.getElementById("sun");
var themoon=document.getElementById("moon");
var radius;
if (window.outerHeight) {
	if (window.outerHeight<=window.outerWidth)
	{
		radius=(window.outerHeight-12*SunMoonImagesize/7)/2;
	}
	else
	{
		radius=(window.outerWidth-12*SunMoonImagesize/7)/2;
	}
	thesun.style.left=(Math.cos(hour/24*2*Math.PI-(Math.PI/2)))*radius+window.outerWidth/2-2*SunMoonImagesize/4+'px';
	thesun.style.top=(Math.sin(hour/24*2*Math.PI-(Math.PI/2)))*radius+window.outerHeight/2-3*SunMoonImagesize/4+'px';
	themoon.style.left=(Math.cos(hour/24*2*Math.PI+(Math.PI/2)))*radius+window.outerWidth/2-2*SunMoonImagesize/4+'px';
	themoon.style.top=(Math.sin(hour/24*2*Math.PI+(Math.PI/2)))*radius+window.outerHeight/2-3*SunMoonImagesize/4+'px';
}
else {
	if (document.documentElement.clientHeight<=document.documentElement.clientWidth)
	{
		radius=(document.documentElement.clientHeight-12*SunMoonImagesize/7)/2;
	}
	else
	{
		radius=(document.documentElement.clientWidth-12*SunMoonImagesize/7)/2;
	}
	thesun.style.left=(Math.cos(hour/24*2*Math.PI-(Math.PI/2)))*radius+document.documentElement.clientWidth/2-2*SunMoonImagesize/4+'px';
	thesun.style.top=(Math.sin(hour/24*2*Math.PI-(Math.PI/2)))*radius+document.documentElement.clientHeight/2-3*SunMoonImagesize/4+'px';
	themoon.style.left=(Math.cos(hour/24*2*Math.PI+(Math.PI/2)))*radius+document.documentElement.clientWidth/2-2*SunMoonImagesize/4+'px';
	themoon.style.top=(Math.sin(hour/24*2*Math.PI+(Math.PI/2)))*radius+document.documentElement.clientHeight/2-3*SunMoonImagesize/4+'px';
}
}

function setmoon() {
var phase;
//http://en.wikipedia.org/wiki/Lunar_phase
phase=document.getElementById('phase').innerHTML;
if (phase=='New Moon') {
	document.getElementById('moon').src='img/moon/newmoon.png';
}
else if(phase=='Waxing Crescent') {
	document.getElementById('moon').src='img/moon/waxcres.png';
}
else if(phase=='First Quarter Moon') {
	document.getElementById('moon').src='img/moon/firstquar.png';
}
else if(phase=='Waxing Gibbous') {
	document.getElementById('moon').src='img/moon/waxgib.png';
}
else if(phase=='Full Moon') {
	document.getElementById('moon').src='img/moon/moon.png';
}
else if(phase=='Waning Gibbous') {
	document.getElementById('moon').src='img/moon/wangib.png';
}
else if(phase=='Last Quarter Moon') {
	document.getElementById('moon').src='img/moon/lastquar.png';
}
else if(phase=='Waning Crescent') {
	document.getElementById('moon').src='img/moon/wangib.png';
}
}


function wthr() {
var thewthrdiv=document.getElementById("weatherdiv");
var thewthrimg=document.getElementById("weatherimg");
var curwthr=document.getElementById("cond").innerHTML;
document.getElementById("tempspan").innerHTML=document.getElementById("temp").innerHTML+"F&deg;";
document.getElementById("condspan").innerHTML=document.getElementById("condtext").innerHTML;
document.getElementById("zipspan").innerHTML=zip;
//http://developer.yahoo.com/weather/#codes
//Codes used Availible (0-47,3200)
// 0  1  2  3  4  5  6  7  8  9
//10 11 12 13 14 15 16 17 18 19
//20 21 22 23 24    26 27 28 29
//30             35    37 38 39
//40 41 42 43 44 45 46 47
if(curwthr>=26&&curwthr<=30||curwthr==44) {
	thewthrimg.src="img/wthr/26d.png";
}
else if(curwthr==16||(curwthr>=41&&curwthr<=43)||curwthr==46) {
	thewthrimg.src="img/wthr/42d.png";
}
else if(curwthr>=23&&curwthr<=24) {
	thewthrimg.src="img/wthr/23d.png";
}
else if(curwthr>=19&&curwthr<=22) {
	thewthrimg.src="img/wthr/20d.png";
}
else if(curwthr>=13&&curwthr<=15) {
	thewthrimg.src="img/wthr/13d.png";
}
else if(curwthr==9) {
	thewthrimg.src="img/wthr/11d.png";
}
else if((curwthr>=11&&curwthr<=12)||curwthr==40) {
	thewthrimg.src="img/wthr/10d.png";
}
else if(curwthr==18||curwthr==10) {
	thewthrimg.src="img/wthr/6d.png";
}
else if((curwthr>=5&&curwthr<=8)||curwthr==35) {
	thewthrimg.src="img/wthr/5d.png";
}
else if((curwthr>=0&&curwthr<=4)||(curwthr>=37&&curwthr<=39)||curwthr==45||curwthr==47||curwthr==17) {
	thewthrimg.src="img/wthr/0d.png";
}
else {
	thewthrimg.src="img/space.png";
}
var length;
var width;
if (window.outerHeight) {
	length=(window.outerHeight-WthrImagesize)/2;
	width=(window.outerWidth-WthrImagesize)/2;
}
else {
	length=(document.documentElement.clientHeight-WthrImagesize)/2;
	width=(document.documentElement.clientWidth-WthrImagesize)/2;
}
thewthrdiv.style.left=width+'px';
thewthrdiv.style.top=length+'px';
}

function liveclock(){
curtime = new Date();
var dow;
var timediv=document.getElementById("thetime");
var hours=curtime.getHours();
var minutes=curtime.getMinutes();
var seconds=curtime.getSeconds();
var day=curtime.getDay();
if (H24=='0')
{
	if (hours>12) {
		hours=hours-12;
	}
	if (hours=='0') {
		hours=12;
	}
}
else if(H24==2)
{
	if(hours<=9) {
		hours="0"+hours;
	}
}
if (day=='0') {
	dow='Su';
}
else if(day==1) {
	dow='M';
}
else if(day==2) {
	dow='Tu';
}
else if(day==3) {
	dow='W';
}
else if(day==4) {
	dow='Th';
}
else if(day==5) {
	dow='F';
}
else if(day==6) {
	dow='Sa';
}
if (minutes<=9) {
	minutes="0"+minutes;
}
if (seconds<=9) {
	seconds="0"+seconds;
}
var ctime=dow+" "+hours+":"+minutes+"<small>:"+seconds+"</small>";
timediv.innerHTML=ctime;
setTimeout(liveclock,1000);
}

function run () {
curtime = new Date();
colors();
placesunmoon();
fetchdata('fetchxml.php?zip='+zip);
setTimeout(setmoon,2000);
setTimeout(wthr,2000);
setTimeout(run,refresh);
}

function firstrun () {
document.getElementById("acss").href='http://jigsaw.w3.org/css-validator/validator?uri='+document.URL;
curtime = new Date();
liveclock();
colors();
placesunmoon();
var cookiezip = readCookie("zip");
var cookieH24 = readCookie("H24");
var cookieref = readCookie("refresh");
if (cookiezip) {
	zip=cookiezip;
}
if (cookieH24) {
	H24=cookieH24;
}
if (cookieref) {
	refresh=cookieref;
}
fetchdata('fetchxml.php?zip='+zip);
setTimeout(setmoon,2000);
setTimeout(wthr,2000);
setTimeout(run,refresh);
}

function change(type) {
var tmprefresh;
tmprefresh=refresh;
var tmpzip;
tmpzip=zip;
var tmpH24;
tmpH24=H24;
if(type=='zip') {
	zip=prompt("Chage weather zipcode to:",zip);
}
else if(type=='H24') {
	H24=prompt("Change clock type to:\n0=12H 1=24H 2=24HH",H24);
}
else if(type=='refresh') {
	refresh=prompt("Change refresh rate in minutes to:",refresh/60000)*60000;
}
if(!zip) {
	zip=tmpzip;
}
if(refresh<=10000) {
	refresh=tmprefresh;
}
if(!H24) {
	H24=tmpH24;
}
createCookie("zip", zip, 365)
createCookie("H24", H24, 365)
createCookie("refresh", refresh, 365)
run();
}