
var ClassroomResources;
if (ClassroomResources === undefined) {
    ClassroomResources = {};
}

// LinkExchange user name and userid for Classroom Resources Team. 
// LinkExchange.ajaxInterface screens out all tags that are not applied by
// ClassroomResources.Tagger. The id is used to construct LinkEx tagpair queries
ClassroomResources.Tagger = {name:"Classroom Resources Team", id:1179};

// The list of standard tags. LinkExchange.ajaxInterface screens out all tags that are
// not in this list.
ClassroomResources.TagInfo = {
    // Topics
    "communications systems": {type:"topic", label:"Communications Systems"},
    "computational biology":  {type:"topic", label:"Computational Biology"},
    "computational finance":  {type:"topic", label:"Computational Finance"},
    "computational methods":  {type:"topic", label:"Computational Methods"},
    "control systems":        {type:"topic", label:"Control Systems"},
    "digital signal processing":       {type:"topic", label:"Digital Signal Processing"},
    "embedded systems":                {type:"topic", label:"Embedded Systems"},
    "image and video processing":      {type:"topic", label:"Image and Video Processing"},
    "measurement and instrumentation": {type:"topic", label:"Measurement and Instrumentation"},
    "numerical and symbolic math":     {type:"topic", label:"Numerical and Symbolic Math"},
    "programming and computer science":{type:"topic", label:"Programming and Computer Science"},
    "project based learning":          {type:"topic", label:"Project-Based Learning"},
    "robotics and mechatronics":       {type:"topic", label:"Robotics and Mechatronics"},
    "statistics and data analysis":    {type:"topic", label:"Statistics and Data Analysis"},
    "system modeling and simulation":  {type:"topic", label:"System Modeling and Simulation"},

    // Departments
    "aerospace engineering":         {type:"department", label:"Aerospace Engineering"},
    "biological and health sciences":{type:"department", label:"Biological and Health Sciences"},
    "biomedical engineering":        {type:"department", label:"Biomedical Engineering"},
    "business economics and finance":{type:"department", label:"Business, Economics, and Finance"},
    "chemical engineering":          {type:"department", label:"Chemical Engineering"},
    "chemistry":                     {type:"department", label:"Chemistry"},
    "civil and environmental engineering":  {type:"department", label:"Civil & Environmental Engineering"},
    "computer science":                     {type:"department", label:"Computer Science"},
    "earth atmospheric and ocean sciences": {type:"department", label:"Earth, Atmospheric, and Ocean Sciences"},
    "electrical and computer engineering":  {type:"department", label:"Electrical and Computer Engineering"},
    "industrial and manufacturing engineering":{type:"department", label:"Industrial & Manufacturing Engineering"},
    "materials sciences":      {type:"department", label:"Materials Sciences"},
    "mathematics":             {type:"department", label:"Mathematics"},
    "mechanical engineering":  {type:"department", label:"Mechanical Engineering"},
    "physics and astronomy":   {type:"department", label:"Physics and Astronomy"},
    "psychology":              {type:"department", label:"Psychology"},
    "statistics":              {type:"department", label:"Statistics"},

    // Resource Types
    "downloadable code": {type:"resourcetype", label:"Downloadable code or models"},
    "course materials":  {type:"resourcetype", label:"Course materials"},
    "textbook":  {type:"resourcetype", label:"Textbook"},
    "tutorial":  {type:"resourcetype", label:"Tutorial"},
    "video":     {type:"resourcetype", label:"Video"},
    "technical literature":  {type:"resourcetype", label:"Technical literature"},
    "mw article":  {type:"resourcetype", label:"MathWorks Article"}
};


// Converts a page URL to a filterSettings structure. Sample usage:
//   decodeFilterSettingsFromUrl(window.location.toString());
ClassroomResources.decodeFilterSettingsFromUrl = function (url, defaultSettings) {
   var i, pp, parameter,
       searchStr = url.substring(url.indexOf("?")).substr(1),
       paramValuePairs = searchStr.split('&'),
       numPairs = paramValuePairs.length,
       settings = defaultSettings;

   for (i = 0; i < numPairs; i++) {
      pp = paramValuePairs[i].split('=');
      parameter = decodeURIComponent(pp[0]);
      // only allow parameters that are specified in defaultSettings
      if (defaultSettings.hasOwnProperty(parameter)) {
         settings[parameter] = decodeURIComponent(pp[1]).replace(/\+/g, " ");
      }
    }
   return settings;
};
  

// Converts a filterSettings structure to a URL query string. It *does* not include
// the url pathname (everything up to & including the "?").
ClassroomResources.encodeFilterSettingsForUrl = function (filterSettings) {
   var setting,
       queryString = '?';
   for (setting in filterSettings) {
     if (filterSettings.hasOwnProperty(setting)) {
        queryString += encodeURIComponent(setting) + '=' + encodeURIComponent(filterSettings[setting]) + '&';
     }
   }
   return queryString;
};


// ClassroomResources.SimpleQuery
// Wrapper for LinkExchange.ajaxInterface. This currently doesn't do much, but it
// provides an abstraction layer, so in the future if we want more complicated
// behavior (e.g., want to pump two queries together into a single stream)
// we would implement that here.
ClassroomResources.SimpleQuery = function (myDataCallback, myLinkExSpec) {

   var customLinkExParams = {"sort":"title", "dir":"asc"};
   var myLinkExAjax = LinkExchange.ajaxInterface(myLinkExSpec, customLinkExParams);
   var publicAPI = {};
   var myCurrentPage = 1;

   publicAPI.getFirstPage = function(queryOpts) {
       if (queryOpts !== undefined) {
          myLinkExAjax.setQuery( queryOpts );
       }
       myLinkExAjax.getFirstResultSet( myDataCallback );
       myCurrentPage = 1;
   };

   publicAPI.getPreviousPage = function () {
      if (myCurrentPage === 2) {
        publicAPI.getFirstPage();
      }
      else {
        myLinkExAjax.getPreviousResultSet(myDataCallback);
        myCurrentPage = myCurrentPage - 1;
      }
   };

   publicAPI.getNextPage = function () {
      if (myLinkExAjax.getNextResultSet(myDataCallback)) {
         myCurrentPage = myCurrentPage + 1;
      }
   };

   publicAPI.getPageInfo = function () {
      return  myLinkExAjax.getPageInfo();
   };

   publicAPI.isLastPage = function () {
      var pageInfo = myLinkExAjax.getPageInfo();
      // using ">=" instead of "===": when totalEntries==0, totalResultSets will be 0, but currentResultSet will be 1
      return (pageInfo.currentResultSet >= pageInfo.totalResultSets);
   };

   publicAPI.isFirstPage = function () {
      return (myCurrentPage === 1);
   };

   return publicAPI;
};

