$(document).ready(function() {
	// SETUP TABS ----------------------------------------------------------------------------------------------------
	
	// Show first tabs
	$('#top_tab .tab_content:first, #bottom_tab .tab_content:first').show();
	
	//Create nav
	$('#top_tab .icon_nav').each(function(){
		$(this).show().appendTo('#top_tab .tab_sub_nav:first');
	});
	$('#bottom_tab .icon_nav').each(function(){
		$(this).show().appendTo('#bottom_tab .tab_sub_nav:first');
	});
	
	$('#top_tab .icon_nav:first, #bottom_tab .icon_nav:first').addClass('icon_selected');
	$('#top_tab').addClass('tab_on');

	// SETUP ARROW SLIDER ----------------------------------------------------------------------------------------------------
	
	var tabWidth = parseInt($('#top_tab .icon_nav:first').outerWidth());
	$('.slide_arrow').css({'display':'block'});
	var arrowOffset = $('.slide_arrow').outerWidth()/2;
	$('.slide_arrow').css({'left':calcArrowPos(0)+'px'});

	function calcArrowPos(index){
		var newPos = (tabWidth * index)+(tabWidth/2)-(arrowOffset);
		return newPos;
	}
	
	// TAB SUB NAV ----------------------------------------------------------------------------------------------------
	
	$('.icon_nav').click(function(){
		var $parentNav = $(this).parent('.tab_sub_nav');
		var parentIndex =  $('.tab_sub_nav').index($parentNav);
		var index = $('.tab_sub_nav:eq('+parentIndex+') .icon_nav').index(this);
		$('.tab_section:eq('+parentIndex+') .tab_content').each(function(){
			$(this).hide();
		});
		$('.tab_sub_nav:eq('+parentIndex+') .icon_nav').each(function(){
			$(this).removeClass('icon_selected');
		});
		$('.tab_section:eq('+parentIndex+') .tab_content:eq('+index+')').show();
		$(this).addClass('icon_selected');
	});
	$('.icon_nav').hover(
		function(){
			var $parentNav = $(this).parent('.tab_sub_nav');
			var parentIndex =  $('.tab_sub_nav').index($parentNav);
			var index = $('.tab_sub_nav:eq('+parentIndex+') > .icon_nav').index(this);
			$('.slide_arrow:eq('+parentIndex+')').stop(true, false).animate({'left':calcArrowPos(index)+'px'}, 750);
			$(this).addClass('icon_hover');
		}, 
		function (){
			var $parentNav = $(this).parent('.tab_sub_nav');
			var parentIndex =  $('.tab_sub_nav').index($parentNav);
			var index = $('.tab_sub_nav:eq('+parentIndex+') .icon_selected').index();
			$('.slide_arrow:eq('+parentIndex+')').stop(true, false).animate({'left':calcArrowPos(index)+'px'}, 750);
			$(this).removeClass('icon_hover');
		}
	);
	
	// TAB ACTIONS ----------------------------------------------------------------------------------------------------
	
	$('.nav_tab').click(function(){
		var $parentContainer = $(this).parent('.tab_section');
		var curZindex = parseInt($($parentContainer).css('z-index'));
		if (curZindex == 1) {
			$($parentContainer).css({'z-index':'2'}).addClass('tab_on').siblings('.tab_section').css({'z-index':'1'}).removeClass('tab_on');
		}
	});
	
	$('.nav_tab').hover(
		function(){
			var $parentContainer = $(this).parent('.tab_section');
			var curZindex = parseInt($($parentContainer).css('z-index'));
			if (curZindex == 1) {
				$(this).addClass('tab_hover');
			};
		},
		function(){
			$(this).removeClass('tab_hover');
		}
	);

	var tab_heights = new Array();
	
	$('.tab_content').each(function(){
		tab_heights.push($(this).height());
	});
	
	$("#js #tabbed_features #top_tab, #js #tabbed_features #bottom_tab").height(Math.max.apply( Math, tab_heights ) + 170);
	$("#js #tabbed_features").height(Math.max.apply( Math, tab_heights ) + 180);
});
