
jQuery(document).ready(function(){
		jQuery('body').append(
		jQuery('<div>', { 'id': 'postal-code-search' }).append(
			jQuery('<form>', { 'id': 'postal-code-search-form' }).append(
				jQuery('<h3>', { 'class': 'postal-code-search-title' }).html( '<img src="/skin/frontend/default/f001_grey/images/fd_logo.png" width=200"  />' )
			).append(
				jQuery('<p>').html( 'Enter your Zipcode to Start the Savings' )
			).append(
				jQuery('<input>', { 'id': 'input-postal-code', 'type': 'text', 'value': '', 'name': 'postal_code' })
			).append(
				jQuery('<input>', { 'id': 'input-postal-code-submit', 'type': 'submit', 'value': ' Submit ' })
			)
		)
	).append(
		jQuery('<a>', { 'id': 'postal-code-search-link', 'href': '#postal-code-search-form' }).html( 'View prices and availability in your area...' ).fancybox()
	).append(
		jQuery('<p>')
	);

	jQuery('#postal-code-search').hide();

	var defaultWebsite = jQuery.cookie( 'defaultWebsite' );
	var currentUrl = jQuery.url( window.location );
	if ( defaultWebsite && currentUrl.attr('host') != defaultWebsite ) {
		var expDate = new Date();
 		expDate.setTime(expDate.getTime() + ( 10 * 12 * 30 * 60 * 1000));
		jQuery.cookie( 'defaultWebsite', defaultWebsite, { 'path': '/', 'domain': 'furnituredeals.com', 'expires': 10000  } );
		var destAddress = currentUrl.attr('protocol') + '://' + defaultWebsite
			+ currentUrl.attr('relative');
		window.location.replace( destAddress );
	}
	else if ( ! defaultWebsite ) {
		jQuery.fancybox.init();

		jQuery('#postal-code-search-link').trigger( 'click' );
		

	}

	jQuery('#postal-code-search-form').ajaxForm({
		url: '/ajax_store_lookup.php',
		type: 'get',
		dataType: 'json',
		clearForm: true,
		success: function(data, jqForm, options){
			var currentUrl = jQuery.url( window.location );
			var storeUrl = jQuery.url( data.storeUrl );

		
				
			if ( currentUrl.attr('host') != storeUrl.attr('host') ) {
				var expDate = new Date();
				expDate.setTime(expDate.getTime() + ( 10 * 12 * 30 * 60 * 1000));
				jQuery.cookie( 'defaultWebsite', storeUrl.attr('host'), { 'path': '/', 'domain': 'furnituredeals.com', 'expires': 10000 } );

				var destAddress = currentUrl.attr('protocol') + '://' + storeUrl.attr('host')
					+ currentUrl.attr('relative');

				jQuery.fancybox.close();

				window.location.replace( destAddress );
			}
			else {
				jQuery.cookie( 'defaultWebsite',  currentUrl.attr('host'), { 'path': '/', 'domain': 'furnituredeals.com', 'expires': 10000 } );
				jQuery.fancybox.close();
			}
		}
	});


});

