var klatka = 0;
var play_ = false ;
var auto = false ;
var cont_play = false ;
var loop = 0 ;
var timer ;

function big_foto( i, obj, index ) {
 if( play_ ) { 
   rob = true ; 
	play_ = false ;
 }
 
 klatka = index ;
 $("#foto img").load( 
  function() {
     $("#foto img").fadeIn();
	  if( rob ) play_ = true ;
  $("div.ov").css( "width", "200px" ) ;	//  progress();		
 	}
 ) ;
 $("#foto img").fadeOut( "normal", function() {
   $("#foto img").attr( "src", "big_foto.php?plik="+i ) ;
 }
 );
// $("#foto img").attr( "src", "foto/big_"+i+"_"+j+".jpg" ) ;
 $("#fotki a span").each(
   function( id ) {
 	 $(this).removeClass("on"); 
	}
 ) ;
 $(obj).find("span").addClass( "on" ) ;
 return false ;
}

function przelicz_offset( off ) {
var offset1 = $("#foto_right").attr( "rel" ) ;
 var offset2 = $("#foto_left").attr( "rel" ) ;
 if( off == 1 ) {
  off = offset1 ;
  offset1++ ;
  offset2++ ;	
 } else {
  off = offset2 ;
  offset1-- ;
  offset2-- ;		
 }
 $("#foto_right").attr( "rel", offset1 ) ;
 $("#foto_left").attr( "rel", offset2 ) ;
}

function foto_right( off, id, pid, dir ) {
// alert( max_foto ) ;
 if( dir == 'r' ) {
  var rel = parseInt($("#foto_right").attr( "rel" )) ;
  klatka++ ; 
  if( klatka == 5 ) {
   if( klatka + rel - 1 >= max_foto ) {
	 play_img() ;
 	 klatka-- ;
	 klatka = 4 ;
	 return false ;	
	} 
   klatka-- ;	
	 $.ajax({
   type: "GET",
   url: "fotki.php",
	dataType: "html",
   data: "id="+id+"&off="+rel+"&pid="+pid,
   success: function(msg){
     $("#fotki").html( msg );
     var el = $("#fotki a.big_foto" ) ;
     $(el[klatka]).click() ;
	  przelicz_offset( off ) ;	
   }
   });
  }
  else {		
   var el = $("#fotki a.big_foto" ) ;
   $(el[klatka]).click() ;
  }	
 }
 else {
  klatka-- ;
  if( klatka > -1 ) {	
   var el = $("#fotki a.big_foto" ) ; 
   $(el[klatka]).click() ;
  }
  else {
   klatka = 0 ;
   var rel = $("#foto_left").attr( "rel" ) ;
	if( rel == -1 ) return false ;
	 $.ajax({
   type: "GET",
   url: "fotki.php",
	dataType: "html",
   data: "id="+id+"&off="+rel+"&pid="+pid,
   success: function(msg){
     $("#fotki").html( msg );
     var el = $("#fotki a.big_foto" ) ;
     $(el[klatka]).click() ;
	  przelicz_offset( off ) ;	
    }
   });
		
  }		
 }
 
 return false ;
}

function progress() {
 if( play_ ) {
  var w = parseInt($("div.ov").css( "width" )) ;
  w = w - 5 ;
  $("div.ov").css( "width", w + "px" ) ;
  if( w == 0 ) { 
   play_step() ;
  }	
 }
 setTimeout ( "progress()", 100 ); 
}

function play_img() {
 if( play_ == false ) {
  play_ = true ;
  klatka-- ;	
  $("div.ov").css( "width", "200px" ) ;
  $("div.ov").css( "display", "block" ) ;	
  $("#odtw").css( "background-image", "url( gfx/stop.png )") ;	
  play_step();	
  progress() ;
 } else {
  $("div.ov").css( "display", "none" ) ;	
  play_ = false ;
  auto = false ;	
  $("#odtw").css( "background-image", "url( gfx/play.png )") ;	
 }
}

function play_step() {
  var fr = $("#foto_right" ) ;
  $(fr).click() ;
}


