<?xml version="1.0" encoding="UTF-8" ?>
<Module>
  <ModulePrefs title="Weird News of the Day"
    author="David Pascual"
    author_email="david@yourstreet.com"
    author_affiliation="YourStreet Inc."
    author_location="San Francisco, CA"
    screenshot="http://hosting.gmodules.com/ig/gadgets/file/100026491092382400168/yourstreet-weird-news.png"
    thumbnail="http://hosting.gmodules.com/ig/gadgets/file/100026491092382400168/thumbnail.png"
    category="news" 
	
    description="Weird news of the day from the United States - powered by YourStreet.com"
    render_inline="optional"
    width="205"
    height="310"
    scaling="false">
    <Require feature="analytics"/>
  </ModulePrefs>
  <Content type="html">
    <![CDATA[
	  <style type="text/css">

div#widget { margin:0px; width:203px; border:1px solid #b6c4cc;
background-color:#d8e8f1; color: #3f6274; font-size:12px;
font-family:Arial, Helvetica, sans-serif; text-align:left }

.content-div { margin:3px; width:195px; height:252px; border:1px solid
#b6c4cc; background-color:#eef3f6; color: #444444; font-size:12px;
font-family:Arial, Helvetica, sans-serif; text-align:left;
overflow:auto }
		a, a:visited { color:#444; text-decoration:none; }
		a:hover { text-decoration:underline; }
		hr { border:0px; height:1px; color:#bfbfbf; background-color:#bfbfbf; margin-left:3px; width:95%; }
		.location { font-weight:bold; }
		.location-long { font-size:11px; }
		.location-header { margin-top:5px; margin-bottom:3px; margin-left:3px; margin-right:3px; }
		.change-location { font-size:10px; text-align:right; color:#7090a0; }
		.tagline { font-size:10px; color:#7090a0; padding-top:2px; padding-bottom:2px; }
		.visit-ys { color:#636e74; font-weight:bold; font-size:11px; text-align:center; }
		.your { color:#116fa0; }
		.street { color:#f1a42c; }
		.new { color:#b34f4f; }
		.update { text-decoration: underline; }
		.title { margin:3px; font-size:11px; font-weight:bold; color:#444; }
		.description { margin:3px; font-size:11px; color:#666; }
		.search-text { font-weight: bold; margin:3px; font-size:11px; color:#666; }
		.search-clear { font-weight: bold; margin:3px; font-size:11px; color:#b34f4f; text-align: right; }
		.search-error { margin-left:3px; margin-bottom:3px; font-weight:bold; font-size:11px; color:#b34f4f; }
		.search-ok { margin-left:3px; margin-bottom:3px; font-weight:bold; font-size:11px; color:#64b34f; }
		.search-info { margin-left:3px; margin-bottom:3px; font-weight:bold; font-size:11px; color:#444; }
		.search-input { font-size:9px; margin:3px; background-color:#dce4e8; }
		.center { text-align: center; }
		.float-left { float:left; }
		.float-right { float:right; }
		.clear { clear:both; }
	  </style>
      <script language="JavaScript" src="http://www.yourstreet.com/javascripts/json2.js"></script>
      <script type="text/javascript">	
      	var url="http://www.yourstreet.com/weird-news/rss";
      	var search_city="";
      	var search_state="";
      	var search_zipcode="";
        var weird=true;
      	var location_identifier="";
		location_identifier = location_identifier.toLowerCase();
      </script>
	  <script type="text/javascript">
		var prefs;
		var city="";
		var state="";
		var zipcode="";
		var recent;
		var last_query="";
		var error_msg="";
		var error_count=0;
		var fetchSuccessfull__MODULE_ID__ = false;

		function init() {
		  prefs = new _IG_Prefs();
		  var city_pref = prefs.getString("city");
		  var state_pref = prefs.getString("state");
		  var zipcode_pref = prefs.getString("zipcode");
		  var recent_pref = prefs.getString("recent");
		  var default_location="";

		  if (recent_pref == "") recent = {"array":[]};
		  else {
		    recent = JSON.parse(recent_pref);
		    printRecent();
		  }

		  if (zipcode_pref!="") default_location="zipcode";        
		  if (city_pref!="" && state_pref!="") default_location="city";

		    search_city=city_pref;
		    search_state=state_pref;
		    location_identifier="weird-news";
		    document.getElementById("location").innerHTML = "<a href='http://www.yourstreet.com/' target='_blank'>Weird News of the Day</a>";
		    //document.getElementById("tagline").innerHTML = "<a href='http://www.yourstreet.com/' target='_blank' class='tagline'>Weird news from the United States</a>";
		    url="http://www.yourstreet.com/" + location_identifier + "/rss";
	            fetchSuccessfull__MODULE_ID__ = false;
		    RSSfetchWrapper__MODULE_ID__();
		}

		function printRecent() {
		  var recent_html="";

		  for (i = 0; i < recent.array.length; i++) {
		    if (recent.array[i] == null) break;

		    var recent_location = recent.array[i].location;
		    var recent_state = recent.array[i].state;

		    if (recent_state!="") recent_html += "<div class='clear float-left'><a href='javascript:loadRecent(" + i + ")' class='description'>" + recent_location + ", " + recent_state + "</a></div><div class='float-right'><a href='javascript:clearRecent(" + i + ")' class='search-clear'>clear</a></div>";
		    else recent_html += "<div class='clear float-left'><a href='javascript:loadRecent(" + i + ")' class='description'>" + recent_location + "</a></div><div class='float-right'><a href='javascript:clearRecent(" + i + ")' class='search-clear'>clear</a></div>";
		  }

		  _gel('recent-location').innerHTML = recent_html;
		}

		function loadRecent(pos) {
		  if (pos==-1) {
                    weird=true;
                    url="http://www.yourstreet.com/weird-news/rss";
		    fetchSuccessfull__MODULE_ID__ = false;
		    RSSfetchWrapper__MODULE_ID__();
                  } else {
                  search_city = recent.array[pos].location;
		  search_state = recent.array[pos].state;
		  search_zipcode = recent.array[pos].location;

		  if (search_state=="") location_identifier=search_zipcode;
		  else location_identifier = replaceChars(search_city) + "-" + search_state;
		  
		  location_identifier = location_identifier.toLowerCase();
		
		  url="http://www.yourstreet.com/" + location_identifier + "/rss";
		  fetchSuccessfull__MODULE_ID__ = false;
		  RSSfetchWrapper__MODULE_ID__();
                  }
		}

		function replaceChars(entry) 
		{
		  out = " "; // replace this
		  add = "-"; // with this
		  temp = "" + entry; // temporary holder

		  while (temp.indexOf(out)>-1) {
		  	pos= temp.indexOf(out);
		    temp = "" + (temp.substring(0, pos) + add + temp.substring((pos + out.length), temp.length));
		  }

		  return temp;
		}

		function RSSfetchWrapper__MODULE_ID__() {
			if(!fetchSuccessfull__MODULE_ID__) {
				RSSfeed__MODULE_ID__();
				setTimeout('RSSfetchWrapper__MODULE_ID__()', 5000);
			}
		    return;
		}

		function RSSfeed__MODULE_ID__() {
		  _IG_FetchXmlContent(url, function (response) {
			fetchSuccessfull__MODULE_ID__ = true;
		    if (response == null || typeof(response) != "object" || response.firstChild == null) {
		      _gel("news-div").innerHTML = "<i>Invalid data.</i>";
		      return;
		    }

		    var itemList = response.getElementsByTagName("item");

		if (itemList.length==0) {
		      if (search_zipcode!="" && last_query=="city") {
		        last_query="zipcode"; 
		        location_identifier=search_zipcode;
		        url="http://www.yourstreet.com/" + location_identifier + "/rss";
		        fetchSuccessfull__MODULE_ID__ = false;
			    RSSfetchWrapper__MODULE_ID__();
		      } else {
		        error_count++;
		        if (error_count>3) _gel('search-info').innerHTML = "<a href='http://www.yourstreet.com/' target='_blank'><span class='search-info'>Please visit us at <span class='your'>Your</span><span class='street'>Street</span></span></a>";
		        else _gel('search-info').innerHTML = "<span class='search-error'>No matching locations.</span>";
		      }
		} else {
		      error_count=0;
		      _gel('search-info').innerHTML = "<span class='search-ok'>Loading...</span>";
		      var pageHtml = "";

		      for ( var i = 0; i < itemList.length; i++ ) {
		        var nodeList = itemList.item(i).childNodes;

			for (var j = 0; j < nodeList.length ; j++) {
		  	  var node = nodeList.item(j);
		  	  if (node.nodeName == "title") { var title = getNodeText(node); }
		  	  if (node.nodeName == "description") { var description = getNodeText(node); }
		  	  if (node.nodeName == "link") { var link = getNodeText(node); }
			}

		        title = (title.length > 50) ? title.substring(0,50) + "..." : title;
		        description = description.substring(0,80) + "...";

			pageHtml += "<div class='title'><a href='" + link + "' target='_blank'>" + title + "</a></div>";
		  	pageHtml += "<div class='description'>" + description + "</div>";
		        pageHtml += "<hr>";
		      }

		      _gel('search-info').innerHTML = "";
		      _gel("news-div").innerHTML = pageHtml;
		      setPreferences();
		      gotoNews();
		}
		  });
		}

		function setPreferences() {
		  city=search_city;
		  state=search_state;
		  zipcode=search_zipcode;

		  //prefs.set("city",city);
		  //prefs.set("state",state);          
		  //prefs.set("zipcode",zipcode);

		  //addRecentLocation();
		}

		function addRecentLocation() {
		  var flag=false;

		  for (var i=0; i<recent.array.length; i++) {
		    if ((city==recent.array[i].location && state==recent.array[i].state) || (zipcode==recent.array[i].location)) {
		      flag=true;
		      break;
		    }
		  }

		  if (!flag) {
		    if (last_query=="city") recent.array[recent.array.length] = {"location": city, "state": state};
		    else recent.array[recent.array.length] = {"location": zipcode, "state": ""};
		  }

		  prefs.set("recent",JSON.stringify(recent));
		  last_query="";

		  printRecent();
		}

		function clearRecent(pos) {
		  var beginning = recent.array.slice(0, pos);
		  var end = recent.array.slice(pos + 1, recent.array.length);

		  recent.array = beginning.concat(end);
		  prefs.set("recent",JSON.stringify(recent));

		  printRecent();
		}

		function clearAllRecent()
		{
		  recent={"array":[]};
		  prefs.set("recent",recent);
		  _gel('recent-location').innerHTML="";
		}

		function getNodeText( node ) {
		  var textContent = "";
		  if ( node.textContent ) { textContent = node.textContent; }
		  else if ( node.text )   { textContent = node.text; }
		  return textContent;
		}

		function showChangeLocation() {
		  var change = document.getElementById("change-location");
		  change.style.display = (change.style.display=='block') ? 'none' : 'block';
		}

		function showGoBack() {
		  var goback = document.getElementById("go-back");
		  goback.style.display = (goback.style.display=='block') ? 'none' : 'block';
		}

		function gotoSearch() {
		  var location_header = document.getElementById("location");
		  //var tagline = document.getElementById("tagline");
		  var change = document.getElementById("change-location");
		  var goback = document.getElementById("go-back");
		  var search_div = document.getElementById("search-div");
		  var news_div = document.getElementById("news-div");

		  news_div.style.display = 'none'; 

		  document.getElementById("search-city").value="";
		  document.getElementById("search-zipcode").value="";

		  search_div.style.display = 'block'; 
		  change.style.display = 'none';
		  goback.style.display = 'block';

		  location_header.innerHTML = "Change Location";
		  //tagline.innerHTML = "Local news for any location in the U.S.";
		}

		function gotoNews() {
		  var location_header = document.getElementById("location");
		  //var tagline = document.getElementById("tagline");
		  var goback = document.getElementById("go-back");
		  var change = document.getElementById("change-location");
		  var search_div = document.getElementById("search-div");
		  var news_div = document.getElementById("news-div");
		  var visit_ys = document.getElementById("visit-ys");

		  if (!weird)
                  {
                    visit_ys.innerHTML = "<a href='http://www.yourstreet.com/" + location_identifier + "/widget' target='_blank'><img src='http://hosting.gmodules.com/ig/gadgets/file/100026491092382400168/get-this.png' border='0'></a>";

		    search_div.style.display = 'none'; 
		    news_div.style.display = 'block';         
		    goback.style.display = 'none';       
		    change.style.display = 'block';

		    if (zipcode!=location_identifier) {
		      location_header.innerHTML = "<a href='http://www.yourstreet.com/" + location_identifier + "' target='_blank'>" + capitalize(city) + " News<a>";
		      //tagline.innerHTML = "<a href='http://www.yourstreet.com/" + location_identifier + "' target='_blank' class='tagline'>Local news for " + city + ", " + state + "</a>";
		    } else {
		      location_header.innerHTML = "<a href='http://www.yourstreet.com/" + location_identifier + "' target='_blank'>" + zipcode + " News<a>";
		      //tagline.innerHTML = "<a href='http://www.yourstreet.com/" + zipcode + "' target='_blank' class='tagline'>Local news for " + zipcode + "</a>";
		    }
                  } else {
                    visit_ys.innerHTML = "<a href='http://www.gmodules.com/ig/creator?synd=open&url=http%3A//hosting.gmodules.com/ig/gadgets/file/100026491092382400168/youstreet-weird-news.xml&pt=%26context%3Dd%26type%3Dgadgets%26synd%3Dig%26lang%3Den%26.lang%3Den%26country%3Dus%26.country%3Dus%26start%3D0%26num%3D1%26target%3Db2Z%26objs%3D12t%2CKE%2CqA%2Ccd0%2Cj4m&sn=b2Z&lang=en' target='_blank'><img src='http://hosting.gmodules.com/ig/gadgets/file/100026491092382400168/get-this.png' border='0'></a>";

		    search_div.style.display = 'none'; 
		    news_div.style.display = 'block';         
		    goback.style.display = 'none';       
		    change.style.display = 'block';

                    location_header.innerHTML = "<a href='http://www.yourstreet.com/' target='_blank'>Weird News of the Day</a>";  
                  }
		}

		function searchLocation() {
                  weird=false;
		  search_city = capitalize(document.getElementById("search-city").value);
		  var state_options = document.getElementById("search-state");
		  search_state = state_options.options[state_options.selectedIndex].value;
		  search_zipcode = document.getElementById("search-zipcode").value;

		  if (search_city!="") {
		    last_query="city";
		    location_identifier=replaceChars(search_city) + "-" + search_state;
			location_identifier = location_identifier.toLowerCase();
		  } else if (search_zipcode!="") {
		    last_query="zipcode";
		    location_identifier=search_zipcode;
		  }        

		  _gel('search-info').innerHTML = "<span class='search-info'>Looking for location... please wait.</span>";  

		  url="http://www.yourstreet.com/" + location_identifier + "/rss";
		  fetchSuccessfull__MODULE_ID__ = false;
		  RSSfetchWrapper__MODULE_ID__();
		}

		function capitalize(str)
		{
		  return str.replace(/\w+/g, function(a){
		    return a.charAt(0).toUpperCase() + a.substr(1).toLowerCase();
		  });
		}
	  </script>
      
	  <script>
        _IG_Analytics("UA-1478650-3", "/weird-news-v3");
      </script>

	  <script type="text/javascript">
		_IG_RegisterOnloadHandler(RSSfetchWrapper__MODULE_ID__);
	  </script>
      
      <div id='widget' style='display:block'>
        <div class='location-header'>
          <div id='location' class='location float-left'></div>
          <div id='change-location' class='float-right' style='display:block'><a href='javascript:gotoSearch();' class='change-location'>change <img src="http://hosting.gmodules.com/ig/gadgets/file/100026491092382400168/magnifier.png" border="0"></a></div>
          <div id='go-back' class='float-right' style='display:none'><a href='javascript:gotoNews();' class='change-location'>go back <img src="http://hosting.gmodules.com/ig/gadgets/file/100026491092382400168/goback.png" border="0"></a></div>
          <div id='tagline' class='tagline clear'></div>
        </div>
        <div id='news-div' class='content-div' style='display:block'>
        </div>
        <div id='search-div' class='content-div' style='display:none'>
          <form name="search" action="javascript:searchLocation();">
          <div class='search-text'>Enter any Location and State</div>
          <input type="text" id="search-city" size="18" class="search-input">
          <select id="search-state" class="search-input">
            <option value="AL">AL</option>
            <option value="AK">AK</option>
            <option value="AZ">AZ</option>
            <option value="AR">AR</option>
            <option value="CA">CA</option>
            <option value="CO">CO</option>
            <option value="CT">CT</option>
            <option value="DE">DE</option>
            <option value="DC">DC</option>
            <option value="FL">FL</option>
            <option value="GA">GA</option>
            <option value="HI">HI</option>
            <option value="ID">ID</option>
            <option value="IL">IL</option>
            <option value="IN">IN</option>
            <option value="IA">IA</option>
            <option value="KS">KS</option>
            <option value="KY">KY</option>
            <option value="LA">LA</option>
            <option value="ME">ME</option>
            <option value="MD">MD</option>
            <option value="MA">MA</option>
            <option value="MI">MI</option>
            <option value="MN">MN</option>
            <option value="MS">MS</option>
            <option value="MO">MO</option>
            <option value="MT">MT</option>
            <option value="NE">NE</option>
            <option value="NV">NV</option>
            <option value="NH">NH</option>
            <option value="NJ">NJ</option>
            <option value="NM">NM</option>
            <option value="NY">NY</option>
            <option value="NC">NC</option>
            <option value="ND">ND</option>
            <option value="OH">OH</option>
            <option value="OK">OK</option>
            <option value="OR">OR</option>
            <option value="PA">PA</option>
            <option value="RI">RI</option>
            <option value="SC">SC</option>
            <option value="SD">SD</option>
            <option value="TN">TN</option>
            <option value="TX">TX</option>
            <option value="UT">UT</option>
            <option value="VT">VT</option>
            <option value="VA">VA</option>
            <option value="WA">WA</option>
            <option value="WV">WV</option>
            <option value="WI">WI</option>
            <option value="WY">WY</option>
          </select>
          <div class='search-text'>Or enter your Zip Code</div>
          <input type="text" id="search-zipcode" size="18" class="search-input">
          <br><input type="submit" class="search-input" value="Search">
          </form>
          <div id='search-info'></div>
          <div id='update' class='search-text float-left'><a href="javascript:loadRecent(-1)">Reload 'Weird News of the Day'</a></div>
          <div id='recent-location'></div>
        </div>
        <div id='visit-ys' class='center'><a href="http://www.gmodules.com/ig/creator?synd=open&url=http%3A//hosting.gmodules.com/ig/gadgets/file/100026491092382400168/youstreet-weird-news.xml&pt=%26context%3Dd%26type%3Dgadgets%26synd%3Dig%26lang%3Den%26.lang%3Den%26country%3Dus%26.country%3Dus%26start%3D0%26num%3D1%26target%3Db2Z%26objs%3D12t%2CKE%2CqA%2Ccd0%2Cj4m&sn=b2Z&lang=en" target="_blank"><img src="http://hosting.gmodules.com/ig/gadgets/file/100026491092382400168/get-this.png" border="0"></a></div>
      </div>
    ]]>
  </Content>
</Module>

