var Layout = function () {
	var isMobileDevice = function() {
		return  ((
			navigator.userAgent.match(/Android/i) ||
			navigator.userAgent.match(/BlackBerry/i) ||
			navigator.userAgent.match(/iPhone|iPad|iPod/i) ||
			navigator.userAgent.match(/Opera Mini/i) ||
			navigator.userAgent.match(/IEMobile/i)
		) ? true : false);
	}

	function removeHash() { 
		var scrollV, scrollH, loc = window.location;
		if ("pushState" in history)
			history.pushState("", document.title, loc.pathname + loc.search);
		else {
			// Prevent scrolling by storing the page's current scroll offset
			scrollV = document.body.scrollTop;
			//scrollH = document.body.scrollLeft;

			loc.hash = "";

			// Restore the scroll offset, should be flicker free
			document.body.scrollTop = scrollV;
			//document.body.scrollLeft = scrollH;
		}
	}
	
	var handleNav = function() {
		if ($(window).outerWidth(true) >= 992) {
			var offset = $('.navbar').outerHeight(true);
		}
		else {
			var offset = $('.navbar-header').outerHeight(true) + 8;
		}
		
		$('.navbar-static-top').affix({
			offset: {
				top: function () {
					return (this.bottom = $('#masthead').outerHeight(true));
				}
			}
		});
		$('.navbar-static-top').on('affixed.bs.affix', function () {
			$('body').addClass('affixednav');
		});
		$('.navbar-static-top').on('affixed-top.bs.affix', function () {
			$('body').removeClass('affixednav');
		});

		if ($('body').hasClass('index')) {
			$('body').scrollspy({
				target: '#navbar',
				offset: offset + 1
			});
			$('body').on('activate.bs.scrollspy', function () {
				$('.navbar-nav a').blur();
			});
			
			$('.navbar-nav a').click(function(e) {			
				var hash = this.hash.substring(this.hash.indexOf('#'));

				e.preventDefault();
				 
				var target = $(this).attr('href').substring($(this).attr('href').indexOf('#'));
			 
				$('html, body').animate({
					scrollTop: $(target).offset().top - offset
				}, 500, function(){
					if(window.location.hash) {
						//window.location.hash = '';
						removeHash();
					}					
				});
				
				$('#navbar').collapse('hide');	
			});
		}

		if (window.location.hash) {
			$('html, body').animate({
				scrollTop: $(window.location.hash).offset().top - offset
			}, 1, function(){
				//window.location.hash = '';
				removeHash();
			});
		}
    }
	
    return {
        init: function () {
            // init core variables
            handleNav();
        },

        initIndexParallax: function () {
			if (!isMobileDevice()) {
				$('#vallalkozasfejlesztes_bg').parallax("50%", 0.5);
				$('#turizmus_bg').parallax("50%", 0.5);
				$('#kornyezetvedelem_bg').parallax("50%", 0.5);
			}
			else {
				$('#vallalkozasfejlesztes_bg').css({ "background-attachment": "scroll", "background-size": "cover" });
				$('#turizmus_bg').css({ "background-attachment": "scroll", "background-size": "cover" });
				$('#kornyezetvedelem_bg').css({ "background-attachment": "scroll", "background-size": "cover" });
			}
        },

        initPageParallax: function () {
			if (!isMobileDevice()) {
				$('#kategoria_cim').parallax("50%", 0.4);
			}
			else {
				$('#kategoria_cim').css({ "background-attachment": "scroll", "background-size": "cover" });
			}
        },

        initIndexCarousel: function () {
			$('#main-carousel').on('slide.bs.carousel', function () {
				$('.carousel-caption').fadeOut();
			});
			$('#main-carousel').on('slid.bs.carousel', function () {
				var caption = $(this).find('.active').find('.carousel-caption');
				caption.slideToggle();
			});
        }
    };
}();
