/*
 * SimpleModal Contact Form
 * http://www.ericmmartin.com/projects/simplemodal/
 * http://code.google.com/p/simplemodal/
 *
 * Copyright (c) 2010 Eric Martin - http://ericmmartin.com
 *
 * Licensed under the MIT license:
 *   http://www.opensource.org/licenses/mit-license.php
 *
 * Revision: $Id: contact.js 254 2010-07-23 05:14:44Z emartin24 $
 */

jQuery(
	function ($) {
	// contact is a variable which is an array
	var contact = {
		message: null,
		init: function () {
			$('a.openCalculator').click(function (e) {
				e.preventDefault();

				// load the contact form using ajax
				$.get("shipping_calculator_popup.php?first=1", function(data){
					// create a modal dialog with the data
					$(data).modal({
						closeHTML: "<a href='#' title='Close' class='modal-close'>x</a>",
						position: ["5%",],
						overlayId: 'contact-overlay',
						containerId: 'calculator-container',
						onOpen: contact.open,
						onShow: contact.show,
						onClose: contact.close
					});
				});
			});
		},
		open: function (dialog) {//alert('open');
// hide any flash objects
        $('object').css('visibility', 'hidden');
        // hide any flash embeds
        $('embed').css('visibility', 'hidden');

			// determine height of window
			dialog.overlay.fadeIn(200, function () {
				dialog.container.fadeIn(200, function () {
					dialog.data.fadeIn(200, function () {
						$('#calculator-container .contact-content').animate({
							height: '560'
						});
					});
				});
			});
		},
		show: function (dialog) {//alert('show');

			$('#calculator-container .newForm').live("click", function (e) {//alert('click inputbutton');
				// reload the form using ajax
				$('#calculator-container .contact-content').animate({
						height: '580'
					}, function () {
						$('#calculator-container .contact-loading').fadeIn(200, function () {//alert('loading');
							$.ajax({
								url: 'shipping_calculator_popup.php',
								data: $('#calculator-container form').serialize(),
								type: 'post',
								cache: false,
								dataType: 'html',
								success: function (data) {//alert(data);
									// put data in window
									$('#calculator-container .contact-content').html(data).fadeIn(200);
									$('#calculator-container .contact-loading').fadeOut(200);
								},
								error: contact.error
							});// end ajax

/*
							$.get("shipping_calculator_popup.php", function(data){
								$('#calculator-container .contact-content').html(data).fadeIn(200);
								$('#calculator-container .contact-loading').fadeOut(200);
							})// end get call
*/
						})
				});// end animate
			}) // end click

			// must put 'live' so will bind future selectors (the input button) as well as the one currently in the dialog
			$('#outside-container .calculateRequest').live("click", function (e) {//alert ('clicked button');
				e.preventDefault();
				var msg = $('#contact-container .contact-message');
				// send form
				$('#calculator-container .pageHeading').html('Getting price quote...');
				$('#calculator-container form').fadeOut(200); // clears form from window

				$('#calculator-container .contact-content').animate({
						height: '580px'
					}, function () {//alert('got to process');
						$('#calculator-container .contact-loading').fadeIn(200, function () {
							$.ajax({
								url: 'shipping_calculator_popup.php',
//								data: $('#calculator-container form').serialize() + '&action=process',
								data: $('#calculator-container form').serialize(),
								type: 'post',
								cache: false,
								dataType: 'html',
								success: function (data) {//alert(data);
									// put data in window
									$('#calculator-container .contact-content').html(data).fadeIn(200);
									$('#calculator-container .contact-loading').fadeOut(200);
								},
								error: contact.error
							});// end ajax
						}); // end of loading data from ajax call
					});
			}) // end click
/*
$('#outside-container .modal-close').live("click", function () {
	$.modal.close();
})
*/

		},
		close: function (dialog) {//alert('close');
			$('#calculator-container .contact-message').fadeOut();
			$('#calculator-container form').fadeOut(200);
			$('#calculator-container .contact-content').animate({
				height: 40
			}, function () {
				$.modal.close();
			});

		    // show any flash objects
			$('object').css('visibility', 'visible');
			// show any flash embeds
			$('embed').css('visibility', 'visible');

		},
		error: function (xhr) {
			alert(xhr.statusText);
		},
		showError: function () {
			$('#calculator-container .contact-message')
				.html($('<div class="contact-error"></div>').append(contact.message))
				.fadeIn(200);
		}
	}; // end of definition of contact var

	contact.init();

});



