// Mats Calculator
jQuery(function($) {
	// Set def easing.
	Veil.toOpen.duration = 600;
	Veil.toOpen.easing = "easeInOutQuad";
	Veil.toClose.duration = Veil.toOpen.duration;
	Veil.toClose.easing = "easeInOutQuad";
	//Prep form elements
		// Make form AJAX
	var options = {
		target: '#prodcalList',
		beforeSubmit: handleSubmit,
		success: handleResult,
		url: 'prodcal-suntouch-exec.asp'
	};
	$('#prodcal').ajaxForm( options );
	$('#prodcal').submit(function() {
		$(this).ajaxSubmit();
		return false;
	});
	
		// Numeric entery
	$("input#prodcalArea").autotab({format:"number"});
	$("input#prodcalCostKWH").autotab({format:"number"});
		// Show Answer on focus
	$("input#prodcalArea").focus(function() {
		autoShow("#sqft .answer");
	});
	$("select#prodcalVoltage").focus(function() {
		autoShow("#volt .answer");
	});
	$("select#prodcalSubfloor").focus(function() {
		autoShow("#subflr .answer");
	});
	$("input#prodcalCostKWH").focus(function() {
		autoShow("#kwh .answer");
	});
	$("input#prodcalCostKWH").focus(function() {
		autoShow("#kwh .answer");
	});
	$("select#prodcalQuoteKits").focus(function() {
		autoShow("#qutkit .answer");
	});
		// Hide Answer on blur
		/*
	$("input#prodcalArea").blur(function() {
		autoHide("#sqft .answer");
	});
	$("select#prodcalVoltage").blur(function() {
		autoHide("#volt .answer");
	});
	$("select#prodcalSubfloor").blur(function() {
		autoHide("#subflr .answer");
	});
	$("input#prodcalCostKWH").blur(function() {
		autoHide("#kwh .answer");
	});
	$("select#prodcalQuoteKits").blur(function() {
		autoHide("#qutkit .answer");
	});
		*/
		
	//Prep help icons
	Veil.hide(".answer", {duration:10,easing:"jswing"});
	$("#helpSqft").click(function() {
		manualShowHide("#sqft .answer");
	});
	$("#helpVolt").click(function() {
		manualShowHide("#volt .answer");
	});
	$("#helpSubFlr").click(function() {
		manualShowHide("#subflr .answer");
	});
	$("#helpKWH").click(function() {
		manualShowHide("#kwh .answer");
	});
	$("#helpQutKit").click(function() {
		manualShowHide("#qutkit .answer");
	});
	// Prep result area
	if( $("#prodcalList").is(":parent") ) {
		handleResult();
	} else {
		Veil.hide("#prodcalResult", {duration:5,easing:"jswing"});
	}
});
var handleSubmit = function(formData, jqForm, options) {
	$(".open-auto").each(function(){ autoHide("#" + $(this).parent().attr("id") + " .answer"); });
	
	// Hide previous result
	if( $("#prodcalList").is(":parent") ) {
		Veil.hide("#prodcalResult", {duration:5,easing:"jswing"});
	}
	
	// Show processing animation
	if( $("#prodcalLoading").not(":hidden") ) {
		$("#prodcalLoading").css("visibility", "visible").hide();
	}
	$("#prodcalLoading").show();
	
	// Switch submit button to disabled state
	$("#prodcalSubmit").hide();
	
	return true;
};
var handleResult = function() {
	// Hide processing animation
	$("#prodcalLoading").hide();
	
	// Switch submut button to enabled state
	$("#prodcalSubmit").show();
		
	Veil.show("#prodcalResult", { duration:800,easing:"jswing",complete:function(){ $().scrollTo("a[name=result]",1000); } });
	$("input[id$=prod1]").each(function() {
		this.checked = true;
		hideProductFromPrint(this.name);
	});
	$("table[id^=prodopt]").hide();
	$("td.ind-sel, td.choose").hide();
	$(".moreoptions").click(function() {
		$(this).hide();
		$(this).parent().find("td.ind-sel, td.choose").show();
		Veil.show( "#" + $(this).siblings("[id^=prodopt]").attr("id") );
	});
	
	// Print buttons
	$(".btn-print").click(function(){
		window.print();
	});
	// Buy buttons
	$(".btn-buy").click(function() {
		this.href = makeCartUrl();
	});
};

var autoShow = function(s) {
	if( !$(s).is(".open-manual") ) {
		$(s).addClass("open-auto");
		Veil.show(s);
	}
};

var autoHide = function(s) {
	if( !$(s).is(".open-manual") ) {
		$(s).removeClass("open-auto");
		Veil.hide(s);
	}
};

var manualShowHide = function(s) {
	if($(s).is(":hidden")) {
		$(s).addClass("open-manual");
	} else {
		$(s).removeClass("open-manual");
	}
	$(s).removeClass("open-auto");
	Veil.showHide(s);
};

var hideProductFromPrint = function(rgName) {								
	$("input[name=" + rgName + "]").each(function(){
		if(this.checked) {
			$(this).parents("tr.product").removeClass("unselected");
			$(this).parents("tr.product").addClass("selected");
		} else {
			$(this).parents("tr.product").removeClass("selected");
			$(this).parents("tr.product").addClass("unselected");
		}
	});
};

var fillForm = function(formName, newVals, autoSubmit) {
	var e;
	var f = $("form[name=" + formName + "]")[0];
	var s = function(elm, val) {
		switch(elm.type) {
			case "hidden" :
			case "text" :
				elm.value = val;
			break;
			case "select-one" :
				$(elm).children("option[value=" + val +"]").each(function() {
					this.selected = true;
				});
			break;
		}
	};
	for(e in newVals) {
		if(typeof f[e] != "undefined") {
			s(f[e], newVals[e]);
		}
	}
	if(autoSubmit) { 
		$(f).submit(function() {
			$(this).ajaxSubmit();
			return false;
		}).submit();
	}
};