// Begin jQuery

$.fn.scrolling = function(){
    $(this).each(function(){
        $('.up a, .down a', this).data('id', '#' + $(this).attr('id').substr(7)).click(function(){
            var list = $($(this).data('id'));
            if (!list.data('count')) list.data('count', $('li.active', list).length);
            var count = list.data('count');
            if ($('li', list).length > count){
                var nodes = $('li.active', list).removeClass('active').hide();
                if (nodes.length){
                    if ($(this).parent().hasClass('down')){
                        var node = nodes.eq(nodes.length-1).next();
                        if (!node.length) node = $('li:first', list);
                        for (var i = 0; i < count; i++){
                            if (node.length){
                                node.addClass('active').hide();
                                node = node.next();
                            } else break;
                        }
                    }
                    else{
                        var node = nodes.eq(0).prev();
                        var limit = count;
                        if (!node.length){
                            node = $('li:last', list);
                            limit = $('li', list).length % count;
                            if (!limit) limit = count;
                        }
                        for (var i = 0; i < limit; i++){
                            if (node.length){
                                node.addClass('active').hide();
                                node = node.prev();
                            } else break;
                        }
                    }
                }
                $('li.active', list).fadeIn();
            }
            return false;
        });
    });
};

$.fn.tooltip = function(){
    $(this).hover(function(){
        var node = $(this);
        node.attr('title', '');
        if (!$('#tooltip').length) $('body').append('<div id="tooltip" style="display:none;"></div>');
        var tooltip = $('.tooltip', node.parent());
        if (window.tooltip_timer) clearTimeout(window.tooltip_timer);
        if (tooltip.length){
            window.tooltip_timer = setTimeout(function(){
                var pos = node.position();
                var box = $('#tooltip').html('<div>' + tooltip.html() + '</div>').css({top: pos.top - $('#tooltip').height() - 25, left: pos.left - 5});
                if (jQuery.browser.msie) box.show();
                else box.fadeIn(500);
            }, 300);
        }
    }, function(){
        if (window.tooltip_timer) clearTimeout(window.tooltip_timer);
        $('#tooltip').hide();
    });
    return this;
};

$(document).ready(function() {

// Top login and add menu buttons
	
	// append an extra span to make the hover effect work
    
    $('#header_buttons li a, #header_buttons_user li a, #header_buttons_rest li a').append('<span class="hover"></span>');
    
    // make the hover animate in and out
    
    $('#header_buttons li a, #header_buttons_user li a, #header_buttons_rest li a').hover(function() {
    	$('.hover', this).stop().animate({'opacity': 1}, 250);
    },function() {
    	$('.hover', this).stop().animate({'opacity': 0}, 450);
    })

// Slide up catering bubble and slide down address change bubble if switching from pickup to delivery method on interior pages

	$("#change_to_delivery").click(function(){
		if($("#catering_bubble").length > 0) {
			$("#catering_bubble").animate({top: "-300"}, 200, function(){
				$("#delivery_address_bubble").show().animate({top: "-96"}, 200);
			});
		} else{
			$("#delivery_address_bubble").show().animate({top: "-96"}, 200);
		}
		return false;
	});
	
	$("#cancel_address_change").click(function(){
		$("#delivery_address_bubble").animate({top: "-316"}, 200, function(){
			$("#delivery_address_bubble").hide();
			$("#catering_bubble").animate({top: "-50"}, 200);
		});
		return false;
	});

// Tooltips - general tooltips used on many pages

	$(".tooltips_window strong").hover(function() {
		$(this).prev("span").animate({opacity: "show", bottom: "18"}, 200);
	}, function() {
		$(this).prev("span").animate({opacity: "hide", bottom: "-15"}, 200);
	});

// Tooltips - specific tooltips for the favorites links on Menu Item pages

	$(".tooltips_window a").hover(function() {
		$(this).prev("span").animate({opacity: "show", bottom: "18"}, 200);
	}, function() {
		$(this).prev("span").animate({opacity: "hide", bottom: "-15"}, 200);
	});

// Accordion

	$(".accordion h4").eq(100).addClass("active");
	$(".accordion p").eq(100).show();
	$(".accordion h4").click(function(){
		$(this).next("p").slideToggle("normal")
		.siblings("p:visible").slideUp("normal");
		$(this).toggleClass("active");
		$(this).siblings("h4").removeClass("active");
	});

// Account info drop form

	$(".package_prices .package_1").click(function(){
    	$("#account_info_drop").slideDown("slow");
    	$(this).addClass("active");
    	$(".active").not(this).removeClass("active");
    	document.frmAddmenu.package.value = "1";
	});
	$(".package_prices .package_2").click(function(){
    	$("#account_info_drop").slideDown("slow");
    	$(this).addClass("active");
    	$(".active").not(this).removeClass("active");
    	document.frmAddmenu.package.value = "2";
	});
	$(".package_prices .package_3").click(function(){
    	$("#account_info_drop").slideDown("slow");
    	$(this).addClass("active");
    	$(".active").not(this).removeClass("active");
    	document.frmAddmenu.package.value = "3";
	});
	
// Menu add item - remove visible form when clicking to another meal item
	$(".menu_items span.item").click(function(){
		$("form.menu_add_item:visible").remove();
  		return false;
	});
	$(".menu_items ul li span.left").click(function(){
		$(this).next("span").css("display", "inline"); 
	});
	$(".menu_items ul li span.right").click(function(){
		$(this).next("span").css("display", "inline"); 
	});

// Menu Filter 
	
	// slide down categories on page load
	$('#menuFilter .content').slideDown(650);
	
	// toggle category visibility
	$('#toggleCategories').click(function(){
		$('#menuFilter .content').slideToggle(350, function(){
			$('#toggleCategories').toggleClass('active');
		});
		return false;
	});

// Menu page highlighted search results

	$(window).load(function () {
		$(".search_result").animate({ backgroundColor: "#fff890" }, 1500);
        $('div.images li img, div.videos li img').each(function(){
            var img = $(this);
            img.css('margin-left', ((60 - img.width()) / 2) + 'px');
        });
        
  	});

// Scrolling

    $('ul.scroll').scrolling();

// Tooltip

    $('#images a, #videos a').tooltip();

// end jQuery

});

