// attach flyouts to menu items
(function($) {

    // Locate any flyouts and assign them to their respective hover by class name
    $(function() {
        
        // semi-global timer variable for folding up flyouts
        var flyoutTimer = false;
        
        // all flyouts are of class 'frii-flyout'
        $('.frii-flyout').each(function() {
            var $flyout = $(this);
            
            /*
             * This flyout will have a class like ".frii-flyout-<menu name>"
             * and its corresponding hover point will have a class like: 
             * ".frii-menu-item-<menu name>"
             */
            var rawClasses = $flyout.attr('class');
            var flyoutClasses = rawClasses.split(/\s+/);
            for(var i = 0; i < flyoutClasses.length; i++) {
                var flyoutClass = flyoutClasses[i];
                
                if(flyoutClass.match(/^frii-flyout-.+/)) {
                    // convert it to a menu item class to look for
                    var menuClass = flyoutClass.replace(/^frii-flyout-/, 'frii-menu-item-');
                    
                    // bind the hover even on that item to show this flyout
                    $(
                    	'.frii-menu-bar .' + menuClass + 
                    	', .frii-other-services-box .' + menuClass +
                    	', .frii-header .' + menuClass
                    	).mouseenter(function() {
                    	
                        clearFlyoutTimer();
                        showFlyout(flyoutClass, menuClass);
                    });
                    
                    $('.' + flyoutClass).mouseenter(function() {
                        clearFlyoutTimer();
                    });
                    
                    // bind to both the menu and flyout mouseout to start a close timer
                    $('.' + menuClass + ', .' + flyoutClass).mouseleave(function() {
                        setFlyoutTimer();
                    });
                }
            }
        });
        
        /* Utility function to open a flyout, closing any others */
        function showFlyout(flyoutClass, menuClass) {
            
            // close all other menus
            closeAllFlyouts(flyoutClass);
            
            // hover the menu 
            $('.' + menuClass).parent().addClass('frii-active-menu');
            
            // slide down the flyout
            if($('.' + flyoutClass).hasClass('frii-fade-in')) {
                $('.' + flyoutClass).fadeIn('fast');
            } else {
                $('.' + flyoutClass).slideDown('fast');
            }
        }
        
        
        /* Utility function to close flyouts */
        function closeAllFlyouts(except) {
            // de-hover all menu items
            $('.frii-active-menu').removeClass('frii-active-menu');
            
            // close all flyouts (except the new active one if provided);
            
            if(except) {
                $('.frii-flyout').filter(':not(.' + except + ')').slideUp('fast');
            } else {
                $('.frii-flyout').slideUp('fast');
            }
        }
        
        /* Utility function to set a timer to close the flyouts */
        function setFlyoutTimer() {
            clearFlyoutTimer();
            
            flyoutTimer = setTimeout(
                function() {
                    closeAllFlyouts();
                },
                4000
            );
        }
        
        /* Utility function to clear the flyout close timer */
        function clearFlyoutTimer() {
            if(flyoutTimer) {
                clearTimeout(flyoutTimer);
                flyoutTimer = false;
            }
        }
        
    });
})(jQuery);;
// Run the slideshow on the home page
(function($) {

    // find all the slideshows
    $(function() {
        $('.frii-slideshow').each(function() {
            var $slideshow = $(this);
            
            // timer for auto rotation
            var autoRotationTimer = false;
            
            // locate all the slide sets
            var $slideSets = $('.frii-slideset', $slideshow);
            
            // create a navigation set for the slide sets
            var $navSet = $('<ul></ul>');
            
            $slideSets.each(function() {
                var $slideSet = $(this);
                
                var $li = $('<li></li>');
                $li.data('slideSetId',  $slideSet.attr('id'));
                
                // attach click handlers to show the related slide set
                $li.click(function(e, automatic) {
                    e.preventDefault();
                    
                    // if this was a manual click turn off the auto-rotation system
                    if(automatic !== true) {
                        autoRotationTimer = stopAutoRotation(autoRotationTimer, $slideshow);
                    }
                    
                    // show the associated slide set
                    showSlideSet($(this).data('slideSetId'));
                    
                    // remove the active class from everyone
                    $(this).parent().children('li').removeClass('frii-active');
                    
                    // add the active class to ourselves
                    $(this).addClass('frii-active');
                });
                
                $navSet.append($li);
                
                // activate the per-slideset navigation
                $('.frii-slide-navigation li', $slideSet).each(function() {
                    var $item = $(this);
                    
                    // bind clicks to show the corresponding slide
                    $item.click(function(e, automatic) {
                        e.preventDefault();
                        
                        // stop auto-rotation
                        if(automatic !== true) {
                            autoRotationTimer = stopAutoRotation(autoRotationTimer, $slideshow);
                        }
                        
                        var id = $('a', this).attr('href');
                        id = id.replace(/#/, '', id);
                        
                        showSlide(id, $slideshow);
                        
                        // remove the active class from everyone
                        $(this).parent().children('li').removeClass('frii-active');
                        
                        // add the active class to ourselves
                        $(this).addClass('frii-active');
                        
                    });
                });
                
            });
            
            
            $('.frii-slideshow-left .frii-slide-navigation', $slideshow).append($navSet);
            
            
            // open the first slide in the deck
            $navSet.children('li').eq(0).trigger('click', true);
            
            // start the auto-rotation system
            autoRotationTimer = startAutoRotation(autoRotationTimer, $slideshow);
            
        });
    });
    
    /* Utility function to show a slide set */
    function showSlideSet(setId) {
        
        // go and close all the other slide sets
        var $set = $('#' + setId);
        $set.parent().children('.frii-slideset').hide();
        
        // open this slide set
        if(jQuery.support.opacity) {
            $set.fadeIn('slow');
        } else {
            $set.show();
        }

        // open the first slide
        $('.frii-slide-navigation a', $set).first().trigger('click', true);
    }
    
    
    /* Utility function to show a single slide */
    function showSlide(slideId, slideshow) {
        
        // go and close all the other slides
        var $slide = $('#' + slideId);
        $slide.parent().children('.frii-slide').hide();

        // open this slide set
        if(jQuery.support.opacity) {
            $slide.fadeIn('fast');
        } else {
            $slide.show();
        }
        
        // fix the translucency on each slide text carrier
        // find the text carrier
        var $textCarrier = $('.frii-slide-text-carrier', $slide);
        
        // make sure it isn't already fixed
        if($textCarrier.hasClass('frii-slide-text-carrier-fixed')) {
            return;
        }
        
        // create a new background div
        var $newCarrier = $('<div></div>');
        $newCarrier.addClass('frii-slide-text-carrier-translucent');
        
        $newCarrier.css('width', $textCarrier.width());
        $newCarrier.css('height', $textCarrier.height());
        
        $textCarrier.after($newCarrier);
        $textCarrier.addClass('frii-slide-text-carrier-fixed');
    }
    
    
    // utility function to stop auto-rotation of the slides
    function stopAutoRotation(timer, slideshow) {
        if(timer !== false) {
            clearTimeout(timer);
            timer = false;
        }
        
        return timer;
    }
    
    // utility function to start auto-rotation of the slides
    function startAutoRotation(timer, slideshow) {
        stopAutoRotation();
        
        // set rotation to start
        timer = setInterval(
            function() {
                rotateSlideSet(slideshow);
            },
            6000
        );
        
        return timer;
    }
    
    // Utility function to rotate to the next slide set in a show
    function rotateSlideSet(slideshow) {
        // find the navigation set
        var $navSet = $('.frii-slideshow-left .frii-slide-navigation li', $(slideshow));
        
        // find the index of the currently-active item
        var activeIndex = 0;
        for(var i = 0; i < $navSet.length; i++) {
            if($($navSet[i]).hasClass('frii-active')) {
                activeIndex = i;
            }
        }
        
        activeIndex++
        if(activeIndex >= $navSet.length) {
            activeIndex = 0;
        }
        
        // make the new slide active
        $($navSet[activeIndex]).trigger('click', true);
    }

})(jQuery);;
/* custom tabbing */
(function($) {
	$(function() {
		// find any tabbed-node content
		$('.node-tabbed-page').each(function() {
			var $page = $(this);
			
			// locate all the tab bodies
			var tabs = [];
			$('.field-name-field-tab-content .field-item', $page).each(function() {
				var $tabContent = $(this);
				var tab = {};
				
				// figure out the title
				tab.title = $('h2', $tabContent).text();
				if(!tab.title) {
					tab.title = 'Tab ' + tabs.length;
				}
				$('h2', $tabContent).first().remove();
				
				// assign it a unique ID
				var id = tab.title;
				id = id.replace(/[^a-zA-Z0-9]+/g, '_').toLowerCase();
				tab.id = id;
				
				// capture the body
				tab.body = $tabContent.html();
				
				tabs.push(tab);
				
			});
			
			// remove the original content
			$('.field-name-field-tab-content', $page).empty();
			
			// put in a wrapper
			var $wrapper = $('<div></div>');
			$wrapper.addClass('frii-tabs');
			
			// add a UL navigation and tab divs
			var $ul = $('<ul></ul>');
			$wrapper.append($ul);
			
			for(var i = 0; i < tabs.length; i++) {
				var tab = tabs[i];
				
				// navigation
				var $li = $('<li></li>');
				var $a = $('<a></a>');
				$a.attr('href', '#' + tab.id);
				$a.text(tab.title);
				$li.append($a);
				$ul.append($li);
				
				// tab body
				var $body = $('<div></div>');
				$body.attr('id', tab.id);
				$body.html(tab.body);
				$wrapper.append($body);
				
			}
			
			$page.append($wrapper);
			
			var $tabs = $wrapper.tabs();
			
			// Annoyingly, Tabs doesn't understand # links correctly on its own
			$('div a', $wrapper).each(function() {
				var $link = $(this);
				
				if($link.attr('href').match(/^#/)) {
					// find the index of the tab this links to
					var id = $link.attr('href').replace(/^#/, '');
					var index = false;
					for(var i = 0; i < tabs.length; i++) {
						if(tabs[i].id == id) {
							index = i;
							break;
						}
					}
					if(index !== false) {
						$link.bind('click', function(e) {
							e.preventDefault();
							
							$tabs.tabs('select', index);
						});
					}
				}
				
				
			});
		});
	});
})(jQuery);

;

