$(document).ready(function(){
var box = '#bshow'; // Div s obrázky
var selIndex = 0;
var SirkaB = $(box).width();
var VyskaB = $(box).height();
//var presrc = 'url(http://shop-admin.cz/Lightboxb/img/bbox/nacitani.gif)';

var obr = new Array();
var pObr = $(box+' a[rel^=lightbox]').length; // pocet obrazku

if (pObr > 0){
  var novHtml = '';          
  var tempObj;
  obr.length = pObr;  
  for(i= 0;i<pObr; i++){
    tempObj = $($(box+' a[rel^=lightbox]')[i]);
    obr[i] = new Array(3);
    
    obr[i][0]= tempObj.attr('href');    
    obr[i][1]= tempObj.attr('title');
    obr[i][2]= tempObj.attr('alt'); 
    novHtml = novHtml + '<div id="'+i+'" class="overImg">'+        
                        '<img id="'+i+'" src="'+obr[i][0]+'" />'+
                        '</div>';
  };
  novHtml = novHtml + '<span id="title"></span><span id="play-stop" class="stop"></span><span id="zpet"></span><span id="vpred"></span>';
  $(box).html(novHtml); 
  UpravStyly();
  //$(box+' ul').css({width:SirkaB*pObr,top:'0px',left:'0px'});
  $(box+' div').css({width:SirkaB,height:VyskaB,'z-index':'199',left:SirkaB});
  $('#zpet, #vpred, #play-stop').css({opacity:0.3,'z-index':'201','cursor':'pointer'});
  
  $(box+' #title').css({'display':'none'});    
  if (obr[0][1].length>0) $(box+' #title').text(obr[0][1]).slideDown(200);
      
  $('#zpet, #vpred, #play-stop').mouseenter(function() {
    $(this).stop().animate({opacity:1},'fast');
    }).mouseleave(function(){
      $(this).stop().animate({opacity:0.3},'fast');    
      });
  $(box+' div').eq(0).css({'z-index':'200',left:0});
  //nactiObr(1);

  var interval;
  interval = setInterval(function(){nactiObr(selIndex+1);}, 5000);
  //clearInterval(interval); 
}

function PlayStop(){
  if ($('#play-stop').hasClass('play')){   
    nactiObr(selIndex+1);
    interval = setInterval(function(){nactiObr(selIndex+1);}, 5000);
    $('#play-stop').removeClass('play');
    $('#play-stop').addClass('stop');
  }else{
    clearInterval(interval);
    $('#play-stop').removeClass('stop');
    $('#play-stop').addClass('play');
  }
}
     
function UprVel(obj){
  var puvS = $(obj).width();
  var puvV = $(obj).height();
  
  $(obj).css({width:SirkaB});

  var marTop = (($(obj).height()-VyskaB)/2)*(-1);   
  $(obj).css({marginTop:marTop});          
}

$(box+' img')
  .load(function(){
    UprVel(this);
    //alert(this.width());
    //alert($(this).attr('src'));        
    return false;
  })
  .each(function(){
    if ( this.complete && this.naturalWidth !== 0 ) {
      $( this ).trigger('load');
    }
  });  

function nactiObr(index){  
  var vlevo = ((selIndex>index));
  if (index < 0) index = pObr-1;
  if (index > pObr-1) index = 0;
  
  var hIndex = selIndex;

  var sObr = $(box+' div[id='+index+']');
  var hObr = $(box+' div[id='+hIndex+']');
  
  if (!$(box+' div[id='+index+'], '+box+' div[id='+hIndex+']').is(':animated')){  
    var nPoz = '-='+SirkaB+'px';
    if (vlevo){
      hObr.css({'z-index':'200',left:0});    
      sObr.css({'z-index':'200',left:(SirkaB*(-1))});
      nPoz = '+='+SirkaB+'px';    
    }else{
      hObr.css({'z-index':'200',left:0});    
      sObr.css({'z-index':'200',left:SirkaB});    
    }       
  
    $(box+' #title').slideUp(200);
    $(box+' #title').text('');
    
    if (obr[index][1].length>0)
      $(box+' #title').text(obr[index][1]);
    else
      $(box+' #title').text('');
        
    $(box+' div[id='+index+'], '+box+' div[id='+hIndex+']').animate({  
      'left': nPoz
      },700,function(){  
      hObr.css({'z-index':'199'});
      if ($(box+' #title').text().length > 0)
        $(box+' #title').slideDown(200);
      }
    );
    selIndex = index;
  } 
}

$(box+' #play-stop').click(function(){
  PlayStop();
  return(false);
});

$(box+' #zpet').click(function(){
  if ($('#play-stop').hasClass('stop')) PlayStop();
  nactiObr(selIndex-1);
  return(false);
});

$(box+' #vpred').click(function(){
  if ($('#play-stop').hasClass('stop')) PlayStop();
  nactiObr(selIndex+1);
  return(false);
});

function UpravStyly(){
   if (/MSIE ((5\.5)|6|7|8)/.test(navigator.userAgent) && navigator.platform == "Win32")
    $(box).addClass('nPng');
/*
  pocObj = $(box+' *').length;
  if (pocObj>0)
    for(i= 0;i<pocObj; i++){
      //alert(pocObj);
      if ($(box+' *').eq(i-1).css('background-image').toLowerCase().match(/\.png/) !== null){
        var pngObj = $(box+' *').eq(i-1);
        var cesta = pngObj.css('background-image').substring(5,pngObj.css('background-image').length-2);
        pngObj.css({
						'filter': "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + cesta + "', sizingMethod='no-repeat')",
						'background-image': 'url(img-bslideshow/gif/bslb-btn-stop.gif)','z-index':'201'});
        alert(cesta);
        //alert(pocObj);
      }
    }
*/
}
    
}); // načtení celého dokumentu
