window.kw = (function (kw, document, $) {
	var oNav, bAjaxing = false;

	kw.init = function () {
		oNav = $('#nav');
		kw.thumbInteraction();
		kw.googleFontLoader();
	};

	kw.navInteraction = function () {
		var oCurrentElement = oNav.find('.current'), iCurrentIndex = oCurrentElement.index(), iCurrentWidth = oCurrentElement.width(), oHoverBlock;

		$('#header')
			.append('<div class="hover-block"><!-- --></div>')
			.find('.current')
			.removeClass('current')
			.find('a')
			.addClass('js-active');

		oHoverBlock = $('.hover-block');

		oHoverBlock.css({'right': kw.getOffsets(iCurrentIndex), 'width': iCurrentWidth});

		oNav.find('li').hover(function () {
			var oNewElement = $(this).index();
			oHoverBlock.stop().animate({'right': kw.getOffsets(oNewElement), 'width': $(this).width()});
		}).find('a').click(function () {
			if (!bAjaxing) {
				iCurrentIndex = $(this).parent().index();
				iCurrentWidth = $(this).parent().width();
			}
		});

		oNav.bind('mouseleave', function () {
			oHoverBlock.stop().animate({ 'right': kw.getOffsets(iCurrentIndex), 'width': iCurrentWidth });
		});
	};

	kw.getOffsets = function (iCurrentIndex) {
		var iOffset = oNav.width(), i;
		for (i = 0; i <= iCurrentIndex; i += 1) {
			iOffset -= oNav.find('li:eq(' + i + ')').width();
		}

		return iOffset;
	};

	kw.thumbInteraction = function () {
		$('.work-preview').each(function () {
			var oImage = $(this).find('img'), sHoverImageSrc = oImage.attr('src').split('.', 1)[0] + '_on.jpg', sImageAlt = oImage.attr('alt');
			oImage.after('<img class="hover" src="' + sHoverImageSrc + '" width="200" height="110" alt="' + sImageAlt + '" />');
		});
	};

	kw.googleFontLoader = function () {
		WebFontConfig = {
			google: { families: ['RockwellRegular'] },
			active: function () { kw.navInteraction(); }
		};
		(function () {
			var wf = document.createElement('script'), s;
			wf.src = ('https:' === document.location.protocol ? 'https' : 'http') + '://ajax.googleapis.com/ajax/libs/webfont/1/webfont.js';
			wf.type = 'text/javascript';
			wf.async = 'true';
			s = document.getElementsByTagName('script')[0];
			s.parentNode.insertBefore(wf, s);
		}());
	};

	return kw;

}(window.kw || {}, document, jQuery));

$(function () { kw.init(); });
