var template_path = '/wp-content/themes/sorrentogrille'
var template_directory = "http://sorrento-grille.com" + template_path;

// On DOM load
$(function() {
	$('.contact_blurb').remove().insertAfter('form')

	// Set our options for 12All
	form_handler.container = '.oted_12allform_container'
	form_handler.form = form_handler.container + ' form'
	form_handler.header = form_handler.container + ' h3:first'
	form_handler.legend = form_handler.container + ' div.legend'
	form_handler.disclaimer = form_handler.container + ' div.disclaimer'

	fancy_fonts()		// sIFR-ify
	validate_forms()	// Because 'nhdnthdrcgd' isn't a valid email address
	
	
	// Insert Flash Banner Ad
	$('#banner').flash(
        { src: template_directory + '/flash/banner.swf',
          width: 782,
          height: 79 }
    );
	
})


// Gift card settings and submit handler, called from the Gift Card validation call in validate_forms
var gift_card = {
	fieldset: '.gift_cards_form fieldset',
	shipping: {
		usps:	'3.85',
		fedex:	'12.00'	
	},
	
	tally: function() {
		var amount = parseInt( $('#card_amount').val() )
		var quantity = parseInt( $('#cart_quantity').val() ) 
		
		// Add gift cards
		$(this.fieldset).append('<input type="hidden" name="item_name_1" value="Gift Card(s) "for $' + amount + '" />')	
		$(this.fieldset).append('<input type="hidden" name="amount_1" value="' + amount + '" />')	
		$(this.fieldset).append('<input type="hidden" name="quantity_1" value="' + quantity + '" />')	

		// Add shipping
		$(this.fieldset).append('<input type="hidden" name="item_name_2" value="' + $("#card_shipping option:selected").text() + '" />')	
		$(this.fieldset).append('<input type="hidden" name="amount_2" value="' + this.shipping[$('#card_shipping').val()] + '" />')		
	}
}

var validate_forms = function() {
	// Validate our gift card form and send it to the gift_card handler
	$('.gift_cards_form').validate({
		rules: {
			card_amount: "required",
			cart_quantity: {
				required:	true,
				min:	  	1
			},
			card_shipping: "required"
		},
		messages: {
			card_amount:	"What dollar amount should we load on each card?",
			cart_quantity:	"How many cards do you want to receive?",
			card_shipping:	"How would you like you order to be shipped?"
		},
		submitHandler: function(form) {
			gift_card.tally()	// Add the PayPal vars to the page...
			form.submit()		// And then submit
		}
	})
	
	// Validate our contact form
	$('.contactform form').validate({
		rules: {
			wpcf_your_name: "required",
			wpcf_msg: "required",
			wpcf_email: {
				required:	true,
				email:		true
			}
		},
		messages: {
			wpcf_your_name: "Please enter your name",
			wpcf_msg:		"Please enter a brief message",
			wpcf_email:		"Please enter a valid email address"
		}
	})
	
	// Validate our email signup form, and pass it to the form_handler's submit function
	$('.oted_12allform_form').validate({
		rules: {
			email: { required: true, email: true }
		},
		messages: { email: "Please enter a valid email address" },
		submitHandler: function() {form_handler.submit(); return false }
	})	
}


// Re-jigger our h2 metrics, and call .sifr to drop the Flash replacement in there
var fancy_fonts = function() {
	$('h2')
	.css({
		fontSize:	'40px',
		height:		'35px'
	})
	.sifr({
		font:	'bradley_hand',
		path:	template_path + '/flash',
		width:	200,
		color:	'#040403'
	})
}