//event binding occurs once at ProductIndex.aspx

function CommentInitializeRequestHandler(sender, args)
{   
   var postElem = args.get_postBackElement();
   if (globalComments.IsCommentsLink(postElem))
   {
      globalComments.Initialize(postElem, args);
   }   
}

function CommentBeginRequestHandler(sender, args)
{
   var postElem = args.get_postBackElement();
   if (globalComments.IsCommentsLink(postElem))
   {
      globalComments.OnBeginRequest();      
   }
   if (globalComments.IsContentRefreshingLink(postElem))
   {
      globalComments.ClearCurrent();
   }
}

function EndRequestHandler(sender, args)
{
   if (globalComments.waitingForPostbackResult)
   {
      globalComments.OnEndRequest();   
   }
}

var globalComments = new CommentSectionHandler();

function CommentSectionHandler()
{
   var CommentProgressElemId;
   var newCommentsElemId;
   var currentlyOpenCommentsElemId;
   var openLinkId;
   var closeLinkId;
   var oldOpenLinkId;
   var oldCloseLinkId;
   
   this.waitingForPostbackResult = false;
   
   this.IsCommentsLink = function(element)
   {
      if (element != null)
      {
         if ((element.id.indexOf('lnkComments') != -1)||(element.id.indexOf('lnkHideComments') != -1))
         {
            return true;
         }
      }
      return false;
   }
   
   this.IsContentRefreshingLink = function(element)
   {
      if (element != null)
      {
         if ((element.id.indexOf('listPager') != -1) || (element.id.indexOf('drpPostBackTarget') != -1))
         {
            return true;
         }
      }
      return false;
   }
   
   this.Initialize = function(postElem, args)
   {
      if (postElem.id.indexOf('lnkComments') != -1)
      {
         if (currentlyOpenCommentsElemId != null)
         {
            oldOpenLinkId  = openLinkId;
            oldCloseLinkId = closeLinkId;
         }
         openLinkId = postElem.id;
         closeLinkId = postElem.attributes["hidelinkid"].value;
         CommentProgressElemId = 'loadingProgress' + postElem.attributes["rowindex"].value;
         newCommentsElemId = 'comments' + postElem.attributes["rowindex"].value;
      }
      if (args != null)
      {
         this.HandleIfClickedCurrent(args);
      }
   }
   
   this.HandleIfClickedCurrent = function(args)
   {
      var comments = $(newCommentsElemId);
      
      //I'm already here     
      if (comments.innerHTML.trim() != "") 
      {
         args.set_cancel(true)
         
         if (((!($(openLinkId)).disabled)&&(!($(closeLinkId)).disabled))||(newCommentsElemId != currentlyOpenCommentsElemId))
         {
            if ((newCommentsElemId == currentlyOpenCommentsElemId)&&(comments.style.display != 'none'))
            {
               this.CloseComments(comments);
            }
            else
            {
               this.OpenComments(comments);
            }
         }
      }
   }
   
   this.ShowProgress = function()
   {
      //($(CommentProgressElemId)).style.display = 'inline';
   }
   
   this.OnBeginRequest = function()
   {
      this.waitingForPostbackResult = true;
      this.ShowProgress();
   }
   
   this.OnEndRequest = function()
   {
      this.HandlePopulatedComment();
   }
   
   this.HandlePopulatedComment = function()
   {
      //($(CommentProgressElemId)).style.display = 'none';
      this.CommentProgressElemId = null;
      this.OpenComments($(newCommentsElemId));
      this.waitingForPostbackResult = false;
   }
   
   this.DisplayHideLink = function(open, close)
   {
      ($(open)).style.display = 'none';
      ($(close)).style.display = 'inline';
   }
   
   this.DisplayShowLink = function(open, close)
   {
      ($(open)).style.display = 'inline';
      ($(close)).style.display = 'none';
   }
   
   this.DisableOpen = function()
   {
      ($(openLinkId)).disabled = true;
      ($(closeLinkId)).disabled = false;  
   }
   
   this.DisableClose = function ()
   {
      ($(openLinkId)).disabled = false;
      ($(closeLinkId)).disabled = true;  
   }

   this.OpenComments = function(comments)
   {
      if ((comments.style.display != 'none') && (comments.style.height.replace('px','') < 100))
      {
         comments.style.height = '0px';
      }
      window.setTimeout(function(){globalComments.DisableOpen();}, 5);
      
      var oldElem;
      if ((currentlyOpenCommentsElemId != null) && (currentlyOpenCommentsElemId != comments.id))
      {
         oldElem = $(currentlyOpenCommentsElemId);
         this.DisplayShowLink(oldOpenLinkId, oldCloseLinkId);
      }
      currentlyOpenCommentsElemId = comments.id;
      
      globalAnimObject = new SlideAnimation(comments, oldElem, 0, 270);         
      globalAnimObject.onEnd = function() {  
                                             if (globalAnimObject != null)
                                             {
                                                if (globalAnimObject.completed) 
                                                {
                                                   globalComments.DisplayHideLink(openLinkId, closeLinkId);
                                                   globalComments.EnableLinks();
                                                   globalAnimObject = null;
                                                }
                                             }
                                          };
      globalAnimObject.Run();
   }
   
   this.EnableLinks = function()
   {
      ($(openLinkId)).disabled = false;
      ($(closeLinkId)).disabled = false;
   }
   
   this.OnCloseComplete = function()
   {  
      if (globalAnimObject != null)
      {    
         if (globalAnimObject.completed)
         {
            globalComments.EnableLinks();
            globalComments.DisplayShowLink(openLinkId, closeLinkId);
            globalAnimObject = null;
         }  
      }
      currentlyOpenCommentsElemId = null;
   }
   
   this.CloseComments = function(comments, callback)
   {  
      window.setTimeout(function(){globalComments.DisableClose();}, 5);  
      
      globalAnimObject = new SlideAnimation(comments, null, 270, 0);
      globalAnimObject.onEnd = function() {  
                                             globalComments.OnCloseComplete();
                                          };
      globalAnimObject.Run();  
   }
   
   this.ClearCurrent = function()
   {
      currentlyOpenCommentsElemId = null;
   }
}

var globalAnimObject;

function SlideAnimation(element, openElementToClose, startHeight, targetHeight){
   var m_tickrate = 10;
   var m_element = element;
   var m_oldElement = openElementToClose;
   var m_velocityStart = 3;
   var m_velocityEnd = 1.2;
   var m_startHeight = startHeight;   
   var m_targetHeight = targetHeight;
   var m_deltaV = (m_velocityStart - m_velocityEnd)/
                  (m_targetHeight - m_startHeight);
   var m_direction = ((m_targetHeight - m_startHeight) > 0)?1:-1;
   var m_v = m_velocityStart;
   var m_currentHeight = m_startHeight;
   
   this.started = false;
   this.completed = false;
   
   element.style.height = m_startHeight + 'px';
   if (element.style.display == 'none')
   {
      element.style.display = 'block';
   }
   if (openElementToClose != null)
   {
      if (openElementToClose.style.display == 'none')
      {
         openElementToClose.style.display = 'block';
      }
   }
   
//   this.HideElement = function(elementId)
//   {
//      ($(elementId)).style.display = 'none';
//   }
   
   this.Run = function()
   {
      this.started = true;
      
      var step =  Math.pow(m_v, 4) * m_direction; //m_v * m_direction; //normalize
      m_v -= step * m_deltaV;
      
      m_currentHeight = m_currentHeight + step;
      
      if (m_currentHeight > 0)
      {
         m_element.style.height = m_currentHeight + 'px';
      }
      else
      {
         m_element.style.height = '0px';
      }
      
      if (m_oldElement != null)
      {
         if ((m_targetHeight - m_currentHeight) > 0)
         {
            m_oldElement.style.height = (m_targetHeight - m_currentHeight) + 'px';
         }
         else
         {
            m_oldElement.style.height = '0px';
         }
      }
      
      var condition = (m_direction == 1)?(m_currentHeight <= m_targetHeight):
                                         (m_currentHeight >= m_targetHeight)
      
      if (condition)
      {
          window.setTimeout('if (globalAnimObject != null) globalAnimObject.Run()', m_tickrate);
      }
      else
      {
         if (m_oldElement != null)
         {
            m_oldElement.style.display = 'none';
         }
         if (m_element.style.height == '0px')
         {
            m_element.style.display = 'none';
         }
         if (this.onEnd != null)
         {
            this.started = false;
            this.completed = true;
            this.onEnd();
         }
      }
   }           
   
   this.onEnd;
}

function ValidateNewComment(elemId)
{
   var textbox = elemId; 
   if (textbox.value.trim() == '') 
   { 
      textbox.className='input-error'; 
      textbox.focus();  
      return false; 
   }
   return true;
}