$(document).ready(function(){
	
	// Google Map
	if ((typeof($('#gmap')[0]) != 'undefined') && (typeof(GBrowserIsCompatible) != 'undefined') && GBrowserIsCompatible()) {
		var html = "Elegant Foods" +
					"<br />537 Atlas Ave." +
					"<br />Madison, WI 53714" +
					"<br /><br />" +
					"<a href='http://www.google.com/maps?hl=en&lr=&q=elegant+foods&near=Madison,+WI&radius=0.0&cid=43073056,-89401111,12620308254026554202&iwstate1=form:to'>Get directions to here</a>"
        var map = new GMap2($('#gmap')[0]);
        map.setCenter(new GLatLng(43.0895, -89.3115), 13);
		map.addControl(new GSmallMapControl());
		map.openInfoWindowHtml(map.getCenter(), html);
	}
			
	// Login Box
	$('a.login_link').facebox();
	
	// Add to Cart buttons
	$('form.addtocart').ajaxForm({
		target: '.cart',
		success: function(){
			$(this).parent().effect('highlight', {color: '#78bde8'}, 3000);
		}
	});
	
	// Checkout time
	$('a#checkout').click(function(){
		if($.data(this, 'clicked')){
			$.data(this, 'clicked', false);
			return false;
		} else {
			$.data(this, 'clicked', true);
			var url = this.href;
			var checkd = $.map(
			                    $('input:checked').get(),
			                    function(el){
			                        return $(el).siblings('input:hidden').val();
			                    }
			                  );
			checkd = checkd.join(",");
			
			$.facebox(function(){
				$.get(url, {og: checkd}, function(data){
					$.facebox.reveal(data);
					$('#order_deliver_on').datepicker({showOn: 'button', buttonImage: '/images/calendar.gif', buttonImageOnly: true});
					$('#order_submit').submit(function(){
						if($.data(this, 'clicked')){
							return false;
						} else {
							if ($('#order_deliver_on').val() == '') {
								alert('You must enter a delivery date.');
								return false;
							} else {
								$.data(this, 'clicked', true);
								$(this).append('<div class="loading"><img src="'+$.facebox.settings.loading_image+'"/></div>');
							}
						}
					});
				});
			});
			return false;
		}
	});
	
	$('.og_remove').click(function(){
	    var $that = $(this);
	    var pid = $(this).siblings('input:hidden').val();
	    var authToken = $('input[@name="authenticity_token"]').val();
	    $.post(this.href, {product: pid, authenticity_token: authToken}, function(){
	        $that.parents('li').remove();
	    });
	    return false;
	});
	
	// Edit User Password
	$('#edit_user').facebox();
		
	// Temp function for layout grid
	$('#switch').click(function(){
		$('#wrap').toggleClass('layout');
	});
});

$(document).unload(function(){
	if (typeof(GUnload) != 'undefined') GUnload();
});