function attachSortByHover(o, h) {
	$(h).bind('mouseleave', function() { $(this).toggle(); });
	$(o).bind('mouseenter', function() { $(h).toggle(); });
}

var compare_popup_obj = null;
var compare_overlay_obj = null;

function slideUpCoupon() {
	if ($(this).hasClass('print')) {
		var slug = $(this).siblings('.slug').html();
//		var slug_encoded = $('<textarea />').html(slug).html();
		var window_popup = window.open('/printcoupon.php?slug='+slug, "coupon1", "location=no,menubar=no,width=740,height=700,toolbar=no");
	}
	$('.coupon_popup:visible').slideUp();
	if (navigator.userAgent.indexOf('MSIE 6.0') != -1) {
		$('.extratireinforightside').show();	
	}
	$('.row_result_divider_coupon').css('z-index','0');
}

$(document).ready(function() {

	$('.rebate').mouseenter(function() {
		var rebate = $(this).next('.rebate_overlay');
		rebate.css('marginTop', '-55px');
		rebate.fadeIn().animate({ 'marginTop': '-63px', 'opacity': '1.0'});
	});
	$('.rebate_overlay').mouseleave(function() {
		var rebate = $(this);
		rebate.animate({ 'marginTop': '-85px', 'opacity': '0.0'}).fadeOut(function() {
			$(this).css('marginTop', '-55px');
		});
	});

	$('#first_name_capture').blur(function() {
		if ($(this).val().length == 0) { $(this).val('First name'); }
	}).focus(function() {
		if ($(this).val() == 'First name') { $(this).val(''); }
	});
	$('#last_name_capture').blur(function() {
		if ($(this).val().length == 0) { $(this).val('Last name'); }
	}).focus(function() {
		if ($(this).val() == 'Last name') { $(this).val(''); }
	});
	$('#phone_number_1').keyup(function() { if ($(this).val().length == 3) { $('#phone_number_2').focus(); } });
	$('#phone_number_2').keyup(function() { if ($(this).val().length == 3) { $('#phone_number_3').focus(); } });
        jQuery.fn.centerScreen = function(loaded) {
                var obj = this;
		var wHeight;
		if (navigator.userAgent.indexOf('Safari')) {
			wHeight = document.getElementsByTagName('html')[0].clientHeight;
		} else {
			wHeight = $(window).height();
		}
		var height_divider = 3;
		if (navigator.userAgent.indexOf('MSIE 6.0')) {
			height_divider = 2;
		}
		
                if(!loaded) {
//                        obj.css('top', $(window).height()/3- this.height()/2);
                        obj.css('top', wHeight/height_divider - this.height()/2);
                        obj.css('left', $(window).width()/2- this.width()/2);
                        $(window).resize(function() { obj.centerScreen(!loaded); });
                } else {
                        obj.stop();
//                        obj.animate({ top: $(window).height()/3- this.height()/2, left: $ (window).width()/2-this.width()/2}, 200, 'linear');
                        obj.animate({ top: wHeight/height_divider - this.height()/2, left: $ (window).width()/2-this.width()/2}, 200, 'linear');
                }
        } 

	$('.overlay').click(function() {
		$('.optional_information').fadeOut();
		$('#optional_information_clone').remove();
	});

	$('.optional_info_form div.quantity ul li').click(function() {
		$('.first_name:visible').focus();
	});
		
	$('.optional_info_form').keypress(function(event) {
		if (event.which == 13) {
			event.preventDefault();
			$('.optional_information_submit:visible').click();
		}
	});

	$('.coupon_offer .print').click(function(event) {
		alert('print here');
	});

	$('.coupon_offer').click(function() {
		var length = $('.coupon_popup:visible').length; 
		var ie6 = 'MSIE 6.0';
		if (length >= 1) {
			$(this).parent().parent().css('z-index','0');
			var o = $(this);
			if ($(this).parent().next(':visible').length == 1) {
					$('.coupon_popup').slideUp();
					if (navigator.userAgent.indexOf(ie6) != -1) {
						$('.extratireinforightside').show();	
					}
			} else {
				$('.coupon_popup:visible').slideUp(function() {
					o.parent().next().slideDown(function() {
						$('.extratireinforightside').hide();	
						$('body, .print').one('click', slideUpCoupon);
					});
				});
			}
		} else {
			$(this).parent().parent().css('z-index','1');
			$(this).parent().next().slideToggle(function() { 
				if (navigator.userAgent.indexOf(ie6) != -1) {
					reIndex();
				}
				$('body, .print').one('click', slideUpCoupon);
			});
		}
/*
		if ($('.coupon_offer:visible').length > 1) {
			var o = $(this);
			$('.coupon_offer:visisble').slideToggle(function() {
				o.parent().next().slideToggle();
			}
		} else {
			$(this).parent().next().slideToggle();
		}
*/
	});

	attachSortByHover('#sort_by_class', '#sort_by_class_hover');
	attachSortByHover('#sort_by_warranty', '#sort_by_warranty_hover');
	attachSortByHover('#sort_by_brand', '#sort_by_brand_hover');
	attachSortByHover('#sort_by_price', '#sort_by_price_hover');

	$('#standard_sizes').click(function() { 
		if (!$(this).hasClass('selected')) { 
			$('#standard_indicator_hidden').val('S');
			$('#tire_search').submit(); 
		} 
	});
	$('#optional_sizes, .plus_size').click(function() { 
		if (!$(this).hasClass('selected')) { 
			$('#standard_indicator_hidden').val('O');
			$('#tire_search').submit(); 
		} 
	});
	$('.sort_by_hover ul li').click(function() {
		$('#sort_by_column_hidden').val($(this).parent().prev().html());
		$('#narrow_by_value_hidden').val($(this).attr('rel'));
		$('#tire_search').submit(); 
	});
	$('.sort_by_hover span.title').click(function() {
		$('#sort_by_column_hidden').val($(this).html());
		$('#narrow_by_value_hidden').val('');
		$('#tire_search').submit();
	});
	
	$('#search .checkbox').click(function() {
		if ($(this).hasClass('selected')) {
			$(this).attr('src', '/img/tire-search/radio-button.png').removeClass('selected');
		} else {
			$(this).attr('src', '/img/tire-search/radio-button-active.png').addClass('selected');
		}
	});
	
	var paginationLI = $('#pagination_control ol li');

	if(paginationLI.size() == 1 && paginationLI.hasClass('first') && paginationLI.hasClass('selected')){
		paginationLI.addClass('last');
	};

/*
	$('#check_all').click(function() {
		if ($(this).html() == 'Check All') {
			$('.checkbox').each(function() {
				$(this).attr('src', '/img/tire-search/radio-button-active.png').addClass('selected');
			});
			$(this).html('Uncheck All');
		} else {
			$('.checkbox').each(function() {
				$(this).attr('src', '/img/tire-search/radio-button.png').removeClass('selected');
			});
			$(this).html('Check All');
		}
	});
*/

	$('#expand_all').click(function() {
		if ($(this).html() == 'Expand all descriptions to see more tire info') {
			$('.row_result.closed .brand_image').each(function(i, obj) {
				var obj = $(obj);
				if (obj != undefined && typeof obj.click == 'function') { 
					obj.click(); 
				}
			});
			$(this).html('Collapse all descriptions to see less tire info');
		} else {
			$('.row_result.open .brand_image').each(function(i, obj) {
				var obj = $(obj);
				if (obj != undefined && typeof obj.click == 'function') { 
					obj.click(); 
				}
			});
			$(this).html('Expand all descriptions to see more tire info');
		}
	});

	$('.add_to_quote').click(add_to_quote);

	/*$('.remove').click(removeQuoteItemFromCookie).hover(function(){
		$(this).css('text-decoration','underline');
	},function(){
		$(this).css('text-decoration','none');
	});*/
	$('.quote_item').hover(addRemoveHover, removeRemoveHover);

//	checkQuoteAgainstCookie();

	if ($('#sort_by .selected').length > 0) {
// Uncomment the next line to re-enable automatic expansion of search results upon sort
//		$('#expand_all').click();
	}
	
	
/* BUILD COMPARE TIRES 


<div id='compare_popup'><div id='inner_rounded_corner'>
	<img id='compare_logo' src='/img/tire-search/compare-tires-icon.png' />
	<div class='header'><h2>COMPARE TIRES</h2><span class='close right'></span><span class='button right print_window'>Print Window</span></div>
	<div class='viewport'>
	<div class='scrollbox'>
		<ul class='top_level'>
			<li class='template' style='display: none;'>
				<div class='tire_image_wrapper'>
					<img class='brand_image' />
					<div class='tire_name'>Brand Name</div>
					<div class='tire_image_holder'>
					<img class='tire_image' />
					</div>
				</div>
				<ul class='inner_list'>
					<li><span class='tire_price_title'>Price:</span> <span class='tire_price'>$210 </span></li>
					<li class='tire_label'></li>
					<li class='tire_warranty'></li>
					<li class='tire_class'></li>
					<li class='tire_speed_rating'></li>
					<li class='tire_front_rear'></li>
					<li class='tire_load_index'></li>
					<li class='sku'></li>
				        <li class='add_to_quote_holder' ><div class='add_to_quote compare'>Add To Quote</div></li>
				</ul>
			</li>
		</ul>
	<div class='clear'></div>
	</div>
	</div>
</div></div>

*/	

compare_overlay_obj =  $("<div id=\"compare_overlay\"/>").appendTo($('body'));
compare_popup_obj = $('<div id="compare_popup" />').appendTo($('body'));
var inner_rounded_corner = $('<div id="inner_rounded_corner" />').appendTo(compare_popup_obj);
$("<img id='compare_logo' src='/img/tire-search/compare-tires-icon.png' />").prependTo(inner_rounded_corner);
$("<div class='header'><h2>COMPARE TIRES</h2><span class='close right'></span><span class='button right print_window'>Print Window</span></div>").appendTo(inner_rounded_corner);
var compare_viewport = $("<div/>").addClass("viewport").appendTo(inner_rounded_corner);
var compare_scrollbox = $("<div/>").addClass("scrollbox").appendTo(compare_viewport);
var cleardiv = $("<div/>").addClass("clear");
var compare_top_level = $('<ul>').addClass('top_level').appendTo(compare_scrollbox).after(cleardiv);


var clone_prime = $("<li class='template' style='display: none;'><div class='tire_image_wrapper'><div id='brand_image_wrap'><img class='brand_image' /></div><div class='tire_name'>Brand Name</div><img class='tire_image' /></div><ul class='inner_list'><li class='tire_price'></li><li class='tire_label'></li><li class='tire_warranty'></li><li class='tire_class'></li><li class='tire_speed_rating'></li><li class='tire_front_rear'></li><li class='tire_load_index'></li><li class='sku'></li><li class='add_to_quote_holder' ><div class='add_to_quote compare'>Get Quote</div></li></ul></li>");

var clone_prime_matched = $("<li class='template matched_set_clone' style='display: none;'><div class='tire_image_wrapper'><div id='brand_image_wrap'><img class='brand_image' /></div><div class='tire_name'>Brand Name</div><img class='tire_image' /></div><ul class='inner_list'><li class='tire_price'></li><li class='tire_label'></li><li class='tire_label2'></li><li class='tire_warranty'></li><li class='tire_class'></li><li class='tire_speed_rating'></li><li class='tire_load_index'></li><li class='sku'></li><li class='add_to_quote_holder' ><div class='add_to_quote compare'>Get Quote</div></li></ul></li>");




	/*
	$('.compare').click(function() {
		$('.template_copy').remove();
		var obj = $('.checkbox.selected');
		var length = obj.length;
		var scrollboxwidth = 0;
		if (obj.length > 0){
		$('.checkbox.selected').each(function(i, o) {
			var brand_image = $(o).parent().siblings('.brand_image').attr('src');
			var tire_name = $(o).parent().siblings('.tire_series_name').html();
			var tire_price = $(o).siblings('.price_quote').html();
			var sku = $(o).siblings('.add_to_quote').children('.sku');
			var extrainfo = $(o).parent().siblings('.extrainfo').children();
			var tire_image = extrainfo.parent().children('.tire_image').children('img').attr('src');
			var tire_label = extrainfo.children('.tire_label').html();
			var tire_warranty = extrainfo.children('.tire_warranty').html();
			var tire_class = extrainfo.children('.tire_class').html();
			var tire_speed_rating = extrainfo.children('.tire_speed_rating').html();
			var tire_front_rear = extrainfo.children('.tire_front_rear').html();
			var tire_load_index = extrainfo.children('.tire_load_index').html();
			

			
			var clone = clone_prime.clone().removeClass('template').addClass('template_copy');
			if (i == (length - 1)) {
				clone.addClass('last');
			}

			$('#compare_popup .top_level').append(clone.show());
			scrollboxwidth = scrollboxwidth + 241;
		});
		$('.scrollbox').width(scrollboxwidth + 'px');
		compare_popup_obj.removeClass('one_compare').removeClass('two_compare').removeClass('three_or_more_compare');

		switch (length) {
			case 1: compare_popup_obj.addClass('one_compare'); break;
			case 2: compare_popup_obj.addClass('two_compare'); break;
			default: compare_popup_obj.addClass('three_or_more_compare'); break;
		}

		 if($.browser.msie == true){
		 	compare_popup_obj.show();
		 	compare_overlay_obj.show();
		 }else{
		 	compare_overlay_obj.fadeIn();
		 	compare_popup_obj.fadeIn();
		 }


		}else{
			alert('You haven\'t selected any tires to compare!');
		}
	});
	

	$('.compare').unbind().addClass('compare2');
	*/

	$('#header_row .compare,.compare.bottom').click(function(event) {
		event.preventDefault();
		$('.template_copy').remove();
		var obj = $('.checkbox.selected');
		var length = obj.length;
		var scrollboxwidth = 0;
		if (obj.length > 0){
		$('.checkbox.selected').each(function(i, o) {	
			
			var thisparent = $(this).parent();
			var inrowform = thisparent.siblings('.inrowform');
			
			var thistiredata = {};
			thistiredata.tire_name = thisparent.siblings('.tire_series_name').html();
			thistiredata.brand_image_src = thisparent.siblings('.brand_image').attr('src');
			thistiredata.tire_image_src = thisparent.siblings('.extrainfo').children('.tire_image').children('img').attr('src');
			thistiredata.sku = $(this).siblings('.add_to_quote').children('.sku').text();

			var set_1_obj = $(this).parent().siblings('.extrainfo').find('.set_1');
			t5 = set_1_obj;
			var set_2_obj = set_1_obj.siblings('.matched_set_2');
			var is_matched_set = false;
			if(set_2_obj.size() > 0){
				is_matched_set = true;
				
			}
	
			
			
			var set_front,set_rear;
			
			var set_1 = {
				'size' : set_1_obj.children('.tire_label').children('.data').html(),
				'warranty' : set_1_obj.children('.tire_warranty').children('.data').html(),
				'tire_class' : set_1_obj.children('.tire_class').children('.data').html(),
				'speed_rating' : set_1_obj.children('.tire_speed_rating').children('.data').html(),
				'load_index' : set_1_obj.children('.tire_load_index').children('.data').html(),
				'front_rear' : set_1_obj.children('.tire_front_rear').children('.data').html(),
				'price' : $(this).siblings('.price_quote').children('.price').html()
			}
			
			if (is_matched_set){
				var set_2 = {
					'size' : set_2_obj.children('.tire_label').children('.data').html(),
					'warranty' : set_2_obj.children('.tire_warranty').children('.data').html(),
					'tire_class' : set_2_obj.children('.tire_class').children('.data').html(),
					'speed_rating' : set_2_obj.children('.tire_speed_rating').children('.data').html(),
					'load_index' : set_2_obj.children('.tire_load_index').children('.data').html(),
					'front_rear' : set_2_obj.children('.tire_front_rear').children('.data').html(),
					'price' : set_2_obj.children('.price_quote').children('.price').html()
				}
			
				set_1.price = set_1_obj.children('.price_quote').children('.price').html();
			
				//DETERMINE WHICH IS FRONT, WHICH IS REAR
				if(set_1.front_rear == "Front"){
					thistiredata.set_front = set_1; 
					thistiredata.set_rear = set_2; 
				}else {
					thistiredata.set_front = set_2; 
					thistiredata.set_rear = set_1;
				}
				
			}else{
				thistiredata.set_single = set_1;
			}
			
			var clone = (is_matched_set) ? clone_prime_matched.clone() : clone_prime.clone();
			clone.removeClass('template').addClass('template_copy');
			
			if (is_matched_set){			
				$('.brand_image', clone).attr('src', thistiredata.brand_image_src);
				$('.tire_name', clone).html(thistiredata.tire_name);
				$('.tire_image', clone).attr('src', thistiredata.tire_image_src);
				$('.tire_price', clone).html('<span class="title">Price:</span> ' + thistiredata.set_front.price + '&nbsp;<span class="frontrear">(Front)</span>&nbsp;&nbsp;' + thistiredata.set_rear.price + '&nbsp;<span class="frontrear">(Rear)</span>');
				$('.tire_label', clone).html('<span class="title">Size:</span> ' + thistiredata.set_front.size + '&nbsp;<span class="frontrear">(Front)</span>');
				$('.tire_label2', clone).html('<span class="title">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span> ' + thistiredata.set_rear.size + '&nbsp;<span class="frontrear">(Rear)</span>');
				$('.tire_warranty', clone).html('<span class="title">Warranty:</span> ' + thistiredata.set_front.warranty);
				$('.tire_class', clone).html('<span class="title">Segment:</span> ' + thistiredata.set_front.tire_class);
				$('.tire_speed_rating', clone).html('<span class="title">Speed Rating:</span> ' + thistiredata.set_front.speed_rating + '&nbsp;<span class="frontrear">(Front)</span>&nbsp;&nbsp;' + thistiredata.set_rear.speed_rating + '&nbsp;<span class="frontrear">(Rear)</span>');
				$('.tire_load_index', clone).html('<span class="title">Load Index:</span> ' + thistiredata.set_front.load_index + '&nbsp;<span class="frontrear">(Front)</span>&nbsp;&nbsp;' + thistiredata.set_rear.load_index + '&nbsp;<span class="frontrear">(Rear)</span>');
				$('.sku', clone).html(thistiredata.sku);			
			}else{
				$('.brand_image', clone).attr('src', thistiredata.brand_image_src);
				$('.tire_name', clone).html(thistiredata.tire_name);
				$('.tire_image', clone).attr('src', thistiredata.tire_image_src);
				$('.tire_price', clone).html('<span class="title">Price:</span> ' + thistiredata.set_single.price);
				$('.tire_label', clone).html('<span class="title">Size:</span> ' + thistiredata.set_single.size);
				$('.tire_warranty', clone).html('<span class="title">Warranty:</span> ' + thistiredata.set_single.warranty);
				$('.tire_class', clone).html('<span class="title">Segment:</span> ' + thistiredata.set_single.tire_class);
				$('.tire_speed_rating', clone).html('<span class="title">Speed Rating:</span> ' + thistiredata.set_single.speed_rating);
				$('.tire_front_rear', clone).html('<span class="title">Front/Rear:</span> ' + thistiredata.set_single.front_rear);
				$('.tire_load_index', clone).html('<span class="title">Load Index:</span> ' + thistiredata.set_single.load_index);
				$('.sku', clone).html(thistiredata.sku);
				
			}
			$('.add_to_quote', clone).click(add_to_quote);

			if (i == (length - 1)) clone.addClass('last');
			
			$('#compare_popup .top_level').append(clone.show());
			scrollboxwidth = scrollboxwidth + 241;
			
		});
		$('.scrollbox').width(scrollboxwidth + 'px');
		compare_popup_obj.removeClass('one_compare').removeClass('two_compare').removeClass('three_or_more_compare');

		switch (length) {
			case 1: compare_popup_obj.addClass('one_compare'); break;
			case 2: compare_popup_obj.addClass('two_compare'); break;
			default: compare_popup_obj.addClass('three_or_more_compare'); break;
		}

		 if($.browser.msie == true){
		 	compare_popup_obj.show();
		 	compare_overlay_obj.show();
		 }else{
		 	compare_overlay_obj.fadeIn();
		 	compare_popup_obj.fadeIn();
		 }


		}else{
			alert('You haven\'t selected any tires to compare!');
		}
	});
	
	

	$('#compare_popup .close,#compare_overlay').click(function() {	
		if($.browser.msie == true){
			compare_popup_obj.hide();	
			compare_overlay_obj.hide();
		}else{
			compare_overlay_obj.fadeOut();
			compare_popup_obj.fadeOut();
		}

		$('.checkbox').each(function() {
			$(this).attr('src', '/img/tire-search/radio-button.png').removeClass('selected');
		});

		$('#check_all').text('Check All');
	});

	$('.print_window','#compare_popup').click(function(){
	var hurp = $(this).parents('#compare_popup').clone();
	
	hurp.find('.template_copy').after('<hr/>');
	
	printHTML(hurp.html());		
	})

	$('#first_page').click(function() { $(this).siblings('ol').children().eq(0).click(); });
	$('#next_page').click(function() {
		var o = $(this).siblings('ol').children('.selected').next();
		if (o) {
			o.click();
		}
	});
	$('#previous_page').click(function() {
		var o = $(this).siblings('ol').children('.selected').prev();
		if (o) {
			o.click();
		}
	});
	$('#last_page').click(function() { 
		var o = $(this).siblings('ol').children();
		o.eq((o.length - 1)).click(); 
	});


	$('.brand_image').click(function() {
		var parent = $(this).parent();
		if (parent.hasClass('closed')) {
			parent.removeClass('closed').addClass('open').children('.extrainfo').slideDown();
		} else {
			parent.removeClass('open').addClass('closed').children('.extrainfo').hide();
		}
	});
});

function promptLoaded() {
	$('input', $(this)).focus();
}

var expiration = 180;

function addRemoveHover() {
	$(this).addClass('removeHover');
}

function removeRemoveHover() {
	$(this).removeClass('removeHover');
}

function updateQuoteItemNumber(oldNumber, newNumber) {
	$.each($('.quote_item_number'), function(i, val) {
		if ($(val).html() == oldNumber) {
			$(val).html(newNumber);
		}
	});
}


function add_to_quote() {
	var l = $('#my_quote').children('.quote_item').length;
	var tsn;
	var name;
	var sku;
	var sku_2 = 0;
	var first_name = $('#first_name_capture').val();
	var last_name = $('#last_name_capture').val();
	if ($(this).hasClass('compare')) {
		name = $(this).parent().parent().siblings().children('.tire_name').html()
		sku= $(this).parent().siblings('.sku').html()
		if (first_name != 'First name') {
			$('.quote_item_sku[value=' + sku+ ']').parent().children('.first_name').val(first_name);
		}
		if (last_name != 'Last name') {
			$('.quote_item_sku[value=' + sku+ ']').parent().children('.last_name').val(last_name);
		}
		$('.quote_item_sku[value=' + sku+ ']').parent().submit();
		return true;
	} else if ($(this).hasClass('spec_sheet')) {
		name = $(this).siblings('.tire_name').html()
		sku= $(this).children('.sku').html()
		if ($(this).children('.sku_2').length == 1) {
			sku_2 = $(this).children('.sku_2').html();
		}
	} else {
		if (first_name != 'First name') {
			$(this).parent().prev().children('.first_name').val(first_name);
		}
		if (last_name != 'Last name') {
			$(this).parent().prev().children('.last_name').val(last_name);
		}

		name= $(this).parent().siblings('.tire_series_name').html();
		sku = $(this).children('.sku').html();
		if ($(this).children('.sku_2').length == 1) {
			sku_2 = $(this).children('.sku_2').html();
		}
	}
	$(this).parent().prev().submit();	
};


var t1,t2,t3,t4,t5;


