var isAddCommentOpen = false;
var isShareOpen = false;
var freeze = false;

function toggleLayer(element) {
  switch(element) {
    case 'addComment':
      _toggleCommentLayer(element);
      break;
  case 'ShareLayer':
      _toggleShareLayer(element);
      break;
  }
}


function _toggleCommentLayer() {
  if(!freeze) {
    freeze = true;
    if(!isAddCommentOpen) {
      _myBlindDown('addComment');
      $('addCommentImage').src = "images/structure/buttons/comment_active.png";
      isAddCommentOpen = true;
    } else {
      _myBlindUp('addComment');
      $('addCommentImage').src = "images/structure/buttons/comment.png";
      isAddCommentOpen = false;
    }
  }
}

function _toggleShareLayer() {
  if(!freeze) {
    freeze = true;
    if(!isShareOpen) {
      Effect.BlindDown('ShareLayer', {duration:0.7, afterFinish:_afterToggle, scaleX:true});
      isShareOpen = true;
    } else {
      Effect.BlindUp('ShareLayer', {duration:0.7, afterFinish:_afterToggle, scaleX:true, scaleY:false});
      isShareOpen = false;
    }
  }
}

function _myBlindDown(element) {
  Effect.BlindDown(element, {duration:0.7, afterFinish:_afterToggle});
}

function _myBlindUp(element) {
  Effect.BlindUp(element, {duration:0.7, afterFinish:_afterToggle});
}


function _afterToggle(){
  freeze = false;
}

function popup(url, winW, winH, template) {
    winX = (screen.width/2)-(winW/2);
    winY = (screen.height/2)-(winH/2);
    popupLegal = window.open(url,template,'width='+winW+',height='+winH+',top='+winY+',left='+winX+',scrollbars=yes');
 }


function enableLikeButton(){
    $$('.i-like').each(function(element){

        $(element).observe('click', function(event){
                if($(this).hasClassName('inactive') === false){
                var id = $(this).up().up().id.replace("comment_id_", "");

                new Ajax.Request("/rating?id=" + id, {
                  method: 'get',
                  onSuccess: function(transport) {
                    $("like-text-" + id).update(transport.responseText);
                    $("like-text-" + id).setStyle({
                                                  border: '1px solid #a9a9a9',
                                                  color: '#a9a9a9'
                                                });

                    $("i-like-" + id).setStyle({cursor: 'default'});
                    $("i-like-" + id).addClassName("inactive");
                    $("i-like-" + id).down().src = "/images/content/like_inactive.png";
                  }
                });
            }
            Event.stop(event);
        });


        $(element).observe('mouseover', function(event) {
            if($(this).hasClassName('inactive') === false){
                $(this).down().src = "/images/content/like_hover.png";
            }
        });
        $(element).observe('mouseout', function(event) {
            if($(this).hasClassName('inactive') === false){
                $(this).down().src = "/images/content/like_active.png";
            }
        });

    });
}


Event.observe(window, 'load', function() {
    enableLikeButton();
});
