var historyYears;
var historyDetails;
var historyData = {};
$(function() {
	historyYears = $("#history-years .scrollable").scrollable({
		vertical:true,
		size: 5
	}).circular({api:true});
	
	historyDetails = $("#history-details .scrollable").scrollable({
		vertical:true,
		size: 1,
		api:true
	});
	/*
	$("#history-year-items").click(function(event) {
		var $target = $(event.target);
		if ($target.hasClass("history-year")) {
			var pageName = $target.text();
			$.ajax({
				url: "/inc/company-history/" + pageName + ".php",
				success: function(data) {
					historyDetails.getItemWrap().html(data).css('top', '0px');
					historyDetails.reload();
					if (historyDetails.getItems().length == 1) {
						$("#history-details .nextPage").addClass('disabled');
					} else {
						$("#history-details .nextPage").removeClass('disabled');
					}
					$("#history-details .prevPage").addClass('disabled');
				}
			});
		}
	});
	*/
	$('#history-years .prevYear').click(function(){
		var index = historyYears.getIndex();
		historyYears.click(index+1);
		getHistory();
	});
	$('#history-years .nextYear').click(function(){
		var index = historyYears.getIndex();
		historyYears.click(index+3);
		getHistory();
	});
	function getHistory(){
		var pageName = $('#history-year-items .active').text();
		
		if(historyData[pageName] != undefined)
		{
			setHistory(historyData[pageName]);
		}
		else
		{
			$.ajax({
				url: "/inc/company-history/" + pageName + ".php",
				success: function(data) {
					historyData[pageName] = data;
					setHistory(data);
				}
			});
		}
	}
	function setHistory(data)
	{
		historyDetails.getItemWrap().html(data).css('top', '0px');	
		if (historyDetails.getItems().length == 1) {
			$("#history-details .nextPage").addClass('disabled');
		} else {
			$("#history-details .nextPage").removeClass('disabled');
		}
		$("#history-details .prevPage").addClass('disabled');
		historyDetails.reload();
		historyDetails.begin();
	}
	$('#history-year-items .history-year').unbind('click');
});

/*
$(document).ready(function(e) {
	
	// build the year list.
	var companyHistory = $('#company-history');
	if (companyHistory) {
		var yearList = $('#company-info-years');
		yearList.empty();
		var years = {};
		$('#company-info-details li').each(function(e) {
			if (this.getAttribute('title')) {
				years[this.getAttribute('title')] = true;
			}
		});

		$.each(years, function(key, value) {
			yearList.append("<li>"+key+"</li>");
		})
		
		
		var textSizeMax = 20;
		var textSizeMin = 10;
		var colorStart = [246,242,172];
		var colorEnd = [44,44,44];
		$('#company-info-years li').mouseover(function() {
			// this item.
			var me = $(this);
			me.css('font-size',textSizeMax+"px");
			me.css('color','rgb('+colorStart[0]+','+colorStart[1]+','+colorStart[2]+')');
			// items before the moused over one
			var prevSiblings = me.prevAll();
			var prevSiblingsLength = prevSiblings.length;
			var prevTextSizeStep = (textSizeMax - textSizeMin) / prevSiblingsLength;
			var prevColorStep = [
				parseInt((colorStart[0] - colorEnd[0]) / prevSiblingsLength),
				parseInt((colorStart[1] - colorEnd[1]) / prevSiblingsLength),
				parseInt((colorStart[2] - colorEnd[2]) / prevSiblingsLength)
			];
			currentTextSize = textSizeMax;
			currentRGB = colorStart;
			prevSiblings.each(function(i) {
				self = $(this);
				self.css('font-size',currentTextSize+"px");
				currentTextSize -= prevTextSizeStep;
				self.css('color','rgb('+currentRGB[0]+','+currentRGB[1]+','+currentRGB[2]+')');
				currentRGB = [
					currentRGB[0]-prevColorStep[0],
					currentRGB[1]-prevColorStep[1],
					currentRGB[2]-prevColorStep[2]
				];
			})

			// items after the moised over one.
			var nextSiblings = me.nextAll();
			var nextSiblingsLength = nextSiblings.length;
			var nextTextSizeStep = (textSizeMax - textSizeMin) / nextSiblingsLength;
			var nextColorStep = [
				parseInt((colorStart[0] - colorEnd[0]) / nextSiblingsLength),
				parseInt((colorStart[1] - colorEnd[1]) / nextSiblingsLength),
				parseInt((colorStart[2] - colorEnd[2]) / nextSiblingsLength)
			];
			currentTextSize = textSizeMax;
			currentRGB = colorStart;
			nextSiblings.each(function(i) {
				self = $(this);
				self.css('font-size',currentTextSize+"px");
				currentTextSize -= nextTextSizeStep;
				self.css('color','rgb('+currentRGB[0]+','+currentRGB[1]+','+currentRGB[2]+')');
				currentRGB = [
					currentRGB[0]-nextColorStep[0],
					currentRGB[1]-nextColorStep[1],
					currentRGB[2]-nextColorStep[2]
				];

			})
			
		});
		$('#company-info-years li').click(function() {
			var self = $(this);
			// scroll to the first item of this year
			var scrollToElement = $('#company-info-details li[title="'+this.innerHTML+'"]:first');
			var itemHeight = scrollToElement.height();
			var totalPrecedingItems = scrollToElement.prevAll().length;
			var distanceFromTop = totalPrecedingItems * itemHeight;
			$('#company-info-details').animate({'top':-distanceFromTop},1000);
		});
		// initialize the color/text of the year list
		var years = $('#company-info-years li');
		var middle = Math.floor(years.length/2);
		$('#company-info-years li:eq('+middle+')').mouseover();
		
		$('#company-history-up').click(function() {
			var currentHeight = parseInt($('#company-info-details').css('top'));
			var itemHeight = $('#company-info-details li:first').height();
			$('#company-info-details').animate({'top':(currentHeight - itemHeight)},200);
		});
		
		$('#company-history-down').click(function() {
			var currentHeight = parseInt($('#company-info-details').css('top'));
			if (currentHeight < 0) {
				var itemHeight = $('#company-info-details li:first').height();
				$('#company-info-details').animate({'top':(currentHeight + itemHeight)},200);
			}
		});
		
		
	} else { alert('Something went horribly wrong.'); }
});
*/