$(document).ready(function(){
	$('#main h2 a, #newsy a, #kadra a, .wiecej, #wywiad a, .prev_spon, .next_spon, #menu_bottom li a, #dodaj, #usun, #zalogowano li a, #dalej, #wroc, #galer a, .powieksz, #name a, #produkt_mies h3 a, .lista_zaw li a').addClass('dhover');
	$('.dhover').hover(function(){
		$(this).addClass('dh')
	});
	$('.dhover').mouseleave(function(){
		$(this).removeClass('dh')
	});

	$('li', '#menu').click(function(){
		var atr = $(this).attr('class');
		var ul = $('ul', this).css('display');
		
		if(atr != 'sfhover' & ul == 'none'){
			$('a', '#menu').removeClass('sfhover');
			$('ul', '#menu').fadeOut('1000');
			$('ul', this).fadeIn('1000');
			$('a', this).addClass('sfhover');
		}
	});
	$('#wiecej').click(function(){
		$('#more').fadeIn('1000');							
	});
	$('#more').mouseleave(function(){
		$('#more').fadeOut('1000');							
	});
	var n_numTabs = $("#main ul > li").size();
	$('#max').text(n_numTabs);
	zmienMain = function(id) {
		var imageWidth = $("#main").width();
		var imageSum = $("#main ul > li").size();
		var imageReelWidth = imageWidth * imageSum;
		$("#main ul").css({'width' : ''+imageReelWidth+'px'});
		ida = id-1;
		var image_reelPosition = ida * imageWidth;
		$("#main ul").animate({
        	left: -image_reelPosition
    	}, 500 );
	};
	next = function(){
		id = $("#akt").text();
		++id;
		if(id > n_numTabs){var id = 1;}
		zmienMain(id);
		$('#akt').text(id);
	};
	var refreshIntervalId = setInterval("next();", 5000);
	
	$('#next').click(function(){
		next();
		clearInterval(refreshIntervalId);
	});
	
	$('#prev').click(function(){
		id = $("#akt").text();
		id = id-1;
		if(id < 1){var id = n_numTabs;}
		zmienMain(id);
		$('#akt').text(id);
		clearInterval(refreshIntervalId);
	});
	
	
	zmienNewsy = function(o, id, nr, poz) {
		var imageHeight = parseInt(nr);
		var imageSum = $(""+id+" ul > li").size();
		var imageReelHeight = imageHeight * imageSum;
		$(""+id+" ul").css({'height' : ''+imageReelHeight+'px'});
		var aktualTop = parseInt($(""+id+" ul").css('top').replace('px', ''));
		
		if(o == 'up') { var image_reelPosition = aktualTop - imageHeight}
	    if(o == 'down') { var image_reelPosition = aktualTop + imageHeight; }
		if (image_reelPosition > 0){ image_reelPosition = 0;}
		var zak = parseInt('-'+imageReelHeight) + poz*imageHeight;
		if (image_reelPosition < zak){image_reelPosition = zak;}
		
		$(""+id+" ul").animate({
        	top: image_reelPosition+'px'
    	}, 500 );
	};
	
	$('#nnewsy').click(function(){
		zmienNewsy('down', '#newsy', 64, 3);
	});
	$('#wnewsy').click(function(){
		zmienNewsy('up', '#newsy', 64, 3);
	})
	
	$('#ngaler').click(function(){
		zmienNewsy('down', '#galer', 64, 3);
	});
	$('#wgaler').click(function(){
		zmienNewsy('up', '#galer', 64, 3);
	})
	
	$('.down, .up, .downa, .upa, .downb, .upb, #prev2, #next2').hover(function(){
		$(this).addClass('zk');
	});
	$('.down, .up, .downa, .upa, .downb, .upb, #prev2, #next2').mouseleave(function(){
		$(this).removeClass('zk');
	});
	
	$('#kadra_down').click(function(){
		zmienNewsy('up', '#kadra', 64, 3);
	});
	$('#kadra_up').click(function(){
		zmienNewsy('down', '#kadra', 64, 3);
	})
	
	$('#wideo_down').click(function(){
		zmienNewsy('up', '#wideo', 64, 3);
	});
	$('#wideo_up').click(function(){
		zmienNewsy('down', '#wideo', 64, 3);
	})
	
	$('#tabela_down').click(function(){
		zmienNewsy('up', '#tabela', 33, 6);
	});
	$('#tabela_up').click(function(){
		zmienNewsy('down', '#tabela', 33, 3);
	})
	
	zmienSpon = function(id) {
		$('#sponsor1').slideUp('slow')
		$('#sponsor2').slideUp('slow')
		$('#sponsor3').slideUp('slow')
		$('#sponsor4').slideUp('slow')
		$('#sponsor5').slideUp('slow')
		$('#sponsor'+id).slideDown('slow');
	};
	
	next_spon = function(){
		id = $("#akt_spon").text();
		var n_numSpon= $("#sponsorzy > div").size();
		++id;
		if(id > n_numSpon){var id = 1;}
		zmienSpon(id);
		$('#akt_spon').text(id);
	};
	var refreshIntervala = setInterval("next_spon();", 5000);
	$('.prev_spon, .next_spon').click(function(){
		var title = $(this).attr('title');
		next_spon(title);
		clearInterval(refreshIntervala);
	})
	
	$('#tabelastrz_down').click(function(){
		zmienNewsy('up', '#tabela_strzelcow', 33, 6);
	});
	$('#tabelastrz_up').click(function(){
		zmienNewsy('down', '#tabela_strzelcow', 33, 6);
	})
	
	$('#wspieraja_down').click(function(){
		zmienNewsy('up', '#wspieraja', 96, 2);
	});
	$('#wspieraja_up').click(function(){
		zmienNewsy('down', '#wspieraja', 96, 2);
	})
	
	$("#foto").load(function() {
		var imageHeight = ($(this).height()-92)/2;
		var imageWidth = $(this).width();
		var ulWidth = imageWidth+38;
		$('#prev2, #next2').css('margin-top', imageHeight+'px');
		$('#zdjecie ul').css('width', ulWidth+'px');
	});
	
	zmienGaleria = function(o) {
		var imageSum = $("#min ul > li").size();
		var imageReelWidth = 135 * imageSum;
		$("#min ul").css({'width' : ''+imageReelWidth+'px'});
		var aktualLeft = parseInt($('ul', '#min').css('left').replace('px', ''));
		
		if(o == 'next') { var image_reelPosition = aktualLeft - 135}
	    if(o == 'prev') { var image_reelPosition = aktualLeft + 135; }
		if (image_reelPosition > 0){ image_reelPosition = 0;}
		var zak = parseInt('-'+imageReelWidth) + 588;
		if (image_reelPosition < zak){image_reelPosition = zak;}
		$("#min ul").animate({
        	left: image_reelPosition+'px'
    	}, 500 );
	};
	
	$('#dalej').click(function(){
		zmienGaleria('next');
	});
	$('#wroc').click(function(){
		zmienGaleria('prev');
	});
	
	$('li:last', '.lista_zaw').addClass('noborder');
	
	  var s1 = $('#home').width();
	  var s2 = $('.wynik').width();
	  var s3 = $('#away').width();
	  var sum = s1 + s2 +s3+88;
	  var margin = (490-sum)/2;
	  var nWidth = 490 - margin;
	  
	  $('#druzyny').css('padding-left', margin+'px');
	  $('#druzyny').css('width', nWidth+'px');

});
