function activeslide() {
	var a = $('#bg img');

	var browserwidth = $(window).width();
	var browserheight = $(window).height();

	var imagewidth = a.width();
	var imageheight = a.height();

	var ratio = imageheight / imagewidth;

	if ((browserheight/browserwidth) > ratio) {
	    a.height(browserheight);
	    a.width(browserheight / ratio);
	} else {
	    a.width(browserwidth);
	    a.height(browserwidth * ratio);
	}

	$('.loupe').each(function() {
		if ($('body').data('top-' + $(this).attr('id')) == null) {
			$('body').data('top-' + $(this).attr('id'), $(this).offset().top);
			$('body').data('left-' + $(this).attr('id'), $(this).offset().left);
		}

		$(this).css({
			'top': $('body').data('top-' + $(this).attr('id')) * (a.height() / 800),
			'left': $('body').data('left-' + $(this).attr('id')) * (a.width() / 1400)
		});

		// 268 je sirka menu
		if ($(this).position().left < 270) {
			$(this).css({'top': -100, 'left': -100});
		}
	});
}

$(window).bind('resize', function()	{
	activeslide();
});

$(document).ready(function() {
	activeslide();

	$('#videomatic-993837602').each(function(){
		$(this).css({'z-index':"1000"});
	});

	$('.loupe').each(function () {
		var distance = 10;
		var time = 250;
		var hide_delay = 500;

		var hide_delay_timer = null;

		var beingShown = false;
		var shown = false;

		var trigger = $('a', this);
		var popup = $('div.bubble', this);

		$([trigger.get(0), popup.get(0)]).mouseover(function () {
			if (hide_delay_timer)
				clearTimeout(hide_delay_timer);

			if (beingShown || shown)
				return;

			beingShown = true;

			popup.css({
				top: (popup.siblings('a').offset().top - 155 > 0) ? -155 : -popup.siblings('a').offset().top + 10,
				left: (popup.siblings('a').offset().left + 108 < $(window).width()) ? -108 : (popup.siblings('a').offset().left - $(window).width() - 118),
				display: 'block'
			}).animate({
				top: '-=' + distance + 'px'
			}, time, 'swing', function() {
				beingShown = false;
				shown = true;
			});

			return false;

		}).mouseout(function () {
			if (hide_delay_timer)
				clearTimeout(hide_delay_timer);

			hide_delay_timer = setTimeout(function () {
				hide_delay_timer = null;

				popup.animate({
					top: '-=' + distance + 'px'
				}, time, 'swing', function () {
					shown = false;
					popup.css('display', 'none');
				});
			}, hide_delay);

			return false;
		});
	});


});

