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 [posx, posy];
}

function dict_show(dict_id,ev) {
  var pojecie = slownik_slowa[dict_id];
  var wytlumaczenie = slownik_wytlumaczenia[dict_id];
  var pozycja = mouse_position(ev);
  $("#dict_word").html(pojecie);
  $("#dict_expl").html(wytlumaczenie);
  $("#dict_frame").css({
    top: pozycja[1] + 10 + 'px',
    left: pozycja[0] + 10 + 'px'
  }).show();
}

function load_rand_img(){
  $('#float_img img').fadeOut('slow',function(){
    $.post(
      "load_rand_img.php",
      {},
      function(data){
        if( data != 'empty' ) {
          var dane = data.split(':');
          if( dane.length == 3 ) {
            var obrazek = dane[0];
            var szer = dane[1];
            var wys = dane[2];
            //alert(obrazek);
            $('#float_img img').attr('src',obrazek).load(function(){
              $(this).css({'width':szer,'height':wys});
              $(this).fadeIn('slow');
            });
          }
        }
        //$('#float_img').html(data);
      }
    );
  });
}

$(function(){

  $('.menu_main').filter(function(){
    return $(this).next('li').hasClass('menu_sub');
  }).click(function(){
    var clicked = $(this);
    $('.menu_sub:visible').slideUp('slow');
    if( clicked.next('li').is(':hidden') ) {
      clicked.next('li').slideDown('slow');
    }
    return false;
  });

  $('.lightbox').lightBox({
    fixedNavigation: true,
    txtImage: "Zdjęcie",
    txtOf: "z"
  });

  if( typeof slownik_slowa != "undefined" ) {
    for( var i = 0; i < slownik_slowa.length; i++ ) {
      $(".content_frame_text p").each(function(){
        var pattern = new RegExp('(' + slownik_slowa[i] + ')', 'gi');
        $(this).html($(this).html().replace(pattern,"<span class=\"dict " + i + "\">$1</span>"));
      });
    }
    $('.dict').mousemove(function(ev){
      var this_classes = $(this).attr('className').split(' ');
      var dict_id = this_classes[1];
      dict_show(dict_id,ev);
    }).mouseout(function(){
      $("#dict_frame").hide();
    });
  }

  $('#full_size_photo').click(function(){
    $(this).fadeOut("slow");
  });

  load_rand_img();
  setInterval("load_rand_img();", 8000);

});

