// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// weather/control.js
//
// copyright (c) 2006-2010 drow <drow@bin.sh>
// all rights reserved.

var form="weather_form",image_dir="/d20/weather/images",fmt={weather:'<table><tr><td width="100">#{image}</td><td><table>#{stats}</table></td></tr>#{desc}</table>',image:'<img src="'+image_dir+'/#{image}" alt="" width="100" height="200" />',stat:'<tr><td class="key" width="80">#{key}:</td><td class="value">#{value}</td></tr>',text:'<tr><td colspan="2" class="value">#{text}</td></tr>'};Object.keys(fmt).each(function(a){fmt[a]=new Template(fmt[a])});fmt.hr='<tr><td colspan="2"><hr /></td></tr>';
function init_form(){client_form(form);var a=$(form).elements,b=get_cookie("enc")||{};if(value=b.climate)setv(a.climate,value);if(value=b.season)setv(a.season,value);if(value=b["super"])setv(a["super"],value);get_weather()}function set_field(a){save_prefs("enc",a);get_weather()}
function get_weather(){var a=$(form).elements,b=selv(a.climate),c=selv(a.season);c=default_weather(b,c);a=parseInt(selv(a["super"]));a=rand(100)<a;var d=rand(100);d<70||(c=a?b=="cold"?cold_supernatural(d,c):b=="warm"?warm_supernatural(d,c):temperate_supernatural(d,c):b=="cold"?cold_weather(d,c):b=="warm"?warm_weather(d,c):temperate_weather(d,c));c=describe_temperature(c);c=describe_precipitation(c);c=describe_wind(c);c.storm=describe_storm(c,b);if(c.supernatural){c.desc=c.supernatural;c.text[c.text.length]=
get_text("desc",c.supernatural);c.text[c.text.length]=get_text("temp",c.temp_desc)}else if(c.storm){c.desc=c.storm;c.text[c.text.length]=get_text("desc",c.storm);c.text[c.text.length]=get_text("temp",c.temp_desc);if(c.prec_unusual){c.desc+=", "+c.prec_desc;c.text[c.text.length]=get_text("prec",c.prec_desc)}if(c.desc.match(/thunderstorm/i)&&rand(10)==0){c.desc+=", Tornado";c.text[c.text.length]=get_text("wind","tornado")}}else{if(c.prec_desc)c.desc=c.prec_desc;else if(c.wind>=3)c.desc="Windstorm";
else if(c.wind>=1)c.desc="Windy";c.text[c.text.length]=get_text("temp",c.temp_desc);c.text[c.text.length]=get_text("prec",c.prec_desc);c.text[c.text.length]=get_text("wind",c.wind_desc)}c=get_image(c);b=fmt_weather(c);$("out").update(b)}function cold_weather(a,b){if(a<73)b.temp.x_adj=10;else if(a<80)b.temp.x_adj=-10;else if(a<90)b.prec=rand(100);else if(a<99){b.temp.x_adj=-10;b.prec=rand(100)+30;b.wind+=3}else{b.temp.x_adj=-10;b.prec=rand(100)+30;b.wind+=4}return b}
function temperate_weather(a,b){if(a<75)b.temp.x_adj=10;else if(a<80)b.temp.x_adj=-10;else if(a<90)b.prec=rand(100);else if(a<99){b.temp.x_adj=-10;b.prec=rand(100)+30;b.wind+=3}else{b.temp.x_adj=-10;b.prec=30;b.wind+=4}return b}function warm_weather(a,b){if(a<90)b.wind+=1;else if(a<99)b.wind+=3;else{b.temp.x_adj=-10;b.prec=30;b.wind+=rand(2)+1}return b}
function cold_supernatural(a,b){if(a<80){b.supernatural="Blacksleet";b.temp.x_adj=-10}else if(a<90){b.supernatural="Lethe Snow";b.temp.x_adj=-10}else{b.supernatural="Gatestorm";b.wind+=4}return b}function temperate_supernatural(a,b){if(a<80)b.supernatural="Acid Rain";else if(a<90)b.supernatural="Prismatic Rain";else if(a<95){b.supernatural="Arcane Storm";b.wind+=3}else{b.supernatural="Gatestorm";b.wind+=4}return b}
function warm_supernatural(a,b){if(a<80)b.supernatural="Acid Rain";else if(a<90)b.supernatural="Prismatic Rain";else if(a<95){b.supernatural="Arcane Storm";b.wind+=3}else{b.supernatural="Gatestorm";b.wind+=4}return b}var temp_by={cold:{base:0,radix:20,s_dev:40},temperate:{base:40,radix:20,s_dev:20},warm:{base:80,radix:20,s_dev:40}},season_adj={spring:0,summer:1,autumn:0,winter:-1};
function default_weather(a,b){a=temp_by[a];b=season_adj[b];return{desc:"Clear",prec:-1,wind:rand(1.5),text:[],temp:{base:a.base,radix:a.radix,s_adj:a.s_dev*b,x_adj:0}}}
function describe_temperature(a){a.temp.base+=a.temp.s_adj;a.temp_f=a.temp.base+rand(a.temp.radix)+a.temp.x_adj;a.temp_l=a.temp_f-10-rand(10);a.temp_high=fmt_temp(a.temp_f);a.temp_low=fmt_temp(a.temp_l);var b=Math.floor(a.temp.radix/3),c=a.temp.base+b;b=a.temp.base+a.temp.radix-b;a.temp_desc=a.temp_f<-20?"Extreme Cold":a.temp_f<0?"Severe Cold":a.temp_f<40?"Cold":a.temp_f>140?"Extreme Heat":a.temp_f>110?"Severe Heat":a.temp_f>90?"Very Hot":"Moderate";a.temp_rel=a.temp_f<c?"Colder than normal":a.temp_f>
b?"Warmer than normal":"Normal";return a}function fmt_temp(a){var b=Math.floor((a-32)*5/9);return""+a+"&deg;F ("+b+"&deg;C)"}
function describe_precipitation(a){a.prec_desc=a.prec<0?"":a.prec<30?"Fog":a.prec<90?a.temp_f<30?"Snow":"Rain":a.temp_f<30?"Sleet":a.temp_f<70?"Hail":"Rain";if(a.prec_desc.match(/(rain|hail)/i))a.prec_type="rain";else if(a.prec_desc.match(/(snow|sleet)/i))a.prec_type="snow";else if(a.prec_desc.match(/(fog)/i))a.prec_type="fog";if(a.prec_desc.match(/(hail|sleet|fog)/i))a.prec_unusual=true;return a}
var wind_data=[{desc:"Light",mph:"1d10"},{desc:"Moderate",mph:"1d10 + 10"},{desc:"Strong",mph:"1d10 + 20"},{desc:"Severe",mph:"1d20 + 30"},{desc:"Windstorm",mph:"1d25 + 50"},{desc:"Hurricane",mph:"1d100 + 75"},{desc:"Tornado",mph:"1d125 + 175"}];function describe_wind(a){if(!(a.wind<0)){var b=wind_data[a.wind];a.wind_desc=b.desc;a.wind_mph=roll_dice(b.mph);a.wind_speed=fmt_speed(a.wind_mph)}return a}function fmt_speed(a){var b=Math.floor(a*1.609);return""+a+" mph ("+b+" kph)"}
function describe_storm(a,b){if(a.prec_type=="rain")if(a.wind==5)return"Hurricane";else if(a.wind==4)return"Severe Thunderstorm";else{if(a.wind==3)return"Thunderstorm"}else if(a.prec_type=="snow")if(a.wind>=4)return"Blizzard";else{if(a.wind==3)return"Snowstorm"}else if(b=="warm")if(a.wind>=4)return"Greater Duststorm";else if(a.wind==3)return"Duststorm"}function get_text(a,b){if(b){b=clean_key(b);a=a+"_"+b;if(a=document.getElementById(a))return a.innerHTML}}
function clean_key(a){a=a.toLowerCase();return a=a.replace(/ /g,"_")}
function get_image(a){a.image=a.desc.match(/prismatic rain/i)?"prismatic_rain.jpg":a.desc.match(/gatestorm/i)?"gatestorm.jpg":a.desc.match(/tornado/i)?"tornado.jpg":a.desc.match(/hurricane/i)?"hurricane.jpg":a.desc.match(/thunderstorm/i)?"thunderstorm.jpg":a.desc.match(/(blizzard|snowstorm)/i)?"blizzard.jpg":a.desc.match(/duststorm/i)?"duststorm.jpg":a.desc.match(/storm/i)?"thunderstorm.jpg":a.desc.match(/(rain|hail)/i)?"rain.jpg":a.desc.match(/(snow|sleet)/i)?"snow.jpg":a.desc.match(/fog/i)?"fog.jpg":
"clear.jpg";return a}function fmt_weather(a){a={image:eval_fmt("image",{image:a.image}),stats:fmt_stats(a),desc:fmt_desc(a.text)};return eval_fmt("weather",a)}
function fmt_stats(a){var b=[fmt_stat("Description",a.desc)];if(a.temp_desc){b.push(fmt.hr);b.push(fmt_stat("Temperature",a.temp_desc));b.push(fmt_stat("High",a.temp_high));b.push(fmt_stat("Low",a.temp_low));b.push(fmt_stat("Relative",a.temp_rel))}if(a.wind_desc){b.push(fmt.hr);b.push(fmt_stat("Wind Force",a.wind_desc));b.push(fmt_stat("Wind Speed",a.wind_speed))}return b.join("")}function fmt_stat(a,b){if(!b)return"";return eval_fmt("stat",{key:a,value:b})}
function fmt_desc(a){list=a.map(fmt_text);return fmt.hr+list.join("")}function fmt_text(a){if(!a)return"";return eval_fmt("text",{text:a})}function eval_fmt(a,b){return fmt[a].evaluate(b)}document.observe("dom:loaded",init_form);
