<?xml version="1.0" encoding="UTF-8" ?>
<Module>
<ModulePrefs title="Melbourne's Water Storages" author="Adam Malcontenti-Wilson" author_email="adman.com@gmail.com" author_location="Melbourne, Australia" description="Shows you Melbourne's current water storage level and water consumption yesterday." height="180" width="320" screenshot="http://adman.com.googlepages.com/mwgadget.png" author_affiliation=" " title_url="http://www.melbournewater.com.au/content/water/water_storages/water_storages.asp#1" thumbnail="http://adman.com.googlepages.com/mwgadget.png" author_photo="http://adman.com.googlepages.com/ME_PIXILATED.JPG" author_quote="If I Got Smart With You, How Would You Know?">
<Require feature="settitle" />
<Require feature="dynamic-height" />
</ModulePrefs>
<UserPref name="showbg" 
     display_name="Show Coloured Background"
     datatype="bool"
     default_value="true" />
<UserPref name="colourtable" 
     display_name="Show Different Coloured Table"
     datatype="bool"
     default_value="true" />
<UserPref
      name="colour"
      display_name="Colour of Background:"
      datatype="string"
      default_value="#FFFFBF"
    />
<UserPref
      name="tablecolour"
      display_name="Colour of Table:"
      datatype="string"
      default_value="#FFFFBF"
    />
<UserPref name="showtitle" 
     display_name="Show Levels in Title"
     datatype="bool"
     default_value="true" />
<UserPref name="showmore" 
     display_name="Show More Link"
     datatype="bool"
     default_value="true" />
<UserPref name="showchange" 
     display_name="Show Change from Yesterday"
     datatype="bool"
     default_value="true" />
<UserPref name="showconsumption" 
     display_name="Show Water Consumption"
     datatype="bool"
     default_value="true" />
<UserPref name="showtarget155" 
     display_name="Show Water Consumption per person (Target 155)"
     datatype="bool"
     default_value="true" />
<UserPref name="showrestrictions" 
     display_name="Show Current Water Restrictions (manually updated)"
     datatype="bool"
     default_value="false" />
  <Content type="html">

  <![CDATA[ 
<style type="text/css">
p.storagesPercentage {
font-size: 110%;
text-align: center;
margin-top: 0;
}
h4:first-line {
font-size: 95%;
line-height: 0.9em;
}
span.storagesPercentage {
font-size: 130%;
color: red ;
font-weight: bolder;
}
span.dateUpdated {
font-weight: normal;
font-size: 75%;
line-height: 50% ;
text-align: center;
margin: 0;
}
#wconsumption,#change,span.target155 {
font-weight: bold;
color:red;
}
#restrictions {
font-weight: bold;
text-decoration: none;
line-height: 120%;
}

table {
	font-size: 75%;
}
tr.total {
	font-style: italic;
}
</style>
<div id="content_div" style="font-family:Arial, Helvetica; text-align:center; font-size:90%; letter-spacing: 80%;">loading...<br /><font size="-2">please make sure you have an active internet connection...</font><p align="center" /><font size="-3">Water Storages data from <br /><img src="http://melbournewater.com.au/images/system/logo2.gif" alt="Melbourne Water" /><br />Target 155 data from <br /><img src="http://www.ourwater.vic.gov.au/__data/assets/image/0019/3529/mainLogo.gif" alt="Our Water, Our Future" /></font></div>
  <script>
  function trim(string){
	return string.replace(/^\s+|\s+$/g, '');
  }
  
  function int(str) {
	if (str == '') { return 0; }
    return parseFloat(str.replace(/^\s+|\s+$/g, '').replace(/,/g,''));
  }
  
  function roundNumber(num, dec) {
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
}
  

    // Must be constructed using the __MODULE_ID__ token. It gets replaced
   // at runtime with the actual ID of your gadget.
   var prefs = new _IG_Prefs(__MODULE_ID__);
   var showbg = prefs.getBool("showbg");
   var colour = prefs.getString("colour");
   var tablecolour = prefs.getString("tablecolour");
   var showtitle = prefs.getBool("showtitle");
   var showmore = prefs.getBool("showmore");
   var showchange = prefs.getBool("showchange");
   var showconsumption = prefs.getBool("showconsumption");
   var colourtable = prefs.getBool("colourtable");
   var showrestrictions = prefs.getBool("showrestrictions");
   var showtarget155 = prefs.getBool("showtarget155");
   var html = "";
   var finished = false;
   
  _IG_FetchContent('http://www.melbournewater.com.au/content/water_storages/water_report/water_report.asp?bhjs=0', function (responseText) {
     // Use the split function to extract substrings separated by comma 
     // delimiters
	 var stage1 = responseText.split('<h4><span class="blureRound_txt">');
	 var stage1a = stage1[1];
	 var stage2 = stage1a.split('</span> full at ');
     var storagesPercentage = stage2[0];
     var stage4 = stage2[1].split("</h4>");
     var dateUpdated = stage4[0];
	 var stage5 = responseText.split('<table class="wwuData" border="0" cellpadding="5" cellspacing="1" summary="Table showing current water storage">');
	 var stage6 = stage5[1].split('</table>');
	 var stage7 = stage6[0].split('<tr');
	 var storages = [,];
	 for (var x = 2; x <= 10; x++) {
		var stage8 = stage7[x].split('<td>');
		var stage9 = stage8[0].split('_reservoir.asp">');
		var stage10 = stage9[1].split('</a></td>');
		var title = stage10[0];
		var storage = [];
		for (var i = 1; i <= 5; i++) {
			var stage11 = stage8[i].replace('<td>','');
			var stage12 = stage11.replace('</td>','');
			storage[i] = stage12;
		}
		storages[x - 2] = [title,trim(storage[1]),trim(storage[2]),trim(storage[3]),trim(storage[4]),trim(storage[5])];
	}
	var stage13 = responseText.split('<p class="consumption_txt">');
	var stage14 = stage13[1].split('ML</p>');
	var consumption = stage14[0];
	
    html += "<p class=\"storagesPercentage\">Melbourne's water storages are currently: <span class=\"storagesPercentage\">" + storagesPercentage + "</span><br /><span class=\"dateUpdated\">(as of " + dateUpdated + ")</span></p>";
	if ( showtarget155 == true ){
		html += '<!-- REPLACE_WITH_TARGET_155 -->';
		_IG_FetchContent('http://www.ourwater.vic.gov.au/', function (responseText) {
			var stage1 = responseText.split('<h2>Melburnians are using</h2>');
			var stage2 = stage1[1].split('</font></strong>&nbsp L / person');
			var consumptionperperson = stage2[0].replace('<p><strong><font size="6">','');
			html = html.replace('<!-- REPLACE_WITH_TARGET_155 -->','<p class="target155">Melburnians are using: <span class="target155">'+consumptionperperson+'</span><span class="target155units">&nbsp; L / person</span></p>');
			if (finished == true) { setContent(html); }
		});
	}
	
	
	if ( showconsumption == true) {
       html += "Melbourne's water consumption yesterday: <span id='wconsumption'>"+ consumption + " <abbr title='megalitres'>ML</abbr></span>";
    }
		
	var morehtml = "<br /><a id=\"morelink\" href=\"http://www.melbournewater.com.au/content/water/water_storages/water_storages.asp#1\" onclick=\"togglemore(); return false;\">more&gt;&gt;</a>";
	morehtml += "<div id=\"more\" style=\"text-align:center; display: none;\"><table width=\"100%\" border='1'";
	if ( colourtable == true ) {
		morehtml += "style=\"background-color: " + tablecolour + "; \"";
	}
	morehtml += "><tr><th>Reservoir</th><th>Capacity</th><th>Current Volume (ML)</th><th>% Full</th><th>Change since Yesterday (ML)</th><th>Rainfall (mm) to 8:00am</th></tr>";
	var totals=[0,0,0,0,0,0];
	for (var x = 0; x <= 8; x++) {
		 var storage = storages[x];
		 totals[1] += int(storage[1]);
		 totals[2] += int(storage[2]);
		 totals[3] += int(storage[3]);
		 totals[4] += int(storage[4]);
		 totals[5] += int(storage[5]);
		 if (storage[5] == '') { storage[5] = '<abbr title="not available">n.a.</abbr>'; }
		 morehtml += "<tr><td>" + storage[0] + "</td><td>" + storage[1] + "</td><td>" + storage[2] + "</td><td>" + storage[3] + "</td><td>" + storage[4] + "</td><td>" + storage[5] + "</td></tr>";
	}
	morehtml += "<tr class=\"total\"><td>Total</td><td>" + totals[1] + "</td><td>" + totals[2] + "</td><td>" + roundNumber((totals[2]/totals[1]) * 100,2) + "</td><td>" + totals[4] + "</td><td>" + totals[5] + "</td></tr>"
	morehtml += "</table><br /><a href='http://www.melbournewater.com.au/content/water/water_storages/water_storages.asp#1' target='_blank'>more info &gt;&gt;&gt;</a></div>";
	if ( showchange == true ) { html += "<br />Change from yesterday: <span id='change'>" + totals[4] + "<abbr title='megalitres'>ML</abbr></span>";}
	if ( showmore == true ) {
		html += morehtml;
    }
	if ( showrestrictions == true) {
		// WARNING: Will not get updated!!
		html += "<br /><a href='http://www.melbournewater.com.au/content/water_conservation/water_restrictions/water_restrictions.asp' id='restrictions' target='_blank'>Stage 3a Water Restrictions are still in place</a>";
	}
    // Output html in div.
    setContent(html);
	finished = true;
    
 if ( showbg == true ) {
_gel('content_div').style.backgroundColor = colour;
document.body.style.backgroundColor = colour;
}
if ( showtitle == true ) {
title = "Storages are: " + storagesPercentage + " full";
_IFPC_SetTitle(title);

}
});
    // Outputs content to the div and resizes the gadget
    function setContent(html) {
        _gel('content_div').innerHTML = html;

       // Tells gadget to resize itself
       _IG_AdjustIFrameHeight();
    }

    function togglemore() {
      _gel('more').style.display = (_gel('more').style.display != 'none' ? 'none' : 'block' );
	  _gel('morelink').innerHTML = (_gel('more').style.display != 'none' ? '&lt;&lt;less' : 'more&gt;&gt;' );
    
    // Tells gadget to resize itself
       _IG_AdjustIFrameHeight();
    }
  </script>
  ]]> 

  </Content>

  </Module>