<?xml version="1.0" encoding="UTF-8"?>
<Module>
  <ModulePrefs 
    title="search eBay"
    directory_title="search eBay"
    description="search eBay USA, Australia, UK, Canada, India, Ireland, Malaysia, Philippines, Singapore, as well as eBay Motors from your iGoogle homepage"
    author="Geoff Yaworski" 
    author_email="gyaworski@hotmail.com"
    author_affiliation=""
    author_location="Denver, CO"
    screenshot="http://hosting.gmodules.com/ig/gadgets/file/105324078185059490343/ebay-screenshot.png"
    thumbnail="http://hosting.gmodules.com/ig/gadgets/file/105324078185059490343/ebay-search.png"
    height="350" 
    width="320" 
    scrolling="true" 
    singleton="false" 
    author_photo="http://hosting.gmodules.com/ig/gadgets/file/105324078185059490343/me2.jpg"
    author_aboutme="I think Google's got the right idea when it comes to software development."
    author_link="http://www.inetfeedback.com"
    author_quote="keep your stick on the ice"
    category="tools"
    category2="lifestyle">
    <Require feature="settitle"/>
    <Require feature="analytics"/>
    <Require feature="setprefs"/>
    <Require feature="dynamic-height"/>
  </ModulePrefs>   

  <UserPref name="siteId" display_name="Country" datatype="enum" default_value="0">
    <EnumValue value="0" display_value="USA"/>
    <EnumValue value="100" display_value="eBay Motors"/>
    <EnumValue value="2" display_value="Canada"/>
    <EnumValue value="15" display_value="Australia"/>
    <EnumValue value="3" display_value="UK"/>
    <EnumValue value="203" display_value="India"/>
    <EnumValue value="205" display_value="Ireland"/>
    <EnumValue value="207" display_value="Malaysia"/>
    <EnumValue value="211" display_value="Philippines"/>
    <EnumValue value="216" display_value="Singapore"/>
  </UserPref>

  <UserPref name="itemSort" display_name="Sort By" datatype="enum" default_value="EndTime">
    <EnumValue value="EndTime" display_value="Ending Soonest"/>
    <EnumValue value="BestMatch" display_value="Best Match"/>
    <EnumValue value="StartDate" display_value="Newly Listed"/>
  </UserPref>

  <UserPref name="maxEntries" display_name="Result Display Count" datatype="enum" default_value="10">
    <EnumValue value="10" display_value="10"/>
    <EnumValue value="20" display_value="20"/>
    <EnumValue value="30" display_value="30"/>
    <EnumValue value="40" display_value="40"/>
    <EnumValue value="50" display_value="50"/>
  </UserPref>     
  
  <UserPref name="queryKeywords" display_name="Default Search Term" datatype="string" default_value=""/>
  <UserPref name="showPictures" display_name="Show Pictures?" datatype="bool" default_value="true"/>   
  <UserPref name="runSearchOnStart" display_name="Run Search On Start?" datatype="bool" default_value="false"/>
  
  <UserPref name="fontSize" display_name="Font Size" datatype="enum" default_value="9pt">
    <EnumValue value="7pt" display_value="7pt"/>
    <EnumValue value="8pt" display_value="8pt"/>
    <EnumValue value="9pt" display_value="9pt"/>
    <EnumValue value="10pt" display_value="10pt"/>
    <EnumValue value="11pt" display_value="11pt"/>
    <EnumValue value="12pt" display_value="12pt"/>
    <EnumValue value="15pt" display_value="15pt"/>
  </UserPref>

  <UserPref name="height" display_name="Gadget Height" datatype="enum" default_value="350">
    <EnumValue value="100" display_value="smallest"/>
    <EnumValue value="200" display_value="smaller"/>
    <EnumValue value="300" display_value="small"/>
    <EnumValue value="350" display_value="normal"/>
    <EnumValue value="400" display_value="large"/>
    <EnumValue value="500" display_value="larger"/>
    <EnumValue value="650" display_value="largest"/>
  </UserPref>

  <Content type="html">
    <![CDATA[
      <table border='0'>
        <tr><td valign='middle'>                   
          <input type="text" name="searchTerm" id="searchTerm" onkeypress="checkEnter(event)"/>
        </td>
        
        <td valign='middle'>  
          <select id="itemSort">
            <option value="EndTime" name="EndTime" id="EndTime">ending soonest</option>
            <option value="BestMatch" name="BestMatch" id="BestMatch">best match</option>
            <option value="StartDate" name="StartDate" id="StartDate">newly listed</option>
            </select>
        </td>
        
        <td valign='middle'>     
          <span id=searchButtonSpan><input type="button" id="searchButton" onclick="doSearch()" value="search"/></span>
        </td>
        
        <td valign='middle'>
          <span id=searchWaitText></span>
        </td>
      
        <td valign='middle'>
          <a target="_blank" href="http://rover.ebay.com/rover/1/711-53200-19255-0/1?type=1&campid=5336132743&toolid=10001&customid="><img border='0' src="http://hosting.gmodules.com/ig/gadgets/file/105324078185059490343/Right-Now-85x35.gif"/></a>
        </td></tr></table>

      <div id="searchResult"></div>
      <font color="red"><div id="error"></div></font>
      
      <script TYPE='text/javascript' SRC='http://w-1.ebay.com/js/2.2/min/Shopping.js'></script>
      <script TYPE='text/javascript' SRC='http://w-1.ebay.com/js/2.2/min/FindItems.js'></script>
      
      <script TYPE='text/javascript'>
        var test = false;
        _IG_RegisterOnloadHandler(doOnload);
        var prefs = new _IG_Prefs();
        var siteId = prefs.getInt("siteId");
        
        if (test) {
          _IG_SetTitle("eBay search ("+getCountry()+") (test)");
        } else {
          _IG_SetTitle("eBay search ("+getCountry()+")");
        }
        
        var fontSize = prefs.getString("fontSize");
        _gel("searchTerm").style.fontSize = fontSize;
        _gel("itemSort").style.fontSize = fontSize;
        _gel("searchButton").style.fontSize = fontSize;
        _gel("searchButtonSpan").style.fontSize = fontSize;
        _gel("searchWaitText").style.fontSize = fontSize;
        _gel("searchResult").style.fontSize = fontSize;
        _gel("error").style.fontSize = fontSize;
        
        if (prefs.getString("queryKeywords").length > 0) {
          _gel("searchTerm").value = prefs.getString("queryKeywords");
        }   
    
        if (navigator.appName == 'Microsoft Internet Explorer') {          
          if (_gel("itemSort").options(prefs.getString("itemSort"))) {            
            _gel("itemSort").selectedIndex = _gel("itemSort").options(prefs.getString("itemSort")).index;          
          }       
        } else {          
          if (_gel("itemSort").options[prefs.getString("itemSort")]) {            
            _gel("itemSort").selectedIndex = _gel("itemSort").options[prefs.getString("itemSort")].index;          
          }   
        }
        
        function getCountry() {        
          if (siteId == 0) {
            return "USA";
          } else if (siteId == 2) {
            return "Canada";
          } else if (siteId == 15) {
            return "Australia";
          } else if (siteId == 3) {
            return "UK";
          } else if (siteId == 100) {
            return "eBay Motors";
          } else if (siteId == 16) {
            return "Austria";
          } else if (siteId == 203) {
            return "India";
          } else if (siteId == 205) {
            return "Ireland";
          } else if (siteId == 207) {
            return "Malaysia";
          } else if (siteId == 211) {
            return "Philippines";
          } else if (siteId == 216) {
            return "Singapore";
          } else {
            return "Unknown Country";
          }
        }
        
        function checkEnter(event) {          
          if (event && event.keyCode == 13) {            
            doSearch();          
          }        
        }        
        
        function doOnload() {
          if (prefs.getBool("runSearchOnStart")) {
            doSearch();
          }

          _IG_AdjustIFrameHeight(prefs.getInt("height"));
        }
        
        function onSuccess(root) {                             
          var items = root.item || [];
          var html = "";          
          html+=("<font color='#ff8000' style='font-size:"+fontSize+"'><b>"+root.totalItems+
                 "</b></font> <font color='#999999' style='font-size:"+fontSize+"'><b>results found for: "+                 
                 "</b></font><font color='#000000' style='font-size:"+fontSize+"'><b>"+
                 trim(_gel("searchTerm").value)+
                 "</b></font>");
    
          if (root.totalItems > prefs.getInt("maxEntries")) {
            html+="<font color='#000000' style='font-size:"+fontSize+"'>&nbsp;(displaying "+prefs.getInt("maxEntries")+")</font>";
          }
          
          html+=("<br><table border=0 style='font-size:"+fontSize+"'>");          
           
          for (var i = 0; i < items.length; ++i) {
            var item = items[i];
            var title = item.title;
            var viewitem = item.viewItemURLForNaturalSearch;
            var timeLeft = item.timeLeft;

            if (null != title && null != viewitem) {
              html+="<tr>";
              var bidCount = item.bidCount;
    
              if (prefs.getBool("showPictures")) {
                if (item.galleryURL!=undefined && item.galleryURL.length>0) {
                  html+="<td><a style='text-decoration:none' target='_blank' href='"+viewitem+"'><img style='border:none' src='"+item.galleryURL+"'/></a></td>";
                } else {
                  html+="<td><i><a style='text-decoration:none' target='_blank' href='"+viewitem+"'>No<br>Image<br>Available</a></td>";
                }
              }
              
              html+="<td style='width:150px'><a target='_blank' href='"+viewitem+"'>"+title+"</a></td>";
                 
              if (item.bidCount==undefined) {
                html+="<td><img src='http:\/\/hosting.gmodules.com\/ig\/gadgets\/file\/105324078185059490343\/bin.gif'></td>";
              } else {
                html+="<td>"+item.bidCount+" Bids</td>";
              }
              
              var siteId = prefs.getInt("siteId");                  
              var curSymbol = getCurrencySym();             
              html+="<td>"+item.convertedCurrentPrice.currencyID+" "+curSymbol+item.convertedCurrentPrice.value+"</td>";        
              html+="<td>"+timeLeft+"</td></tr>";              
              html+="<tr><td colspan=5><div style='height:1px; margin:0px; font-size:0px; border-bottom:1px solid #ccc; margin-left:0px; margin-right:0px'></div></td></tr>";
            }
          }
                
          html+="</table>";                
          _gel("searchResult").innerHTML = html;
          drawSearchButton();
          _IG_AdjustIFrameHeight(prefs.getInt("height"));
        }

        function onFailure(errors) {
          _gel("searchResult").innerHTML = "";
          
          if (errors == null) {
            _gel("error").innerHTML = "An unknown error has occurred, please try again.";
            return;
          }
          
          var html = "An error has occurred:<br><br>";
          
          for (var i=0; i<errors.length; i++) {
            var error = errors[i];
            
            html+="error classification: "+error.errorClassification+
                  "<br>error code: "+error.errorCode+
                  "<br>message: "+error.longMessage+
                  "<br>severity: "+error.severityCode;
            
            if (error.errorParameters != null && error.errorParameters.length>0) {
              html+="<br>parameter detail:";           
            
              for (var j=0; j<error.errorParameters.length; j++) {
                html+="<br>&nbsp;&nbsp;id: "+error.errorParameters[j].ParamID+" value: "+error.errorParameters[j].value;
              }
            }
            
            if (errors.length>1 && i-1<errors.length) {
              html+="<br>";
            }
        
            if (!test) {
              _IG_Analytics("UA-523490-20", "\/"+prefs.getInt("siteId")+"\/error\/"+error.errorCode);
            }
          }
          
          _gel("error").innerHTML = html;
          drawSearchButton();
        }
        
        function drawSearchButton() {
          _gel("searchWaitText").innerHTML = "";          
          _gel("searchButtonSpan").innerHTML = "<input type='button' id='searchButton' onclick='doSearch()' value='search'/>";
          _gel("searchButton").style.fontSize = fontSize;
        }
        
        function drawSearchWait() {
          _gel("searchButtonSpan").innerHTML = "<img src='http://hosting.gmodules.com/ig/gadgets/file/105324078185059490343/ajax-loader.gif'/>";
          _gel("searchWaitText").style.fontSize = fontSize;
          _gel("searchWaitText").innerHTML = "searching ...";
        }
        
        function doSearch() {
            _gel("error").innerHTML = "";
            _gel("searchResult").innerHTML = "";
            drawSearchWait();            
            var searchTerm = trim(_gel("searchTerm").value);
            
            if (!test) {
              _IG_Analytics("UA-523490-20", "\/"+prefs.getInt("siteId")+"\/"+searchTerm+"\/"+prefs.getBool("runSearchOnStart"));
            }
            
            var itemSort = _gel("itemSort").options[_gel("itemSort").selectedIndex].value;
                        
            if (searchTerm.length == 0) {
              _gel("error").innerHTML = "please enter a search term";
              drawSearchButton();
              return;
            }
            
            var config = new com.ebay.shoppingservice.ShoppingConfig({siteId: prefs.getInt("siteId"), appId: 'GEOFFYAWORR7E7JAA898F49881MIK2', trackingId: 5336048799, trackingPartnerCode: 9});
            var shopping = new com.ebay.shoppingservice.Shopping(config);           
            var request = new com.ebay.shoppingservice.FindItemsRequestType({queryKeywords: searchTerm, maxEntries: prefs.getInt("maxEntries"), itemSort: itemSort });
            var callback = new com.ebay.shoppingservice.ShoppingCallback({success: onSuccess, failure: onFailure});
            shopping.findItems(request, callback);
        }

        function trim(stringToTrim) {          
          return stringToTrim.replace(/^\s+|\s+$/g,"");        
        }
        
        function getCurrencySym() {           
          if (siteId == 3) {
            return "£";
          } else if (siteId==16 || siteId==203 || siteId==205 || siteId==207
                     || siteId==211) {
            return "";
          } else {
            return "$";
          }
        }
      </script>
     
    ]]>
  </Content>
</Module>