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,
      data: "vote_value="+vote_value+"&item_id="+item_id+"&item_type="+item_type+"&vote_icon="+vote_icon,
      success: function(html){
        $('#vote_'+ item_type +'_layer_'+ item_id).css('display', 'none');
        $('#'+item_type+'_'+item_id).empty();
        //$('#topics_'+item_id).hide();
        $('#'+item_type+'_'+item_id).append(html); 
      }
    });
}

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 (/ /g,"-");
        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) {
        blitzalert(html,"Favorite");
      }
    });
}

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

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

$(function() {
//////////////////////////////////////
    $(".popMore").click(function () {
        status = $(this).find("span").css('display');
        $(".popMore span").hide();
        if(status == 'inline') {
            $(this).find("span").hide();
        } else {
            $(this).find("span").show();
        }                  
    });
/////////////////////////////////////
    $(".header .nav .subs,.newsPicWrapper").hover(function () {
          $(this).addClass("hover");
    }, function() 
        {
            $(this).removeClass('hover');
     });
});

function update_latest(category,type,limit,direction) {
    $('#ajax_image-'+type).css('display','inline');        
        if ( typeof update_latest.pageCounter == 'undefined' ) {
            update_latest.pageCounter=0; }
        if (direction=="forward") {
           ++update_latest.pageCounter;
        }
        if (direction=="backwards" && update_latest.pageCounter>=1) {
            --update_latest.pageCounter;
        }
            $.ajax({
            type: "POST",
            url: "/topics/updateLatest/",
            cache: false,
            data: "category="+category+"&type="+type+"&limit="+limit+"&page="+update_latest.pageCounter,
            success: function(html){
                $('#ajax_image-'+type).css('display','none');
                    if (html.length>30){
                        $('span#carousel-vertical-'+type).empty();
                        $('span#carousel-vertical-'+type).append(html); 
                                        }
                                    }
                    });
}

function openCloseBar(divId) {
   cook=readCookie(divId);
   var exdate=new Date();
   exdate.setDate(exdate.getDate()+(30));   
   expires= exdate.toGMTString();
   if (cook!=null && cook.charAt(0)=='0') {
        document.cookie= divId+'=1; expires='+expires+'; path=/';
        $("#"+divId).show('slow');                          
        document.getElementById('toggleBarView'+divId).innerHTML = '<img src="/images/triangle_open.png">'; 
   } else {
        //document.cookie= divId+"="+'1'+expires+"; path=/";
        document.cookie=divId+'=0; expires='+expires+'; path=/'
        $("#"+divId).hide('slow');
        document.getElementById('toggleBarView'+divId).innerHTML = '<img src="/images/triangle_closed.png">';                
   }  
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

function login (){
 $.ajax({
    type: "POST",
    url: "/users/popLogin/",
    cache: false,
    //data: "category="+category+"&type="+type+"&limit="+limit+"&page="+update_latest.pageCounter,
    success: function(html){
        blitzalert(html,"Login / Register");

    }
 });
}

function userregister (){
    $('#ajax_image_register').css('display','inline');
     username = (document.getElementById("username").value);
     password = (document.getElementById("password").value);
     passconf = (document.getElementById("passconf").value);
     email = (document.getElementById("email").value);

     $.ajax({
        type: "POST",
        url: "/users/register/",
        cache: false,
        data: "username="+username+"&password="+password+"&passconf="+passconf+"&email="+email+"&method=js",
        success: function(html){
            $('#ajax_image_register').css('display','none');
            if (html=="good") {
                $('#register').empty();
                $('#login').empty();
                $('#register').append('<h1>Great! You are registered. Now keep an open eye on your inbox... your verification code should arrive any moment now.</h1>');
            } else {               
                $('#registererrors').empty();
                $('#registererrors').append(html);
            } 
        }
     });
}

function userlogin (){
    $('#ajax_image_login').css('display','inline');
     username = (document.getElementById("usernamelogin").value);
     password = (document.getElementById("passwordlogin").value);
     $.ajax({
        type: "POST",
        url: "/users/login/",
        cache: false,
        data: "username="+username+"&password="+password+"&method=js",
        success: function(html){
            $('#ajax_image_login').css('display','none');
            if (html=="good") {
                setTimeout
                (
                    function()
                    {
                        $.alerts._hide('slow');
                        $.alerts._remove();
                    },
                    500
                );
                location.reload();                    
            } else {               
                $('#loginerrors').empty();
                $('#loginerrors').append(html);
            } 
        }
     });
}

function addTweet (){
    $("#ajax-image").css("display","inline");
    tweet = document.getElementById('tweet').value;
    page_user_id = document.getElementById('page_user_id').value;
    tweet_type = document.getElementById('tweet_type').value;
    if (tweet==''){
       $("#ajax-image").css("display","none");
    } else {
        $.ajax({
          type: "POST", 
          url: "/users/ajaxAddTweet",
          cache: false,
          data: "tweet="+tweet+"&page_user_id="+page_user_id+"&tweet_type="+tweet_type,
          success: function(result){
              $("#ajax-image").css("display","none");
              $("#tweets").empty();
              $("#tweets").append(result);
          }
        });
    }
}
