/**
 * @author bheyer
 */
var boxSideMargins = 10;
var boxTopMargins = 5;
var activeBoxId = "";
var tapeWidth = 0;
var activeTabId = "";
var globalLeft = 0;

function tabClick(e) {
	/* reset tabs */				
	$("#tabs li div").removeClass("te_active_right").removeClass("te_active_left").removeClass("tb_active").removeClass("te_active_first").removeClass("te_active_last");
	$("#tabs li:first div:first").addClass("te_first");
		
	/* make new tab active */
	/* check for only one tab */
	//alert($(this).parent().find("li").size()); 
	if ($(this).parent().find("li").size() == 1) {
		$(this).find("div:first").addClass("te_active_first")
			.next().addClass("tb_active")
			.next().addClass("te_active_last");
	}
	/* check if new tab is first */
	else if ($(this).find("a").attr("name") == $("#tabs li:first a").attr("name")) {
		$(this).find("div:first").addClass("te_active_first")
			.next().addClass("tb_active")
			.parent().next().find("div:first").addClass("te_active_right");
	}
	/* check if new tab is last */
	else if ($(this).find("a").attr("name") == $("#tabs li:last a").attr("name")) {
		$(this).find("div:first").addClass("te_active_left")
			.next().addClass("tb_active")
			.next().addClass("te_active_last");
	}
	/* tab is in the middle */	
	else {
		$(this).find("div:first").addClass("te_active_left")
			.next().addClass("tb_active")
			.parent().next().find("div:first").addClass("te_active_right");
	}
		
	/* swap tabs */
	$("div.tab").hide();	
	$($(this).find("a").attr("name")).show();
	activeTabId = $(this).find("a").attr("name").replace("#", "");	
	setTapeWidth();
	
	e.preventDefault();
	return false;
}

 
 $(document).ready(function() {
 	$("#tabs li").each(function() {
		$(this).html("<div class='tab_edge te_inner'></div><div class='tab_body'>" + $(this).html() + "</div>");		
	});
	
	$("#tabs li:first div.tab_edge").addClass("te_active_first")
		.next().addClass("tb_active")
		.parent().next().find(".tab_edge").addClass("te_active_right");
	

	if ($("#tabs li").size() == 1)
		$("#tabs li:last").html($("#tabs li:last").html() + "<div class='tab_edge te_active_last'></div>");
	else
		$("#tabs li:last").html($("#tabs li:last").html() + "<div class='tab_edge te_last'></div>");

 	$("#tabs li").click(tabClick);
 	$("#tabs li a").click(function(e) {
		$(this).parent().click();
		return false;
	});
	
 	$("div.tab").hide().each(function() {
		$(this).html("<div class='tab_in_cont'>" + $(this).html() + "</div>");
	});
		
	$("div.tab:last").after("<div class='tab_footer'> </div>");
				
	if (document.location.hash != "") {
		var gotoTab = document.location.hash.substr(2);
		var temp = new Array();
		temp = gotoTab.split("@");
			
		$("#" + temp[0]).show();
		activeTabId = temp[0];
		$("#tabs li a[name='#" + temp[0] + "']").click();
	} else {
		$($("#tabs li a").attr("name")).show();
		activeTabId = $("#tabs li a").attr("name").replace("#", "");
	}			
 });
 
 
 
/* SLIDER BOX CODE */
/* TODO: Combine with above code */
/* TODO: Generalize */
function scrollTo(boxId, time) {
	if (time == null) time = 500;
	
	var destOffset = $("#" + boxId).offset().left;
	var currOffset = $("#" + activeBoxId).offset().left;
	var gotoLeftDist = (currOffset - destOffset) + globalLeft;
	globalLeft = gotoLeftDist;
		
	$("#" + activeTabId + " .sb_tape").animate({left: gotoLeftDist + boxSideMargins + "px"}, time);
	
	activeBoxId = boxId;
}

function setTapeWidth() {	
	globalLeft = 0;
		
	activeBoxId = $("#" + activeTabId + " .sb_tape div:first").attr("id");
	
	if (activeBoxId == undefined) return;	
	
	tapeWidth = $("#" + activeTabId + " .sb_tape div").length * 
		(parseInt($("#" + activeTabId + " .sb_boxes").css("width").replace("px", "")) + boxSideMargins);
		
	$("#" + activeTabId + " .sb_tape").css({
		"top" : boxTopMargins,
		"left" : boxSideMargins,
		"width" : tapeWidth
	}).find("div").css({
		"width" : parseInt($("#" + activeTabId + " .sb_boxes").css("width").replace("px", "")) 
			- (2 * boxSideMargins) + "px",
		"height" : parseInt($("#" + activeTabId + " .sb_boxes").css("height").replace("px", "")) 
			- (2 * boxTopMargins) + "px",
		"marginRight" : boxSideMargins
	});	
	
	$("#" + activeTabId + " .sb_navi li").removeClass("sbn_active");
	$("#" + activeTabId + " .sb_navi li:first").addClass("sbn_active");
	$("#" + activeTabId + " .sb_navi li:last").css("border", "none");
}
				
$(document).ready(function() {
	var quickScroll = false;
	setTapeWidth();					 
	
	$(".sb_navi li").click(function() { $(this).find("a").click(); });
	$(".sb_navi a").click(function(e) {
		$(".sb_navi li").removeClass("sbn_active");
		$(this).parent().addClass("sbn_active");

		if (quickScroll)
			scrollTo($(this).attr("href").substr($(this).attr("href").indexOf("#") + 1), 0);
		else
			scrollTo($(this).attr("href").substr($(this).attr("href").indexOf("#") + 1));
			
		quickScroll = false;

		e.preventDefault();
		return false;
	});		
	
	if (document.location.hash != "") {
		var gotoTab = document.location.hash.substr(2);
		var temp = new Array();
		temp = gotoTab.split("@");
			
		quickScroll = true;
		$(".sb_navi a[href=#" + temp[1] + "]").click();
	}
});
 
 
 
