function PendulumSim(length_m, gravity_mps2, initialAngle_rad, timestep_ms, callback) {
	var velocity = 0;
	var angle = initialAngle_rad;
	var k = -gravity_mps2/length_m;
	var timestep_s = timestep_ms / 1000;
	return setInterval(function () {
		var acceleration = k * Math.sin(angle);
		velocity += acceleration * timestep_s;
		angle    += velocity     * timestep_s;
		callback(angle);
	}, timestep_ms);
}

$(function() {
	$("#head-wrapper header h2, .promocje h2, .katalog h2").text("");
	
	
	$.each($('.gallery').not('.wyroznienia, .nagrody'), function() {
		$.each($('a', this), function(i, e) {
			i++;
			if(i/3 === parseInt(i/3,10))
				$(e).css({marginRight: "0"});
		});
	});

	if($(".page").length)
		$(".gallery a").each(function() {
			if($(this).attr('href').indexOf('pdf') == -1) {
				$(this).colorbox({
					transition: "fade",
					rel: "gallery",
					maxWidth: '60%',
					maxHeight: '60%'
				});
			}
		});
	
	$('#offer li').click(function() {
		window.location = $('a', this).attr('href');
	});

	if(!($.browser.msie && parseInt($.browser.version, 10) < 9)) {
		var canvas = document.getElementById('canvas');
		var context = canvas.getContext('2d');
		var prev=0;
		var sim = PendulumSim(1, 9.80665, Math.PI*99/50, 10, function (angle) {
			var rBar = Math.min(canvas.width, canvas.height) * 0.005;

			var jakosc = new Image();
			jakosc.src = $("#canvas img").attr('src');

			context.fillStyle = "rgba(255,255,255,0.5)";
			context.globalCompositeOperation = "destination-out";
			context.fillRect(0, 0, canvas.width, canvas.height);
			context.globalCompositeOperation = "source-over";

			context.save();
			context.translate(130, -60);
			context.rotate(angle);
			context.drawImage(jakosc, -75, 0);
			context.restore();
			
			prev=angle;
		});
	}
	
	var height_zaklad = 960;
	var height_wyroznienia = $('.wyroznienia').height();
	var height_nagrody = $('.nagrody').height();
	
	$('.hide').css({height: 125});
	$('.wyroznienia').css({height: 200});
	$('.nagrody').css({height: 155});
	
	$('.show-more').toggle(function() {
		if($(this).prev().is('.zaklad'))
		{
			$(this).prev().animate({height: height_zaklad});
			$(this).html('Ukryj &laquo;');
		}
		if($(this).prev().is('.wyroznienia'))
		{
			$(this).prev().animate({height: height_wyroznienia});
			$(this).html('Ukryj wyróżnienia &laquo;');
		}
		if($(this).prev().is('.nagrody'))
		{
			$(this).prev().animate({height: height_nagrody});
			$(this).html('Ukryj nagrody &laquo;');
		}
	}, function() {
		if($(this).prev().is('.zaklad'))
		{
			$(this).prev().animate({height: 125});
			$(this).html('Pokaż więcej &raquo;');
		}
		if($(this).prev().is('.wyroznienia'))
		{
			$(this).prev().animate({height: 200});
			$(this).html('Pokaż więcej wyróżnień &raquo;');
		}
		if($(this).prev().is('.nagrody'))
		{
			$(this).prev().animate({height: 155});
			$(this).html('Pokaż więcej nagród &raquo;');
		}
	});
});

$(window).load(function() {
	var galleryElements = $('.offer a');
	$.each(galleryElements, function(i, e) {
		i++;
		$('img', e).css({top: 54-$('img', e).height()/2});
	});
	
	if($('.page').length)
		/*$('.gallery a[title]').qtip({
			content: {
				attr: 'alt'
			},
			position: {
				my: 'bottom left',
				target: 'mouse',
				adjust: {
					x: 5,
					y: -5
				}
			},
			show: {
				effect: function(offset) {
					$(this).fadeIn(200);
				}
			},
			style: {
				classes: 'ui-tooltip-dark ui-tooltip-shadow ui-tooltip-rounded'
			}
		});*/
		
		$('.offer a img[alt]').each(function() {
			var opis = $(this).attr('alt');
			$(this).parent().attr({'title': opis});
			$(this).after('<span>'+opis+'</span>');
		});
});
