<?xml version="1.0" encoding="UTF-8"?>
<Module>
<ModulePrefs title="Zillow ZIndex Graph" height="210" title_url="http://www.zillow.com" description="Google Gadget for Zillow's ZIndex Graph for a US city, state or zipcode. Version 0.6" screenshot="http://www.rajgad.com/files/zindexgraph_ggadget4_sm.png" thumbnail="http://www.rajgad.com/files/zindexgraph_ggadget4_sm.png" author="Amit D. Chaudhary" author_location="Bellevue, WA USA. " author_email="amit_ml@rajgad.com" author_affiliation="Self" author_link="http://blog.rajgad.com" scrolling="true"/>
<UserPref name="LocationType" display_name="Type of Location" default_value="none" datatype="enum">
	   <EnumValue value="citystate" display_value="City State"/>
	   <EnumValue value="state" display_value="State"/>
	   <EnumValue value="zip" display_value="Zipcode"/>
	   <EnumValue value="none" display_value="All of USA"/>
</UserPref> 
<UserPref name="USCity" display_name="US city"/>
<UserPref name="USCityState" display_name="State for city"/>
<UserPref name="USZipcode" display_name="US zipcode"/>
<UserPref name="GraphType" display_name="Type of ZIndex graph" default_value="percent" datatype="enum">
	   <EnumValue value="percent"/>
	   <EnumValue value="dollar"/>
</UserPref> 
<UserPref name="DurationType" display_name="Duration for ZIndex graph" default_value="1year" datatype="enum">
	   <EnumValue value="1year" display_value="1 year"/>
	   <EnumValue value="5years" display_value="5 years"/>
	   <EnumValue value="10years" display_value="10 years"/>
</UserPref> 
 <Content type="html">
 <!-- (C) 2006 Amit D. Chaudhary (amit_ml@rajgad.com)  -->
 <!-- License GNU GPL v2, http://www.gnu.org/copyleft/gpl.html -->
 <![CDATA[ 
 <div id="content__MODULE_ID__"></div>
 <script type="text/javascript"> 
    function example__MODULE_ID__() { 
    var prefs = new _IG_Prefs(__MODULE_ID__);

	// Read the preferences including location for ZIndex
    var LocationType = prefs.getString("LocationType");
    var city = prefs.getString("USCity");
    var state = prefs.getString("USCityState");
    var zipcode = prefs.getString("USZipcode");
    var graphType = prefs.getString("GraphType");
    var durationType = prefs.getString("DurationType");
    var graphTitle;

	// Create the Zillow API URI by adding location details.
	// Please replace the zws-id in the URI below with your own before copying it over.
	// http://www.zillow.com/corp/Terms.htm
    var uri = "http://www.zillow.com/webservice/GetRegionChart.htm?zws-id=X1-ZWz1chr4di3bwr_3xvfc&unit-type="
    		+ graphType;
	
	if (LocationType == "zip") {
		uri += "&zip=" + zipcode;
		graphTitle = "Zip: " + zipcode ;
	}
	else if (LocationType == "citystate") {
		uri += "&city=" + city + "&state=" + state;
		graphTitle = "City: " + city + "," + state;
	}
	else if (LocationType == "state") {
		uri += "&state=" + state;
		graphTitle = "State: " + state;
	} else if (LocationType == "none") {
		graphTitle = "USA";
	}
	
	if (durationType == "1year") {
		graphTitle += " (1 year)";
	} else if (durationType == "5years") {
		graphTitle += " (5 years)";
	} else if (durationType == "10years") {
		graphTitle += " (10 years)";
	}
	

	// Check response for xml fetch.
    _IG_FetchXmlContent(uri, function (response) {
           if (response == null || typeof(response) != "object" || 
                      response.firstChild == null) {
              _gel("content__MODULE_ID__").innerHTML = "<i>Invalid data.</i>";
              return;
           }

			var responsecode = response.getElementsByTagName("code").item(0).firstChild.nodeValue;
			var html;
			
			// Check response from zillow API call. Print error and return, if needed
	        if (responsecode != 0 ) {
	        	html = "<i>Request to Zillow failed. Response: " + 
	        			response.getElementsByTagName("text").item(0).firstChild.nodeValue +
		        		"(" + responsecode + ")</i>";
	        	
             	_gel("content__MODULE_ID__").innerHTML = html;
              	return;
           }

			// Find URL in the response, this is the location for the image showing ZIndex trend.
			// Create html with img tag and details about zillow and me.
			var imgURI = response.getElementsByTagName("url").item(0).firstChild.nodeValue;
			html = "ZIndex graph for " + graphTitle + "<br>";
	
			// Change img URI, if different duration (default is 1year) is requested.
			if (durationType != "1year") {
				imgURI = imgURI.replace(/1year/, durationType);
			}
			
			html += "<a target=_blank href=http://www.zillow.com><img src=" + imgURI + 
						"></img></a><br>"
						
			// Add information as per the Zillow API Terms of use
			html += "© Zillow, Inc., 2006. Use is subject to <a target=_blank href=http://www.zillow.com/corp/Terms.htm>" +
				"Terms of Use</a>. What's a <a target=_blank href=http://www.zillow.com/howto/WhatsaZindex.htm> Zindex</a>?<br>";
				
	        // Display HTML in the gadget window. _gel is shortcut for document.getElementById()
	        _gel('content__MODULE_ID__').innerHTML = html; 
    }); 
}
 _IG_RegisterOnloadHandler(example__MODULE_ID__);
 </script>
 ]]> 
 </Content>
 </Module>