var WT_clear = function(_tag) {
	for (var i in _tag.WT) {
		if(i.indexOf("z_") == 0) {
			delete(_tag.WT[i]);
		}
		if(i.indexOf("si_") == 0) {
			delete(_tag.WT[i]);
		}
	}
};

(function($) {
	$(function() {
/* */
		brandLineShortName = $("input[name=brandLineCodeB]").val();
		if(typeof brnadLineShortName == 'undefined') brandLineShortName = '';

		// 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 $details_tabs = $("#tab_overview, #tab_warranty, #tab_specs, #tab_testimonials, #tab_learnmore");
		var current_techspec_group = 0;

		$details_tabs.click(function() {
			tab_name = $(this).attr('id').substr(4);

			dcsMultiTrack("DCS.dcsuri", "/details_" + tab_name, "WT.ti", "TireDetails: "+tab_name);
			_gaq.push(['myTracker._trackPageview', '/tiredetails/'+tab_name+'/'+brandLineShortName]);
		});
		
		$(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().css('height:0;overflow:hidden;');
			// $('div.tabcontent, table.tabcontent, div.default_tabcontent, table.default_tabcontent', tab_wrapper).addClass('hidden');
	
			$(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();
			// $('#'+current_tab_id+'_content').removeClass('hidden');
	
			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";

/* *
		$('.col2box').each(function() {
			var box	= $(this);
			var pr	= $('.performance_ratings', this);
			var myHeight = 200;
			
			if(box.height() == 0){
				var hiddenContent = $(this).closest('div.tabcontent, tr.prehide');
				
				hiddenContent.css('visibility','hidden').css('display', 'block');
				myHeight = Math.max(box.height(), (pr.height()));
				hiddenContent.css('display', 'none').css('visibility','inherit');
			}
			else{
				myHeight = Math.max(box.height(), (pr.height()));
			}

			box.height(myHeight);
			pr.height("100%");
		});
/* */

		$('.performance_ratings:first').each(function() {
			var pr	= $(this);
			var myHeight = 200;
			
			if(pr.height() == 0){
				var hiddenContent = $(this).parents('div.tabcontent:hidden, tr.prehide');
				
				hiddenContent.css('visibility','hidden').css('display', 'block');
				myHeight = pr.height();
				hiddenContent.css('display', 'none').css('visibility','inherit');
			}
			else{
				myHeight = pr.height();
			}

			$(".shim").height(myHeight);
			pr.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
		$current_techspec_group = 0;

		group0 = null;
		group1 = null;
		var mysheet=document.styleSheets[0];
		if(mysheet != null) {
			var myrules=mysheet.cssRules? mysheet.cssRules: mysheet.rules;
			for (i=0; i<myrules.length; i++){
				if(myrules[i].selectorText != null) {
					if(myrules[i].selectorText.toLowerCase()==".group0"){
						group0=myrules[i];
						if(group1 != null)
							break;
					}
					if(myrules[i].selectorText.toLowerCase()==".group1"){
						group1=myrules[i];
						if(group0 != null)
							break;
					}
				}
			}
		}
		
		if(((typeof brand != 'undefined') && (brand == 'Michelin')) && ((typeof country != 'undefined') && (country == 'United States')) ) {
			$more_tds = $('td.more');
			$more_tds.each(function() {
				$td = $(this);
				my_direction = $td.children('div').attr('class');
				my_new_markup = '<div class="more_wrapper"><div class="more_top"></div><div class="more_label"></div><div class="more_bottom"></div></div>';
				$td.html(my_new_markup);
				$morewrap = $('div.more_wrapper');
				$moretop = $('div.more_top');
				$morelabel = $('div.more_label');
				$morebottom = $('div.more_bottom');
				
				var $hiddenContent = $morewrap.parents('div.tabcontent:hidden');
				$hiddenContent.css('visibility','hidden').css('display', 'block');

				$morewrap.height($morewrap.parent().height());
				$morelabel.height($morewrap.height() - $moretop.height() - $morebottom.height());

				$hiddenContent.css('display', 'none').css('visibility','inherit');
			});
		}

		$('td.more').addClass('clickable').click(function() {
			if($current_techspec_group == 1) {
				group1.style.display = 'none';
				if($.browser.msie)
					group0.style.display = 'block';
				else
					group0.style.display = 'table-cell';
			} else {
				group0.style.display = 'none';
				if($.browser.msie)
					group1.style.display = 'block';
				else
					group1.style.display = 'table-cell';
			}
			$(this).children('div').toggleClass('right');
			$current_techspec_group = 1 - $current_techspec_group;
		});
		
/*
		$group1 = $('#techspecs .group1, #techspecs_all .group1'); 
		$group1.hide();
		$group0 = $('#techspecs .group0, #techspecs_all .group0'); 
		$('td.more').addClass('clickable').click(function() {
			if(current_techspec_group == 1) {
				$group1.hide();
				$group0.show();
				$(this).children('div').toggleClass('right');
			} else {
				$group1.hide();
				$group0.show();
				$(this).children('div').toggleClass('right');
			}
		});
/*
		$group1 = $('#techspecs .group1, #techspecs_all .group1'); 
		//$group1.hide();
		// $group0 = $('#techspecs td:not(.group1, .alwayson),#techspecs_all td:not(.group1, .alwayson)');
		$group0 = $('#techspecs .group0, #techspecs_all .group0'); 
		$('td.more').addClass('clickable').click(function() {
			if($current_techspec_group == 1) {
				$group1.hide();
				$group0.show();
				$(this).children('div').toggleClass('right');
			} else {
				$group0.hide();
				$group1.show();
				$(this).children('div').toggleClass('right');
			}
			$current_techspec_group = 1 - $current_techspec_group;
		});
/* */

		// 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((typeof language != 'undefined') && (language == 'French')) { 
				if($(this).html() == 'Montrer toutes les dimensions') $(this).html('Cacher les dimensions suppl&eacute;mentaires');
				else $(this).html('Montrer toutes les dimensions');
			} else {
				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'));
		});
		
		$form = $("#locate_dealer_form,div#locate_dealer form");
		$zipCode = $('input[name=zipCode]');
		$searchedAddress = $('input[name=searchedAddress]');
		if($searchedAddress.length == 0) {$form.append('<input type="hidden" name="searchedAddress" value="" />'); $searchedAddress = $('input[name=searchedAddress]');}
		$latitude = $('input[name=latitude]');
		if($latitude.length == 0) {$form.append('<input type="hidden" name="latitude" value="" />'); $latitude = $('input[name=latitude]');}
		$longitude = $('input[name=longitude]');
		if($longitude.length == 0) {$form.append('<input type="hidden" name="longitude" value="" />'); $longitude = $('input[name=longitude]');}
		$brandLineName = $('input[name=brandLineName]');
		if($brandLineName.length == 0) {$form.append('<input type="hidden" name="brandLineName" value="" />'); $brandLineName = $('input[name=brandLineName]');}
		$brandLineCode = $('input[name=brandLineCode]');
		if($brandLineCode.length == 0) {$form.append('<input type="hidden" name="brandLineCode" value="" />'); $brandLineCode = $('input[name=brandLineCode]');}
		$zip = $('input[name=zip]');
		if($zip.length == 0) {$form.append('<input type="hidden" name="zip" value="" />'); $zip = $('input[name=zip]');}
		$brandLineName = $('input[name=brandLineName]');
		if($brandLineName.length == 0) {$form.append('<input type="hidden" name="brandLineName" value="" />'); $brandLineName = $('input[name=brandLineName]');}
		$radius = $('input[name=radius]');
		if($radius.length == 0) {$form.append('<input type="hidden" name="radius" value="10" />'); $radius = $('input[name=radius]');}

		if((typeof country != 'undefined') && (country == 'United States')) {
			$form.submit(function(event) {
				if($(this).attr('valid') == 'valid') {
					return true;
				} else {
					event.preventDefault();
					search_address = $zipCode.val();
					var geocoder = new GClientGeocoder();
					geocoder.getLocations(search_address,function(response) {
						if (!response || response.Status.code != 200) {
							// $("#top_left h1.top_title").after('<p class="error">Error: Could not find your searched location. Please try again.');
						} else {
							my_place = response.Placemark[0];
							if(my_place.AddressDetails.Accuracy >= 1 && (my_place.AddressDetails.Country.CountryName == "USA" || my_place.AddressDetails.Country.CountryName == "Canada" || my_place.AddressDetails.Country.CountryName == "Mexico")) {
								point = new GLatLng(my_place.Point.coordinates[1], my_place.Point.coordinates[0]);
								// my_zip = (my_place.hasOwnProperty('PostalCodeNumber')) ? my_place.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.PostalCode.PostalCodeNumber : '';
								
								$latitude.val(point.y);
								$longitude.val(point.x);
								// $zip.val(my_zip);
								$searchedAddress.val(my_place.address);
								
								$form.attr('valid', 'valid').submit();
							} else {
								// $("#top_left h1.top_title").after('<p class="error">Error: Could not find your searched location. Please try again.');
							}
						}
					});
				}
			});
		}

		
		if($.fixIESelectWidth) $("select").fixIESelectWidth();
/* */
		
	});
})(jQuery);
