
function setZIndex(newTab) {
	var startIndex = 13;
	
	$("#tabLinks a").each(function(i){
		$(this).css({ zIndex: startIndex - i });
	});
	newTab.css({ zIndex: startIndex + 1 });
	
}

function specZebraTable() {
	$('.zebraTable tr:even').addClass('even');
	$('.zebraTable tr:odd').addClass('odd');
}

function initProductTabs() {
	$("#bottomWrapper").after('<div class="ie-pusher" style="width:10px; height:1px; clear:both;"> <!--  --> </div>');
	var minimumHeight = 400;
	var newHeight = 400;
	var $curTab = $('#tabsWrapper > div.selected');

	$('#tabsWrapper > div').each(function(i){
	
		$(this).css({ display: 'none' });
		
		if( $(this).hasClass('selected') ) {
			$curTab = $(this);
			$('#tabLinks a:eq('+i+')').addClass('selected').css({ zIndex: 15 });
		}
		
	});
	
	//var $curTab = $('#tabsWrapper > div.selected');
	if(window.location.href.match('supportTab=open')) {
		if($curTab.length < 1) {
			$curTab = $('#supportTab');
			$('#tabLinks a#support').addClass('selected').css({ zIndex: 15 });
			
			setHistTabCookie($('#tabLinks a#support').attr('id'));
		}
	}
	else {
		if($curTab.length < 1) {
			$curTab = $('#featuresTab');
			$('#tabLinks a#features').addClass('selected').css({ zIndex: 15 });
		}
	}
	
	$curTab.css({
		opacity: 0,
		display: 'block',
		//left: '15px',
		top: '0px'
	}).animate({
		opacity: 1
	}, menuSpeed);
	
	$('#tabLinks a').each(function(i){
		var $trigger = $(this);
		var $newTab = $('#tabsWrapper > div.canFade:eq('+i+')');
		
		//alert($trigger.attr('id'));
		//alert($newTab.attr('id'));
		
		$trigger.click(function() {
			//alert("Setting active tab->"+$trigger.attr('id'));
			//$.cookie("histTab", $trigger.attr('id'));
			setHistTabCookie($trigger.attr('id'));
			
			$("#tabLinks 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();
				}
				if($newTab.height() < 200) {
					$newTab.css('height', '200px');
				}
				$("#tabsWrapper").animate({
					height: newHeight+'px'
				});
				$newTab.css({
					display: 'block',
					opacity: 0,
					//left: '15px',
					top: '0px'
				}).animate({
					opacity: 1
				}, menuSpeed, function(){
					$curTab = $newTab;
				});
			});
			
			//event.preventDefault();
			return false;
		});
		
	});

	
}

function setHistTabCookie(id){
	var date = new Date();
    date.setTime(date.getTime() + (60 * 60 * 1000));
	$.cookie("histTab"+productModelNumber, id, {expires: date });
	//alert(" set histTab"+productModelNumber+" -->"+id);
}

function selectHistoricalTab(){
	
	var hisTabId =$.cookie("histTab"+productModelNumber);
	//alert(" got histTab"+productModelNumber+" -->"+hisTabId);
	//alert($('#tabLinks a#'+hisTab).attr('id'));
	if(hisTabId && hisTabId!=''){
		var hisTab=$('#tabLinks a#'+hisTabId)	
		
		if(hisTab)hisTab.click();
	}
}


$(document).ready( function() {
	initProductTabs();
	specZebraTable();
	$('form#buyNow').attr("name", "frmCart").attr("target", "_blank");
	$('a#reviewButton').attr("target","_blank");
	$('div#availableAt a').attr("target", "_blank");
	$('div#supportBy a').attr("target", "_blank");
	$('#reviewCountDisplay').text( $('#reviewCount').text() ); //Copy number of reviews to display span to overlay over the button - EP
	$('div#relatedAcc .extlink').attr("target","_blank");
	if(!window.location.href.match('supportTab=open'))selectHistoricalTab();

});