(function( $ ) {
    $.fn.extend({
        cache: {},

        ctaText: function() {
            if ( !$('.call_to_action').length ) return false;
            
            var list = $(this);
            
            list.each(function(i) {
                if ( $(this).hasClass('active') ) {
                    $(this).fadeOut(1000, function() {
                        $(this).removeClass('active').addClass('hidden');
                        // If you reach the end, start at the beginning
                        if ( i+1 >= $(list).length ) {
                            $(list[0]).fadeIn(1000, function() {
                                $(this).removeClass('hidden').addClass('active');
                            });
                        } else {
                            $(list[i+1]).fadeIn(1000, function() {
                                $(this).removeClass('hidden').addClass('active');
                            });
                        }
                    });
                }
            });  
        },
        
       launchCarousel: function() {
            if ( !$('#cottage_inn_carousel').length ) return false;
            
            //Set opacity on slide content background
            $('div.slide_content').css('opacity', 0.7);
            			
			$("#cottage_inn_carousel").carouFredSel({
				items: {
					visible : 4,
					width : 240
				},
				scroll: {
					items : 1,
					easing : "easeOutBounce",
					pauseOnHover: true,
					duration : 800
				},
				width: 974,
				auto : true,
				prev : "#foo1_prev",
				next : "#foo1_next"
			}).find(".slide").hover(
				function() { $(this).find("div").slideDown(); },
				function() { $(this).find("div").slideUp();	}
			);  
        },
        
        menuModal: function() {
        	if ( !$('.menu_item').length ) return false;
        	
        	//Collect all anchors in the menu
/*
        	var anchors = $('.menu_item a');

            anchors.each(function() {
            	var el 	= this;
            	var img = $(this).children()[0];
            	//Set all annchor href to image src
            	el.setAttribute('href', img.getAttribute('src') );
            });
*/
        	
        	$(".menu_item img").fancybox({
				'opacity'		: true,
				'overlayShow'	: false,
				'transitionIn'	: 'elastic',
				'transitionOut'	: 'none'
			});
        
        }
        
    });
})(jQuery);

$('document').ready(function() {
    // Set opacity of the primary sub-nav for IE
    $('#nav_wrapper #primary_nav ul li ul').css('opacity', 0.90);
    
    //Launch Carousel
    $('#cottage_inn_carousel').launchCarousel();
    
    // Fire the rotating CTA's on the homepage
    setInterval(function() {
        $('.call_to_action').ctaText();
    }, 6000);
    
    //Launch Menu Modals
    //$('.menu_item').menuModal();
});
