// menu

function getViewportLine() {
	var top = window.pageYOffset || document.documentElement.scrollTop;
	var height = document.documentElement.clientHeight;
	return Math.round(top + height / 3);
}

function getPosition($el) {
	var top = $el.offset().top;

	return {
		top: top,
		bottom: top + $el.height()
	};
}

function getList(els) {
	var list = {};

	els.each(function () {
		var hash = $(this).attr('href');
		var name = hash.substring(1);
		list[name] = getPosition($(hash));
	});

	return list;
}

function scrollToSection(hash) {
	var menuHeight = 60; // cca
	$('html,body').animate({
		scrollTop: $(hash).offset().top - menuHeight
	}, 'slow');
}

$(function () {
	function onSectionChange(name) {
		links.parent().removeClass('active');
		$('#li-' + name).addClass('active');
	}

	var links = $('#navigation ul li a');
	var currentSection = null;

	function checkSection() {
		var list = getList(links);

		var line = getViewportLine();
		for (var key in list) {
			if (list[key].top < line && list[key].bottom > line && currentSection !== key) {
				currentSection = key;
				onSectionChange(key);
				return;
			}
		}
	}

	$(window).scroll(checkSection);
	setInterval(checkSection, 300);

	window.onhashchange = function (e) {
		e.preventDefault();
		scrollToSection('#' + window.location.href.split('#')[1]);
	};

	links.click(function (e) {
		e.preventDefault();
		var hash = $(this).attr('href');
		scrollToSection(hash);
	});

	var pieces = window.location.href.split('#');
	if (pieces[1]) {
		scrollToSection('#' + pieces[1]);
	}

	$('section h2').click(function () {
		var id = $(this).parent().attr('id');
		if (id) {
			scrollToSection('#' + id);
		}
	})
});

// reference
$(function () {
	$('#portfolio .references article:not(.best)').hide();

	var callback = function () {
		$('#portfolio .buttons h3').removeClass('active');
		var $el = $(this);
		$el.parent().addClass('active');
		var category = $el.attr('data-category');

		$('#portfolio .references article:not(.' + category + ')').hide();
		$('#portfolio .references article:.' + category).show();

		return false;
	};

	$('#portfolio .buttons a').click(callback);
});

$(function () {
	// antispam
	$('#nevyzadanost-container').hide().find('input').val(3 * 5 - 2);

	// slogan tracking
	$('h2.motto').blur(function () {
		var newText = $(this).text().replace('100% testováno na lidech.', '');
		_gaq.push(['_trackEvent', 'Slogan', 'Custom', newText]);
	});

	// sortable people
	$('#lide div.crew').sortable();

	/* Rounded corners for IE */
	if ($.browser.msie && $.browser.version.substr(0,1) < 9) {
		$('#navigation li a').append('<span class="blc" /><span class="brc" />');
		$('.buttons h3 a').append('<span class="tlc" /><span class="trc" /><span class="blc" /><span class="brc" />');
		$('.btn').append('<span class="tlc" /><span class="trc" /><span class="blc" /><span class="brc" />');

		var settings = {
			tl: {
				radius: 5
			},
			tr: {
				radius: 5
			},
			bl: {
				radius: 5
			},
			br: {
				radius: 5
			},
			antiAlias: true
		}

		curvyCorners(settings, '.input-wrapper');
	}
});

