/**
 * n13v8.js
 * $Revision: 1.6 $
 *
 *  . LIMITED USE PILOT COMPONENT ONLY NOT FOR MASS PRODUCTOIN
 *  . generates an automatic 1.x listing from crs service JSON
 *  . uses /web/fw/j/jsTemplate.js
 *
 *   (c) 1992-2008 Cisco Systems, Inc. All rights reserved.
 *   Terms and Conditions: http://cisco.com/en/US/swassets/sw293/sitewide_important_notices.html'
 *
 **/


/*   this goes into component script */
if (typeof(cdc)                 == "undefined") cdc = {};
if (typeof(cdc.lowLevelListing) == "undefined") cdc.lowLevelListing = {
  /***************** Mandatory Search Service Params ****************/
  // pagelevel          
	serverName        : "tools.cisco.com",
	entitlement       : "Required: Please specify Entitlement",
	listingname       : "lll",
	styleTemplateName : this.autoListingTmplSrc,
  // listinglevel
	documentType      : "Required: Please specify Doctype",

  /***************** Query object ****************/
  queryUrl          : function (aObj) {
    var url = "";
    // required service url bits
    url += 'http://';
    url += aObj.serverName || this.serverName;
    url += '/support/cwsx/crs/service/csf/crs/';
    url += aObj.entitlement || this.entitlement;
    if (this.hasVal(aObj.categoryName)) { // category name is optional?
      url += '/';
      url += aObj.categoryName;
    }
    url += '?';
    url += 'cdcdoctype=';
    url += aObj.documentType || this.documentType;
    url += '&listingname=';
    url += aObj.listingname || this.listingname;

    // optional params
    if (this.hasVal(aObj.dateRange)) {
      url += '&cdcdate=';
      url += aObj.dateRange;
    }
    if (this.hasVal(aObj.output)) {
      url += '&output=';
      url += aObj.output;
    }
    if (this.hasVal(aObj.sortlistingby)) {
      url += '&sortlistingby=';
      url += aObj.sortlistingby;
    }
    if (this.hasVal(aObj.listingSize)) {
      url += '&listingsize=';
      url += aObj.listingSize;
    }
    // used by jQuery...do not modify
    url += '&callback=?';
    return url;
  },

  /************************* JS Template ****************/
  // template for auto listing
  autoListingTmplSrc : '\
    <ul class="resource">\
    <% var docsArray = crsresponse.allDocs[0].docs;\
       var linkTitleRxp = /\s*\\[.*\\]/g;\
       for(i=0; i < docsArray.length; i++){ \
       var getlinkClass = function(str){\
          if (docsArray[i].contentType == "application/pdf" || str.indexOf(".pdf") >= 0) {\
            return "pdf";\
          } else if (str.indexOf(".zip") >= 0) {\
            return "zip";\
          } else {\
            return "web";\
          };\
        };\
        var linkClass = getlinkClass(docsArray[i].url);\
        var linkTitle = docsArray[i].title %>\
        <li class="${ linkClass }">\
          <h4>\
            <a href="${ docsArray[i].url }" target="_blank">${ linkTitle.indexOf("[") >=0 ? linkTitle.replace(linkTitleRxp,"") : linkTitle }</a>\
            <span class="fileinfo"><% if (linkClass != "web") { %>\
                (${linkClass.toUpperCase(),cdc.lowLevelListing.showFileSize(docsArray[i].size)})\
            <% } %></span>\
          </h4>\
        </li>\
      <% }; %>\
    </ul>',
  // create new tempalte object
  autoListingTemplate : function() {
    return new cdc.util.JsTemplate(this.autoListingTmplSrc);
  },
  /***************** updateElement(element,paramHash) ****************/
  // do the ajax call and populate the element with template output
  updateElement : function (element, paramHash) {
    var serviceQuery = cdc.lowLevelListing.queryUrl(paramHash);
    jQuery.getJSON  (serviceQuery, null, function (data) {
      element.innerHTML = cdc.lowLevelListing.autoListingTemplate().process (data);
    });
  },
  /***************** showFileSize(int) ****************/
  // formats a file size for display
  showFileSize : function(val){
    if (typeof(val) == 'undefined' || val == 0) {
      return;
    };
    var sizeLabel,sizeQuot = "";
    var d = val.toString().length;
    switch(true) { // compares case evals to switch expression
      case d >= 4 && d < 7:
        sizeLabel = "KB";
        sizeQuot = 1000;
        break;
      case d >= 7 && d < 10:
        sizeLabel = "MB";
        sizeQuot = 1000000;
        break;
      case d >= 10 && d < 13:
        sizeLabel = "GB";
        sizeQuot = 1000000000;
        break;
      default:
        sizeLabel = "B";
        sizeQuot = 1;
    }
    return " - " + Math.round(val/sizeQuot) + " " + sizeLabel;
  },
  /***************** hasVal(thing) ****************/
  // returns true if thing is not undefined or empty
  hasVal : function (thing) {
    if (thing && (thing != 'undefined' || thing != "")) {
      return true;
    } else {
      return false;
    }
  }
};


jQuery(document).ready(function(){
  jQuery.getScript('/web/fw/j/jsTemplate.js',function(){
     jQuery('.n13v8-pilot').each(function(){
       var thisHash = {};
       jQuery(this).children().each(function(){
         theParam = this.className.split(' ')[1];
         theValue = this.innerHTML;
         thisHash[theParam] = theValue;
       });
       cdc.lowLevelListing.updateElement (this, thisHash); // move to namespace
     });
  });
});
