
function setZIndex(newTab) {
	var startIndex = 13;
	
	$("#tabNav a").each(function(i){
		$(this).css({ zIndex: startIndex - i });
	});
	newTab.css({ zIndex: startIndex + 1 });
	
}

function initOurStoryTabs() {
	$("#outerWrapper").after('<div class="ie-pusher" style="width:10px; height:1px; clear:both;"> <!--  --> </div>');
	var minimumHeight = 400;
	var newHeight = 435;
	var $curTab = $('#tabInner > div.selected');

	$('#tabInner > div').each(function(i){
	
		$(this).css({ display: 'none' });
		
		if( $(this).hasClass('selected') ) {
			$curTab = $(this);
			$('#tabNav a:eq('+i+')').addClass('selected').css({ zIndex: 15 });
		}
		
	});
	
	//var $curTab = $('#tabInner > div.selected');
	
	if($curTab.length < 1) {
		$curTab = $('#featuresTab');
		$('#tabNav a#features').addClass('selected').css({ zIndex: 15 });
	}
	
	$curTab.css({
		opacity: 0,
		display: 'block',
		left: '15px',
		top: '0px'
	}).animate({
		opacity: 1
	}, menuSpeed);
	
	$('#tabNav a').each(function(i){
		var $trigger = $(this);
		var $newTab = $('#tabInner > div.canFade:eq('+i+')');
		
		//alert($trigger.attr('id'));
		//alert($newTab.attr('id'));
		
		$trigger.click(function() {
			//$.cookie("histTab", $trigger.attr('id'));
			return true;
			
			$("#tabNav a").removeClass('selected');
			$trigger.addClass('selected');				//Set selected class on new link
			
			setZIndex($trigger);
			
			$curTab.animate({
				opacity: 0
			}, menuSpeed, function(){
				$curTab.css({ display: 'none' });
				if($newTab.height() < minimumHeight) {
					newHeight = minimumHeight;
				} else {
					newHeight = $newTab.height();
				}
				$('#tabInner').height = newHeight;
				
				$newTab.css({
					display: 'block',
					opacity: 0,
					left: '15px',
					top: '0px'
				});
				
				$('#tabInner').animate({
					height: newHeight+'px'},
					menuSpeed,
					"swing",
					function() {
						$newTab.animate({
							opacity: 1
						}, menuSpeed, function(){
							$curTab = $newTab;
						});
					}
				);
				
				/*$('#tabInner').animate({
					height: newHeight+'px'
				});
				$newTab.animate({
					opacity: 1
				}, menuSpeed, function(){
					$curTab = $newTab;
				});*/
			});
			
			//event.preventDefault();
			return false;
		});
		
	});

	
}

function initFeatures() {
	$("#features").prepend("<a id='backButton' class='replaced prev'>Click to go back</a><a id='forwardButton' class='replaced next'>Click me to go forward</a>");
};

function initCarousel(){
 $(".carousel").jCarouselLite({
        btnNext: ".next",
        btnPrev: ".prev",
        visible: 1,
        circular:false,
        speed: menuSpeed
  });
 /*This fixes a width error caused by jCarouselLite*/
 /*$(".carousel").css("width", "307px");*/
 /*$("#newProds ul").css("width", "2400px");*/
}

function categoryDropdowns() {
	var $menuBox = $('#subcategoryBox');
	var selectedText = $('a.selected', $menuBox).text();
	
	var $menuTrigger = $('<div/>').attr({ className:'selectedText' })
						.append(
							$('<span />').attr({ className:'selectedTextInner' }).text(selectedText)
						).css({
							cursor: 'pointer'
						});
	var $menu = $('> ul', $menuBox).attr({id: 'subcategoryMenu'}).before($menuTrigger);
	
	var hideDelayTimer = null;
	var beingShown = false;
	var shown = false;	

	$([$menuTrigger.get(0), $menu.get(0)]).click(function () {
		if (hideDelayTimer) clearTimeout(hideDelayTimer);
		if (beingShown || shown) {
			$menu.slideUp(menuSpeed, function () {
				shown = false;
			});
		} else {
			// reset position of info box
			beingShown = true;
			
			$menu.slideDown(menuSpeed, function () {
				beingShown = false;
				shown = true;
			});
		}
		return false;
	});
	
	$([$menuTrigger.get(0), $menu.get(0)]).mouseover(function () {
		if (hideDelayTimer) clearTimeout(hideDelayTimer);
		return false;
	}).mouseout(function () {
		if (hideDelayTimer) clearTimeout(hideDelayTimer);
		hideDelayTimer = setTimeout(function () {
			hideDelayTimer = null;
			$menu.slideUp(menuSpeed, function () {
				shown = false;
			});

		}, hideDelay);

		return false;
	});
	
	
}


function get(name) {
	var q = location.search.substring(1).split(/[=&]/);
	for (var j=0; j<q.length; j+=2) {
		if (q[j] == name) {
			return q[j+1];
		};
	};
	return null;
};

$(document).ready( function() {
	isTab = get("tab");
	var curDiv = isTab;
	//alert(isTab);
	
	
	/*var histTabId =$.cookie("histTab")
	
	switch(histTabId) {
		case "ourstoryLink" :
			isTab='ourstory';
			break;
		case "communityLink" :
			isTab='community';
			break;
		case "innovationLink" :
			isTab='innovation';
			break;
		case "inspirationLink" :
			isTab='inspiration';
			break;
	}*/
			
	if (isTab == "" || isTab == null) {
		/*initFeatures();
		initCarousel();*/		
	}else{
		switch(isTab) {
		case "ourstory" :
			$('#ourstoryLink').addClass('selected');
			$('#ourstoryTab').addClass('selected');
			break;
		case "community" :
			$('#communityLink').addClass('selected');
			$('#communityTab').addClass('selected');
			break;
		case "innovation" :
			$('#innovationLink').addClass('selected');
			$('#innovationTab').addClass('selected');
			break;
		case "inspiration" :
			$('#inspirationLink').addClass('selected');
			$('#inspirationTab').addClass('selected');
			break;
		}
		//categoryDropdowns();
		initOurStoryTabs();
		/*initFeatures();
		initCarousel();*/	
	}

});