function findPos(obj) {
  var curleft = curtop = 0;
  if (obj.offsetParent) {
    do {
      curleft += obj.offsetLeft;
      curtop += obj.offsetTop;
    } while (obj = obj.offsetParent);
  }
  return { 'left':curleft, 'top':curtop };
}


function mouse_position(e) {
  var posx = 0;
  var posy = 0;
  if (!e) var e = window.event;
  if (e.pageX || e.pageY) {
    posx = e.pageX;
    posy = e.pageY;
  } else if (e.clientX || e.clientY) {
    posx = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
    posy = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
  }

  return { left:posx, top:posy };
}

$(function() {

  $("#previous").click(function(){
    var current = $(".news_entry").filter(function(index){
      return $(this).css('display') == 'block';
    });
    current.fadeOut('fast',function(){
      $(this).prev(".news_entry").fadeIn('fast',function(){
        if( $(this).prev(".news_entry").length == 0 ) $("#previous").hide(); else $("#previous").show();
        if( $(this).next(".news_entry").length == 0 ) $("#next").hide(); else $("#next").show();
      });
    });
    return false;
  });

  $("#next").click(function(){
    var current = $(".news_entry").filter(function(index){
      return $(this).css('display') == 'block';
    });
    current.fadeOut('fast',function(){
      $(this).next(".news_entry").fadeIn('fast',function(){
        if( $(this).next(".news_entry").length == 0 ) $("#next").hide(); else $("#next").show();
        if( $(this).prev(".news_entry").length == 0 ) $("#previous").hide(); else $("#previous").show();
      });
    });
    return false;
  });

  $('.lightbox').lightBox({fixedNavigation:true});

  //przycisk anuluj przy dodawaniu wydarzenia
  $('#cancel_add_event').click(function(){
    $('#add_event_form').fadeOut('fast');
  });

  $('.event a').hide();

  $('.event').mousemove(function(){
    $(this).children('a').show();
  }).mouseout(function(){
    $(this).children('a').hide();
  });

  //podpowiedzi do nadawcy przy wysylaniu wiadomosci
  $('#add_receiver').click(function(){
    if( friends.length == 0 ) {
      alert('Brak ewentualnych odbiorców.');
      return false;
    }
    var thiz = $('#odbiorca_text');
    var pos = findPos(thiz[0]);
    data = '<div id="hint_name_part">Szukaj <input type="text" name="name_part" id="name_part"></div>';
    data += '<div id="hints">';
    for( i = 0; i < friends.length; i++ ) {
      data += '<p id="'+friends[i][0]+'">'+friends[i][1]+'</p>';
    }
    data += '</div>';
    if( data == '' ) {
      $('#hint').html('').hide();
    } else {
      data += '<div id="hide_hint">zamknij listę</div>';
      $('#hint').html(data).css({'top': pos.top + thiz.outerHeight(),'left': pos.left}).show();
      $('#hint p').click(function(){
        if( thiz.val() == '' ) {
          thiz.val($(this).html());
          $('#odbiorca').val($(this).attr('id'));
        } else {
          curr_val = thiz.val();
          if( curr_val.indexOf($(this).html()) == -1 ) {
            thiz.val(thiz.val() + ',' + $(this).html());
            $('#odbiorca').val($('#odbiorca').val() + ';' + $(this).attr('id'));
          }
        }
        $('#hint').html('').hide();
      });

      $('#name_part').keyup(function(event){
        var part = $(this).val();
        var pattern = new RegExp(part,'i');
        $('#hints p').each(function(i){
          if( pattern.test(this.innerHTML) ) this.style.display='block'; else this.style.display='none';
        });
      });

      $('#hide_hint').click(function(){
        $('#hint').hide();
      });
    }
    return false;
  });

});

function add_event(ev_date,ev_hour,e) {
  var pozycja = mouse_position(e);

  $('#event_hour').val(ev_hour);
  $('#ev_hour').html(ev_hour+':00');
  $('#ev_date').html(ev_date);
  $('#add_event_form').css({
    'top':pozycja.top+'px',
    'left':pozycja.left+'px'
  }).fadeIn('fast');
}

function show_description(desc,e) {
  var pozycja = mouse_position(e);
  $('#description').html(desc);
  $('#description').css({
    'top':pozycja.top + 'px',
    'left':pozycja.left + 10 + 'px'
  }).show();
}

function hide_description() {
  $('#description').hide();
}

function event_done(ev_id) {
  if( confirm('Ustawić jako wykonane?') ) {
    $('#event_done_id').val(ev_id);
    $('#event_done').submit();
    return false;
  } else {
    return false;
  }
}

function event_delete(ev_id) {
  if( confirm('Usun±ć wydarzenie?') ) {
    $('#event_delete_id').val(ev_id);
    $('#event_delete').submit();
    return false;
  } else {
    return false;
  }
}
