$(document).ready(function(){
	
	$('#searchBox form').keydown(function(e){
		if(e.keyCode == 13) {
			$(this).submit();
		}
	});

	$('a.button.soon').hover(function() {
		$(this).data('text', $(this).text());
		$(this).width($(this).width());
		$(this).text('Coming Soon');
	}, function() {
		$(this).text($(this).data('text'));
	});
	
	$('input[placeholder],textarea[placeholder]').addPlaceholder();

	var isiPad = navigator.userAgent.match(/iPad/i) != null;
	if(isiPad && !$('#content.home').length) {
		var footerTopOffset = $('#footer').offset().top;
		var contentBottomOffset = $('#header').outerHeight(true) + $('#content').outerHeight(true) - $('#footer').outerHeight() + 7;
		if (contentBottomOffset > footerTopOffset) {
			$('#footer').css({
				'position': 'absolute',
				'top': contentBottomOffset+'px'
			});
		}
	}
	
	if (location.hash == '#flyers') {
		$('a[href="#flyersBox"]').click()
	}
		
	$(window).load(function() {
		$('.cookingContent .image img').each(function() {
			var productImage = $(this).closest('.container').find('.productImage');
			if(productImage.width() > productImage.height()) {
				$(this).height(100);
			}
			else {
				$(this).width(100);
			}
		});
		
		if(isiPad && !$('#content.home').length) {
			var footerTopOffset = $('#footer').offset().top;
			var contentBottomOffset = $('#header').outerHeight(true) + $('#content').outerHeight(true) - $('#footer').outerHeight() + 7;
			if (contentBottomOffset > footerTopOffset) {
				$('#footer').css({
					'position': 'absolute',
					'top': contentBottomOffset+'px'
				});
			}
		}
	});

	

    (function(){
        $('#signUpLink').show();
        $('#signUpLink').click(function(e){
            e.preventDefault();
            $('#signUpTab').slideToggle();
        });
		var form = $('#emailSignUpForm');
        form.submit(function(e){
            e.preventDefault();
            $.post(form.attr('action'), form.serialize(), function(data){
				//console.log(data);
                if (data.success) {
                    $('#signUpTab p').text(data.success).css('color', 'green');
                }
                else 
                    if (data.error) {
                        $('#signUpTab p').text(data.error).css('color', 'red');
                    }
                    else {
                        $('#signUpTab p').text('There was an error submitting your email.').css('color', 'red');
                    }
            }, "json");
            
        });
    }());
    
    (function(){
        //Location Picker
        var isVisible = false, isLoaded = false, isLoading = false, container = $('#locationsTab'), locationBox = $('#locationsBox'), chooseLink = locationBox.find('.chooseLocation'), currentLocation = locationBox.find('.currentLocation'), footLocationLink = $('#footerNav .locationLink');
        var bulkHeight = $('#header').outerHeight() + $('#subNav').outerHeight() + $('#footer').outerHeight(), isScrollable = false;
	    var topPos;
		if($('#content.home').length) {
			topPos = 121;
		}
		else {
			topPos = 98;
		}
		var makeLocationScrollAble = function() {
			contentHeight = $(window).height()-bulkHeight;
			if(!isScrollable && contentHeight < container.outerHeight()) {
				container.css('top', topPos+'px').appendTo('body').append('<div class="spaceEnforer" style="height:40px;"></div>');
				locationBox.css('top', topPos+'px').appendTo('body');
				isScrollable = true;
			}
			else if(isScrollable) {
				container.appendTo('#header');
				container.find('.spaceEnforer').remove();
				locationBox.appendTo('#header');
				isScrollable = false;
			}
			
		};
		var resizeTimeout;
	    $(window).resize(function() {
			clearTimeout(resizeTimeout);
			resizeTimeout = setTimeout(makeLocationScrollAble, 100);
		});
		$(window).load(makeLocationScrollAble);
		chooseLink.click(function(e){
            e.preventDefault();
            if (isLoading) 
                return;
            if (isVisible) {
                container.slideUp();
                isVisible = false;
            }
            else {
                if (isLoaded) {
                    container.slideDown();
                    isVisible = true;
                }
                else {
                    isLoading = true;
                    container.css('right', (locationBox.outerWidth() + 10) + 'px');
                    container.load(chooseLink.attr('href') + '?ajax', function(){
                        isLoaded = true;
                        isVisible = true;
                        isLoading = false;
                        container.slideDown(function() {
							makeLocationScrollAble();
						});
                    });
                }
            }
        });
        $('#locationsTab .locationsNav a').live('click', function(e){
            e.preventDefault();
            if (isLoading) 
                return;
            isLoading = true;
            var link = $(this);
            container.load(link.attr('href') + '?ajax', function(){
                var linkText = link.text(), linkUrl = link.attr('href'), linkUrlSplit = linkUrl.split('/');
                chooseLink.attr('href', linkUrl);
                footLocationLink.attr('href', linkUrl);
                isLoading = false;
				makeLocationScrollAble();
            });
        });
        $('.locationBox').live('click', function() {
			var url = $(this).find('.infoButton a').attr('href');
			if(url) window.location = url;
        });
    }());
});

