var frame = ($.browser.webkit) ? 'body' : 'html';
scrolling = false;
mobile = (window.innerWidth <= 535) ? true : false;

function showTab(tab) {
	if(	$('#'+tab).hasClass('closed-box')) {
		$('.box-outer').addClass('closed-box');
		$('#'+tab).removeClass('closed-box');
		$('.box-inner').slideUp(50);
		$('#'+tab+' .box-inner').slideDown(50);
		$('#'+tab+' .box-inner').slideDown(50);
	}
}

function activate(tab){
if(tab) { // alert(tab);
	$('#nav a').removeClass('active');
	$('#nav-'+tab).addClass('active');
	if(tab == 'event') {
		$('#nav').addClass('first-link-active');
	} else {
		$('#nav').removeClass('first-link-active');
	}
}
}

function navScroll(){

	var pages = [];
	
	$('#nav a.local').each(function() {
		var page = {};
		
		var href = $(this).attr("href");
		var href = href.split("#");

		page.href = href[1];
		page.offset = $("#" + page.href).offset();
		pages.push(page);
	});

	var buffer = $(window).height() / 3;

 	$(document).scroll(function() {
	
		var scrollTop = $(frame).scrollTop();
		var last = $(pages).size() - 1;
	
		$(pages).each(function(index, page) {
			if ( scrollTop > (pages[index].offset.top - buffer) ) {
				if (scrollTop > (pages[last].offset.top - buffer) || scrollTop < (pages[index + 1].offset.top - buffer)) {
					if(!scrolling) {
						activate(page.href);
					}
				}
			}
		});
 	});
}

// function scrollPage(target) {
// 	var offset = $("#" + target).offset();
// 	if(offset){
// 		scrolling = true;
// // 		$(frame).animate({scrollTop: offset.top - 129}, 500, function(){ scrolling = false; });
// 		$(frame).animate({scrollTop: offset.top}, 500, function(){ scrolling = false; });
// 	}
// }

// function scroller(hash) {
// 		$.localScroll({
// 			onBefore: function() { scrolling = true; },
// 			onAfter: function() {
// 				scrolling = false; 
// // 				var hash = window.location.hash.replace('#', '');
// // 				activate(hash);
// 			},
// 			duration: 300
//  			,easing: 'swing'
// 		});
// 		activate(hash);
// 
// }

$(document).ready(function() {

	if (navigator.appVersion.indexOf("MSIE") != -1) {
      windowHeight = document.documentElement.offsetHeight;
    } else {
      windowHeight = window.innerHeight;
    }
	
	if(!mobile) {
		if($('.standalone').length == 0) {
			var lastPage = $('.page').last();
			var marginBottom = windowHeight - $(lastPage).height() - 171;
			$('#content-outer').css('margin-bottom', marginBottom);
		}
		$('#news').addClass('closed-box');
		$('.closed-box .box-inner').hide();
	}
	
	navScroll();

// 	if(window.location.hash) {
		$.localScroll({
			hash: true,
			onBefore: function() { scrolling = true; },
			onAfter: function() {
				scrolling = false; 
				var hash = window.location.hash.replace('#', '');
				activate(hash);

			},
			duration: 300
 			,easing: 'swing'
		});
// 	}
	
	$(function() {
		$('.thumb a').lightBox({fixedNavigation:true});
	});

});
