function vote(vote_value, item_id, item_type, vote_icon) {
    $('#vote_'+ item_type +'_layer_'+ item_id).css('display', 'inline');
    $.ajax({
      type: "POST",
      url: "/votes/ajaxAddVote/",
      cache: false,
      dataType: "json",
      data: "vote_value="+vote_value+"&item_id="+item_id+"&item_type="+item_type+"&vote_icon="+vote_icon,
      success: function(result){
        $('#vote_'+ item_type +'_layer_'+ item_id).css('background-color', 'white');
        $('#vote_'+ item_type +'_layer_'+ item_id +' > *').css('display', 'none');
        
        if(result['voted'] != '') {
            item = $("#vote_"+ item_type +"_"+ item_id +"_"+ vote_icon);
            if(item.attr('class') == 'on') {
                arr = item.html().split(' | ');
                item.html(arr[0] +' | '+ result['voted']['sum_of_votes']);
            } else {
                selected = $("#"+ item_type +"_"+ item_id +" a.on");                
                if(selected.length) {
                    selected_result = selected.html().split(' | ');
                    if(selected_result[1] <= result['voted']['sum_of_votes']) {
                        selected.html(selected_result[0]);
                        selected.removeClass('on');
                        item.attr('class', 'on');
                        item.append(' | '+ result['voted']['sum_of_votes']);                                                    
                    }
                } else {
                    item.attr('class', 'on');
                    item.append(' | '+ result['voted']['sum_of_votes']);
                }
                
            }            
        }
        //jAlert(result['text'], 'Vote');
      }
    });
}

function sendComment(){
    $("#ajax_loader").css("display","block");
    $("div#comment_error").html('');
    $("#submitcomment").attr('disabled','disabled');    
    
    $.ajax({
      type: "POST",
      dataType: "json",
      url: "/topics/ajaxAddComment/",
      cache: false,
      data: $("form").serialize(), //"title="+title+"&content="+content+"&topicId="+topicId+"&category="+category+"&username="+username,
      success: function(result){
        if(result['error'] && result['error'].length) {
          $("div#comment_error").html(result['error']);          
        } else {
          $("#content").attr('value','');
          $("input#title").attr('value','');
          $("ul#comments").append(result['comment']);
        }
      
        $("#ajax_loader").css("display","none")
        $("#submitcomment").removeAttr('disabled');
      }
    });   
}
          
function getCategoriesByGroupId(value) {
    $("#ajax_loader").css("display","block");
    
    $.ajax({
      type: "POST",
      url: "/groups/ajaxGetCategoriesByGroupId/",
      cache: false,
      data: "group_id="+value,
      success: function(result){
        $("#group_categories").html(result);
        
        $("#ajax_loader").css("display","none")
      }
    });
}

function searchsite(value) {
    if(value.length) {
        value = value.replace (" ","-");
        window.location='/tags/' + value; 
    }
}

function favoriteitem(item_id, item_type) {
    $.ajax({
      type: "POST",
      url: "/users/favoriteitem/",
      cache: false,
      data: "item_id="+item_id+"&item_type="+item_type,
      success: function(html) {
        alert(html);
      }
    });
}

//function addfriend(user_id_invite, accepted) {
//    $.ajax({
//      type: "POST",
//      url: "/users/ajaxAddFriend/",
//      cache: false,
//      data: "user_id="+user_id_invite+"&accepted="+accepted,
//      success: function(html) {
//        alert(html);
//      }
//    });
//}

function addFriend(obj, user_id_invite) {
    //obj.removeAttribute('onclick');
    //obj.removeAttribute('class');
    //obj.innerHTML = 'Your request sent.';
    obj.onclick = 'return false;';
    
    $.ajax({
      type: "POST",
      url: "/users/ajaxAddFriend/",
      dataType: "json",
      cache: false,
      data: "user_id="+ user_id_invite,
      success: function(result){
        if(result['joined'] == 'false') {
            //obj.innerHTML = 'You may not add this friend.';
            jAlert('You may not add this friend.', 'Add friend');
        } else {
            jAlert(result['joined'], 'Add friend');
        }
      }
    });
}

function replyAddFriend(obj, user_id, status) {
    parent = obj.parentNode;                                     
    parent.innerHTML = "<span>Accept</span> <span>Reject</span>";
    
    $.ajax({
      type: "POST",
      url: "/users/ajaxReplyAddFriend/",
      dataType: "json",
      cache: false,
      data: "user_id="+ user_id +"&status="+ status,
      success: function(result){
        jAlert(status+'ed', 'Add friend');
        parent.innerHTML = status+'ed'; 
      }
    });
}

//function groupInvite(obj, group_id, status) {
//    parent = obj.parentNode;

//    parent.innerHTML = "<span>Accept</span> <span>Reject</span>";
//    
//    $.ajax({
//      type: "POST",
//      url: "/groups/ajaxGroupInvite/",
//      dataType: "json",
//      cache: false,
//      data: "group_id="+ group_id +"&status="+status,
//      success: function(result){
        //result['joined'];
//          parent.innerHTML = status +'ed';
//      }
//    });
//}

function joinToGroup(obj, user_id, group_id) {
    //$("#ajax_loader").css("display","block");
    
    obj.removeAttribute('onclick');
    obj.removeAttribute('class');
    
    $.ajax({
      type: "POST",
      url: "/groups/ajaxJoinToGroup/",
      dataType: "json",
      cache: false,
      data: "group_id="+ group_id +"&user_id="+ user_id,
      success: function(result){
        if(result['joined'] == 'true') {
            obj.innerHTML = 'Your request sent.';
        } else {
            obj.innerHTML = 'You may not join to this group.';
        }
      }
    });
}
                      
function replyJoinToGroup(obj, group_id, status) {
    parent = obj.parentNode;                                     
    parent.innerHTML = "<span>Accept</span> <span>Reject</span>";
    
    $.ajax({
      type: "POST",
      url: "/groups/ajaxReplyJoinToGroup/",
      dataType: "json",
      cache: false,
      data: "group_id="+ group_id +"&status="+ status,
      success: function(result){
        jAlert(status+'ed', 'Invite');
        parent.innerHTML = status+'ed'; 
      }
    });
}

function addMenuItem() {
    $('#menu_items_div').append('<label for="">&nbsp;</label><input type="text" name="group_categories[]" value=""/><br />');                
}

function report (id){
    document.anchors["report_item"].removeAttribute("onclick");
    document.anchors["report_item"].removeAttribute("href");
    $("#ajax_loader").css("display","inline");
    $("#report_item").replaceWith("Item Reported");
    $.ajax({
      type: "POST",
      url: "/topics/ajaxReport/",
      dataType: "json",
      cache: false,
      data: "item_id="+ id,
      success: function(result){
        jAlert('Item reported, thanks', 'Report');
        $("#ajax_loader").css("display","none");
      }
    });   
}

function checkboxState(type) {
    if (type=='free') {
        if ($('#freeHTML').attr('checked')) {
           $("#anchor-text").attr("disabled", "disabled");
           $("#link-url").attr("disabled", "disabled");
           $("#link-description").attr("disabled", "disabled");
           $("#regular-link").attr("checked",false)
           if ($('#html-textarea').attr('disabled',true))
                $("#html-textarea").removeAttr("disabled");
        }
    
    }
    else if (type=='anchor') {
        if ($('#regular-link').attr('checked')) {
           $('#html-textarea').attr("disabled", "disabled");
           $("#freeHTML").attr("checked",false)
           if ($("#anchor-text").attr("disabled",true))
                $("#anchor-text").removeAttr("disabled");
           if ($("#link-url").attr("disabled",true) )
                $("#link-url").removeAttr("disabled");           
           if ($("#link-description").attr("disabled",true))
                $("#link-description").removeAttr("disabled");                
        }    
    
    }

}