function zipField(){

	var searchField = $('#js-searchMeToo');

        searchField.focus(function(){

		if ($(this).attr('value').search( "ZIP") > 0){
			$(this).attr('value', '');
		}

	});

        searchField.focusout(function(){

		if (!$(this).attr('value').length){
			$(this).attr('value', 'Enter Address or ZIP');
		}

	});
}
function tooltipInit(){
	if($('.js-question').length){
		
		$('.js-question img').click(function(){
			$('.mi-tooltip').css('display','none');
			$(this).parent().find('.mi-tooltip').css('display', 'block');
			var commentText = $(this).parent().find('span.scroll-content').html();
			if (commentText.length > 217) {
				$( ".scroll-parent" ).css('display' , 'block');
				$(".mi-scrollUp").css('display' , 'block');
				$(".mi-scrollDn").css('display' , 'block');
				$('.mi-tooltip').css('height','265px');
				$('span.scroll-content').css('width' , '219px');
				$( "span.mi-right .ui-slider" ).slider({
					orientation: "vertical",
					range: "min",
					min: 0,
					max: 100,
					value: 100,
					slide: function( event, ui ) {
						var maskHeight = $("span.mi-right span.mi-scroller").height();
						var scrollHeight = $("span.mi-right span.scroll-content").height();
						var scrollableArea = scrollHeight - maskHeight;
						$("span.mi-right span.scroll-content").css('margin-top', 0-((100-ui.value) * (scrollableArea/100)) + "px");  
					}
				});
			}
			$('.mi-right img').click(function(e){
				e.preventDefault();
				$(this).parent().parent().css('display', 'none');
			});
		});
		
				$( "span.mi-right .ui-slider" ).slider('value' , [100]);
				$("span.mi-scrollUp").click(function(e){
					e.preventDefault();
					$( "span.mi-right .ui-slider" ).slider('value' , [100]);
					$("span.mi-right span.scroll-content").css('margin-top', "0");  
				});
				$("span.mi-scrollDn").click(function(e){
					e.preventDefault();
					$( "span.mi-right .ui-slider" ).slider('value' , [1]);
						var maskHeight = $("span.mi-right span.mi-scroller").height();
						var scrollHeight = $("span.mi-right span.scroll-content").height();
						var scrollableArea = scrollHeight - maskHeight;
						$("span.mi-right span.scroll-content").css('margin-top', (0-((100) * (scrollableArea/100))) + "px");  
				});
		
	}
}

function ajaxInit(){
	var hasMousewheel = false;
        $.getScript( 'js/lazyLoad/jquery.mousewheel.js', function(){
                        setTimeout(function(){
				hasMousewheel = true;
				}, 200);
        });
	$('div.js-ajaxMenu .submit').click(function(e){
		e.preventDefault();
		loadSub($(this).attr('rel'));	
	});
	$('.form_box a').click(function(){
		var dropDown =$(this).parent().parent().find('.drop-down');
		dropDown.toggle();	
		$(this).parent().parent().find('li').hover(function(){
			$(this).parent().find('li').removeClass('selected');
			$(this).addClass('selected');
			$(this).parent().parent().parent().parent().find('.form_box span').html($(this).html());	
			$(this).parent().parent().parent().parent().find('.form_box input').attr('value',  $(this).html());	
		},
		function(){
			$(this).removeClass('selected');
		});
		$(this).parent().parent().find('li').click(function(){dropDown.hide();});
		$(this).parent().parent().mouseleave(function(){dropDown.hide();});
		$(this).parent().parent().mouseover(function(){
			if(hasMousewheel){
				$(this).mousewheel(function(e,delta){
					$(this).find('li').unbind();
					e.preventDefault();
					var scrollSelect;
					var realDelta = delta>0?1:-1;
					if ($(this).find('li.selected').index() + realDelta >=0){
						scrollSelect = $(this).find('li.selected').index() + realDelta;
						$(this).find('li').removeClass('selected');
						$(this).find('li:eq('+scrollSelect+')').addClass('selected');
						if(scrollSelect > ($(this).find('li').length -1)){
							scrollSelect = ($(this).find('li').length -1);
							$(this).find('li').removeClass('selected');
							$(this).find('li:eq('+scrollSelect+')').addClass('selected');
						}
					$( ".ui-slider" ).slider("value", (100-(scrollSelect/($(this).find('li').length -1)) * 100));
					scrollContent = $(this).find('.scroll-content');
					scrollPane = $(this).find('.scroll-pane');
					scrollContent.css( "margin-top", Math.round(
                                                ((scrollSelect/($(this).find('li').length -1)) * 100)/ 100 * ( scrollPane.height() - scrollContent.height() )
                                        ) + "px" );
                        		$(this).find('.form_box span').html($(this).find('.selected').html());
                        		$(this).find('.form_box input').attr('value',  $(this).find('.selected').html());
					}
					$(this).find('li').hover(function(){
                        $(this).parent().find('li').removeClass('selected');
                        $(this).addClass('selected');
                        $(this).parent().parent().parent().parent().find('.form_box span').html($(this).html());
                        $(this).parent().parent().parent().parent().find('.form_box input').attr('value',  $(this).html());
                },
                function(){
                        $(this).removeClass('selected');
                });

				});
			}
		});
	});

		$( ".ui-slider" ).slider({
			orientation: "vertical",
			range: "min",
			min: 0,
			max: 100,
			value: 60,
			slide: function( event, ui ) {
				var scrollPane = $(this).parent().parent();
				var scrollContent = scrollPane.find('.scroll-content');
				if ( scrollContent.height() > scrollPane.height() ) {
					scrollContent.css( "margin-top", Math.round(
						(ui.value) / 100 * ( scrollPane.height() - scrollContent.height() )
					) + "px" );
				} else {
					scrollContent.css( "margin-top", 0 );
				}
			}
		});	
	       	// cuz we love out tooltips
		tooltipInit();
$('div.js-ajaxMenu div.mi-menuHeader span').click(function(){
		loadSub('#blankMe');
});
	
}

function loadSub(item){
		$('.js-ajaxMenu').html('');
		$('.js-ajaxMenu').append($(item));
		ajaxInit();
}

function imageMenuInit(){
	tooltipInit();
	$(".mi-tireSelector ul li:first-child").addClass('js-selected');
	$(".mi-tireSelector ul li:first-child .js-collapse").css({ "height" : $(".mi-tireSelector ul li:first-child .js-collapse").height() + "px", "display": "block" });
	zipField();
}

