//to extend this feature add to the table element  class="shortcuts_item"
var currentSelectionLift = 0;
var currentSelectionPost = 0;
var currentUrl = '';

$(document).ready(function() {
    //hover effect on buttons
    $(".button").mouseover(function() {
      $(this).addClass('button_hover'); 
    });
    $(".button").mouseout(function() {
      $(this).removeClass('button_hover'); 
    });


    //hide flash message
    setTimeout(clearFlashMessage,4000);
    $("#close_flash_message").click(function(){
      clearFlashMessage();
    });

    //hide message
    $("#close_message").click(function(){
      $("#message_bcg").slideUp(500);
      $("#message").slideUp(500);
      message_ajax();
    });

    //after click on comment and reply button focus to texarea
    $(".focus_down").click(function(){
        $.scrollTo($("#post"),250);
        $("#post textarea").focus();
    });

    //focus on input in forms
    $("#input_focus input").focus();  

    //scroll to last read post
    $.scrollTo($(".last_read_post"),250,{offset:-100});

   //$(".pokus h2").addClass("selected_post");
/*     $(document).shortkeys({
         'J':          function () { liftNavigate('j');},
         'K':          function () { liftNavigate('k');},
         'Enter':      function () { 
                                  if((currentUrl != '')){
                                      if(!(currentUrl===undefined)) {
                                          window.location = currentUrl;
                                      }
                                  }},
         'U':          function () { alert('u'); },
         'N':          function () { postNavigate('n');},
         'P':          function () { postNavigate('p');},
     });*/
});

function clearFlashMessage() {
    $("#flash_message").fadeOut('slow'); 
}

function message_ajax(){
    iter=0;
    id_value=0;
    result="";
    while(!(id_value===undefined)){
      id_value = $("#message span").eq(iter).attr("id");
      if(!(id_value===undefined))
        result+=id_value;
      iter++;
    }

    data={"message":result};
    $.ajax({
        type: "POST",
        url: "/ajax/message/",
        data: data,            
        async: false,
    });
}

function liftNavigate(direction) {
    // is any item selected?
    if($(".shortcuts_item .selected_item").size() == 0) {
        currentSelectionLift = -1;
    }

    if(direction == 'k' && currentSelectionLift != -1) {
        if(currentSelectionLift != 0) {
          currentSelectionLift--;
        }
    } else if (direction == 'j') {
        if(currentSelectionLift != ($(".shortcuts_item").size()-1)) {
          currentSelectionLift++;
        }
    }
    setSelected(currentSelectionLift);
}

function setSelected(liftItem) {
    $(".shortcuts_item a").removeClass("selected_item");
    $(".shortcuts_item a").eq(liftItem).addClass("selected_item");
    currentUrl = $(".shortcuts_item a").eq(liftItem).attr("href");
    $.scrollTo($(".shortcuts_item a").eq(liftItem),100,{offset:-100});
}

function postNavigate(direction){
    // is any item selected?
    if($(".shortcuts_post .selected_post").size() == 0) {
        currentSelectionPost = -1;
    }

    if(direction == 'p' && currentSelectionPost != -1) {
        if(currentSelectionPost != 0) {
          currentSelectionPost--;
        }
    } else if (direction == 'n') {
        if(currentSelectionPost != ($(".shortcuts_post").size()-1)) {
          currentSelectionPost++;
        }
    }
    setOn(currentSelectionPost);
}

function setOn(postItem) {
    $(".shortcuts_post h2").removeClass("selected_post");
    $(".shortcuts_post h2").eq(postItem).addClass("selected_post");
    $.scrollTo($(".shortcuts_post h2").eq(postItem),250,{offset:-100});
}
