$(function() {
	
	// Generic Tab functionality
	var tabs = $('li[id^=tab], li[id^=tab] span.sub').not('.empty');
	var tab_contents = $('div.tabcontent, table.tabcontent, div.default_tabcontent, table.default_tabcontent');
	var current_techspec_group = 0;
	
//	console.log(tabs);
//	console.log(tab_contents);

	$(tabs).hover(function() {
		$(this).addClass('hover');
	}, function() {
		$(this).removeClass('hover');
	}).addClass('clickable').click(function() {
		var current_tab_id = $(this).attr('id');
		var tab_wrapper = $(this).closest('.tabwrapper, body');
		var result_count = $(this).find('.result_count').html();
		$("#result_count").html(result_count+" tire"+((result_count != 1) ? 's' : ''));

//		$(tabs).removeClass('current');
//		$(tab_contents).hide();
		$('li[id^=tab], li[id^=tab] span.sub', tab_wrapper).removeClass('current');
		$('div.tabcontent, table.tabcontent, div.default_tabcontent, table.default_tabcontent', tab_wrapper).hide();

		$(this).addClass('current');

		if($(this).filter('li').size() != 0 && $(this).children('span.subtabs').size() != 0) {
			$(this).find('span.sub:first').addClass('current');
			current_tab_id = $(this).find('span.sub:first').attr('id');
		} else if($(this).filter('span').size() != 0) {
			$(this).closest('li').addClass('current');
			current_tab_id = $(this).attr('id');
		}
		
		$('#'+current_tab_id+'_content').show();

		return false;
	});

	// Comparison Page by Vehicle Switch Comp Tables
	$("img.totech").addClass('clickable').click(function() {
		$("div#tab_performanceratings_content").hide();
		$("div#tab_techspecs_content").show().find("ul#tabs_performanceratings_comparison li").eq(0).addClass('clickable').click();
		$("b#search_title").html('Technical Specs');
		$("img.switch").toggle();
	});
	$("img.toperf").addClass('clickable').click(function() {
		$("div#tab_techspecs_content").hide();
		$("div#tab_performanceratings_content").show();
		$("b#search_title").html('Performance Ratings');
		$("img.switch").toggle();
	});

	// Performance Ratings Slider
	var newPos = "0";
	// $("table.tire_results .prehide_sub").hide();
	$("table.tire_results .col2").filter(function() { return ($(this).find(".col2box").size() != 0); }).each(function() {
		var hiddenContent1 = $(this).parents(':not(:visible)').filter(':last');
		var hiddenContent2 = $(this).parents('tr[class*=prehide]');
		var hiddenContent = hiddenContent1.get().concat(hiddenContent2.get());
		hiddenContent = $(hiddenContent);
		

		var box	= $(this).children(".col2box");
		var pr	= $(this).children(".col2box").children(".performance_ratings");
		if($(hiddenContent).size() != 0) {
			$(hiddenContent).css('visibility','hidden').show();
			var myHeight = Math.max($(box).height(), ($(pr).height()));
			$(hiddenContent).hide().css('visibility','visible');
		} else {
			var myHeight = Math.max($(box).height(), ($(pr).height()));
		}

		$(box).css("height", myHeight);
		$(pr).css("height", "100%");
	});
	$(".toggle").addClass('clickable').click(function() {
		$('.performance_ratings').filter(function() {
			return(parseInt($(this).css('left')) != 332);
		}).animate({
			left: '332px'
		}).children('.toggle').removeClass('right');

		$(this).parents(".performance_ratings").animate({
			left: (332 - parseInt($(this).parents(".performance_ratings").css('left'))+'px')
		}, false, false, function() {
			if($(this).css('left') == "0px") {
				$(this).children('.toggle').addClass('right');
			} else {
				$(this).children('.toggle').removeClass('right');
			}
		});
	});

	// Technical Specs Tabs
	var current_techspec_group = 0;
	$('#techspecs .group1, #techspecs_all .group1').hide();
	$('#techspecs td:not(.group1, .alwayson),#techspecs_all td:not(.group1, .alwayson)').addClass('group0');
	$('td.more').addClass('clickable').click(function() {
		$('#techspecs .group'+current_techspec_group+',#techspecs_all .group'+current_techspec_group).hide();
		current_techspec_group = 1 - current_techspec_group;
		if($(this).children('div').hasClass('right')) {
			$(this).children('div').removeClass('right');
		} else {
			$(this).children('div').addClass('right');
		}
		$('#techspecs .group'+current_techspec_group+',#techspecs_all .group'+current_techspec_group).show();
	});

	// More table switcher
	var rowHeaders	= $("div#tab_techspecs_content tr.column_titles");
	var moreCells	= $("div#tab_techspecs_content .more");

	if(rowHeaders.length != 0 && moreCells.length != 0) {
		var rowCells	= $(rowHeaders).filter(":first").children("td, th");
		var rowContent	= $("tr.techspecs").filter(":not(.column_titles)");
		var rowContentCells = new Array();
		var temp;
		
		var morePositions = new Array();
		for(i=0;i<rowCells.length;i++) {
			if($(rowCells[i]).hasClass(".more, .col5")) {
				morePositions.push(i - morePositions.length + 2);
			}
		}
		morePositions.push(rowCells.length-1);

		for(j=0;j<morePositions.length-1;j++) {
			rowContentCells[j] = new Array();
		}

		for(k=1;k<morePositions.length;k++) {
			rowContentCells[k-1] = $(rowCells).slice(morePositions[k-1]+k-2,morePositions[k]+k-2).get().concat(
				$(rowHeaders).not(":first").children("td, th").slice(morePositions[k-1]+k-2,morePositions[k]+k-2).get()
			);

			for(j=0;j<rowContent.length;j++) {
				multirow_adjust = 0;
				if($(rowContent).eq(j).hasClass('.multicell_middle, .multicell_bottom')) {
					multirow_adjust = morePositions[0];
				}
				
				temp = rowContentCells[k-1].concat(
					$(rowContent).eq(j).children("td, th").slice(morePositions[k-1]-multirow_adjust,morePositions[k]-multirow_adjust).get()
				);

				rowContentCells[k-1] = temp;
			}
		}

		more1 = $(".more").eq(0);
		more1 = $(".more").eq(2).add(more1);

		more2 = $(".more").eq(1);
		more2 = $(".more").eq(3).add(more2);
		
		showTablePage(rowContentCells, 1, more1, more2);
		$(more1).children('div').addClass('clickable').click();

		$("#ed1").addClass('clickable').click(function() {
			showTablePage(rowContentCells, 1, more1, more2)
		});
		$("#ed2").addClass('clickable').click(function() {
			showTablePage(rowContentCells, 2, more1, more2)
		});
		$("#ed3").addClass('clickable').click(function() {
			showTablePage(rowContentCells, 3, more1, more2)
		});
		
		$(more1).children("div").addClass('clickable').click(function() {
			if($(this).hasClass('left')) {
				showTablePage(rowContentCells, 1, more1, more2);
			} else {
				showTablePage(rowContentCells, 2, more1, more2);
			}
		});
		$(more2).children("div").addClass('clickable').click(function() {
			if($(this).hasClass('left')) {
				showTablePage(rowContentCells, 2, more1, more2);
			} else {
				showTablePage(rowContentCells, 3, more1, more2);
			}
		});

	}

	function showTablePage(rowContentCells, page, more1, more2) {
		switch(page) {
			case 1:
				$(more2).hide();
				$(more1).show().children("div").removeClass("left").addClass("right");
				$(rowContentCells[1]).hide();
				$(rowContentCells[2]).hide();
				$(rowContentCells[0]).show();
				break;
			case 2:
				$(more2).show().children("div").removeClass("left").addClass("right");
				$(more1).show().children("div").removeClass("right").addClass("left");
				$(rowContentCells[0]).hide();
				$(rowContentCells[2]).hide();
				$(rowContentCells[1]).show();
				break;
			case 3:
				$(more1).hide();
				$(more2).show().children("div").removeClass("right").addClass("left");
				$(rowContentCells[0]).hide();
				$(rowContentCells[1]).hide();
				$(rowContentCells[2]).show();
				break;
		}
	}
	
	
	$("p.showall").addClass('clickable').click(function() {
		$("table#techspecs, table#techspecs_all").toggle();
		if($(this).html() == 'Show All Sizes') $(this).html('Hide Additional Sizes');
		else $(this).html('Show All Sizes');
	});
	
	$("img#locate_dealer_submit").addClass('clickable').click(function() {
		$(this).closest('form').submit();
	});
	
	$("a.help").addClass('clickable').click(function() {
		var newwin=window.open(this.href,'contact','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,width=830,height=620');
		return false;
	});
	
	$("div#locate_dealer input[name=zipCode]").focus(function() {
		if($(this).val() == $(this).attr('alt')) $(this).val('');
	}).blur(function() {
		if($(this).val() == '') $(this).val($(this).attr('alt'));
	});
	
	if($.fixIESelectWidth) $("select").fixIESelectWidth();
});