//ScrollHistory
function ScrollHistory (target, years) {
	//alert("ScrollHistory(" + [target, years] + ")");
	
	this.target = target;
	this.content = target + " .content";
	this.btn_minus = target + " .minus";
	this.btn_plus = target + " .plus";
	this.visible_items = 7;
	this.years = years;
	this.years_length = years.length;
	this.max = this.years_length - this.visible_items;
	this.min = 0;
	
	var center_offset = Math.floor(this.visible_items / 2);
	this.slide = current_year != null ? this.getIndexByYear(current_year) - center_offset : 0;
	
	var p = this;
	$(target).append('<span class="content"></span>');
	$(target).prepend('<a class="minus"><span>-</span></a>');
	$(target).append('<a class="plus"><span>+</span></a>');
	
	$(this.btn_minus).click(function () {p.slideToPrevious();});
	$(this.btn_plus).click(function () {p.slideToNext();});
	
	this.slideTo(this.slide);
	
	return this;
}

ScrollHistory.prototype.slideToPrevious = function () {
	//alert("ScrollHistory.slideToPrevious()");
	
	this.slideTo(this.slide - 1);
}

ScrollHistory.prototype.slideToNext = function () {
	//alert("ScrollHistory.slideToNext()");
	
	this.slideTo(this.slide + 1);
}

ScrollHistory.prototype.slideTo = function (value) {
	//alert("ScrollHistory.slideTo(" + value + ")");
	
	value = Math.max(Math.min(value, this.max), this.min);
	
	$(this.content).empty();
	
	var i;
	var num;
	var year;
	for (i = 0; i < this.visible_items; i++) {
		num = value + i; 
		year = this.years[num];
		$(this.content).append('<a class="y' + year + '" href="/history/' + year + '/">&nbsp;' + year + '&nbsp;</a>');
		if (i != this.visible_items - 1) $(this.content).append('|');
	}
	
	var display;
	display = value == 0 ? "none" : "inline";
	$(this.btn_minus + " span").css("display", display);
	
	display = value == this.max ? "none" : "inline";
	$(this.btn_plus + " span").css("display", display);
	
	this.slide = value;
}

ScrollHistory.prototype.getIndexByYear = function (value) {
	//alert("ScrollHistory.getIndexByYear()");
	
	var index = 0;
	
	var i;
	for (i = 0; i < this.years_length; i++) {
		if (this.years[i] == value) break;
	}
	
	return i;
}

var scrollbar;
var scrollhistory;
$(function () {
		scrollbar = new ScrollBar('#area .slider', '#area');
		scrollhistory = new ScrollHistory("#navih", [1979, 1980, 1990, 1995, 2000, 2003, 2004, 2005, 2006, 2007, 2008, 2009]);
	}
);


