var l;
var l_v;
var pos_last_horizontal;
var pos_last_vertical;
var li_width;
var li_height;

function main_gallery() {
   var $next_img;
   if ($('#galeria_top img.current').next().length > 0) {
      $next_img = $('#galeria_top img.current').next();   
   }
   else {
      $next_img = $('#galeria_top img').first();
   }
   $('#galeria_top img.current').fadeOut("slow", function(){
      $(this).removeClass("current");
   });
   $next_img.fadeIn("slow", function() {
      $(this).addClass("current");
   });
   
}

function init_main_gallery() {
   setInterval("main_gallery();",5000);
}

function slider() {
   $('#slider_content ul li').each(function(index) {
     $(this).animate({left : '-=' + li_width}, 1000, function(){
        if (index == (l-1)) {
           $('#slider_content ul li').first().css({left : pos_last_horizontal}).remove().appendTo('#slider_content ul');
        }   
     });
   });
}

function init_slider() {
   l = $('#slider_content ul li').size();
   if (l > 1) {
      li_width = $('#slider_content ul li').width();
      var wd = l*li_width + 10;
      $('#slider_content ul').css("width", wd+"px");
      var pos = 0;
      $('#slider_content ul li').each(function() {
            $(this).css({left : pos + "px"});
            pos += li_width;
      });
      pos_last_horizontal = $('#slider_content ul li').last().css("left");
      
      setInterval("slider();",5000);
   }
}

function slider_vertical() {
   /*$('#slider_vertical ul li').each(function(index) {
     $(this).animate({top : '-=' + li_height}, 1000, function(){
        if (index == (l_v-1)) {
           $('#slider_vertical ul li').first().css({top : pos_last_vertical}).remove().appendTo('#slider_vertical ul');
        }   
     });
   });*/
	var $el = $('#slider_vertical ul li').first();
	$el.animate({top : '-=' + li_height}, 1000, function() {
		$(this).css({top : li_height}).remove().appendTo('#slider_vertical ul');
	});
	$el.next().animate({top : '-=' + li_height}, 1000);
}

function init_slider_vertical() {
   li_height = 0;
   l_v = $('#slider_vertical ul li').size();
   $('#slider_vertical ul li').each(function() {
      if ($(this).height() > li_height) {
         li_height = $(this).height();
      }   
   });
   li_height += 5;
   $('#slider_vertical').css("height", li_height + "px");
   if (l_v > 1) {
      var pos = 0;  
      $('#slider_vertical ul li').each(function(i) {
			if (i == 0) {
		 		$(this).css({
			 		top: pos + "px"
		 		});
		 	}
		 	else {
			 	$(this).css({
		 			top: li_height + "px"
		 		});
		 	}
         //pos += li_height;   
      });
      //pos_last_vertical = pos - li_height;
		
      setInterval("slider_vertical();", 5000);
   }
}

$(document).ready( function() {
   $(window).load(function() {
      init_main_gallery();
      init_slider();
      init_slider_vertical();
   });
   
   $('#swieta_content').click(function() {
	$(this).closest('#swieta_wrap').fadeOut();   
   });
});
