<?xml version="1.0" encoding="UTF-8" ?> 
<Module>
  <ModulePrefs title="Campus Directory" 
               title_url="http://www.telecom.binghamton.edu/directory/directory.search"
               description="Version: 1.1: Binghamton University's Campus Directory.  Displays up to 15 listings with name and campus phone number.  Includes a quick link to send email.  Requires a Binghamton University BMail Account."
               screenshot="http://www.telecom.binghamton.edu/images/gdirectory.png"
               thumbnail="http://www.telecom.binghamton.edu/images/gdirectory_thumb.png"
               scrolling="true"
               author="Scott Geiger"
               author_email="geigers@binghamton.edu"/>
  <UserPref name="num_rows" display_name="Show Rows:" datatype="enum" default_value="15">
    <Enumvalue value="1" display_value="1" />  
    <Enumvalue value="2" display_value="2" />  
    <Enumvalue value="3" display_value="3" />  
    <Enumvalue value="4" display_value="4" />  
    <Enumvalue value="5" display_value="5" />  
    <Enumvalue value="6" display_value="6" />  
    <Enumvalue value="7" display_value="7" />  
    <Enumvalue value="8" display_value="8" />  
    <Enumvalue value="9" display_value="9" />  
    <Enumvalue value="10" display_value="10" />  
    <Enumvalue value="11" display_value="11" />  
    <Enumvalue value="12" display_value="12" />  
    <Enumvalue value="13" display_value="13" />  
    <Enumvalue value="14" display_value="14" />  
    <Enumvalue value="15" display_value="15" />  
  </UserPref>
  <Content type="html">
<![CDATA[ 
  <style type="text/css">
    div#wrapper { padding: 2px; font-size: 12px; }
    div#wrapper form input { font-size: 12px; }
    div#wrapper form input.button { padding: 0;  font-size: 10px; }
    div#results { border: 1px solid #C1C1C1; }
    div#results p { margin:0; padding: 2px 0 0 2px; }
    .altrow { background-color: #DEDEDE; }
    .image_link { border: 0; vertical-align: bottom; } 
    .AccordionTitle, .AccordionContent, .AccordionContainer {
      position:relative;
    }
    .AccordionTitle {
      margin: 0;
      padding: 1px;
      overflow:hidden;
      cursor:pointer;
      font-size:12px;
      display:table-cell;
      -moz-user-select:none;
    }
    .AccordionTitle img {
      margin: 0;
      padding: 1px;
      vertical-align: abs-middle;
      height: 9px;
      width: 9px;
    }
    .AccordionContent {
      height:0px;
      overflow:hidden;
      display:none;
    }
    .AccordionContainer
    {
      margin: 0;
      padding: 2px;
      border-top: solid 1px #C1C1C1;
      border-bottom: solid 1px #C1C1C1;
      border-left: solid 2px #C1C1C1;
      border-right: solid 2px #C1C1C1;
    }
  </style>

  <script type="text/javascript">      

    var ContentHeight = 48;
    var TimeToSlide = 180.0;
    var openAccordion = '';

    function runAccordion(index) {
      var nID = "Accordion" + index + "Content";
      if(openAccordion == nID)
        nID = '';
   
      setTimeout("animate(" + new Date().getTime() + "," + TimeToSlide + ",'" + openAccordion + "','" + nID + "')", 33);
      openAccordion = nID;
    }

    function animate(lastTick, timeLeft, closingId, openingId) {  
      var curTick = new Date().getTime();
      var elapsedTicks = curTick - lastTick;
     
      var opening = (openingId == '') ? null : document.getElementById(openingId);
      var closing = (closingId == '') ? null : document.getElementById(closingId);
     
      if(timeLeft <= elapsedTicks)
      {
        if(opening != null)
        {
          opening.style.height = ContentHeight + 'px';
          document.getElementById('accimage').innerHTML = "<img src=\"" + images[6].src + "\" />";
        }       

        if(closing != null)
        {
          closing.style.display = 'none';
          closing.style.height = '0px';
          document.getElementById('accimage').innerHTML = "<img src=\"" + images[5].src + "\" />";
        }
        return;
      }
     
      timeLeft -= elapsedTicks;
      var newClosedHeight = Math.round((timeLeft/TimeToSlide) * ContentHeight);
    
      if(opening != null)
      {
        if(opening.style.display != 'block')
          opening.style.display = 'block';
        opening.style.height = (ContentHeight - newClosedHeight) + 'px';
      }
     
      if(closing != null)
        closing.style.height = newClosedHeight + 'px';
    
      setTimeout("animate(" + curTick + "," + timeLeft + ",'"
          + closingId + "','" + openingId + "')", 33);
    }

    function doSearch() {
      var f = document.getElementById('search_form');
      var v_search = encodeURIComponent(f.search.value);
      var url = "http://www.telecom.binghamton.edu/directory/directory.results?output=xml&provider=google&search=" + v_search;

      //_IG_FetchXmlContent(url, function (response) {
      var params = {};
      params["CONTENT_TYPE"] = gadgets.io.ContentType.DOM;
      gadgets.io.makeRequest(url, callback, params); 
    }

    function callback(obj) {
        var response = obj.data;
        if (response == null || typeof(response) != "object" || response.firstChild == null) {
           document.getElementById("content_div").innerHTML = "<i>Invalid data.</i>";
           return;
        }  

        // Get userprefs
        var prefs = new gadgets.Prefs();
        var nr = prefs.getInt("num_rows");

        var html = "";
        var cnt = response.getElementsByTagName("ENTRY").length;
        if (cnt > 0) {
          if (nr > cnt) { nr = cnt; } 
          html += "<i>Showing " + nr + " rows of " + cnt + "</i>\n";
          html += "<div id=\"results\">\n";
    
          // Get a list of the <ENTRY> element nodes in the file
          var itemList = response.getElementsByTagName("ENTRY");
          
          // Loop through all <ENTRY> nodes
          for (var i = 0; i < nr ; i++) { 
            // For each <ENTRY> node, get child nodes.
            var nodeList = itemList.item(i).childNodes;
        
            // Loop through child nodes.
            var link_id = "";
            var dir_type = "Other";
            var name = "&nbsp;";
            var dn = "&nbsp;";
            var email = "";
            for (var j = 0; j < nodeList.length ; j++) {
              var node = nodeList.item(j);
              if (node.firstChild != null) {
                switch(node.nodeName) {
                  case "ID":         link_id  = node.firstChild.nodeValue; break;
                  case "DIR_TYPE":   dir_type = node.firstChild.nodeValue; break;
                  case "NAME":       name     = node.firstChild.nodeValue; break;
                  case "FIRST_NAME": name     = node.firstChild.nodeValue; break;
                  case "LAST_NAME":  name     = name + " " + node.firstChild.nodeValue; break;
                  case "DIR_NUMBER": dn       = node.firstChild.nodeValue; break;
                  case "EMAIL":      email    = node.firstChild.nodeValue; break;
                  default: break;
                }
              }
            }       
            // Append extracted data to the HTML string.
            var dtype = "";
            switch(dir_type){
              case "Faculty/Staff": dtype = "<img title=\"Faculty/Staff\" alt=\"F\" src=\"" + images[1].src + "\" />"; break;
              case "Department":    dtype = "<img title=\"Department\" alt=\"D\" src=\"" + images[3].src + "\" />"; break;
              case "Student":       dtype = "<img title=\"Student\" alt=\"S\" src=\"" + images[0].src + "\" />"; break;
              default:              dtype = "<img title=\"Other\" alt=\"O\" src=\"" + images[2].src + "\" />";
            }
            html += "<p";
            if (i % 2 == 1 ) {
              html += " class=\"altrow\"";
            }
// see http://userscripts.org/scripts/review/10396
            html += "><span class=\"icon\">" + dtype + "</span> " + name;
            if (email != "") {
              html += " <a href=\"javascript:sendTo('" + email + "');\"> <img title=\"" + email + "\" class=\"image_link\" src=\"" + images[4].src + "\" /></a>";
            }
            html += "<br /><span style=\"width: 16px\">&nbsp;</span>" + dn + "</p>\n"; 
          }
          html += "</div>\n";
        } else {
          html += "<i>No Data Found</i>\n";
        } // end: if (cnt > 0) { 
        html += "</div>\n";

      // Display HTML string in <div>
      document.getElementById('content_div').innerHTML = html;
      runAccordion(1);
    }

    function sendTo(email) {
      var url="https://mail.google.com/a/binghamton.edu/?view=cm&tf=1&ui=2&fs=1&to=" + email;
      window.open(url,"compose","height=560,width=780"); 
    }
  
    // Preload the images using gadgets.io.getProxyUrl()
    function load(imageList) {
      var ret = [];
      for (var i = 0, j = imageList.length; i < j; ++i) {
        var img = document.createElement("img");
        img.src = gadgets.io.getProxyUrl(imageList[i]);
        ret.push(img);
      }
      return ret;
    }

    var files = [
    "http://www.telecom.binghamton.edu/images/pencil-ico.png",
    "http://www.telecom.binghamton.edu/images/book-ico.png",
    "http://www.telecom.binghamton.edu/images/gear-ico.png",
    "http://www.telecom.binghamton.edu/images/assoc-ico.png",
    "http://www.telecom.binghamton.edu/images/email.png",
    "http://www.telecom.binghamton.edu/images/add.png",
    "http://www.telecom.binghamton.edu/images/subtract.png"
    ];

    var images = load(files);
    runAccordion(1);
  </script>
 
  <div id="wrapper"> 
    <div id="AccordionContainer" class="AccordionContainer">
      <div onclick="runAccordion(1);">
        <div class="AccordionTitle" onselectstart="return false;">
          <span id="accimage"></span> Search:
        </div>
      </div>
      <div id="Accordion1Content" class="AccordionContent">
        <form id="search_form" action="javascript:doSearch();" method="GET">
          <span class="formElements"><input type="text" name="search" /> <input class="button" type="submit" name="" value="Search" /></span>
        </form>
      </div>
    </div>
    <div id="content_div"></div>
  </div><!-- end div#wrapper -->

  ]]> 
  </Content>
</Module>
