// POKAZANIE TOPLAYER-A
function showTL(elem){
    $('#container').after('<div id="top_layer"><a href="#" onclick="closeTL(); return false;">Zamknij</a>"</div><div style="width: 100%; opacity: 0.5; z-index: 999; display: none;" class="bgbody"></div>');
  
    var h = $(document).height();
    var de = document.documentElement;
    var ww = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
    var wh = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
    
    $("div.bgbody").css("height",h+"px");
    $("div.bgbody").css("display","block");
    $("div#top_layer").css("display","block");
          
    var a = $('.thumbs li.show').html();
    $('#top_layer').prepend(a);
    
    var iw = parseInt($("div#top_layer img").width());
    var ih = parseInt($("div#top_layer img").height());

    if(iw > ww-20){
      iw = ww - 150;
      $("div#top_layer img").css("width",iw);
    } else if(ih > wh-20){
      ih = wh - 150;
      $("div#top_layer img").css("height",ih);
    }
    iw = $("div#top_layer img").width();
    ih = $("div#top_layer img").height();
   // $("div#top_layer").css({"left" : (ww/2)-(iw/2), "margin-top" : -((wh/2)-(ih/2))});
   // $("div#top_layer").css({"left" : (ww/2)-(iw/2), "top" : (wh/2)-(ih/2)});
    if(iw<60){
      $("div#top_layer").css("width","60px")
    } else {
      $("div#top_layer").css("width",iw);
    }
    $("div#top_layer").css({"left" : (ww/2)-(iw/2), "top" : (wh/2)-(ih/2)});
}

// ZAMKNIĘCIE TOPLAYER-A
function closeTL(){    
    $("div#top_layer").remove();
    $("div.bgbody").remove();
    
    return false;
}

$(document).ready(function(){
  // NAWIGACJA GALERII
  
  for(var i=0; i<$('.gallery .thumbs li').length; i++){
    if($('.gallery .thumbs li img').eq(i).width < 276){
      $('.gallery .thumbs li img').css('width', 'auto');
    }
  }
  
  $('.gallery .thumbs li').eq(0).addClass('show');
  
  // poprzedni obrazek
  function gallery_prev(elem){
    for(a=0; a<elem.length; a++){
        if(elem.eq(a).hasClass('show')) b = a;
      }
      
      elem.removeClass('show');
      
      if(b>0){
        elem.eq(b-1).addClass('show');
      }else{
        elem.eq(elem.length - 1).addClass('show');      
      }
  }
  
  // następny obrazek
  function gallery_next(elem){      
    for(a=0; a<elem.length; a++){
        if(elem.eq(a).hasClass('show')) b = a;
      }
      
      elem.removeClass('show');
      
      if(b<elem.length-1){
        elem.eq(b+1).addClass('show');
      }else{
        elem.eq(0).addClass('show');      
      }  
  }
  
  // podpięcie akcji pod przyciski
  elem = $('.gallery .thumbs li');
  
  $('.gallery .navi .prev').click(function(){
    gallery_prev(elem);
  });
  
  $('.gallery .navi .next').click(function(){
    gallery_next(elem);
  });
  
  $('.gallery .navi .full').click(function(){
    showTL(elem);
  });
  
  $('div.bgbody').live('click', function(){
    closeTL();
  });
});