
/**
*  Page initialiser
*
*/


    $(function() {
        jQuery(".tab-widget").tabbing();
    });


/**
*
*   TABBING PLUGIN
*
* @author deastwell
*/

(function($) {

    $.tabbing = function($tabs, options){

            var tabbingWidget = this;

            // Set options for this object
            tabbingWidget.options = options;


            $tabs.each(function() {

                var $tabbingWidget = $(this);

                // click on the tab
                $tabbingWidget.find(tabbingWidget.options.tabSelector).bind("click keypress", function(e){
                    // active on mouseclick by default
                    var active = e.type === "click" ? true : false;

                    // and active on 'enter' click
                    if(e.type === "keypress" && e.keyCode === 13){
                        active = true;
                    }

                    if(active){
                        e.preventDefault();
                        var $tab = $(e.currentTarget); // the current tab is what we've clicked
                        
                        var $tabPanel = $($tab.attr("href")); // the tab-panel it's going to open is its href e.g. $("#hp-grow")
                        tabbingWidget.closeAll($tabbingWidget);
                        tabbingWidget.openPanel($tab, $tabPanel);
                    }
                });


            });

    };

    $.tabbing.prototype = {

        closeAll : function closeAll($tabbingWidget){
            $tabbingWidget.find(this.options.tabParentSelector).removeClass("selected");
            $tabbingWidget.find(this.options.panelSelector).css("display","none");
        },

        openPanel : function openPanel($activeTab, $activePanel){
            $activeTab.parents(this.options.tabParentSelector).addClass("selected");
            $activePanel.css("display","block");
        }

    };

    /**
    * @example $(".tab-widget").tabbing({tabSelector:"a.tab"});
    **/

    $.fn.tabbing = function(options) {

        // Set defaults
        options = options || {};
        options.tabSelector = options.tabSelector || ".tabs a";
        options.tabParentSelector = options.tabParentSelector || ".tabs li";
        options.panelSelector = options.panelSelector || ".tab-panel > li";

        new $.tabbing(this, options);

        // Return this jquery node collection for chaining jquery methods
        return this;
    };

})(jQuery);

(function($) {

    $.bubble = { 
        bubbleClass: "bubble",
        triggerElClass: "map-button",
        options: {},
        
        hideBubble: function($bubble, duration) {
            // Hide bubble using visibility rather than display:none - this way we can still read the height property
            if (duration === 0 || $('html').hasClass("ie7") || $('html').hasClass("ie8")) {
                $bubble.hide();
            } else {
                $bubble.fadeOut(duration);
            }
        },
        
        fadeUp: function($el) {
            // Hide using visibility rather than display:none - this way we can still read the height property
            if($('html').hasClass("ie7") || $('html').hasClass("ie8")){
                $el.show();
            }else{
                $el.fadeIn('slow');
            }
        },
        
        showBubble: function(triggerEl, cssPos) {
            var $bubble;
            // hide all bubbles
            $.bubble.hideBubble($("." + $.bubble.bubbleClass), 0);
            // get the bubble related to this triggerEl
            $bubble = $.bubble.getBubble(triggerEl);
            if(cssPos){
                $bubble.css(cssPos);
            }
            // show this bubble
            $.bubble.fadeUp($bubble);
        },
        
         /**
         * return correct bubble for this trigger element
         */
        getBubble: function(triggerEl) {
            return $($(triggerEl).attr("href"));
        },
        bindBodyClick: function() {
            $('body').click(function(event) {
                var isBubble, isButton, $parents = $(event.target).parents(),
                    $thisAndAncestors = $(event.target).add($parents),
                    numAncestors = $thisAndAncestors.length;
                // if this or any of it's parents is a bubble or a trigger element
                $thisAndAncestors.each(function(i) {
                    var coords, cssPos;
                    isBubble = $(this).hasClass($.bubble.bubbleClass);
                    isButton = $(this).hasClass($.bubble.triggerElClass);
                    if (isBubble || isButton) {
                        if (isButton) {
                            event.preventDefault();
                            coords = event.target.coords.split(',');
                            $bubble = $.bubble.getBubble(this);
                            var leftModifier = $bubble.hasClass("bubble-left") ? -320 : 73;
                            var topModifier = $bubble.hasClass("bubble-left") ? -105 : -105;
                            cssPos = { 
                                       'top' : (parseInt(coords[1]) + topModifier) + "px", 
                                       'left' : (parseInt(coords[0]) + leftModifier) + "px"
                                      }
                                      
                            
                            // if this clicked element's bubble isn't already visible...
                            if ($bubble.css("display") !== "block") {
                                // .. show the bubble
                                $.bubble.showBubble(this, cssPos);
                            }
                        }
                        // else it's a bubble, do nothing
                        return false;
                    } else {
                        // Last item, we've not found a bubble or a trigger element , so...
                        if (i === numAncestors - 1) {
                            // ...hide all bubbles
                            $.bubble.hideBubble($("." + $.bubble.bubbleClass), 300);
                        }
                    }
                });
            });
        },
        /* CONTROLLER */
        init: function($triggerEls) {
            // Add all bubbles to the page for this instantiation
            $triggerEls.each(function() {
                var $bubble;
                
                // get bubble to display
                $bubble = $.bubble.getBubble(this);
                
                // Hide bubble
                $.bubble.hideBubble($bubble, 0);
                
            });
            // Just one click handler, on the body, we find out what was clicked and then act accordingly
            $.bubble.bindBodyClick("bodyClick");
        }
    };
    $.fn.bubble = function(options) {
        // Set $.bubble.options defaults
        $.bubble.options = options || {};
           
         new $.bubble.init(this);
        
        // Return this jquery node collection for chaining jquery methods
        return this;
    }
})(jQuery);
