$(window).load(function() {
	createSlideshow();
	createClientShow();
	createBaner();
});

function createSlideshow() {
	$('body').prepend('<div id="slider-back"></div><div id="slider-out"><div id="slider-jq"></div></div>');
	$('#slider-back').hide();
	$('#slider-out').hide();
	$('#slider-back').height($('body').height()+40);
	
	var photoCount = $('a:.jql').size();
	var elements = new Array();
	
	var scrollTop = $(window).scrollTop();

	$('a:.jql')
	.each(function(index){
		this.index = index;
		elements.push($(this));
	})
	.click(function() {	
		var img = new Image();
		
		var scrollTop = $(window).scrollTop();
		
		$('#slider-out').css('top', scrollTop+10);
		
		$('#slider-back').show();
		$('#slider-out').show();
		$('#slider-jq').html('');
		var title = this.title;
		var index = this.index;
		
		$(img)
			.hide()
			.load(function () {
				var leftArr = null;
	
				if (index > 0) {
					var leftArr = $('<a>&laquo; poprzednie</a>');
					$(leftArr).attr("class","arr_l");	
					$(leftArr).click(function(){
						elements[index-1].click();
					});
				}
				var rightArr = null;
				if (index < photoCount-1) {
					var rightArr = $('<a>następne &raquo;</a>');
					$(rightArr).attr("class","arr_r");
					$(rightArr).click(function(){
						elements[index+1].click();
					});
				}
				
				$('#slider-jq').append(this);
				
				var desc = $("<span class=\"slider-desc\"></span>");
				
				$(desc).append($(leftArr));
				$(desc).append($(rightArr));
				
				$('#slider-jq').animate({
				    height: $(this).height()+20,
				    width: $(this).width()
				  }, 500, function() {
					  $(img).fadeIn("slow");
					  $(desc).append(title);
					  $('#slider-jq').append($(desc));
				  });
				
			})
			.attr('src', this.href)
			.click(function(){
				$('#slider-back').hide();
				$('#slider-out').hide();
			});
			return false;
	});
}

var idxC = 0;
function runC() {
	$($clients[idxC]).animate({left: -240}, 500, function() {});
	
	idxC++;
	if (idxC == $clients.size()) {
		idxC = 0;
	}
	$($clients[idxC]).css('left',240);
	$($clients[idxC]).animate({left: 0}, 500, function() {});
		
	setTimeout("runC();",3000);
}

function createClientShow() {

	$clients = $('#clients').children();
	$clients.css('left',240);
	$($clients[idxC]).css('left',0);
	
	var max = 0;
	
	for (var i=0; i<$clients.size(); i++) {
		var m = parseInt($($clients[i]).css('height').replace('px',''));
		
		if (max < m) {
			max = m;
		}
	}
	
	$('#clients').css('height', max);
	
	setTimeout('runC();',3000);
}

var idx = 0;

$imgs = null;

function runBaner() {
		
	$($imgs[idx]).animate({opacity: 0}, 500, function() {});
	
	idx++;
	if (idx == $imgs.size()) {
		idx = 0;
	}
		
	$($imgs[idx]).animate({opacity: 1}, 500, function() {});
		
	setTimeout("runBaner();",5000);
}

function createBaner() {
	
	$imgs = $('#slider').children();

	$imgs.fadeTo(0,'0');
	
	$($imgs[idx]).fadeTo(0,'1');
	
	setTimeout('runBaner();',5000);
}




