var scrollLoader;

$(document).ready(function() {
	if (!ie || ie > 6) scrollLoader = new ScrollLoader();
});

var onScroll = function() {
	scrollLoader.onScroll();
}


//--------------------- ScrollLoader ---------------------//
var ScrollLoader = function() {
	this.window = $(window);
	this.html = $('body');
	this.contents = $("#contents");
	this.loading = $('<div id="loading" class="loader"><img src="images/common/loader.gif" width="24" height="24" /></div>');
	this.pageHeight = 0;
	this.isLoading = false;
	this.index = 0;
	this.currentContent = 0;
	
	//this.labels = ['index', 'memoir', 'no241', 'access', 'surrounding', 'info', 'faq', 'inquiry'];
	this.labels = ['index', 'memoir', 'no241', 'access', 'surrounding', 'info', 'inquiry'];
	
	this.init();
}
ScrollLoader.prototype.init = function() {
	this.window.scroll(onScroll);
	
	var arr = location.href.split('/');
	var key = arr[arr.length-1].split('.')[0];
	if (!key) this.index = 0;
	else this.index = this.labels.indexOf(key);
	this.currentContent = this.index;
	
	this.changeMenu();
	this.checkAddon();
	this.calcHeight();
}
ScrollLoader.prototype.calcHeight = function() {
	this.pageHeight = this.html.height();
	//trace2('height: ' + this.pageHeight);
}
ScrollLoader.prototype.onScroll = function() {
	var scrollTop = this.window.scrollTop()
	var scrollBottom = scrollTop + this.window.height();
	
	if (this.index < (this.labels.length - 1)) {
		if ((this.pageHeight - scrollBottom) < 10 && !this.isLoading) {
			/*
			trace2('---bottom---');
			trace2('scrollBottom: ' + scrollBottom);
			trace2('pageHeigh: ' + this.pageHeight);
			trace2('window: ' + this.window.height());
			trace2('------------');
			*/
			this.index++;
			this.isLoading = true;
			this.loading.css({opacity:1}).appendTo(this.contents).hide().fadeIn(300);
			
			setTimeout(function() {
				scrollLoader.fetchContent();
			}, 1000);
		}
		this.checkCurrentContent(scrollTop);
	}
	else if (this.index == (this.labels.length - 1)) {
		if ((this.pageHeight - scrollBottom) < 10 && !this.isLoading) {
			if (this.currentContent != this.index) {
				this.currentContent = this.index;
				this.changeMenu();	
			}
		}
		else this.checkCurrentContent(scrollTop);
	}
}
ScrollLoader.prototype.fetchContent = function() {
	$.get(this.labels[this.index] + '.html', function(data){
		$(data).each(function(){
			if ($(this).is('#contents')) {
				var article = $(this).children('#article_' + scrollLoader.labels[scrollLoader.index])[0];
				scrollLoader.loading.animate({opacity:0}, {duration:300});
				
				setTimeout(function() {
					$((article)).appendTo(scrollLoader.contents).hide().fadeIn(400);
					scrollLoader.loading.remove();
					scrollLoader.checkAddon();
					scrollLoader.loadScrollSmoothy();
					
					scrollLoader.calcHeight();
					scrollLoader.isLoading = false;
				}, 400);
			}
		});
	});
}
ScrollLoader.prototype.checkAddon = function() {
	if (this.index == 2 || this.index == 4) initFancybox();
	//else if (this.index == 6) initFaq();
	else if (this.index == 6) inquiry = new Inquiry;
}
ScrollLoader.prototype.checkCurrentContent = function(pos) {
	if (pos < 1) return;
	
	for (var i=this.labels.length-1; i>=0; i--) {
		var label = this.labels[i];
		
		if (i < this.index || (i == this.index && $('#article_' + label).length > 0)) {
			var contentPos = $($('#article_' + label)[0]).offset().top;
			
			if (pos > contentPos) {
				if (this.currentContent != i) {
					this.currentContent = i;
					this.changeMenu();	
				}
				return;
			}
		}
	}
}
ScrollLoader.prototype.changeMenu = function() {
	for (var i=this.labels.length-1; i>0; i--) {
		if (i == this.currentContent) $('#nav_' + this.labels[i]).addClass('navon');
		else $('#nav_' + this.labels[i]).removeClass('navon');
	}
}
ScrollLoader.prototype.loadScrollSmoothy = function() {
	$.getScript('./js/scrollsmoothly.js');
}
//--------------------- ScrollLoader ---------------------//




