/*
   Page: /include/javascript/recommendscripts.js

   Defines the functions to make ajax calls to specify recommendation

      Used in conjunction with the /forums/include/recommend_incl.jsp'

      Depends on some YUI scripts
*/

   /**
    @return true if we're successful; false if we're not, for example, if we need to redirect to login page
    */
   function recPrepareToAct(pElementId, pEncodedCurrentUrl, pRootContext)
   {
      var returnVal = false;
      if (isLoggedOutAvoidVisitor())
      {
//         if (window.opDebugWindow) debug("recPrepareToAct: about to redirect");
         // redirect to  login page
         window.location = pRootContext + "/login.jsp?uri=" + escape(pEncodedCurrentUrl);
/*
         if (isLoggedOutAvoidVisitor())
         {
            //if (window.opDebugWindow) debug("favPrepareToAct: about to redirect");
            // redirect to  login page
            window.location = "<pe:rootcontext/>/login.jsp?uri=<pe:rootcontext/><%= EncodeUtil.encode(FormUtil.getParameter(request, "returnUrl", "/index.jsp")) %>";
         }
         else
         {
*/

      }
      else
      {
         recWorking(pElementId);
         returnVal = true;
      }
      return returnVal;
   }

   var recWorking = function (pElementId)
   {
      document.getElementById("initiateOption" + pElementId).style.display = "none";
      document.getElementById("cancelOption" + pElementId).style.display = "none";
      document.getElementById("atWork" + pElementId).style.display = "";
   }

   var recFunctionFailure = function(o)
   {
      elementId = o.argument[1];
      alert('Unable to complete your request (status=' + o.status + '; o.argument=' + o.argument + '; statusText=' + o.statusText + ')');
      recWorking(elementId);
   }


   /**
    If the Ajax call is successful, then put the results
    */
   var recFunctionSuccess = function(o)
   {
      isRecommended = o.argument[0];
      elementId = o.argument[1];
  //    if (window.opDebugWindow) debug("recFunctionSuccess: o.argument=" + o.argument);
      document.getElementById("atWork" + elementId).style.display = "none";

      // If we're successful in initiating, then we don't want to show postive option anymore
      if (isRecommended == "true")
      {
//               if (window.opDebugWindow) debug("recommendPost: hide initiateOption");
         document.getElementById("initiateOption" + elementId).style.display = "none";
         document.getElementById("cancelOption" + elementId).style.display = "";
      }
      else
      {
  //             if (window.opDebugWindow) debug("recommendPost: hide cancelOption");
         document.getElementById("cancelOption" + elementId).style.display = "none";
         document.getElementById("initiateOption" + elementId).style.display = "";
      }
   }


   function recommendElement(pElementId, pIsRecommend, pEncodedCurrentUrl, pRootContext, pIsPost)
   {
      var callback =
      {
         success: recFunctionSuccess,
         failure: recFunctionFailure,
         argument: [pIsRecommend, pElementId]
      }

      //if (window.opDebugWindow) debug("recommendPost: top: elementId=" + pElementId + "; pIsRecommend=" + pIsRecommend);
      if (recPrepareToAct(pElementId, pEncodedCurrentUrl, pRootContext))
      {

         // Make the async Ajax call
         var urlToLoad = pRootContext + "/action/forums/recommendelement?elementID=" + pElementId +
                         "&recommended=" + pIsRecommend + "&post=" + pIsPost;
         var transaction = YAHOO.util.Connect.asyncRequest('GET', urlToLoad, callback, null);
      }
   }
