﻿//-----------------------------------------------------------------------------
// XTemplate
//
// Copyright 2005-2010 - Xcential Group LLC.
//
//-----------------------------------------------------------------------------

//=============================================================================
// Constructor

function XTemplate(
   id,
   tabIndex,
   create
)
{
   id = (id == null) ? null : id;
   tabIndex = (tabIndex == null) ? null : tabIndex;
   create = (create == null) ? true : false;

   if (create)
      return new XTemplate(id, tabIndex, false);

   //--------------------------------------------------------------------------
   // Private Interface

   //--------------------------------------------------------------------------
   // Privileged Interface

   this.valueOf = function()
   {

      return oSpec;
   }

   //--------------------------------------------------------------------------

   this.setObjectValue = function(
      id,
      tabIndex
   )
   {
      id = (id == null) ? null : id;
      tabIndex = (tabIndex == null) ? null : tabIndex;

      oSpec = XTemplate.getSpecFrom(id, tabIndex);

      return oSpec;
   }

   //--------------------------------------------------------------------------
   // Initialization

   var oSpec = this.setObjectValue(id, tabIndex);

}

XTemplate.prototype.objectClass = "XTemplate";

//=============================================================================
// Static Interface

//-----------------------------------------------------------------------------

XTemplate.getSpecFrom = function(
   idOrRef,
   tabIndex
)
{
   idOrRef = (idOrRef == null) ? XUtils.generateId("urn:xcential-com:template:", XUtils.SCHEME_RANDOM) : idOrRef;
   tabIndex = (tabIndex == null) ? null : tabIndex;

   var spec = null;
   switch (typeof(idOrRef))
   {
      case "string":
         var id = idOrRef;
         spec = new Array();
         spec.id = id;
         spec.className = "XTemplate";
         spec.tabIndex = tabIndex;
         spec.htmlNode = null;
         spec.mouseIn = false
         spec.mouseOut = true;
         spec.visible = true;
         spec.left = null;
         spec.top = null;
         spec.right = null;
         spec.bottom = null;
         spec.width = null;
         spec.height = null;
         spec.shadowSize = 0;
         spec.quirksMode = false;
         break;
      case "number":
         throw XMsg("Cannot create a control from a number.");
         break;
      case "boolean":
         throw XMsg("Cannot create a control from a boolean.");
         break;
      case "function":
         try
         {
            return XTemplate.getSpecFrom(idOrRef(), tabIndex);
         }
         catch (error)
         {
            XApp.logEvent(XApp.EVENT_ERROR, error);
            throw XMsg("Cannot create a control from a boolean.", error);
         }
      case "object":
         if (idOrRef.objectClass != null)
         {
            spec = XTemplate.getSpecFrom(idOrRef.valueOf(), tabIndex);
         }
         else
         {
            spec = idOrRef;
            if (spec.className == null)
            {
               XApp.logEvent(XApp.EVENT_ERROR, error);
               throw XMsg("Cannot create a control from an invalid function or object.", error);
            }
         }
         break;
   }

   return spec;
}

//=============================================================================
// Public Interface

XTemplate.prototype.getPagerHTML = function(
   totalResults,
   itemsPerPage,
   startIndex,
   linkClass,
   baseRef,
   eventName,
   busy
)
{
   itemsPerPage = (itemsPerPage == null) ? 10 : itemsPerPage;
   startIndex = (startIndex == null) ? 1 : startIndex;
   linkClass = (linkClass == null) ? "Link" : linkClass;
   baseRef = (baseRef == null) ? null : baseRef;
   eventName = (eventName == null) ? null : eventName;
   busy = (busy == null) ? false : busy;

   var spec = this.valueOf();

   var tabIndex = spec.tabIndex;

   var totalPages = Math.ceil(totalResults / itemsPerPage);
   var currentPage = Math.floor((startIndex+itemsPerPage-2)/itemsPerPage)+1;
   currentPage = (currentPage > totalPages) ? totalPages + 1 : currentPage;
   var lowPage = ((currentPage - 2) > 0) ? currentPage - 2 : 1;
   var highPage = ((currentPage + 2) < totalPages) ? currentPage + 2 : totalPages;

   var ref = null;
   var event = null;
   if (XString(eventName).isSomething())
   {
      event = eventName + "(event, " + itemsPerPage + ", {startAt})";
   }
   else if (XString(baseRef).isSomething())
   {
      ref = baseRef;
      ref += (((/\?/).test(ref)) ? "&" : "?") + "itemsPerPage=" + itemsPerPage ;
      ref += "&startIndex={startAt}";
   }

   var pagerHTML = "";

   if (busy)
   {
      pagerHTML += "&#160<span " +
         "id=\"" + spec.id + ".busyMessage\" " +
         "class=\"PagerBusyMessage Busy\" " +
         "tabindex=\"-1\" " +
         ">" +
         "&#160;Refreshing...&#160;" +
      "</span>";
      return pagerHTML;
   }

   if (totalResults > itemsPerPage)
   {
      if (startIndex > 1)
      {
         var previousIndex = ((startIndex - itemsPerPage) > 1) ? startIndex - itemsPerPage : 1;
         pagerHTML += "<a " +
            "class=\"" + linkClass + "\" " +
            ((XString(ref).isSomething()) ? "href=\"" + XString(ref.replace(/{startAt}/, previousIndex)).encode(XString.ENCODE_ENTITY) + "\" " : "") +
            ((XString(event).isSomething()) ? "onclick=\"" + event.replace(/{startAt}/, previousIndex) + "\" " : "") +
            ((tabIndex != null) ? "tabindex=\"" + (tabIndex++) + "\" " : "") +
            "title=\"Previous page\" " +
         ">&lt;</a> ";
         pagerHTML += " <span class=\"Accent\">|</span>";
      }
      for (var i=lowPage; i<=highPage; i++)
      {
         if (i == currentPage)
         {
            pagerHTML += " <b>" + i + "</b> ";
            pagerHTML += (i<highPage) ? " <span class=\"Accent\">|</span>" : "";
         }
         else
         {
            var index = ((i-1) * itemsPerPage) + 1;
            pagerHTML += " <a " +
               "class=\"" + linkClass + "\" " +
               ((XString(ref).isSomething()) ? "href=\"" + XString(ref.replace(/{startAt}/, index)).encode(XString.ENCODE_ENTITY) + "\" " : "") +
               ((XString(event).isSomething()) ? "onclick=\"" + event.replace(/{startAt}/, index) + "\" " : "") +
               ((tabIndex != null) ? "tabindex=\"" + (tabIndex++) + "\" " : "") +
               "title=\"Page " + i + "\">" + i + "</a> ";
            pagerHTML += (i<highPage || (totalResults > (startIndex + itemsPerPage - 1))) ? " <span class=\"Accent\">|</span>" : "";
         }
      }
      if (totalResults > (startIndex + itemsPerPage - 1))
      {
         var nextIndex = (((currentPage-1)+1) * itemsPerPage) + 1;
         pagerHTML += " <a " +
            "class=\"" + linkClass + "\" " +
            ((XString(ref).isSomething()) ? "href=\"" + XString(ref.replace(/{startAt}/, nextIndex)).encode(XString.ENCODE_ENTITY) + "\" " : "") +
            ((XString(event).isSomething()) ? "onclick=\"" + event.replace(/{startAt}/, nextIndex) + "\" " : "") +
            ((tabIndex != null) ? "tabindex=\"" + (tabIndex++) + "\" " : "") +
            "title=\"Next page\" " +
         ">&gt;</a>";
      }

      if (event && (true || lowPage > 1 || highPage < Math.ceil(totalResults / itemsPerPage)))
      {
        pagerHTML += " <span class=\"Accent\">|</span> ";
        pagerHTML += "<input " +
            "class=\"PagerGoInput TextInput Input\" " +
            "type=\"text\" " +
            "name=\"" + spec.id + ".goInput\" " +
            "id=\"" + spec.id + ".goInput\" " +
            "style=\"" +
               "color: #BBBBBB; " +
            "\" " +
            "size=\"2\" " +
            ((tabIndex != null) ? "tabindex=\"" + (tabIndex++) + "\" " : "") +
            "onfocus=\"XUI('" + spec.id + ".goInput').clearTitle()\" " +
            "onblur=\"XUI('" + spec.id + ".goInput').setTitle('Page')\" " +
            "onkeydown=\"XUI.doKeyDown(event,'" + spec.id + ".goButton')\" " +
            "value=\"Page\" " +
         "/>";
         pagerHTML += "<img " +
            "class=\"PagerGoButton LinkImage\" " +
            "id=\"" + spec.id + ".goButton\" " +
            "src=\"/app/pkgs/xfw/images/gotoButton.gif\" " +
            ((tabIndex != null) ? "tabindex=\"" + (tabIndex++) + "\" " : "") +
            "title=\"Goto page\" " +
            "onkeydown=\"XUI.doKeyDown(event,'" + spec.id + ".goButton')\" " +
            "onclick=\"" + event.replace(/{startAt}/, "null") + "\" " +
         "/>";
         pagerHTML += " of " + Math.ceil(totalResults / itemsPerPage)
      }
   }

   return pagerHTML;
}

//=============================================================================

