(function () {
	'use strict';

	var INTERVAL = 10;

	var _showSlide = function (el) {
		_curr_slide = el;
		var t = $(el);

		$('#intro .slides_menu .active, #intro .slide.active').removeClass('active');

		$(t.attr('href')).addClass('active');
		t.parent().addClass('active');

		_setTimer();
	};

	var _setTimer = function () {
		_timer && clearTimeout(_timer);

		_timer = setTimeout(function () {
			var i = _slides.toArray().indexOf(_curr_slide);
			i += 1;
			if (i === _slides.size()) i = 0;
			_showSlide(_slides[i]);
		}, INTERVAL * 1000);
	};

	// init --------------------------------------------------------------------

	var _slides = $('#intro .slides_menu a'),
		_curr_slide = null,
		_timer = null;

	_slides.click(function (event) {
		_showSlide(this);
		return false;
	});

	_showSlide(_slides[0]);
}());

(function self(opts) {
	'use strict';

	var SLIDE_WIDTH = 180,
		INTERVAL = 8;
	
	var _updateSlides = function () {
		_first_slide = _normalizeSlide(_curr_slide + _in_view + _on_right);
		_last_slide = _normalizeSlide(_curr_slide + _in_view + _on_right - 1);
	};

	var _normalizeSlide = function (index) {
		if (index < 0)
			return _normalizeSlide(_slides.size() + index);
		if (index >= _slides.size())
			return _normalizeSlide(index % _slides.size());
		return index;
	};

	var _showSlide = function (dir) {
		var new_slide = _normalizeSlide(_curr_slide + dir),
			last_el = $(_slides.get(_last_slide)),
			first_el = $(_slides.get(_first_slide));

		_left -= dir * SLIDE_WIDTH;
		_el.find('.slides').css({ left: _left + 'px' });

		if (dir > 0) {
			first_el.css({ left: parseInt(last_el.css('left'), 10) + SLIDE_WIDTH + 'px' });
		}
		else {
			last_el.css({ left: parseInt(first_el.css('left'), 10) - SLIDE_WIDTH + 'px' });
		}

		_curr_slide = new_slide;
		_updateSlides();
		_setTimer();
	};

	var _setTimer = function () {
		_timer && clearTimeout(_timer);
		_timer = setTimeout(function () {
			_showSlide(1);
		}, INTERVAL * 1000);
	};

	var _init = function () {
		_slides = _el.find('.slides > li');
		_on_left = ~~((_slides.size() - _in_view) / 2);
		_on_right = _slides.size() - _in_view - _on_left;
		_updateSlides();

		_slides.each(function (i) {
			if (i >= _in_view + _on_right) {
				i -= _slides.size();
			}

			$(this).css({ left: i * SLIDE_WIDTH + 'px' });
		});

		_el.find('.arr.next').click(function () {
			_showSlide(1);
			return false;
		});
		_el.find('.arr.prev').click(function () {
			_showSlide(-1);
			return false;
		});

		_setTimer();
	};

	// init --------------------------------------------------------------------

	var _el = opts.el,
		_left = 0,
		_in_view = opts.visible_slides,
		_on_left = 0,
		_on_right = 0,
		_curr_slide = 0,
		_last_slide = null,
		_first_slide = null,
		_slides = null,
		_timer = null;

	_init();

	// call self unless arguments array is empty
	if (arguments.length > 1) {
		self.apply(null, [].slice.call(arguments, 1));
	}
}(
	{ el: $('#whowe .slider'), visible_slides: 1 },
	{ el: $('#refs .slider'), visible_slides: 4 }
));

(function () {
	'use strict';

	var _showSlide = function (el) {
		_curr_slide = el;
		var t = $(el);

		$('#refs .slides .active, #refs .refs_list .active').removeClass('active');

		$(t.attr('href')).addClass('active');
		t.parent().addClass('active');
	};

	// init --------------------------------------------------------------------

	var _slides = $('#refs .slides a'),
		_curr_slide = null;

	_slides.click(function (event) {
		_showSlide(this);
		return false;
	});

	_showSlide(_slides[0]);
}());

(function () {
	'use strict';
	
	var SCROLL_MIN = 40;

	var _scrolled = false;

	$(window).scroll(function (event) {
		if (window.scrollY > SCROLL_MIN) {
			if (_scrolled) return;
			_scrolled = true;
			$('#top').addClass('scrolled');
		}
		else {
			if (!_scrolled) return;
			_scrolled = false;
			$('#top').removeClass('scrolled');
		}
	});
}());

(function () {
	'use strict';

	var OFFSET = -140;

	$(document).on('click', 'a[href^=#]', function (event) {
		var t = $(this),
			id = t.attr('href');

		if (id === '#') id = 'body';

		$.scrollTo(Math.max(~~$(id).offset().top + OFFSET, 0), 1000);
		return false;
	});
}());

(function () {
	'use strict';

	var _req = function (id) {
		if ($.trim($('#' + id).val()) === '') {
			throw new Error('Pola oznaczone gwiazdką są wymagane');
		}
	};

	var _email = function (id) {
		// http://www.regular-expressions.info/email.html
		var r = /[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/i

		if (!$.trim($('#' + id).val()).match(r)) {
			throw new Error('Podaj prawidłowy adres e-mail');
		}
	};

	$('#contact form').submit(function (event) {
		try {
			_req('contact_name');
			_req('contact_email');
			_req('contact_msg');
			_email('contact_email');
		}
		catch (e) {
			alert(e.message);
			return false;
		}
		return true;
	});
}());

$(document).ready(function() {

	$('#contact .data .map').fancybox({
		width: '80%',
		height: '80%'
	});

});

