function initMemberfunctions() {
  if(memberBlock = $$('div.content.side div.panel.memberfunction')[0]) {
    if(betygUl = $$('div.content.side div.panel.memberfunction ul.betyg')[0]) {
      if(betygLi = $$('div.content.side div.panel.memberfunction ul.betyg li a')) {
        var godisId = betygLi[0].readAttribute('href');
        idRegEx = /^\/godis\/namn\/(\d+).*$/i;
        godisId = godisId.match(idRegEx)[1];
        params = {
          godisId: godisId
        }
        url = '/includes/currentRating.php';
        new Ajax.Request(
          url,
          {
            onSuccess: function(response) {
              setCurrentRating(response.responseText,godisId);
            },
            parameters: params
          }
        );
      }
    }
 
    if(commentForm = $$('div.content.side div.panel.memberfunction form')[0]) {
      commentForm.onsubmit = function() {
        commentField = $$('div.content.side div.panel.memberfunction form textarea')[0];
        if (commentField.value == '') {
          commentField.highlight({startcolor:'#ff0000',endcolor:'#ffffff'});
        } else {
          reloadCommentsList(commentField);
        }
        return false;
      }
    }

    url = '/includes/checkLoginAjax.php';
    new Ajax.Request(
      url,
      {
        onSuccess: function(response) {
          if(response.responseText != 'true') {
            if(memberBlock = $$('div.content.side div.panel.memberfunction')[0]) {
              if(!(myP = $$('p.infoMsgAjaxNoClose')[0])) {
                var myP = new Element('p', { 'class':'infoMsgAjaxNoClose' }).update("Du måste vara inloggad för att komma åt medlemsfunktionerna.");
                myP.hide();
                blockerDiv = new Element('div', {'class':'blockerDiv'});
                blockerHeight = memberBlock.getHeight() + 10;
                blockerWidth = memberBlock.getWidth();
                blockerDiv.setStyle({'width':blockerWidth+'px','height':blockerHeight+'px'});
                memberBlock.insert({'before':blockerDiv});
                memberBlock.insert({'before':myP});
                memberBlock.appear({duration: 0.3, from: 1.0, to: 0.2});
                myP.appear({duration: 0.3});
              }
            }
          } else {
           if(errorMsg = $$('div.content.side p.infoMsgAjaxNoClose')[0]) {
            if(memberBlock = $$('div.content.side div.panel.memberfunction')[0]) {
              if(blockerDiv = $$('div.blockerDiv')[0]) {
                blockerDiv.remove();
              }
              memberBlock.appear({duration: 0.2});
              errorMsg.fade({duration: 0.2});
              errorMsg.remove();
            }
          }       
          }
        }
      }
    );
  }
}

function getPos(e) {
  pointerOffsetX = -75;
  pointerOffsetY = -70;
  posY = (parseInt(Event.pointerY(e))+pointerOffsetY)+'px'; 
  posX = (parseInt(Event.pointerX(e))+pointerOffsetX)+'px';
  showRatingMsg(this,this.readAttribute('title'),posX,posY);
  this.writeAttribute('title','');
}

function setCurrentRating(rating,godisId) {
  betygLi = $$('div.content.side div.panel.memberfunction ul.betyg li a');
  for(i=0, l = betygLi.length; i < l; i++) {
    betygLi[i].update();
    if(i < rating) {
      betygLi[i].setStyle({backgroundImage:"url('/images/betygIkon.png')"});
    } else {
      betygLi[i].setStyle({backgroundImage:"url('/images/betygIkonBlank.png')"});
    }
    var setOnclick = function(betygLi,i,godisId) {
      betygLi[i].onclick = function() {
        setNewRating(i+1,godisId);
        return false;
      }
    }
    setOnclick(betygLi,i,godisId);
    
    Event.observe(betygLi[i],'mouseover',getPos);
    
    var setOnmouseover = function(betygLi,i) {
      betygLi[i].onmouseover = function() {
          for(j=0; j<betygLi.length; j++) {
            betygLi[j].setStyle({backgroundImage:"url('/images/betygIkonBlank.png')"});
          }
          for(j=0; j<=i; j++) {
            betygLi[j].setStyle({backgroundImage:"url('/images/betygIkon.png')"});
          }
      }
    }
    setOnmouseover(betygLi,i);
    
    var setOnmouseout = function(betygLi,i) {
      betygLi[i].onmouseout = function() {
        for(j=0; j<=i; j++) {
          betygLi[j].setStyle({backgroundImage:"url('/images/betygIkonBlank.png')"});
        }        
        if(ratingMsg = $$('p.ratingMsg')[0]) {
          this.writeAttribute('title',ratingMsg.innerHTML);
          Event.stopObserving(ratingMsg,'mousemove');
          ratingMsg.remove();
        }
      }
    }
    setOnmouseout(betygLi,i);
  }
  $$('div.content.side div.panel.memberfunction ul.betyg')[0].onmouseout = function() {
    restoreRating(betygLi, rating);
  }
}

function restoreRating(betygLi, rating) {
  for(i=0, l = betygLi.length; i < l; i++) {
    if(i < rating) {
      betygLi[i].setStyle({backgroundImage:"url('/images/betygIkon.png')"});
    } else {
      betygLi[i].setStyle({backgroundImage:"url('/images/betygIkonBlank.png')"});
    }
  }
}

function showRatingMsg(element,msg,posX,posY) {
  if(!($$('p.ratingMsg')[0])) {
    ratingMsg = new Element('p', {'class':'ratingMsg'}).update(msg);
    ratingMsg.setStyle({
      top: posY,
      left: posX
    });
    pointerOffsetX = -75;
    pointerOffsetY = -70;
    Event.observe(element, 'mousemove', function(event){
      if(ratingMsg = $$('p.ratingMsg')[0]) {
        ratingMsg.setStyle({
          top: (parseInt(Event.pointerY(event))+pointerOffsetY)+'px', 
          left: (parseInt(Event.pointerX(event))+pointerOffsetX)+'px'
        });
      }
    });
    $$('body')[0].insert({'top':ratingMsg});
  }
}

function setNewRating(rating,godisId) {
  url = '/includes/setNewRating.php';
  params = {
    newRating: rating,
    godisId: godisId
  };
  new Ajax.Request(
    url, {
      onSuccess: function(response) {
        if(response.responseText != 'fel') {
          setCurrentRating(response.responseText,godisId);
          flashRatings();
          reloadUserRatings();
          reloadCommentsList();
        }
      },
      parameters: params
    }
  );
}

function reloadUserRatings() {
  if($('userRatings')) {
    url = $$('div.content.side div.panel.memberfunction form')[0].readAttribute('action');
    $('userRatings').fade({duration: 0.2, afterFinish: function() {
      new Ajax.Updater('userRatings', url, {parameters: {
        getRating:'true'
      }});
      $('userRatings').appear({duration: 0.2});
    }});
  }
}

function reloadCommentsList(commentField) {
  commentForm = $$('div.content.side div.panel.memberfunction form')[0]
  commentForm.setStyle({clear: 'left'});
  commentForm.fade({from:1.0, to: 0.01, duration: 0.2});
  loaderImg = new Element('img', {'src':'/images/ajaxLoader.gif','class':'ajaxLoader'});
  loaderImgX = parseInt(commentForm.positionedOffset()[0] + (commentForm.getWidth() / 2) - 15);
  loaderImgY = parseInt(commentForm.positionedOffset()[1] + (commentForm.getHeight() / 2 - 15));
  loaderImg.setStyle({'top':loaderImgY+'px','left':loaderImgX+'px'});
  commentForm.insert({'before':loaderImg});
  url = commentForm.readAttribute('action');
  if(commentField) {
    params = {
      comment:commentField.value
    }
  } else {
    params = {}
  }
  commentsList = $('comments');
  commentsList.blindUp({duration: 1, afterFinish: function() {
    new Ajax.Updater('comments', url, { parameters: params });
    if(commentField) {commentField.value='';}
    commentsList.blindDown({duration: 1, afterFinish: function() {
      loaderImg.remove();
      commentForm.fade({from: 0.01, to: 1.0, duration: 0.2});            
    }});
  }});
}

function updateRatingList() {
  if($candyTable = $$('table#candyTable')[0]) {
    findAndReplace('\\*', '<img src="/images/betygIkon.png" alt="*" width="21" height="20" />', $candyTable);
  }
}

function flashRatings() {
  ratings = $$('div.content.side div.panel.memberfunction ul.betyg')[0];
  success = new Element('img', { 'src':'/images/saveSuccess.png', 'class':'saveSuccess' });
  ratings.insert({after: success});
  success.fade({duration: 1});
}
