$(document).ready(function(){
	var mtd = mtd || {};
	$("body").append("<script type='text/javascript'>var ie6 = false;</script><!--[if IE 6]><script type='text/javascript'>ie6 = true;</script><![endif]-->");
	
	$('.cSelectBox').selectbox({// calls the custom select script to turn regular selects into the custom ones
		onChangeCallback: genSelectCallBack,
		debug: false
	});		
	function genSelectCallBack(args){
		if(typeof(jspSelectCallBack) !== 'undefined'){
			jspSelectCallBack(args.selectedId, args.selectedText, args.selectedValue); //define this function on the jsp you need the callback functionality
		}
	}
    arrangeCustomSelects("");	// sets appropriate z-indexes on the custom selects
	
	if($().jqm) {
		initModals();
	}
	
	// FAQ tab functionality (some is automatically handled by tabs.plugin.js)
	mtd.currentFAQCategory = 0;
	mtd.textslide = 600;
	$(".faq_nav .trigger a:not('.active')").nextAll().addClass("hidden");	// initially hide non active tabs
	$(".faq_nav .trigger a").click(function() {
		mtd.currentFAQCategory = $(".faq_nav .trigger a").index(this);
		$(".faq_nav .trigger a:not(:eq(" + mtd.currentFAQCategory + "))").nextAll().addClass("hidden");
		$(this).nextAll().removeClass("hidden");
	});
	// handles when you click on each question
	$(".faq_content h3").addClass("hidden");
	$(".faq_content .q").nextAll().hide();
	$(".faq_content .q").toggle(function() {
		$(this).addClass("active");
		$(this).nextAll().slideDown(mtd.textslide);
	}, function() {
		$(this).removeClass("active");
		$(this).nextAll().slideUp(mtd.textslide);
	});
	// end FAQ tab functionality

	$("#diagnose-submit").click(function() {
		diagnose();	
	});
	
	// expand and hide the order details
	mtd.numPackages = $(".order_content .package").size();
	mtd.packageArray = new Array(mtd.numPackages+1);
	mtd.currentPackage = -1;
	mtd.allopen = false;
	mtd.allclosed = false;
	mtd.slidespeed = 800;
	for(var i = 0; i <= mtd.numPackages; i++) {
		mtd.packageArray[i] = false;	// more_info container closed
	}
	// handle the expand all link
	$(".order_content .expand_all a").click(function() {
		if(!mtd.packageArray[0]) {
			for(var i = 0; i <= mtd.numPackages; i++) {
				mtd.packageArray[i]	= true;
			}
			$(this).text("Hide All").addClass("open");
			$(".order_content .package .expand a").each(function() {
				$(this).text("Hide Details").addClass("open");
			})
			$(".order_content .package .more_info").each(function() {
				$(this).slideDown(mtd.slidespeed);
			});
		} else {
			for(var i = 0; i <= mtd.numPackages; i++) {
				mtd.packageArray[i]	= false;
			}
			$(this).text("Expand All").removeClass("open");
			$(".order_content .package .expand a").each(function() {
				$(this).text("View Details").addClass("closed");
			})
			$(".order_content .package .more_info").each(function() {
				$(this).slideUp(mtd.slidespeed);
			});
		}
	});
	// handle each individual View Details link
	$(".order_content .package .expand a").click(function() {
		mtd.currentPackage = $(".order_content .package .expand a").index(this) + 1;
		if(!mtd.packageArray[mtd.currentPackage]) {
			mtd.packageArray[mtd.currentPackage] = true;
			$(this).text("Hide Details").addClass("open");
			$(this).parent().parent().next(".more_info").slideDown(mtd.slidespeed);
			// if all tabs are open, change the expand all link to the hide all functionality
			mtd.allopen = true;
			for(var i = 1; i <= mtd.numPackages; i++) {
				if(!mtd.packageArray[i]) {
					mtd.allopen = false;
				}
			}
			if(mtd.allopen) {
				mtd.packageArray[0] = true;
				$(".order_content .expand_all a").text("Hide All").addClass("open");
			}
		} else {
			mtd.packageArray[mtd.currentPackage] = false;
			$(this).text("View Details").removeClass("open");
			$(this).parent().parent().next(".more_info").slideUp(mtd.slidespeed);
			// if all tabs are closed, change the hide all link to the expand all functionality
			mtd.allclosed = true;
			for(var i = 1; i <= mtd.numPackages; i++) {
				if(mtd.packageArray[i]) {
					mtd.allclosed = false;
				}
			}
			if(mtd.allclosed) {
				mtd.packageArray[0] = false;
				$(".order_content .expand_all a").text("Expand All").removeClass("open");
			}
		}
	});
	// end expand and hide order details
});

function positionModal(modalWrapper) {	// jqModal doesn't center the modal horizontally, so we handle that here
	// the modal is positioned from the left by 50% of the window width
	if($(modalWrapper).height() >= $(window).height()) {
		if($("body").scrollTop()) {
			$(modalWrapper).css("top", $("body").scrollTop() + "px");
		} else if($("html").scrollTop()) {
			$(modalWrapper).css("top", $("html").scrollTop() + "px");
		} else {
			$(modalWrapper).css("top", "0");
		}
		$(modalWrapper).css("position", "absolute").css("margin-top", "0");
	} else {
		$(modalWrapper).css("position", "").css("margin-top", "-" + ($(modalWrapper).height()/2) + "px").css("top", "");
	}
	$(modalWrapper).css("margin-left", "-" + ($(modalWrapper).width()/2) + "px");	// calculate half of the modal width and set the modal's margin-left to be the negative of the result
	
	$(".jqmWindow .cSelectBox").selectbox({});	// calls the custom select script to turn regular selects into the custom ones.  The scope is limited to this modal window only!
	arrangeCustomSelects(".jqmWindow ");	// sets appropriate z-indexes on the custom selects
	$(".selectbox-wrapper").css("display", "none");
}

function arrangeCustomSelects(scope){	// sets z-indexes on the custom selects so that a drop down from a custom select located higher on the page will not display underneath a custom select that's lower on the pgae 
	var selects = jQuery(scope + '.cBoxFloatWrap');
	var zIndex = selects.length;
	selects.each(function(){
		jQuery(this).css('z-index', zIndex);
		zIndex--;
	});
}

$('#find_service_centers').bind("click", findServiceCenters);
	
function findServiceCenters(evt) {
	evt.preventDefault();
	if(checkAllErrors('#doc-mtd',2)){
		document.locatorForm.submit();
	}
}

$('#sidebar_find_service_centers').bind("click", sidebarFindServiceCenters);
	
function sidebarFindServiceCenters(evt) {
	evt.preventDefault();
	if(checkAllErrors('#doc-mtd',21)){
		document.sidebarLocatorForm.submit();
	}
}

function createAddressNickName(fname, lname, addr1, addr2, city){
	(addr2 == '') ? fullAddr = addr1 + ' ' + city : fullAddr = addr1 + ' ' + addr2 + ' ' + city;
	var nickName = fname + ' ' + lname + ', ' + fullAddr;
	return nickName
}
function initQuickView(bAddTrigger){
	// Start Quick View rollover functionality 
	$(".list_item .prod_img").mouseenter(function() {
		$(this).children(":last").show();
	}).mouseleave(function() {
		$(this).children(":last").fadeOut(200);
	});
	if(bAddTrigger){ //if dom has been updated after initial page load, reapply triggers
		$('.jqmWindow').jqmAddTrigger($('.prod_img'));
	}
	// End Quick View rollover functionality
}
function initModals() {
	// jqModal call
	$(".jqmWindow").jqm({	// call to jqModal's main function with the following parameters:
		trigger: '.prod_img, .wishList_btn, .add_to_garage, .send_wishlist, .account_wrapper .session .edit, .account_wrapper .addr_item .edit, .update, .pw_recall, .new_addr',
		closeClass: '.modal .close, .modal .cancel',
		ajax: '@href',
		modal: true,
		onLoad: function() { positionModal(this); }
	});
	$('.waitbox').jqm({
		closeClass: 'close',
		overlayClass: 'jqmWaitOverlay'
	});
	$('.ajaxErrorMsg').jqm({
		closeClass: 'close',
		overlayClass: 'jqmAjaxErrorOverlay',
		onHide: function(h) {
			h.w.slideUp("fast",function() { if(h.o) {$('.errMsgDisplay').html('');h.o.remove(); }}); 
        } 
	});
	// end jqModal call
	initQuickView(false);
	
}
function scrollToTop(){
	window.scrollTo(0,0); 
} 
