<?xml version="1.0" encoding="UTF-8" ?>
<Module>
<ModulePrefs scrolling='true' height='310' title="Happy Hours by UrbanDrinks.com"  author="UrbanDrinks.com" author_email="gadget@urbandrinks.com"
  description="Displays currently running happy hours near you" title_url="http://www.urbandrinks.com"
   screenshot="http://www.urbandrinks.com/images/gadget/gadget_screenshot.png"
   thumbnail="http://www.urbandrinks.com/images/gadget_thumb.png">
 <Require feature="analytics" />
 </ModulePrefs>
  <UserPref name="address" display_name="Address" default_value="Portland OR" required="true"/>
  <UserPref name="timeframe" display_name="Timeframe" datatype="enum" default_value="now_and_later">
    <EnumValue value="now" display_value="Now"/>
    <EnumValue value="now_and_later" display_value="Now and later today"/>
  </UserPref>
  <UserPref name="showdistance" datatype="bool" display_name="Show Distance?" default_value="true" />
  <UserPref name="showratings" datatype="bool" display_name="Show Ratings?" default_value="true" />
  <UserPref name="within" display_name="Within how many miles?" default_value="5" />
  <UserPref name="limit" display_name="How many to display?" default_value="5" />
  <UserPref name="order_by" display_name="Order Results By" datatype="enum" default_value="distance">
    <EnumValue value="distance" display_value="Distance"/>
    <EnumValue value="place_name" display_value="Place Name"/>
    <EnumValue value="hh_start" display_value="Happy Hour Start Time"/>
    <EnumValue value="hh_end" display_value="Happy Hour End Time"/>
    <EnumValue value="rating_avg" display_value="Rating"/>
    <EnumValue value="random" display_value="Random"/>
  </UserPref>
  <UserPref name="order_dir" display_name="Order Direction" datatype="enum" default_value="asc">
    <EnumValue value="asc" display_value="Ascending"/>
    <EnumValue value="desc" display_value="Descending"/>
  </UserPref>
  <UserPref name="beer" datatype="bool" display_name="Beer" default_value="true" />
  <UserPref name="cocktails" datatype="bool" display_name="Cocktails" default_value="true" />
  <UserPref name="wine" datatype="bool" display_name="Wine" default_value="true" />
  <UserPref name="food" datatype="bool" display_name="Food" default_value="true" />
 
  <Content type="html">
    <![CDATA[
  <div id="content_div" style='font-family:Arial,Helvetica,sans-serif;font-size:12px'>
  </div>

<script type="text/javascript">

var prefs = new _IG_Prefs();
function displayUD(){
  var url = "";
  url += "http://www.urbandrinks.com/api.xml?api_key=gadgetj7xuyy";
  var address = prefs.getString("address");  
  address = address.replace(/&/g, 'and');
  var title_address = address;
  address = encodeURI(address);
  address = address.replace(/%20/g, '+');
  var specials_arr = [];
    if (prefs.getBool("beer")==true) { 
      specials_arr[specials_arr.length] = 'b';    
    }
    if (prefs.getBool("cocktails")==true) { 
      specials_arr[specials_arr.length] = 'c';    
    }
    if (prefs.getBool("wine")==true) { 
      specials_arr[specials_arr.length] = 'w';    
    }  
    if (prefs.getBool("food")==true) { 
      specials_arr[specials_arr.length] = 'f';    
    }
  url += "&specials="  + specials_arr.join(); 
  var gadget_url = "/google_gadget?address=" + address
  _IG_Analytics("UA-821774-3", gadget_url);
  url += "&address="   + address;
  url += "&within="    + prefs.getString("within");
  url += "&limit="     + prefs.getString("limit");
  url += "&timeframe=" + prefs.getString("timeframe");
  url += "&order_by="  + prefs.getString("order_by");
  url += "&order_dir=" + prefs.getString("order_dir");  
  
    _IG_FetchXmlContent(url, function (response) {
      if (response == null || typeof(response) != "object" || response.firstChild == null) {
      _gel("content_div").innerHTML = "<i>Sorry, we could not find any happy hours for the address and city you chose.</i>" + 
      "<br/><br/>Please check the address you entered and try again.  If you selected the \"Now\" timeframe, it might just " +
      "be that there are no happy hours going on right now.<br/> " + 
      "Also, UrbanDrinks currently has happy hours for the following cities and their suburbs only:" +
      "<ul>" +
        "<li>Portland, OR</li><li>Seattle, WA</li><li>Columbus, OH</li><li>Phoenix, AZ</li><li>Washington, DC</li><li>Dallas, TX</li><li>Pittsburgh, PA</li>" +
      "</ul>" +
      "<br/>Feel free to <a href='http://www.urbandrinks.com/feedback'>email us</a> if you would like to " +
      "suggest a city for us to add next or have any other questions or comments.";      
      return;
      }

      var html = "<div class='places' style='padding: 8px;font-family:Arial, Helvetica;" +
      "text-align:left;font-size:12px'>\n";
      //html +="<div style='text-align:center; font-size: 110%; color: black; " +
      "font-weight: 700;'>";

      //var elems = response.getElementsByTagName("title");
      //var title_node = elems.item(0);
      //var title = title_node.firstChild.nodeValue;
      //var title = "Happy Hours Near " + title_address;
      //html += title + "</div><br>";
      //html += "</div>";
      var places = response.getElementsByTagName("place");
      for (var i = 0; i < places.length ; i++) {
        // For each <place> node, get child nodes.
        var nodeList = places.item(i).childNodes;
        // Loop through child nodes. Extract data from the text nodes that are
        // the children of the associated name, price, and calories element nodes.
        for (var j = 0; j < nodeList.length ; j++) {
          var node = nodeList.item(j);
          if (node.nodeName == "place_name") {
            var place_name = node.firstChild.nodeValue;
          }
          if (node.nodeName == "url") {
            var place_url = node.firstChild.nodeValue;
          }
          if (node.nodeName == "rating_avg") {              
            var rating_avg = node.firstChild.nodeValue;        
          }            
          if (node.nodeName == "distance") {
            var distance = node.firstChild.nodeValue;
          }
          if (node.nodeName == "happy_hour") {
            hh_nodes = node.childNodes;
            for (var k = 0; k < hh_nodes.length ; k++) {
              var hh_node = hh_nodes.item(k);
              if (hh_node.nodeName == "time_range") {
                var hh_time = hh_node.firstChild.nodeValue;
              }
              if (hh_node.nodeName == "specials_list") {
                var specials = hh_node.firstChild.nodeValue;
              }
            }
          }
        }
  
        html += "<div class='place' style='margin-bottom:8px;float:left;width:100%'>\n";
        html += "<div class='place_top' style='height:18px;width:100%'>\n" +
          "<a href='" + place_url + "' target='top'>" + String(place_name) + "</a>\n";
        if (prefs.getBool("showdistance")==true) {
          html += "<span class='distance' style='padding-left:10px'>" + distance + " miles</span>";
        }

        html += "</div>\n"; //end place_top div  
        html += "<div class='place_bottom' style='float:left;height:24px;width:100%'>\n";
        if (prefs.getBool("showratings")==true) {
          html += "<div class='rating' style='float:left'>";
          for (var x = 1; x <= 5 ; x++) {
            if (rating_avg >= (x)) {
              html += '<img src="http://www.urbandrinks.com/images/star-on.gif" alt="" border="0" width="10" height="9">';
            } else if (rating_avg >= (x-1) && rating_avg <= (x)) {
              if (rating_avg >= (x - 0.5)) {
                html += '<img src="http://www.urbandrinks.com/images/star-half.gif" alt="" border="0" width="10" height="9">';       
              } else {
                html += '<img src="http://www.urbandrinks.com/images/star-off.gif" alt="" border="0" width="10" height="9">';       
              }
            } else {
                html += '<img src="http://www.urbandrinks.com/images/star-off.gif" alt="" border="0" width="10" height="9">';       
            }
          } 
          html += "</div>\n"; //end rating div
        };
        html += "<div class='hh_time' style='float:left;color:green;padding-left:10px;padding-right:10px'>\n" + hh_time + "\n</div>\n"; 
        html += "<div class='icons' style='float:right;padding:0px;margin-top:-4px;height:18px;'>\n";
        var icons = ['beer','cocktails','wine','food'];
        for(z=0; z<4; z++) {
          icon = icons[z];
          icon_regexp = new RegExp(icon);
          match = specials.match(icon_regexp);
          if (match) {
            html += "<img src='http://www.urbandrinks.com/images/gadget/" + icon + ".png' alt='" + icon + "' title='" + icon + "'/>\n";
          } else {
            html += "<img src='http://www.urbandrinks.com/images/gadget/" + icon + "_gray.png' alt='" + icon + "' title='" + icon + "'/>\n";
          }
        }        
        html += "</div>\n"; //end icons div
        html += "</div>\n"; //end place_bottom div
        html += "</div>\n"; //end place div 
      }
      html += "</div>\n"; //end places div 
      html += "<div style='font-size:11px;color:#555;margin-top:0px'>Click on the arrow in the title bar to change your address &amp; other preferences.</div>";
      // Display HTML string in <div>
      _gel('content_div').innerHTML = html;
  }, { refreshInterval: (300) });
}

_IG_RegisterOnloadHandler(displayUD);


</script>

]]>
</Content>
</Module>