// jQuery functions
$(function(){
	var rndNum = Math.ceil(2 + (Math.random() * 7));
	var rndNum2 = Math.ceil(2 + (Math.random() * 5));
	$("body.inside #wrapper").css("background-image","url(assets/img/bg-metal" + rndNum + ".jpg)");
	$("body#pg-index #wrapper").css("background-image","url(assets/img/bg-metal1" + rndNum2 + ".jpg)");
	
	$('ul#menu').supersubs({ 
            minWidth:    12,   // minimum width of sub-menus in em units 
            maxWidth:    27,   // maximum width of sub-menus in em units 
            extraWidth:  1     // extra width can ensure lines don't sometimes turn over 
                               // due to slight rounding differences and font-family 
        }).superfish({
				delay: 200,
				animation: {opacity:'show',height:'show'},
				speed: 'fast',
				dropShadows: false
			});
	
	$('table.listtab tr:even').addClass('alt');
	$('table.listtab tbody td:first-child').addClass('first');

	//var origianlSubmitEvent = $('#rfq').submit;

	$("div.error").hide();
	$("#submit").click(function(){
		$("#rfq").validate({
				errorElement: "span",
				errorClass: "required",
				errorLabelContainer: "#messageBox ul",
				wrapper: "li",
				submitHandler: function(form) {			
					form.submit();
			   },
			   invalidHandler: function(form, validator) {
				  var errors = validator.numberOfInvalids();
				  if (errors) {
					var message = errors == 1
					  ? 'You missed <em>1 field</em>. It has been highlighted'
					  : 'You missed <em>' + errors + '</em> fields. They have been highlighted';
					$("div.error span").html(message);
					$("div.error").show();
				  } else {
					$("div.error").hide();
				  }
				},
				rules: {
					companyname: "required",
					street: "required",
					city: "required",
					state: "required",
					zip: "required",					
					estimatedquote: "required",
					estimatedshipping: "required",
					quoteduedate: "required",
					tooling: "required",
					firstname: "required",
					lastname: "required",
					phonenumber: "required",
					email: {
						required: true,
						email: true
					}
				},
				messages: {
					companyname: "Please enter the Company Name",
					street: "Please enter the Street",
					city: "Please enter the City",
					state: "Please enter the State",
					zip: "Please enter the Zip Code",					
					estimatedquote: "Please enter the Estimated quote quantity",
					estimatedshipping: "Please enter the Estimated shipping quantity requirements",
					quoteduedate: "Please enter the Quote Due Date",
					tooling: "Please respond with Yes or No",
					firstname: "Please enter the First Name",
					lastname: "Please enter the Last Name",
					phonenumber: "Please enter the Phone number",
					email: {
						required: "We need your email address to contact you"	,
						email: "Your email address must be in the format of name@domain.com"
					}
				}
		});
		$.scrollTo(0, 1000);
	});
	
	$("#submit-contact").click(function(){
		$("#contactform").validate({
				errorElement: "span",
				errorClass: "required",
				errorLabelContainer: "#messageBox ul",
				wrapper: "li",
				submitHandler: function(form) {			
					var options = {
						 success: function(responseText) {
							 $("div.error span").html(responseText);							 
							 $("div.error").show().addClass('responsemessage');
						 }
					 };
					$(form).ajaxSubmit(options);					
			   },
			   invalidHandler: function(form, validator) {
				  var errors = validator.numberOfInvalids();
				  if (errors) {
					var message = errors == 1
					  ? 'You missed <em>1 field</em>. It has been highlighted'
					  : 'You missed <em>' + errors + '</em> fields. They have been highlighted';
					$("div.error span").html(message);
					$("div.error").show();
				  } else {
					$("div.error").hide();
				  }
				},
				rules: {
					firstname: "required",
					lastname: "required",
					email: {
						required: true,
						email: true
					}
				},
				messages: {
					firstname: "Please enter your First Name",
					lastname: "Please enter your Last Name",
					email: {
						required: "We need your email address to contact you"	,
						email: "Your email address must be in the format of name@domain.com"
					}
				}
		});	
		$.scrollTo(0, 1000);
	});	
});

$(window).load(function () { 
	Cufon.replace('.bxw h2,.bxb h2,.bxw h3,.heading');
	$('.bxw h2,.bxb h2,.bxw h3,.heading').css({'visibility':'visible'});
});






