var scrolling = false;

$(document).ready(function(){
	readyTab();
});

function readyTab(){
	addEvent();
	scrollLeftEvent();
	scrollRightEvent();
	updateTab();
}

function scrollLeft() {
	if (!scrolling) {
		if ($(".tctRect").position().left < 0) {
			scrolling = true;
			var newOffset = (Math.round($(".tctRect").position().left / 130) * 130) + 130;
			$(".tctRect").animate({ left: newOffset+'px' }, 160, "linear", function() { updateTab(); });
		}
	}
}
function scrollRight() {
	if (!scrolling) {
		if ((($(".tctRect > a").length * 130) + $(".tctRect").position().left) > 910) {
			scrolling = true;
			var newOffset = (Math.round($(".tctRect").position().left / 130) * 130) - 130;
			$(".tctRect").animate({ left: newOffset+'px' }, 160, "linear", function() { updateTab(); });
		}
	}
}

function updateTab() {
	var tabs = $(".tctRect > a").length;
	var width = tabs * 130;
	var offset = $(".tctRect").position().left;
	// Show Left Scroll
	if (offset < 0) {
		$(".tctScrollL").fadeIn(160);
		if ((width + offset) > 910) {
			$(".tctScrollR").fadeIn(160);
		} else {
			$(".tctScrollR").fadeOut(160);
		}
	} else {
		$(".tctScrollL").fadeOut(160);
		if (width > 910) {
			$(".tctScrollR").fadeIn(160);
		} else {
			$(".tctScrollR").fadeOut(160);
		}
	}
	
	scrolling = false;
}

function scrollLeftEvent() {
	$(".tctScrollL").click(function(e){ e.preventDefault(); scrollLeft();});
}
function scrollRightEvent() {
	$(".tctScrollR").click(function(e){ e.preventDefault(); scrollRight();});
}

function addEvent() {
	$(".tctRect > a").unbind("mouseenter").unbind("mouseleave").fadeTo(1, 0.75);
	$(".tctRect > .tctA").fadeTo(1, 1.0);
	$(".tctRect > .tctI").each(function() {
		$(this).fadeTo(1, 0.75);
		$(this).mouseenter(function() {
			$(this).fadeTo(160, 1.0);
		}).mouseleave(function() {
			$(this).fadeTo(160, 0.75);
		});
	});
}
